1 { interfaces: Ci } = Components
2 XPathResult = Ci.nsIDOMXPathResult
4 { getPref } = require 'prefs'
6 # All elements that have one or more of the following properties
7 # qualify for their own marker in hints mode
8 MARKABLE_ELEMENT_PROPERTIES = [
16 "contains(@class, 'button')"
17 "@contenteditable='' or translate(@contenteditable, 'TRUE', 'true')='true'"
20 # All the following elements qualify for their own marker in hints mode
28 "input[not(@type='hidden' or @disabled or @readonly)]"
32 # Marker class wraps the markable element and provides
33 # methods to manipulate the markers
35 # Creates the marker DOM node
36 constructor: (@element) ->
37 document = @element.ownerDocument
38 window = document.defaultView
39 @markerElement = document.createElement 'div'
40 @markerElement.className = 'VimFxReset VimFxHintMarker'
43 show: -> @markerElement.className = 'VimFxReset VimFxHintMarker'
46 hide: -> @markerElement.className = 'VimFxReset VimFxHiddenHintMarker'
48 # Positions the marker on the page. The positioning is absulute
49 setPosition: (rect) ->
50 @markerElement.style.left = rect.left + 'px'
51 @markerElement.style.top = rect.top + 'px'
53 # Assigns hint string to the marker
54 setHint: (@hintChars) ->
55 # number of hint chars that have been matched so far
56 @enteredHintChars = ''
58 document = @element.ownerDocument
60 while @markerElement.hasChildNodes()
61 @markerElement.removeChild @markedElement.firstChild
63 fragment = document.createDocumentFragment()
64 for char in @hintChars
65 span = document.createElement 'span'
66 span.className = 'VimFxReset'
67 span.textContent = char.toUpperCase()
69 fragment.appendChild span
71 @markerElement.appendChild fragment
73 # Add another char to the `enteredHintString`,
74 # see if it still matches `hintString`, apply classes to
75 # the distinct hint characters and show/hide marker when
76 # the entered string partially (not) matches the hint string
77 matchHintChar: (char) ->
78 # Handle backspace key by removing a previously entered hint char
79 # and resetting its class
80 if char == 'Backspace'
81 if @enteredHintChars.length > 0
82 @enteredHintChars = @enteredHintChars.slice(0, -1)
83 @markerElement.children[@enteredHintChars.length]?.className = 'VimFxReset'
84 # Otherwise append hint char and change hint class
86 @markerElement.children[@enteredHintChars.length]?.className = 'VimFxReset VimFxCharMatch'
87 @enteredHintChars += char.toLowerCase()
89 # If entered hint chars no longer partially match the hint chars
90 # then hide the marker. Othersie show it back
91 if @hintChars.search(@enteredHintChars) == 0 then @show() else @hide()
93 # Checks if the marker will be matched if the next character entered is `char`
95 char == 'Backspace' or @hintChars.search(@enteredHintChars + char.toLowerCase()) == 0
97 # Checks if enterd hint chars completely match the hint chars
99 return @hintChars == @enteredHintChars
102 # Selects all markable elements on the page, creates markers
103 # for each of them The markers are then positioned on the page
105 # The array of markers is returned
106 Marker.createMarkers = (document, startIndex) ->
107 hintChars = getPref('hint_chars').toLowerCase()
109 set = getMarkableElements(document)
113 for i in [0...set.snapshotLength] by 1
114 e = set.snapshotItem(i)
115 if rect = getElementRect e
116 elements.push [e, rect]
118 elements.sort ([e1, r1], [e2, r2]) ->
119 # <a> links should always be on the top. E.g. not links should go down
120 e1tagName = e1.tagName.toLowerCase()
121 e2tagName = e2.tagName.toLowerCase()
122 if e1tagName == 'a' and e2tagName != 'a'
124 else if e1tagName != 'a' and e2tagName == 'a'
126 else if r1.area < r2.area
128 else if r1.area > r2.area
133 # start from the end because the list is sorted in ascending order
134 j = elements.length + startIndex - 1
135 for [element, rect] in elements
136 # Get a hint for an element
137 hint = indexToHint(--j, hintChars)
138 marker = new Marker(element)
139 marker.setPosition rect
145 # Function generator that creates a function that
146 # returns hint string for supplied numeric index.
148 # Helper function that returns a permutation number `i`
149 # of some of the characters in the `chars` agrument
154 l = Math.floor(i / n); k = i % n;
156 return f(l - 1, chars) + chars[k]
159 # split the characters into two groups:
161 # * left chars are used for the head
162 # * right chars are used to build the tail
163 left = chars[...chars.length / 3]
164 right = chars[chars.length / 3...]
166 n = Math.floor(i / left.length)
168 return f(n - 1, right) + left[m]
171 # Returns elements that qualify for hint markers in hints mode.
172 # Generates and memoizes an XPath query internally
173 getMarkableElements = do ->
174 # Some preparations done on startup
175 elements = Array.concat \
177 ["*[#{ MARKABLE_ELEMENT_PROPERTIES.join(" or ") }]"]
179 xpath = elements.reduce((m, rule) ->
180 m.concat(["//#{ rule }", "//xhtml:#{ rule }"])
183 namespaceResolver = (namespace) ->
184 if (namespace == "xhtml") then "http://www.w3.org/1999/xhtml" else null
186 # The actual function that will return the desired elements
187 return (document, resultType = XPathResult.ORDERED_NODE_SNAPSHOT_TYPE) ->
188 document.evaluate xpath, document.documentElement, namespaceResolver, resultType, null
190 # Checks if the given TextRectangle object qualifies
191 # for its own Marker with respect to the `window` object
192 isRectOk = (rect, window) ->
193 rect.width > 2 and rect.height > 2 and \
194 rect.top > -2 and rect.left > -2 and \
195 rect.top < window.innerHeight - 2 and \
196 rect.left < window.innerWidth - 2
198 # Will scan through `element.getClientRects()` and look for
199 # the first visible rectange. If there are no visible rectangles, then
200 # will look at the children of the markable node.
202 # The logic has been copied over from Vimiun
203 getElementRect = (element) ->
204 document = element.ownerDocument
205 window = document.defaultView
206 docElem = document.documentElement
209 clientTop = docElem.clientTop || body?.clientTop || 0;
210 clientLeft = docElem.clientLeft || body?.clientLeft || 0;
211 scrollTop = window.pageYOffset || docElem.scrollTop;
212 scrollLeft = window.pageXOffset || docElem.scrollLeft;
214 clientRect = element.getBoundingClientRect()
215 rects = [rect for rect in element.getClientRects()]
216 rects.push clientRect
219 if isRectOk rect, window
221 top: rect.top + scrollTop - clientTop
222 left: rect.left + scrollLeft - clientLeft
225 area: clientRect.width * clientRect.height
228 # If the element has 0 dimentions then check what's inside.
229 # Floated or absolutely positioned elements are of particular interest
231 if rect.width == 0 or rect.height == 0
232 for childElement in element.children
233 if computedStyle = window.getComputedStyle childElement, null
234 if computedStyle.getPropertyValue('float') != 'none' or \
235 computedStyle.getPropertyValue('position') == 'absolute'
237 return childRect if childRect = getElementRect childElement
241 exports.Marker = Marker