2 # Copyright Simon Lydell 2015, 2016.
4 # This file is part of VimFx.
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
20 # This file is the equivalent to commands.coffee, but for frame scripts,
21 # allowing interaction with web page content. Most “commands” here have the
22 # same name as the command in commands.coffee that calls it. There are also a
23 # few more generalized “commands” used in more than one place.
25 markableElements = require('./markable-elements')
26 prefs = require('./prefs')
27 SelectionManager = require('./selection')
28 translate = require('./translate')
29 utils = require('./utils')
30 viewportUtils = require('./viewport')
32 {FORWARD, BACKWARD} = SelectionManager
33 {isProperLink, isTextInputElement, isTypingElement, isContentEditable} = utils
35 XULDocument = Ci.nsIDOMXULDocument
37 # <http://www.w3.org/html/wg/drafts/html/master/dom.html#wai-aria>
38 CLICKABLE_ARIA_ROLES = [
39 'link', 'button', 'tab'
40 'checkbox', 'radio', 'combobox', 'option', 'slider', 'textbox'
41 'menuitem', 'menuitemcheckbox', 'menuitemradio'
44 createComplementarySelectors = (selectors) ->
47 selectors.map((selector) -> ":not(#{selector})").join('')
50 FOLLOW_DEFAULT_SELECTORS = createComplementarySelectors([
51 'a', 'button', 'input', 'textarea', 'select'
52 '[role]', '[contenteditable]'
55 FOLLOW_SELECTABLE_SELECTORS =
56 createComplementarySelectors(['div', 'span']).reverse()
60 commands.go_up_path = ({vim, count = 1}) ->
61 {pathname} = vim.content.location
62 newPathname = pathname.replace(/// (?: [^/]+(/|$) ){1,#{count}} $ ///, '')
63 if newPathname == pathname
64 vim.notify(translate('notification.go_up_path.limit'))
66 vim.content.location.pathname = newPathname
68 commands.go_to_root = ({vim}) ->
69 # `.origin` is `'null'` (as a string) on `about:` pages.
70 if "#{vim.content.location.origin}/" in [vim.content.location.href, 'null/']
71 vim.notify(translate('notification.go_up_path.limit'))
73 vim.content.location.href = vim.content.location.origin
75 commands.scroll = (args) ->
77 return unless activeElement = utils.getActiveElement(vim.content)
79 # If no element is focused on the page, the active element is the topmost
80 # `<body>`, and blurring it is a no-op. If it is scrollable, it means that
81 # you can’t blur it in order to scroll `<html>`. Therefore it may only be
82 # scrolled if it has been explicitly focused.
83 if vim.state.scrollableElements.has(activeElement) and
84 (activeElement != vim.content.document.body or
85 vim.state.explicitBodyFocus)
88 vim.state.scrollableElements.filterSuitableDefault()
89 viewportUtils.scroll(element, args)
91 commands.mark_scroll_position = ({vim, keyStr, notify = true}) ->
92 element = vim.state.scrollableElements.filterSuitableDefault()
93 vim.state.marks[keyStr] =
94 if element.ownerDocument.documentElement.localName == 'svg'
95 [element.ownerGlobal.scrollY, element.ownerGlobal.scrollX]
97 [element.scrollTop, element.scrollLeft]
99 vim.notify(translate('notification.mark_scroll_position.success', keyStr))
101 commands.scroll_to_mark = (args) ->
102 {vim, amounts: keyStr} = args
103 unless keyStr of vim.state.marks
104 vim.notify(translate('notification.scroll_to_mark.none', keyStr))
107 args.amounts = vim.state.marks[keyStr]
108 element = vim.state.scrollableElements.filterSuitableDefault()
109 viewportUtils.scroll(element, args)
111 helper_follow = (options, matcher, {vim, pass}) ->
112 {id, combine = true, selectors = FOLLOW_DEFAULT_SELECTORS} = options
113 if vim.content.document instanceof XULDocument
117 pass = if selectors.length == 2 then 'first' else 'single'
119 vim.state.markerElements = [] if pass in ['single', 'first']
122 filter = (element, getElementShape) ->
123 type = matcher({vim, element, getElementShape})
125 type = vim.hintMatcher(id, element, type)
126 if pass == 'complementary'
127 type = if type then null else 'complementary'
130 shape = getElementShape(element)
132 unless shape.nonCoveredPoint then switch
133 # CodeMirror editor uses a tiny hidden textarea positioned at the caret.
134 # Targeting those are the only reliable way of focusing CodeMirror
135 # editors, and doing so without moving the caret.
136 when id == 'normal' and element.localName == 'textarea' and
137 element.ownerGlobal == vim.content
138 rect = element.getBoundingClientRect()
139 # Use `.clientWidth` instead of `rect.width` because the latter includes
140 # the width of the borders of the textarea, which are unreliable.
141 if element.clientWidth == 1 and rect.height > 0
145 y: rect.top + rect.height / 2
146 offset: {left: 0, top: 0}
148 area: rect.width * rect.height
151 # Putting a large `<input type="file">` inside a smaller wrapper element
152 # with `overflow: hidden;` seems to be a common pattern, used both on
153 # addons.mozilla.org and <https://blueimp.github.io/jQuery-File-Upload/>.
154 when id in ['normal', 'focus'] and element.localName == 'input' and
155 element.type == 'file' and element.parentElement?
156 parentShape = getElementShape(element.parentElement)
157 shape = parentShape if parentShape.area <= shape.area
159 if not shape.nonCoveredPoint and pass == 'complementary'
160 shape = getElementShape(element, -1)
162 return unless shape.nonCoveredPoint
164 originalRect = element.getBoundingClientRect()
165 length = vim.state.markerElements.push({element, originalRect})
166 wrapper = {type, shape, elementIndex: length - 1}
168 if wrapper.type == 'link'
172 # Combine links with the same href.
173 if combine and wrapper.type == 'link' and
174 # If the element has an 'onclick' attribute we cannot be sure that all
175 # links with this href actually do the same thing. On some pages, such
176 # as startpage.com, actual proper links have the 'onclick' attribute,
177 # so we can’t exclude such links in `utils.isProperLink`.
178 not element.hasAttribute?('onclick') and
179 # GitHub’s diff expansion buttons are links with both `href` and
180 # `data-url`. They are JavaScript-powered using the latter attribute.
181 not element.hasAttribute?('data-url')
184 wrapper.parentIndex = parent.elementIndex
185 parent.shape.area += wrapper.shape.area
186 parent.numChildren += 1
188 wrapper.numChildren = 0
189 hrefs[href] = wrapper
194 if pass == 'complementary'
197 selectors[if pass == 'second' then 1 else 0]
199 wrappers: markableElements.find(vim.content, filter, selector)
200 viewport: viewportUtils.getWindowViewport(vim.content)
204 commands.follow = helper_follow.bind(
205 null, {id: 'normal'},
206 ({vim, element, getElementShape}) ->
207 document = element.ownerDocument
208 isXUL = (document instanceof XULDocument)
211 # Bootstrap. Match these before regular links, because especially slider
212 # “buttons” often get the same hint otherwise.
213 when element.hasAttribute?('data-toggle') or
214 element.hasAttribute?('data-dismiss') or
215 element.hasAttribute?('data-slide') or
216 element.hasAttribute?('data-slide-to')
218 when isProperLink(element)
220 when isTypingElement(element)
222 when element.localName in ['a', 'button'] or
223 element.getAttribute?('role') in CLICKABLE_ARIA_ROLES or
224 # <http://www.w3.org/TR/wai-aria/states_and_properties>
225 element.hasAttribute?('aria-controls') or
226 element.hasAttribute?('aria-pressed') or
227 element.hasAttribute?('aria-checked') or
228 (element.hasAttribute?('aria-haspopup') and
229 element.getAttribute?('role') != 'menu')
231 when utils.isFocusable(element) and
232 # Google Drive Documents. The hint for this element would cover the
233 # real hint that allows you to focus the document to start typing.
234 element.id != 'docs-editor'
236 when element != vim.state.scrollableElements.largest and
237 vim.state.scrollableElements.has(element)
239 when element.hasAttribute?('onclick') or
240 element.hasAttribute?('onmousedown') or
241 element.hasAttribute?('onmouseup') or
242 element.hasAttribute?('oncommand') or
244 element.classList?.contains('js-new-tweets-bar') or
245 element.hasAttribute?('data-permalink-path') or
247 element.hasAttribute?('data-app-action') or
248 element.hasAttribute?('data-uri') or
249 element.hasAttribute?('data-page-action') or
250 # Google Drive Document.
251 element.classList?.contains('kix-appview-editor')
253 # Facebook comment fields.
254 when element.parentElement?.classList?.contains('UFIInputContainer')
255 type = 'clickable-special'
256 # Putting markers on `<label>` elements is generally redundant, because
257 # its `<input>` gets one. However, some sites hide the actual `<input>`
258 # but keeps the `<label>` to click, either for styling purposes or to keep
259 # the `<input>` hidden until it is used. In those cases we should add a
260 # marker for the `<label>`. Trying to access `.control` on an element in
261 # `about:config` throws an error, so exclude XUL pages.
262 when not isXUL and element.localName == 'label' and element.control and
263 not getElementShape(element.control).nonCoveredPoint
265 # Last resort checks for elements that might be clickable because of
268 # It is common to listen for clicks on `<html>` or `<body>`. Don’t
269 # waste time on them.
270 element not in [document.documentElement, document.body]) and
271 (utils.includes(element.className, 'button') or
272 utils.includes(element.getAttribute?('aria-label'), 'close') or
273 # Do this last as it’s a potentially expensive check.
274 (utils.hasEventListeners(element, 'click') and
275 # Twitter. The hint for this element would cover the hint for
276 # showing more tweets.
277 not element.classList?.contains('js-new-items-bar-container')))
278 # Make a quick check for likely clickable descendants, to reduce the
279 # number of false positives. the element might be a “button-wrapper” or
280 # a large element with a click-tracking event listener.
281 unless element.querySelector?('a, button, input, [class*=button]')
283 # When viewing an image it should get a marker to toggle zoom. This is the
284 # most unlikely rule to match, so keep it last.
285 when document.body?.childElementCount == 1 and
286 element.localName == 'img' and
287 (element.classList?.contains('overflowing') or
288 element.classList?.contains('shrinkToFit'))
290 type = null if isXUL and element.classList?.contains('textbox-input')
294 commands.follow_in_tab = helper_follow.bind(
295 null, {id: 'tab', selectors: ['a']},
297 type = if isProperLink(element) then 'link' else null
301 commands.follow_copy = helper_follow.bind(
305 when isProperLink(element)
307 when isContentEditable(element)
309 when isTypingElement(element)
316 commands.follow_focus = helper_follow.bind(
317 null, {id: 'focus', combine: false},
320 when element.tabIndex > -1
322 when element != vim.state.scrollableElements.largest and
323 vim.state.scrollableElements.has(element)
330 commands.follow_selectable = helper_follow.bind(
331 null, {id: 'selectable', selectors: FOLLOW_SELECTABLE_SELECTORS},
333 isRelevantTextNode = (node) ->
334 # Ignore whitespace-only text nodes, and single-letter ones (which are
335 # common in many syntax highlighters).
336 return node.nodeType == 3 and node.data.trim().length > 1
338 if Array.some(element.childNodes, isRelevantTextNode)
345 commands.focus_marker_element = ({vim, elementIndex, options}) ->
346 {element} = vim.state.markerElements[elementIndex]
347 # To be able to focus scrollable elements, `FLAG_BYKEY` _has_ to be used.
348 options.flag = 'FLAG_BYKEY' if vim.state.scrollableElements.has(element)
349 utils.focusElement(element, options)
351 vim.setHover(element)
353 commands.click_marker_element = (
354 {vim, elementIndex, type, preventTargetBlank}
356 {element} = vim.state.markerElements[elementIndex]
357 if element.target == '_blank' and preventTargetBlank
358 targetReset = element.target
360 if type == 'clickable-special'
363 isXUL = (element.ownerDocument instanceof XULDocument)
366 if element.localName == 'tab' then ['mousedown'] else 'click-xul'
369 utils.simulateMouseEvents(element, sequence)
370 utils.openDropdown(element)
371 element.target = targetReset if targetReset
373 commands.copy_marker_element = ({vim, elementIndex, property}) ->
374 {element} = vim.state.markerElements[elementIndex]
375 utils.writeToClipboard(element[property])
377 commands.element_text_select = ({vim, elementIndex, full, scroll = false}) ->
378 {element} = vim.state.markerElements[elementIndex]
379 window = element.ownerGlobal
380 selection = window.getSelection()
381 range = window.document.createRange()
383 # Try to scroll the element into view, but keep the caret visible.
385 viewport = viewportUtils.getWindowViewport(window)
386 rect = element.getBoundingClientRect()
388 when rect.bottom > viewport.bottom
390 when rect.top < viewport.top and rect.height < viewport.height
396 prefs.root.get('general.smoothScroll') and
397 prefs.root.get('general.smoothScroll.other')
399 element.scrollIntoView({
401 behavior: if smooth then 'smooth' else 'instant'
405 range.selectNodeContents(element)
407 result = viewportUtils.getFirstNonWhitespace(element)
409 [node, offset] = result
410 range.setStart(node, offset)
411 range.setEnd(node, offset)
413 range.setStartBefore(element)
414 range.setEndBefore(element)
416 utils.clearSelectionDeep(vim.content)
418 selection.addRange(range)
420 commands.follow_pattern = ({vim, type, options}) ->
421 {document} = vim.content
423 # If there’s a `<link rel=prev/next>` element we use that.
424 for link in document.head?.getElementsByTagName('link')
425 # Also support `rel=previous`, just like Google.
426 if type == link.rel.toLowerCase().replace(/^previous$/, 'prev')
427 vim.content.location.href = link.href
430 # Otherwise we look for a link or button on the page that seems to go to the
431 # previous or next page.
432 candidates = document.querySelectorAll(options.pattern_selector)
434 # Note: Earlier patterns should be favored.
437 # Search for the prev/next patterns in the following attributes of the
438 # element. `rel` should be kept as the first attribute, since the standard way
439 # of marking up prev/next links (`rel="prev"` and `rel="next"`) should be
440 # favored. Even though some of these attributes only allow a fixed set of
441 # keywords, we pattern-match them anyways since lots of sites don’t follow the
442 # spec and use the attributes arbitrarily.
443 attrs = options.pattern_attrs
446 # First search in attributes (favoring earlier attributes) as it's likely
447 # that they are more specific than text contexts.
449 for regex in patterns
450 for element in candidates
451 return element if regex.test(element.getAttribute?(attr))
453 # Then search in element contents.
454 for regex in patterns
455 for element in candidates
456 return element if regex.test(element.textContent)
461 utils.simulateMouseEvents(matchingLink, 'click')
462 # When you go to the next page of GitHub’s code search results, the page is
463 # loaded with AJAX. GitHub then annoyingly focuses its search input. This
464 # autofocus cannot be prevented in a reliable way, because the case is
465 # indistinguishable from a button whose job is to focus some text input.
466 # However, in this command we know for sure that we can prevent the next
467 # focus. This must be done _after_ the click has been triggered, since
468 # clicks count as page interactions.
469 vim.markPageInteraction(false)
471 vim.notify(translate("notification.follow_#{type}.none"))
473 commands.focus_text_input = ({vim, count = null}) ->
474 {lastFocusedTextInput} = vim.state
476 if lastFocusedTextInput and utils.isDetached(lastFocusedTextInput)
477 lastFocusedTextInput = null
479 candidates = utils.querySelectorAllDeep(
480 vim.content, 'input, textarea, textbox, [contenteditable]'
482 inputs = Array.filter(candidates, (element) ->
483 return isTextInputElement(element) and utils.area(element) > 0
485 if lastFocusedTextInput and lastFocusedTextInput not in inputs
486 inputs.push(lastFocusedTextInput)
487 inputs.sort((a, b) -> a.tabIndex - b.tabIndex)
489 if inputs.length == 0
490 vim.notify(translate('notification.focus_text_input.none'))
496 when lastFocusedTextInput
497 inputs.indexOf(lastFocusedTextInput) + 1
500 index = Math.min(num, inputs.length) - 1
501 select = (count? or not vim.state.hasFocusedTextInput)
502 utils.focusElement(inputs[index], {select})
503 vim.state.inputs = inputs
505 commands.clear_inputs = ({vim}) ->
506 vim.state.inputs = null
508 commands.move_focus = ({vim, direction}) ->
509 return false unless vim.state.inputs
510 index = vim.state.inputs.indexOf(utils.getActiveElement(vim.content))
511 # If there’s only one input, `<tab>` would cycle to itself, making it feel
512 # like `<tab>` was not working. Then it’s better to let `<tab>` work as it
514 if index == -1 or vim.state.inputs.length <= 1
515 vim.state.inputs = null
519 nextInput = inputs[(index + direction) %% inputs.length]
520 utils.focusElement(nextInput, {select: true})
523 # This is an attempt to enhance Firefox’s native “Find in page” functionality.
524 # Firefox starts searching after the end of the first selection range, or from
525 # the top of the page if there are no selection ranges. If there are frames, the
526 # top-most document in DOM order with selections seems to be used.
528 # Replace the current selection with one single range. (Searching clears the
529 # previous selection anyway.) That single range is either the first visible
530 # range, or a newly created (and collapsed) one at the top of the viewport. This
531 # way we can control where Firefox searches from.
532 commands.find_from_top_of_viewport = ({vim, direction}) ->
533 viewport = viewportUtils.getWindowViewport(vim.content)
535 range = viewportUtils.getFirstVisibleRange(vim.content, viewport)
537 window = range.startContainer.ownerGlobal
538 selection = window.getSelection()
539 utils.clearSelectionDeep(vim.content)
541 # When the next match is in another frame than the current selection (A),
542 # Firefox won’t clear that selection before making a match selection (B) in
543 # the other frame. When searching again, selection B is cleared because
544 # selection A appears further up the viewport. This causes us to search
545 # _again_ from selection A, rather than selection B. In effect, we get stuck
546 # re-selecting selection B over and over. Therefore, collapse the range
547 # first, in case Firefox doesn’t.
549 selection.addRange(range)
550 # Collapsing the range causes backwards search to keep re-selecting the same
551 # match. Therefore, move it one character back.
552 selection.modify('move', 'backward', 'character') if direction == BACKWARD
555 result = viewportUtils.getFirstVisibleText(vim.content, viewport)
557 [textNode, offset] = result
559 utils.clearSelectionDeep(vim.content)
560 window = textNode.ownerGlobal
562 range = window.document.createRange()
563 range.setStart(textNode, offset)
564 range.setEnd(textNode, offset)
565 selection = window.getSelection()
566 selection.addRange(range)
568 commands.esc = (args) ->
570 commands.blur_active_element(args)
572 utils.clearSelectionDeep(vim.content)
574 {document} = vim.content
575 if document.exitFullscreen
576 document.exitFullscreen()
578 document.mozCancelFullScreen()
580 commands.blur_active_element = ({vim}) ->
581 vim.state.explicitBodyFocus = false
582 utils.blurActiveElement(vim.content)
584 helper_create_selection_manager = (vim) ->
585 window = utils.getActiveElement(vim.content)?.ownerGlobal ? vim.content
586 return new SelectionManager(window)
588 commands.enable_caret = ({vim}) ->
589 return unless selectionManager = helper_create_selection_manager(vim)
590 selectionManager.enableCaret()
592 commands.move_caret = ({vim, method, direction, select, count}) ->
593 return unless selectionManager = helper_create_selection_manager(vim)
595 if selectionManager[method]
596 error = selectionManager[method](direction, select)
598 error = selectionManager.moveCaret(method, direction, select)
602 commands.toggle_selection = ({vim, select}) ->
603 return unless selectionManager = helper_create_selection_manager(vim)
605 vim.notify(translate('notification.toggle_selection.enter'))
607 selectionManager.collapse()
609 commands.toggle_selection_direction = ({vim}) ->
610 return unless selectionManager = helper_create_selection_manager(vim)
611 selectionManager.reverseDirection()
613 commands.get_selection = ({vim}) ->
614 return unless selectionManager = helper_create_selection_manager(vim)
615 return selectionManager.selection.toString()
617 commands.collapse_selection = ({vim}) ->
618 return unless selectionManager = helper_create_selection_manager(vim)
619 selectionManager.collapse()
621 commands.clear_selection = ({vim}) ->
622 utils.clearSelectionDeep(vim.content)
624 module.exports = commands