| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 function focus(element) { | 5 function focus(element) { |
| 6 // Focus the target element in order to send keys to it. | 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 | 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, | 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 | 9 // because this may cause us to lose the current cursor position in the |
| 10 // element. | 10 // element. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 if (element != prevActiveElement && element.value && | 31 if (element != prevActiveElement && element.value && |
| 32 element.value.length && element.setSelectionRange) { | 32 element.value.length && element.setSelectionRange) { |
| 33 try { | 33 try { |
| 34 element.setSelectionRange(element.value.length, element.value.length); | 34 element.setSelectionRange(element.value.length, element.value.length); |
| 35 } catch (error) { | 35 } catch (error) { |
| 36 if (!(error instanceof TypeError) && !(error instanceof DOMException && | 36 if (!(error instanceof TypeError) && !(error instanceof DOMException && |
| 37 error.code == DOMException.INVALID_STATE_ERR)) | 37 error.code == DOMException.INVALID_STATE_ERR)) |
| 38 throw error; | 38 throw error; |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 if (element != doc.activeElement) | 41 var activeElement = doc.activeElement; |
| 42 // If the element is in a shadow DOM, then as far as the document is concerned |
| 43 // the shadow host is the active element. We need to go through the tree of |
| 44 // shadow DOMs to check that the element we gave focus to is now active |
| 45 if (element != activeElement) { |
| 46 var shadowRoot = activeElement.shadowRoot; |
| 47 while (shadowRoot) { |
| 48 var activeElement = shadowRoot.activeElement; |
| 49 if (element == activeElement) { |
| 50 // the shadow DOM's active element is our requested element. We're good. |
| 51 break; |
| 52 } |
| 53 // The shadow DOM's active element isn't our requested element, check to |
| 54 // see if there's a nested shadow DOM. |
| 55 shadowRoot = activeElement.shadowRoot; |
| 56 } |
| 57 } |
| 58 |
| 59 if (element != activeElement) |
| 42 throw new Error('cannot focus element'); | 60 throw new Error('cannot focus element'); |
| 43 } | 61 } |
| OLD | NEW |