]> git.gir.st - VimFx.git/blob - extension/packages/hints.coffee
Group all hint injection code in hints.coffee
[VimFx.git] / extension / packages / hints.coffee
1 utils = require 'utils'
2 { getPref } = require 'prefs'
3 { Marker } = require 'marker'
4 { addHuffmanCodeWordsTo } = require 'huffman'
5
6 { interfaces: Ci } = Components
7
8 HTMLDocument = Ci.nsIDOMHTMLDocument
9 XULDocument = Ci.nsIDOMXULDocument
10 XPathResult = Ci.nsIDOMXPathResult
11
12 CONTAINER_ID = 'VimFxHintMarkerContainer'
13
14 # All the following elements qualify for their own marker in hints mode
15 MARKABLE_ELEMENTS = [
16 "a"
17 "iframe"
18 "area[@href]"
19 "textarea"
20 "button"
21 "select"
22 "input[not(@type='hidden' or @disabled or @readonly)]"
23 "embed"
24 "object"
25 ]
26
27 # All elements that have one or more of the following properties
28 # qualify for their own marker in hints mode
29 MARKABLE_ELEMENT_PROPERTIES = [
30 "@tabindex"
31 "@onclick"
32 "@onmousedown"
33 "@onmouseup"
34 "@oncommand"
35 "@role='link'"
36 "@role='button'"
37 "contains(@class, 'button')"
38 "contains(@class, 'js-new-tweets-bar')"
39 "@contenteditable='' or translate(@contenteditable, 'TRUE', 'true')='true'"
40 ]
41
42
43 # Remove previously injected hints from the DOM
44 removeHints = (document) ->
45 if container = document.getElementById(CONTAINER_ID)
46 document.documentElement.removeChild(container)
47
48 for frame in document.defaultView.frames
49 removeHints(frame.document)
50
51
52 # Like `injectMarkers`, but also sets hints for the markers
53 injectHints = (document) ->
54 markers = injectMarkers(document)
55 hintChars = utils.getHintChars()
56
57 addHuffmanCodeWordsTo markers,
58 alphabet: hintChars
59 setCodeWord: (marker, hint, index) -> marker.setHint(hint)
60
61 return markers
62
63
64 # Creates and injects markers into the DOM
65 injectMarkers = (document) ->
66 # First remove previous hints container
67 removeHints(document)
68
69 # For now we aren't able to handle hint markers in XUL Documents :(
70 if document instanceof HTMLDocument# or document instanceof XULDocument
71 if document.documentElement
72 # For performance use Document Fragment
73 fragment = document.createDocumentFragment()
74
75 # Select all markable elements in the document, create markers
76 # for each of them, and position them on the page.
77 # Note that the markers are not given hints.
78 set = getMarkableElements(document)
79 markers = []
80 for i in [0...set.snapshotLength] by 1
81 element = set.snapshotItem(i)
82 if rect = getElementRect(element)
83 marker = new Marker(element)
84
85 marker.setPosition(rect)
86 fragment.appendChild(marker.markerElement)
87
88 marker.weight = rect.area
89
90 markers.push(marker)
91
92 container = createHintsContainer(document)
93 container.appendChild(fragment)
94 document.documentElement.appendChild(container)
95
96 for frame in document.defaultView.frames
97 markers = markers.concat(injectMarkers(frame.document))
98
99 return markers or []
100
101
102 createHintsContainer = (document) ->
103 container = document.createElement('div')
104 container.id = CONTAINER_ID
105 container.className = 'VimFxReset'
106 return container
107
108
109 # Returns elements that qualify for hint markers in hints mode.
110 # Generates and memoizes an XPath query internally
111 getMarkableElements = do ->
112 # Some preparations done on startup
113 elements = [
114 MARKABLE_ELEMENTS...
115 "*[#{ MARKABLE_ELEMENT_PROPERTIES.join(' or ') }]"
116 ]
117
118 reduce = (m, rule) ->
119 m.concat(["//#{ rule }", "//xhtml:#{ rule }"])
120 xpath = elements
121 .reduce(reduce, [])
122 .join(' | ')
123
124 namespaceResolver = (namespace) ->
125 if namespace == 'xhtml' then 'http://www.w3.org/1999/xhtml' else null
126
127 # The actual function that will return the desired elements
128 return (document, resultType = XPathResult.ORDERED_NODE_SNAPSHOT_TYPE) ->
129 return document.evaluate(xpath, document.documentElement, namespaceResolver, resultType, null)
130
131
132 # Uses `element.getBoundingClientRect()`. If that does not return a visible rectange, then looks at
133 # the children of the markable node.
134 #
135 # The logic has been copied over from Vimiun originally.
136 getElementRect = (element) ->
137 document = element.ownerDocument
138 window = document.defaultView
139 docElem = document.documentElement
140 body = document.body
141
142 clientTop = docElem.clientTop or body?.clientTop or 0
143 clientLeft = docElem.clientLeft or body?.clientLeft or 0
144 scrollTop = window.pageYOffset or docElem.scrollTop
145 scrollLeft = window.pageXOffset or docElem.scrollLeft
146
147 clientRect = element.getBoundingClientRect()
148
149 if isRectOk(clientRect, window)
150 return {
151 top: clientRect.top + scrollTop - clientTop
152 left: clientRect.left + scrollLeft - clientLeft
153 width: clientRect.width
154 height: clientRect.height
155 area: clientRect.width * clientRect.height
156 }
157
158 # If the rect has 0 dimensions, then check what's inside.
159 # Floated or absolutely positioned elements are of particular interest.
160 if clientRect.width is 0 or clientRect.height is 0
161 for childElement in element.children
162 if computedStyle = window.getComputedStyle(childElement, null)
163 if computedStyle.getPropertyValue('float') != 'none' or \
164 computedStyle.getPropertyValue('position') == 'absolute'
165
166 return getElementRect(childElement)
167
168 return
169
170
171 # Checks if the given TextRectangle object qualifies
172 # for its own Marker with respect to the `window` object
173 isRectOk = (rect, window) ->
174 minimum = 2
175 rect.width > minimum and rect.height > minimum and \
176 rect.top > -minimum and rect.left > -minimum and \
177 rect.top < window.innerHeight - minimum and \
178 rect.left < window.innerWidth - minimum
179
180
181 exports.injectHints = injectHints
182 exports.removeHints = removeHints
Imprint / Impressum