From 84f5954ad52e82b795b1e493a9111355ddaa0f07 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 21 May 2017 22:44:22 -0700 Subject: [PATCH] handle scanning text for janky websites better #23 --- ext/fg/js/source-range.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/ext/fg/js/source-range.js b/ext/fg/js/source-range.js index 58b6a415..95177f0b 100644 --- a/ext/fg/js/source-range.js +++ b/ext/fg/js/source-range.js @@ -84,8 +84,22 @@ class TextSourceRange { } static shouldEnter(node) { + if (node.nodeType !== 1) { + return false; + } + const skip = ['RT', 'SCRIPT', 'STYLE']; - return !skip.includes(node.nodeName); + if (skip.includes(node.nodeName)) { + return false; + } + + const style = window.getComputedStyle(node); + const hidden = + style.visibility === 'hidden' || + style.display === 'none' || + parseFloat(style.fontSize) === 0; + + return !hidden; } static seekForward(node, offset, length) { @@ -106,7 +120,7 @@ class TextSourceRange { } static seekForwardHelper(node, state) { - if (node.nodeType === 3) { + if (node.nodeType === 3 && node.parentElement && TextSourceRange.shouldEnter(node.parentElement)) { const offset = state.node === node ? state.offset : 0; const remaining = node.length - offset; const consumed = Math.min(remaining, state.remainder); @@ -143,7 +157,7 @@ class TextSourceRange { } static seekBackwardHelper(node, state) { - if (node.nodeType === 3) { + if (node.nodeType === 3 && node.parentElement && TextSourceRange.shouldEnter(node.parentElement)) { const offset = state.node === node ? state.offset : node.length; const remaining = offset; const consumed = Math.min(remaining, state.remainder);