Skip to content

Commit 9e5016d

Browse files
43081jshs96c
authored andcommitted
rewrite shadow dom tests for v1
1 parent c3c1fcd commit 9e5016d

File tree

2 files changed

+230
-208
lines changed

2 files changed

+230
-208
lines changed

javascript/atoms/dom.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -574,24 +574,26 @@ bot.dom.isShown = function(elem, opt_ignoreOpacity) {
574574
if (bot.dom.getEffectiveStyle(e, 'display') == 'none') {
575575
return false;
576576
}
577-
var parent;
578-
do {
579-
parent = bot.dom.getParentNodeInComposedDom(e);
580-
if (parent instanceof ShadowRoot) {
581-
if (parent.host.shadowRoot != parent) {
582-
// There is a younger shadow root, which will take precedence over
583-
// the shadow this element is in, thus this element won't be
584-
// displayed.
585-
return false;
586-
} else {
587-
parent = parent.host;
588-
}
589-
} else if (parent && (parent.nodeType == goog.dom.NodeType.DOCUMENT ||
590-
parent.nodeType == goog.dom.NodeType.DOCUMENT_FRAGMENT)) {
591-
parent = null;
577+
578+
var parent = bot.dom.getParentNodeInComposedDom(e);
579+
580+
if (parent instanceof ShadowRoot) {
581+
if (parent.host.shadowRoot != parent) {
582+
// There is a younger shadow root, which will take precedence over
583+
// the shadow this element is in, thus this element won't be
584+
// displayed.
585+
return false;
586+
} else {
587+
parent = parent.host;
592588
}
593-
} while (elem && elem.nodeType != goog.dom.NodeType.ELEMENT);
594-
return !parent || displayed(parent);
589+
}
590+
591+
if (parent && (parent.nodeType == goog.dom.NodeType.DOCUMENT ||
592+
parent.nodeType == goog.dom.NodeType.DOCUMENT_FRAGMENT)) {
593+
return true;
594+
}
595+
596+
return parent && displayed(parent);
595597
};
596598
} else {
597599
// Any element with a display style equal to 'none' or that has an ancestor
@@ -601,7 +603,7 @@ bot.dom.isShown = function(elem, opt_ignoreOpacity) {
601603
return false;
602604
}
603605
var parent = bot.dom.getParentElement(e);
604-
return !parent || displayed(parent);
606+
return parent && displayed(parent);
605607
};
606608
}
607609
return bot.dom.isShown_(elem, !!opt_ignoreOpacity, displayed);
@@ -1260,12 +1262,19 @@ bot.dom.getOpacityNonIE_ = function(elem) {
12601262
*/
12611263
bot.dom.getParentNodeInComposedDom = function(node) {
12621264
var /**@type {Node}*/ parent = node.parentNode;
1265+
// Shadow DOM V0 (deprecated)
12631266
if (node.getDestinationInsertionPoints) {
12641267
var destinations = node.getDestinationInsertionPoints();
12651268
if (destinations.length > 0) {
12661269
parent = destinations[destinations.length - 1];
12671270
}
12681271
}
1272+
// Shadow DOM v1
1273+
if (parent.shadowRoot && node.assignedSlot !== undefined) {
1274+
// Can be null on purpose, meaning it has no parent as
1275+
// it hasn't yet been slotted
1276+
parent = node.assignedSlot;
1277+
}
12691278
return parent;
12701279
};
12711280

@@ -1359,8 +1368,10 @@ bot.dom.isNodeDistributedIntoShadowDom = function(node) {
13591368
elemOrText = /** @type {!Text} */ (node);
13601369
}
13611370
return elemOrText != null &&
1362-
elemOrText.getDestinationInsertionPoints &&
1363-
elemOrText.getDestinationInsertionPoints().length > 0;
1371+
(elemOrText.assignedSlot != null ||
1372+
(elemOrText.getDestinationInsertionPoints &&
1373+
elemOrText.getDestinationInsertionPoints().length > 0)
1374+
);
13641375
};
13651376

13661377

0 commit comments

Comments
 (0)