Skip to content
This repository was archived by the owner on Sep 20, 2019. It is now read-only.

Commit 9c89b27

Browse files
committed
Adds support for Document/ShadowRoot activeElement.
1 parent fedfe02 commit 9c89b27

4 files changed

Lines changed: 396 additions & 0 deletions

File tree

src/ShadowDOM/wrappers/Document.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
var ShadowRoot = scope.wrappers.ShadowRoot;
2121
var TreeScope = scope.TreeScope;
2222
var cloneNode = scope.cloneNode;
23+
var defineGetter = scope.defineGetter;
2324
var defineWrapGetter = scope.defineWrapGetter;
2425
var elementFromPoint = scope.elementFromPoint;
2526
var forwardMethodsToWrapper = scope.forwardMethodsToWrapper;
@@ -50,6 +51,31 @@
5051
defineWrapGetter(Document, 'body');
5152
defineWrapGetter(Document, 'head');
5253

54+
defineGetter(Document, 'activeElement', function() {
55+
var activeElement = wrap(unwrap(this).activeElement);
56+
57+
// Loop while activeElement is not a shallow child of this document.
58+
while (!this.contains(activeElement)) {
59+
var lastHost = activeElement;
60+
// Iterate until we hit activeElement's containing ShadowRoot (which
61+
// isn't this one) or document.
62+
while (activeElement.parentNode) {
63+
activeElement = activeElement.parentNode;
64+
}
65+
66+
// If we've reached a ShadowRoot, move to its host.
67+
if (activeElement.host) {
68+
activeElement = activeElement.host;
69+
// Otherwise, we've reached a different document - this document is
70+
// not an ancestor of the active element.
71+
} else {
72+
return null;
73+
}
74+
}
75+
76+
return activeElement;
77+
});
78+
5379
// document cannot be overridden so we override a bunch of its methods
5480
// directly on the instance.
5581

src/ShadowDOM/wrappers/ShadowRoot.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
var setInnerHTML = scope.setInnerHTML;
2222
var unsafeUnwrap = scope.unsafeUnwrap;
2323
var unwrap = scope.unwrap;
24+
var wrap = scope.wrap;
2425

2526
var shadowHostTable = new WeakMap();
2627
var nextOlderShadowTreeTable = new WeakMap();
@@ -71,6 +72,30 @@
7172

7273
getSelection: function() {
7374
return document.getSelection();
75+
},
76+
77+
get activeElement() {
78+
var activeElement = wrap(unwrap(this).ownerDocument.activeElement);
79+
80+
// Loop while activeElement is not a shallow child of this ShadowRoot.
81+
while (!this.contains(activeElement)) {
82+
// Iterate until we hit activeElement's containing ShadowRoot (which
83+
// isn't this one) or document.
84+
while (activeElement.parentNode) {
85+
activeElement = activeElement.parentNode;
86+
}
87+
88+
// If we've reached a ShadowRoot, move to its host.
89+
if (activeElement.host) {
90+
activeElement = activeElement.host;
91+
// Otherwise, we've reached a document - this ShadowRoot is not an
92+
// ancestor of the active element.
93+
} else {
94+
return null;
95+
}
96+
}
97+
98+
return activeElement;
7499
}
75100
});
76101

0 commit comments

Comments
 (0)