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

Commit e327439

Browse files
committed
Merge pull request #457 from webcomponents/bicknellr/activeElement
Fixes #110: Adds support for Document/ShadowRoot activeElement.
2 parents fedfe02 + 4d3dd37 commit e327439

5 files changed

Lines changed: 380 additions & 0 deletions

File tree

src/ShadowDOM/wrappers/Document.js

Lines changed: 29 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,34 @@
5051
defineWrapGetter(Document, 'body');
5152
defineWrapGetter(Document, 'head');
5253

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+
5382
// document cannot be overridden so we override a bunch of its methods
5483
// directly on the instance.
5584

src/ShadowDOM/wrappers/HTMLElement.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@
333333
}
334334

335335
[
336+
'focus',
336337
'getBoundingClientRect',
337338
'getClientRects',
338339
'scrollIntoView'

src/ShadowDOM/wrappers/ShadowRoot.js

Lines changed: 28 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,33 @@
7172

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

0 commit comments

Comments
 (0)