1 utils = require 'utils'
2 { getPref } = require 'prefs'
3 { Marker } = require 'marker'
4 { addHuffmanCodeWordsTo } = require 'huffman'
6 { interfaces: Ci } = Components
8 HTMLDocument = Ci.nsIDOMHTMLDocument
9 XULDocument = Ci.nsIDOMXULDocument
10 XPathResult = Ci.nsIDOMXPathResult
11 HTMLAnchorElement = Ci.nsIDOMHTMLAnchorElement
13 CONTAINER_ID = 'VimFxHintMarkerContainer'
15 # All the following elements qualify for their own marker in hints mode
23 "input[not(@type='hidden' or @disabled or @readonly)]"
28 # All elements that have one or more of the following properties
29 # qualify for their own marker in hints mode
30 MARKABLE_ELEMENT_PROPERTIES = [
38 "contains(@class, 'button')"
39 "contains(@class, 'js-new-tweets-bar')"
40 "@contenteditable='' or translate(@contenteditable, 'TRUE', 'true')='true'"
44 # Remove previously injected hints from the DOM
45 removeHints = (document) ->
46 if container = document.getElementById(CONTAINER_ID)
47 document.documentElement.removeChild(container)
49 for frame in document.defaultView.frames
50 removeHints(frame.document)
53 # Like `injectMarkers`, but also sets hints for the markers
54 injectHints = (document) ->
55 markers = injectMarkers(document)
56 hintChars = utils.getHintChars()
58 addHuffmanCodeWordsTo markers,
60 setCodeWord: (marker, hint, index) -> marker.setHint(hint)
65 # Creates and injects markers into the DOM
66 injectMarkers = (document) ->
67 # First remove previous hints container
70 # For now we aren't able to handle hint markers in XUL Documents :(
71 if document instanceof HTMLDocument# or document instanceof XULDocument
72 if document.documentElement
73 # For performance use Document Fragment
74 fragment = document.createDocumentFragment()
76 # Select all markable elements in the document, create markers
77 # for each of them, and position them on the page.
78 # Note that the markers are not given hints.
79 set = getMarkableElements(document)
81 for i in [0...set.snapshotLength] by 1
82 element = set.snapshotItem(i)
83 if rect = getElementRect(element)
84 marker = new Marker(element)
86 marker.setPosition(rect)
87 fragment.appendChild(marker.markerElement)
89 marker.weight = rect.area * marker.calcBloomRating()
93 container = createHintsContainer(document)
94 container.appendChild(fragment)
95 document.documentElement.appendChild(container)
97 for frame in document.defaultView.frames
98 markers = markers.concat(injectMarkers(frame.document))
103 createHintsContainer = (document) ->
104 container = document.createElement('div')
105 container.id = CONTAINER_ID
106 container.className = 'VimFxReset'
110 # Returns elements that qualify for hint markers in hints mode.
111 # Generates and memoizes an XPath query internally
112 getMarkableElements = do ->
113 # Some preparations done on startup
116 "*[#{ MARKABLE_ELEMENT_PROPERTIES.join(' or ') }]"
119 reduce = (m, rule) ->
120 m.concat(["//#{ rule }", "//xhtml:#{ rule }"])
125 namespaceResolver = (namespace) ->
126 if namespace == 'xhtml' then 'http://www.w3.org/1999/xhtml' else null
128 # The actual function that will return the desired elements
129 return (document, resultType = XPathResult.ORDERED_NODE_SNAPSHOT_TYPE) ->
130 return document.evaluate(xpath, document.documentElement, namespaceResolver, resultType, null)
133 # Uses `element.getBoundingClientRect()`. If that does not return a visible rectange, then looks at
134 # the children of the markable node.
136 # The logic has been copied over from Vimiun originally.
137 getElementRect = (element) ->
138 document = element.ownerDocument
139 window = document.defaultView
140 docElem = document.documentElement
143 clientTop = docElem.clientTop or body?.clientTop or 0
144 clientLeft = docElem.clientLeft or body?.clientLeft or 0
145 scrollTop = window.pageYOffset or docElem.scrollTop
146 scrollLeft = window.pageXOffset or docElem.scrollLeft
148 clientRect = element.getBoundingClientRect()
150 if isRectOk(clientRect, window)
152 top: clientRect.top + scrollTop - clientTop
153 left: clientRect.left + scrollLeft - clientLeft
154 width: clientRect.width
155 height: clientRect.height
156 area: clientRect.width * clientRect.height
159 # If the rect has 0 dimensions, then check what's inside.
160 # Floated or absolutely positioned elements are of particular interest.
161 if clientRect.width is 0 or clientRect.height is 0
162 for childElement in element.children
163 if computedStyle = window.getComputedStyle(childElement, null)
164 if computedStyle.getPropertyValue('float') != 'none' or \
165 computedStyle.getPropertyValue('position') == 'absolute'
167 return getElementRect(childElement)
172 # Checks if the given TextRectangle object qualifies
173 # for its own Marker with respect to the `window` object
174 isRectOk = (rect, window) ->
176 rect.width > minimum and rect.height > minimum and \
177 rect.top > -minimum and rect.left > -minimum and \
178 rect.top < window.innerHeight - minimum and \
179 rect.left < window.innerWidth - minimum
182 exports.injectHints = injectHints
183 exports.removeHints = removeHints