1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 function focus(element
) {
6 // Focus the target element in order to send keys to it.
7 // First, the currently active element is blurred, if it is different from
8 // the target element. We do not want to blur an element unnecessarily,
9 // because this may cause us to lose the current cursor position in the
11 // Secondly, we focus the target element.
12 // Thirdly, if the target element is newly focused and is a text input, we
13 // set the cursor position at the end.
14 // Fourthly, we check if the new active element is the target element. If not,
17 // - |document.activeElement| is the currently focused element, or body if
18 // no element is focused
19 // - Even if |document.hasFocus()| returns true and the active element is
20 // the body, sometimes we still need to focus the body element for send
21 // keys to work. Not sure why
22 // - You cannot focus a descendant of a content editable node
23 // - V8 throws a TypeError when calling setSelectionRange for a non-text
24 // input, which still have setSelectionRange defined. For chrome 29+, V8
25 // throws a DOMException with code InvalidStateError.
26 var doc
= element
.ownerDocument
|| element
;
27 var prevActiveElement
= doc
.activeElement
;
28 if (element
!= prevActiveElement
&& prevActiveElement
)
29 prevActiveElement
.blur();
31 if (element
!= prevActiveElement
&& element
.value
&&
32 element
.value
.length
&& element
.setSelectionRange
) {
34 element
.setSelectionRange(element
.value
.length
, element
.value
.length
);
36 if (!(error
instanceof TypeError
) && !(error
instanceof DOMException
&&
37 error
.code
== DOMException
.INVALID_STATE_ERR
))
42 var activeElement
= doc
.activeElement
;
43 // If the element is in a shadow DOM, then as far as the document is
44 // concerned, the shadow host is the active element. We need to go through the
45 // tree of shadow DOMs to check that the element we gave focus to is now
47 if (element
!= activeElement
) {
48 var shadowRoot
= activeElement
.shadowRoot
;
50 var activeElement
= shadowRoot
.activeElement
;
51 if (element
== activeElement
) {
52 // the shadow DOM's active element is our requested element. We're good.
55 // The shadow DOM's active element isn't our requested element, check to
56 // see if there's a nested shadow DOM.
57 shadowRoot
= activeElement
.shadowRoot
;
60 if (element
!= activeElement
)
61 throw new Error('cannot focus element');