diff options
| author | Stephen Blott | 2015-03-06 11:47:39 +0000 |
|---|---|---|
| committer | Stephen Blott | 2015-03-06 11:47:39 +0000 |
| commit | 29eb444f2fb519a0cae32363d8462aa06ef133a7 (patch) | |
| tree | 6bd3289189ac14fee2b4c86feed735b3ef48997a /lib/dom_utils.coffee | |
| parent | c2638cd00b19e04bdfed164de6b1a3d4051cfbd9 (diff) | |
| parent | b959fa5c6b36ba19340f195d822b4b3cca8ccd2d (diff) | |
| download | vimium-29eb444f2fb519a0cae32363d8462aa06ef133a7.tar.bz2 | |
Merge branch 'fix-focus-html5-inputs'
Diffstat (limited to 'lib/dom_utils.coffee')
| -rw-r--r-- | lib/dom_utils.coffee | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index f53e28d1..0231f994 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -167,15 +167,20 @@ DomUtils = node = node.parentNode false - # True if element contains the active selection range. + # True if element is editable and contains the active selection range. isSelected: (element) -> + selection = document.getSelection() if element.isContentEditable - node = document.getSelection()?.anchorNode + node = selection.anchorNode node and @isDOMDescendant element, node else - # Note. This makes the wrong decision if the user has placed the caret at the start of element. We - # cannot distinguish that case from the user having made no selection. - element.selectionStart? and element.selectionEnd? and element.selectionEnd != 0 + if selection.type == "Range" and selection.isCollapsed + # The selection is inside the Shadow DOM of a node. We can check the node it registers as being + # before, since this represents the node whose Shadow DOM it's inside. + containerNode = selection.anchorNode.childNodes[selection.anchorOffset] + element == containerNode # True if the selection is inside the Shadow DOM of our element. + else + false simulateSelect: (element) -> # If element is already active, then we don't move the selection. However, we also won't get a new focus @@ -185,11 +190,17 @@ DomUtils = handlerStack.bubbleEvent "click", target: element else element.focus() - unless @isSelected element - # When focusing a textbox (without an existing selection), put the selection caret at the end of the - # textbox's contents. For some HTML5 input types (eg. date) we can't position the caret, so we wrap - # this with a try. - try element.setSelectionRange(element.value.length, element.value.length) + # If the cursor is at the start of the element's contents, send it to the end. Motivation: + # * the end is a more useful place to focus than the start, + # * this way preserves the last used position (except when it's at the beginning), so the user can + # 'resume where they left off'. + # NOTE(mrmr1993): Some elements throw an error when we try to access their selection properties, so + # wrap this with a try. + try + if element.selectionStart == 0 and element.selectionEnd == 0 + element.setSelectionRange element.value.length, element.value.length + + simulateClick: (element, modifiers) -> modifiers ||= {} |
