2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
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 HTMLAnchorElement = Ci.nsIDOMHTMLAnchorElement
25 HTMLButtonElement = Ci.nsIDOMHTMLButtonElement
26 HTMLInputElement = Ci.nsIDOMHTMLInputElement
27 HTMLTextAreaElement = Ci.nsIDOMHTMLTextAreaElement
28 HTMLSelectElement = Ci.nsIDOMHTMLSelectElement
29 HTMLFrameElement = Ci.nsIDOMHTMLFrameElement
30 HTMLIFrameElement = Ci.nsIDOMHTMLIFrameElement
31 XULDocument = Ci.nsIDOMXULDocument
32 XULButtonElement = Ci.nsIDOMXULButtonElement
33 XULControlElement = Ci.nsIDOMXULControlElement
34 XULMenuListElement = Ci.nsIDOMXULMenuListElement
35 XULTextBoxElement = Ci.nsIDOMXULTextBoxElement
43 on: (event, listener) ->
44 (@listeners[event] ?= []).push(listener)
46 emit: (event, data) ->
47 for listener in @listeners[event] ? []
51 blurActiveElement = (window, { force = false } = {}) ->
52 # Only blur focusable elements, in order to interfere with the browser as
54 activeElement = getActiveElement(window)
55 if activeElement and (activeElement.tabIndex > -1 or force)
58 getActiveElement = (window) ->
59 { activeElement } = window.document
60 if activeElement instanceof HTMLFrameElement or
61 activeElement instanceof HTMLIFrameElement
62 return getActiveElement(activeElement.contentWindow)
66 # Focus an element and tell Firefox that the focus happened because of a user
67 # keypress (not just because some random programmatic focus).
68 focusElement = (element, options = {}) ->
69 focusManager = Cc['@mozilla.org/focus-manager;1']
70 .getService(Ci.nsIFocusManager)
71 focusManager.setFocus(element, focusManager.FLAG_BYKEY)
72 element.select?() if options.select
74 isProperLink = (element) ->
75 # `.getAttribute` is used below instead of `.hasAttribute` to exclude `<a
76 # href="">`s used as buttons on some sites.
77 return element.getAttribute('href') and
78 (element instanceof HTMLAnchorElement or
79 element.ownerDocument instanceof XULDocument) and
80 not element.href.endsWith('#') and
81 not element.href.startsWith('javascript:')
83 isTextInputElement = (element) ->
84 return (element instanceof HTMLInputElement and element.type in [
85 'text', 'search', 'tel', 'url', 'email', 'password', 'number'
87 element instanceof HTMLTextAreaElement or
88 # `<select>` elements can also receive text input: You may type the
89 # text of an item to select it.
90 element instanceof HTMLSelectElement or
91 element instanceof XULMenuListElement or
92 element instanceof XULTextBoxElement
94 isContentEditable = (element) ->
95 return element.isContentEditable or
96 isGoogleEditable(element)
98 isGoogleEditable = (element) ->
99 # `g_editable` is a non-standard attribute commonly used by Google.
100 return element.getAttribute?('g_editable') == 'true' or
101 element.ownerDocument.body?.getAttribute('g_editable') == 'true'
103 isActivatable = (element) ->
104 return element instanceof HTMLAnchorElement or
105 element instanceof HTMLButtonElement or
106 (element instanceof HTMLInputElement and element.type in [
107 'button', 'submit', 'reset', 'image'
109 element instanceof XULButtonElement
111 isAdjustable = (element) ->
112 return element instanceof HTMLInputElement and element.type in [
113 'checkbox', 'radio', 'file', 'color'
114 'date', 'time', 'datetime', 'datetime-local', 'month', 'week'
116 element instanceof XULControlElement or
117 # Youtube special case.
118 element.classList?.contains('html5-video-player') or
119 element.classList?.contains('ytp-button')
121 getFocusType = (event) ->
122 target = event.originalTarget
124 when isTextInputElement(target) or isContentEditable(target)
126 when isActivatable(target)
128 when isAdjustable(target)
134 return element.clientWidth * element.clientHeight
137 sss = Cc['@mozilla.org/content/style-sheet-service;1']
138 .getService(Ci.nsIStyleSheetService)
139 uri = Services.io.newURI("chrome://vimfx/skin/#{ name }.css", null, null)
140 method = sss.AUTHOR_SHEET
141 unless sss.sheetRegistered(uri, method)
142 sss.loadAndRegisterSheet(uri, method)
145 sss.unregisterSheet(uri, method)
148 # Simulate mouse click with a full chain of events. ('command' is for XUL
150 eventSequence = ['mouseover', 'mousedown', 'mouseup', 'click', 'command']
151 simulateClick = (element) ->
152 window = element.ownerDocument.defaultView
154 for type in eventSequence
155 mouseEvent = new window.MouseEvent(type, {
156 # Let the event bubble in order to trigger delegated event listeners.
158 # Make the event cancelable so that `<a href="#">` can be used as a
159 # JavaScript-powered button without scrolling to the top of the page.
162 element.dispatchEvent(mouseEvent)
163 simulatedEvents[type] = mouseEvent
164 return simulatedEvents
166 listen = (element, eventName, listener) ->
167 element.addEventListener(eventName, listener, USE_CAPTURE)
169 element.removeEventListener(eventName, listener, USE_CAPTURE)
172 suppressEvent = (event) ->
173 event.preventDefault()
174 event.stopPropagation()
176 # Write a string to the system clipboard.
177 writeToClipboard = (text) ->
178 clipboardHelper = Cc['@mozilla.org/widget/clipboardhelper;1']
179 .getService(Ci.nsIClipboardHelper)
180 clipboardHelper.copyString(text)
182 # Executes function `func` and measures how much time it took.
183 timeIt = (func, name) ->
186 console.timeEnd(name)
189 createBox = (document, className, parent = null, text = null) ->
190 box = document.createElement('box')
191 box.className = className
192 box.textContent = text if text?
193 parent.appendChild(box) if parent?
196 setAttributes = (element, attributes) ->
197 for attribute, value of attributes
198 element.setAttribute(attribute, value)
201 insertText = (input, value) ->
202 { selectionStart, selectionEnd } = input
204 input.value[0...selectionStart] + value + input.value[selectionEnd..]
205 input.selectionStart = input.selectionEnd = selectionStart + value.length
207 openTab = (window, url, options) ->
208 { gBrowser } = window
209 window.TreeStyleTabService?.readyToOpenChildTab(gBrowser.selectedTab)
210 gBrowser.loadOneTab(url, options)
212 # Remove duplicate characters from string (case insensitive).
213 removeDuplicateCharacters = (str) ->
214 return removeDuplicates( str.toLowerCase().split('') ).join('')
216 # Escape a string to render it usable in regular expressions.
217 regexpEscape = (s) -> s and s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
219 removeDuplicates = (array) ->
220 # coffeelint: disable=no_backticks
221 return `[...new Set(array)]`
222 # coffeelint: enable=no_backticks
224 formatError = (error) ->
225 stack = String(error.stack?.formattedStack ? error.stack ? '')
227 .filter((line) -> line.includes('.xpi!'))
228 .map((line) -> ' ' + line.replace(/(?:\/<)*@.+\.xpi!/g, '@'))
230 return "#{ error }\n#{ stack }"
232 observe = (topic, observer) ->
233 observer = {observe: observer} if typeof observer == 'function'
234 Services.obs.addObserver(observer, topic, false)
236 Services.obs.removeObserver(observer, topic, false)
239 has = Function::call.bind(Object::hasOwnProperty)
242 constructor: ({start, step}) ->
245 tick: -> @value += @step
247 getCurrentWindow = ->
248 windowMediator = Cc['@mozilla.org/appshell/window-mediator;1']
249 .getService(Components.interfaces.nsIWindowMediator)
250 return windowMediator.getMostRecentWindow('navigator:browser')
252 getCurrentLocation = (browser = null) ->
253 browser = getCurrentWindow().gBrowser.selectedBrowser unless browser?
254 return new browser.ownerGlobal.URL(browser.currentURI.spec)
284 removeDuplicateCharacters