|
20 | 20 | var ShadowRoot = scope.wrappers.ShadowRoot; |
21 | 21 | var TreeScope = scope.TreeScope; |
22 | 22 | var cloneNode = scope.cloneNode; |
| 23 | + var defineGetter = scope.defineGetter; |
23 | 24 | var defineWrapGetter = scope.defineWrapGetter; |
24 | 25 | var elementFromPoint = scope.elementFromPoint; |
25 | 26 | var forwardMethodsToWrapper = scope.forwardMethodsToWrapper; |
|
50 | 51 | defineWrapGetter(Document, 'body'); |
51 | 52 | defineWrapGetter(Document, 'head'); |
52 | 53 |
|
| 54 | + defineGetter(Document, 'activeElement', function() { |
| 55 | + var unwrappedActiveElement = unwrap(this).activeElement; |
| 56 | + if (!unwrappedActiveElement || !unwrappedActiveElement.nodeType) return null; |
| 57 | + |
| 58 | + var activeElement = wrap(unwrappedActiveElement); |
| 59 | + |
| 60 | + // Loop while activeElement is not a shallow child of this document. |
| 61 | + while (!this.contains(activeElement)) { |
| 62 | + var lastHost = activeElement; |
| 63 | + // Iterate until we hit activeElement's containing ShadowRoot (which |
| 64 | + // isn't this one) or document. |
| 65 | + while (activeElement.parentNode) { |
| 66 | + activeElement = activeElement.parentNode; |
| 67 | + } |
| 68 | + |
| 69 | + // If we've reached a ShadowRoot, move to its host. |
| 70 | + if (activeElement.host) { |
| 71 | + activeElement = activeElement.host; |
| 72 | + // Otherwise, we've reached a different document - this document is |
| 73 | + // not an ancestor of the active element. |
| 74 | + } else { |
| 75 | + return null; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return activeElement; |
| 80 | + }); |
| 81 | + |
53 | 82 | // document cannot be overridden so we override a bunch of its methods |
54 | 83 | // directly on the instance. |
55 | 84 |
|
|
0 commit comments