2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014, 2015, 2016.
4 # Copyright Wang Zhuochun 2013.
6 # This file is part of VimFx.
8 # VimFx is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # VimFx is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
22 # This file contains lots of different helper functions.
24 nsIClipboardHelper = Cc['@mozilla.org/widget/clipboardhelper;1']
25 .getService(Ci.nsIClipboardHelper)
26 nsIDomUtils = Cc['@mozilla.org/inspector/dom-utils;1']
27 .getService(Ci.inIDOMUtils)
28 nsIEventListenerService = Cc['@mozilla.org/eventlistenerservice;1']
29 .getService(Ci.nsIEventListenerService)
30 nsIFocusManager = Cc['@mozilla.org/focus-manager;1']
31 .getService(Ci.nsIFocusManager)
32 nsIStyleSheetService = Cc['@mozilla.org/content/style-sheet-service;1']
33 .getService(Ci.nsIStyleSheetService)
34 nsIWindowMediator = Cc['@mozilla.org/appshell/window-mediator;1']
35 .getService(Ci.nsIWindowMediator)
37 # For XUL, `instanceof` checks are often better than `.localName` checks,
38 # because some of the below interfaces are extended by many elements.
39 XULDocument = Ci.nsIDOMXULDocument
40 XULButtonElement = Ci.nsIDOMXULButtonElement
41 XULControlElement = Ci.nsIDOMXULControlElement
42 XULMenuListElement = Ci.nsIDOMXULMenuListElement
43 XULTextBoxElement = Ci.nsIDOMXULTextBoxElement
45 # Full chains of events for different mouse actions. ('command' is for XUL
47 EVENTS_CLICK = ['mousedown', 'mouseup', 'click', 'command']
48 EVENTS_HOVER_START = ['mouseover', 'mouseenter', 'mousemove']
49 EVENTS_HOVER_END = ['mouseout', 'mouseleave']
53 # Element classification helpers
55 isActivatable = (element) ->
56 return element.localName in ['a', 'button'] or
57 (element.localName == 'input' and element.type in [
58 'button', 'submit', 'reset', 'image'
60 element instanceof XULButtonElement
62 isAdjustable = (element) ->
63 return element.localName == 'input' and element.type in [
64 'checkbox', 'radio', 'file', 'color'
65 'date', 'time', 'datetime', 'datetime-local', 'month', 'week'
67 element.localName in ['video', 'audio', 'embed', 'object'] or
68 element instanceof XULControlElement or
69 # Youtube special case.
70 element.classList?.contains('html5-video-player') or
71 element.classList?.contains('ytp-button')
73 isContentEditable = (element) ->
74 return element.isContentEditable or
75 isIframeEditor(element) or
77 element.getAttribute?('g_editable') == 'true' or
78 element.ownerDocument?.body?.getAttribute('g_editable') == 'true' or
79 # Codeacademy terminals.
80 element.classList?.contains('real-terminal')
82 isIframeEditor = (element) ->
83 return false unless element.localName == 'body'
86 element.id == 'innerdocbody' or
88 (element.classList.contains('xe_content') and
89 element.classList.contains('editable')) or
91 element.classList.contains('wysiwyg') or
92 # The wasavi extension.
93 element.hasAttribute('data-wasavi-state')
95 isProperLink = (element) ->
96 # `.getAttribute` is used below instead of `.hasAttribute` to exclude `<a
97 # href="">`s used as buttons on some sites.
98 return element.getAttribute('href') and
99 (element.localName == 'a' or
100 element.ownerDocument instanceof XULDocument) and
101 not element.href.endsWith('#') and
102 not element.href.endsWith('#?') and
103 not element.href.startsWith('javascript:')
105 isTextInputElement = (element) ->
106 return (element.localName == 'input' and element.type in [
107 'text', 'search', 'tel', 'url', 'email', 'password', 'number'
109 element.localName == 'textarea' or
110 element instanceof XULTextBoxElement or
111 isContentEditable(element)
113 isTypingElement = (element) ->
114 return isTextInputElement(element) or
115 # `<select>` elements can also receive text input: You may type the
116 # text of an item to select it.
117 element.localName == 'select' or
118 element instanceof XULMenuListElement
122 # Active/focused element helpers
124 # NOTE: In frame scripts, `document.activeElement` may be `null` when the page
125 # is loading. Therefore always check if anything was returned, such as:
127 # return unless activeElement = utils.getActiveElement(window)
128 getActiveElement = (window) ->
129 {activeElement} = window.document
130 return null unless activeElement
131 # If the active element is a frame, recurse into it. The easiest way to detect
132 # a frame that works both in browser UI and in web page content is to check
133 # for the presence of `.contentWindow`. However, in non-multi-process
134 # `<browser>` (sometimes `<xul:browser>`) elements have a `.contentWindow`
135 # pointing to the web page content `window`, which we don’t want to recurse
136 # into. This seems to be the only way to detect such elements.
137 if activeElement.localName != 'browser' and activeElement.contentWindow
138 return getActiveElement(activeElement.contentWindow)
142 blurActiveElement = (window) ->
143 # Blurring a frame element also blurs any active elements inside it. Recursing
144 # into the frames and blurring the “real” active element directly would give
145 # focus to the `<body>` of its containing frame, while blurring the top-most
146 # frame gives focus to the top-most `<body>`. This allows to blur fancy text
147 # editors which use an `<iframe>` as their text area.
148 window.document.activeElement?.blur()
150 blurActiveBrowserElement = (vim) ->
151 # - Blurring in the next tick allows to pass `<escape>` to the location bar to
152 # reset it, for example.
153 # - Focusing the current browser afterwards allows to pass `<escape>` as well
154 # as unbound keys to the page. However, focusing the browser also triggers
155 # focus events on `document` and `window` in the current page. Many pages
156 # re-focus some text input on those events, making it impossible to blur
157 # those! Therefore we tell the frame script to suppress those events.
159 activeElement = getActiveElement(window)
160 vim._send('browserRefocus')
163 window.gBrowser.selectedBrowser.focus()
166 # Focus an element and tell Firefox that the focus happened because of a user
167 # action (not just because some random programmatic focus). `.FLAG_BYKEY` might
168 # look more appropriate, but it unconditionally selects all text, which
169 # `.FLAG_BYMOUSE` does not.
170 focusElement = (element, options = {}) ->
171 nsIFocusManager.setFocus(element, options.flag ? 'FLAG_BYMOUSE')
172 element.select?() if options.select
174 getFocusType = (element) -> switch
175 when isTypingElement(element)
177 when isActivatable(element)
179 when isAdjustable(element)
188 listen = (element, eventName, listener, useCapture = true) ->
189 element.addEventListener(eventName, listener, useCapture)
191 element.removeEventListener(eventName, listener, useCapture)
194 listenOnce = (element, eventName, listener, useCapture = true) ->
197 element.removeEventListener(eventName, fn, useCapture)
198 listen(element, eventName, fn, useCapture)
200 onRemoved = (window, element, fn) ->
201 mutationObserver = new window.MutationObserver((changes) ->
202 for change in changes then for removedElement in change.removedNodes
203 if removedElement == element
204 mutationObserver.disconnect()
208 mutationObserver.observe(element.parentNode, {childList: true})
209 module.onShutdown(mutationObserver.disconnect.bind(mutationObserver))
211 suppressEvent = (event) ->
212 event.preventDefault()
213 event.stopPropagation()
215 simulateMouseEvents = (element, sequenceType) ->
216 window = element.ownerGlobal
217 rect = element.getBoundingClientRect()
219 eventSequence = switch sequenceType
227 for type in eventSequence
228 buttonNum = if type in EVENTS_CLICK then 1 else 0
229 mouseEvent = new window.MouseEvent(type, {
230 # Let the event bubble in order to trigger delegated event listeners.
231 bubbles: type not in ['mouseenter', 'mouseleave']
232 # Make the event cancelable so that `<a href="#">` can be used as a
233 # JavaScript-powered button without scrolling to the top of the page.
234 cancelable: type not in ['mouseenter', 'mouseleave']
235 # These properties are just here for mimicing a real click as much as
240 # `page{X,Y}` are set automatically to the correct values when setting
241 # `client{X,Y}`. `{offset,layer,movement}{X,Y}` are not worth the trouble
245 # To exactly calculate `screen{X,Y}` one has to to check where the web
246 # page content area is inside the browser chrome and go through all parent
247 # frames as well. This is good enough. YAGNI for now.
248 screenX: window.screenX + rect.left
249 screenY: window.screenY + rect.top
251 element.dispatchEvent(mouseEvent)
260 return element.clientWidth * element.clientHeight
262 containsDeep = (parent, element) ->
263 parentWindow = parent.ownerGlobal
264 elementWindow = element.ownerGlobal
266 # Owner windows might be missing when opening the devtools.
267 while elementWindow and parentWindow and
268 elementWindow != parentWindow and elementWindow.top != elementWindow
269 element = elementWindow.frameElement
270 elementWindow = element.ownerGlobal
272 return parent.contains(element)
274 createBox = (document, className = '', parent = null, text = null) ->
275 box = document.createElement('box')
276 box.className = "#{className} vimfx-box"
277 box.textContent = text if text?
278 parent.appendChild(box) if parent?
281 # Returns the minimum of `element.clientHeight` and the height of the viewport,
282 # taking fixed headers and footers into account. Adapted from Firefox’s source
283 # code for `<space>` scrolling (which is where the arbitrary constants below
286 # coffeelint: disable=max_line_length
287 # <https://hg.mozilla.org/mozilla-central/file/4d75bd6fd234/layout/generic/nsGfxScrollFrame.cpp#l3829>
288 # coffeelint: enable=max_line_length
289 getViewportCappedClientHeight = (element) ->
290 window = element.ownerGlobal
291 viewport = getWindowViewport(window)
292 headerBottom = viewport.top
293 footerTop = viewport.bottom
294 maxHeight = viewport.height / 3
295 minWidth = Math.min(viewport.width / 2, 800)
297 # Restricting the candidates for headers and footers to the most likely set of
298 # elements results in a noticeable performance boost.
299 candidates = window.document.querySelectorAll(
300 'div, ul, nav, header, footer, section'
303 for candidate in candidates
304 rect = candidate.getBoundingClientRect()
305 continue unless rect.height <= maxHeight and rect.width >= minWidth
306 # Checking for `position: fixed;` is the absolutely most expensive
307 # operation, so that is done last.
309 when rect.top <= headerBottom and rect.bottom > headerBottom and
310 isPositionFixed(candidate)
311 headerBottom = rect.bottom
312 when rect.bottom >= footerTop and rect.top < footerTop and
313 isPositionFixed(candidate)
316 return Math.min(element.clientHeight, footerTop - headerBottom)
318 getWindowViewport = (window) ->
320 clientWidth, clientHeight # Viewport size excluding scrollbars, usually.
321 scrollWidth, scrollHeight
322 } = window.document.documentElement
323 {innerWidth, innerHeight} = window # Viewport size including scrollbars.
324 # We don’t want markers to cover the scrollbars, so we should use
325 # `clientWidth` and `clientHeight`. However, when there are no scrollbars
326 # those might be too small. Then we use `innerWidth` and `innerHeight`.
327 width = if scrollWidth > innerWidth then clientWidth else innerWidth
328 height = if scrollHeight > innerHeight then clientHeight else innerHeight
338 injectTemporaryPopup = (document, contents) ->
339 popup = document.createElement('menupopup')
340 popup.appendChild(contents)
341 document.getElementById('mainPopupSet').appendChild(popup)
342 listenOnce(popup, 'popuphidden', popup.remove.bind(popup))
345 insertText = (input, value) ->
346 {selectionStart, selectionEnd} = input
348 input.value[0...selectionStart] + value + input.value[selectionEnd..]
349 input.selectionStart = input.selectionEnd = selectionStart + value.length
351 isPositionFixed = (element) ->
352 computedStyle = element.ownerGlobal.getComputedStyle(element)
353 return computedStyle?.getPropertyValue('position') == 'fixed'
355 querySelectorAllDeep = (window, selector) ->
356 elements = Array.from(window.document.querySelectorAll(selector))
357 for frame in window.frames
358 elements.push(querySelectorAllDeep(frame, selector)...)
361 scroll = (element, args) ->
362 {method, type, directions, amounts, properties, adjustment, smooth} = args
364 for direction, index in directions
365 amount = amounts[index]
366 options[direction] = -Math.sign(amount) * adjustment + switch type
371 if properties[index] == 'clientHeight'
372 getViewportCappedClientHeight(element)
374 element[properties[index]]
376 Math.min(amount, element[properties[index]])
377 options.behavior = 'smooth' if smooth
378 element[method](options)
380 setAttributes = (element, attributes) ->
381 for attribute, value of attributes
382 element.setAttribute(attribute, value)
385 setHover = (element, hover) ->
386 method = if hover then 'addPseudoClassLock' else 'removePseudoClassLock'
387 while element.parentElement
388 nsIDomUtils[method](element, ':hover')
389 element = element.parentElement
397 constructor: ({start: @value = 0, @step = 1}) ->
398 tick: -> @value += @step
404 on: (event, listener) ->
405 (@listeners[event] ?= new Set()).add(listener)
407 off: (event, listener) ->
408 @listeners[event]?.delete(listener)
410 emit: (event, data) ->
411 @listeners[event]?.forEach((listener) ->
415 has = (obj, prop) -> Object::hasOwnProperty.call(obj, prop)
417 # Check if `search` exists in `string` (case insensitively). Returns `false` if
418 # `string` doesn’t exist or isn’t a string, such as `<SVG element>.className`.
419 includes = (string, search) ->
420 return false unless typeof string == 'string'
421 return string.toLowerCase().includes(search)
424 nextTick = (window, fn) -> window.setTimeout((-> fn()) , 0)
426 regexEscape = (s) -> s.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
428 removeDuplicates = (array) -> Array.from(new Set(array))
430 # Remove duplicate characters from string (case insensitive).
431 removeDuplicateCharacters = (str) ->
432 return removeDuplicates( str.toLowerCase().split('') ).join('')
434 # Calls `fn` repeatedly, with at least `interval` ms between each call.
435 interval = (window, interval, fn) ->
437 currentIntervalId = null
440 currentIntervalId = window.setTimeout((-> fn(next)), interval)
443 window.clearTimeout(currentIntervalId)
451 formatError = (error) ->
452 stack = String(error.stack?.formattedStack ? error.stack ? '')
454 .filter((line) -> line.includes('.xpi!'))
455 .map((line) -> ' ' + line.replace(/(?:\/<)*@.+\.xpi!/g, '@'))
457 return "#{error}\n#{stack}"
459 getCurrentLocation = ->
460 window = getCurrentWindow()
461 return new window.URL(window.gBrowser.selectedBrowser.currentURI.spec)
463 getCurrentWindow = -> nsIWindowMediator.getMostRecentWindow('navigator:browser')
465 hasEventListeners = (element, type) ->
466 for listener in nsIEventListenerService.getListenerInfoFor(element)
467 if listener.listenerObject and listener.type == type
471 loadCss = (uriString) ->
472 uri = Services.io.newURI(uriString, null, null)
473 method = nsIStyleSheetService.AUTHOR_SHEET
474 unless nsIStyleSheetService.sheetRegistered(uri, method)
475 nsIStyleSheetService.loadAndRegisterSheet(uri, method)
477 nsIStyleSheetService.unregisterSheet(uri, method)
480 observe = (topic, observer) ->
481 observer = {observe: observer} if typeof observer == 'function'
482 Services.obs.addObserver(observer, topic, false)
484 Services.obs.removeObserver(observer, topic, false)
487 openPopup = (popup) ->
488 window = popup.ownerGlobal
489 # Show the popup so it gets a height and width.
490 popup.openPopupAtScreen(0, 0)
491 # Center the popup inside the window.
493 window.screenX + window.outerWidth / 2 - popup.clientWidth / 2,
494 window.screenY + window.outerHeight / 2 - popup.clientHeight / 2
497 openTab = (window, url, options) ->
499 window.TreeStyleTabService?.readyToOpenChildTab(gBrowser.selectedTab)
500 gBrowser.loadOneTab(url, options)
502 writeToClipboard = (text) -> nsIClipboardHelper.copyString(text)
516 blurActiveBrowserElement
529 getViewportCappedClientHeight
546 removeDuplicateCharacters