aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2015-03-05 06:46:46 +0000
committerStephen Blott2015-03-05 06:46:46 +0000
commit07f754d5936232dbc86aa70235c89b07f1cc4712 (patch)
tree3622d188dfdd3387c84a91dff5f3ba1f5569578a
parent6dde837b75edbdd794e26f50abbc7dfad14fbbaf (diff)
parent8c40600b0c7930b8b7010c29066c8dbaf91b20cc (diff)
downloadvimium-07f754d5936232dbc86aa70235c89b07f1cc4712.tar.bz2
Merge branch 'link-hint-text'
-rw-r--r--content_scripts/link_hints.coffee5
-rw-r--r--lib/dom_utils.coffee19
2 files changed, 23 insertions, 1 deletions
diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee
index 2abfa001..f904c7d5 100644
--- a/content_scripts/link_hints.coffee
+++ b/content_scripts/link_hints.coffee
@@ -231,6 +231,8 @@ LinkHints =
# Remove rects from elements where another clickable element lies above it.
nonOverlappingElements = []
# Traverse the DOM from first to last, since later elements show above earlier elements.
+ # NOTE(smblott). filterHints.generateLinkText also assumes this order when generating the content text for
+ # each hint. Specifically, we consider descendents before we consider their ancestors.
visibleElements = visibleElements.reverse()
while visibleElement = visibleElements.pop()
rects = [visibleElement.rect]
@@ -469,7 +471,7 @@ filterHints =
linkText = element.firstElementChild.alt || element.firstElementChild.title
showLinkText = true if (linkText)
else
- linkText = element.textContent || element.innerHTML
+ linkText = DomUtils.textContent.get element
{ text: linkText, show: showLinkText }
@@ -479,6 +481,7 @@ filterHints =
fillInMarkers: (hintMarkers) ->
@generateLabelMap()
+ DomUtils.textContent.reset()
for marker, idx in hintMarkers
marker.hintString = @generateHintString(idx)
linkTextObject = @generateLinkText(marker.clickableItem)
diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee
index 2ae9412e..f53e28d1 100644
--- a/lib/dom_utils.coffee
+++ b/lib/dom_utils.coffee
@@ -295,5 +295,24 @@ DomUtils =
document.body.removeChild div
coordinates
+ # Get the text content of an element (and its descendents), but omit the text content of previously-visited
+ # nodes. See #1514.
+ # NOTE(smblott). This is currently O(N^2) (when called on N elements). An alternative would be to mark
+ # each node visited, and then clear the marks when we're done.
+ textContent: do ->
+ visitedNodes = null
+ reset: -> visitedNodes = []
+ get: (element) ->
+ nodes = document.createTreeWalker element, NodeFilter.SHOW_TEXT
+ texts =
+ while node = nodes.nextNode()
+ continue unless node.nodeType == 3
+ continue if node in visitedNodes
+ text = node.data.trim()
+ continue unless 0 < text.length
+ visitedNodes.push node
+ text
+ texts.join " "
+
root = exports ? window
root.DomUtils = DomUtils