Skip to content

Commit

Permalink
rewrite shadow dom tests for v1
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j committed Apr 19, 2018
1 parent 3db00d0 commit ae758b0
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 208 deletions.
51 changes: 31 additions & 20 deletions javascript/atoms/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,24 +574,26 @@ bot.dom.isShown = function(elem, opt_ignoreOpacity) {
if (bot.dom.getEffectiveStyle(e, 'display') == 'none') {
return false;
}
var parent;
do {
parent = bot.dom.getParentNodeInComposedDom(e);
if (parent instanceof ShadowRoot) {
if (parent.host.shadowRoot != parent) {
// There is a younger shadow root, which will take precedence over
// the shadow this element is in, thus this element won't be
// displayed.
return false;
} else {
parent = parent.host;
}
} else if (parent && (parent.nodeType == goog.dom.NodeType.DOCUMENT ||
parent.nodeType == goog.dom.NodeType.DOCUMENT_FRAGMENT)) {
parent = null;

var parent = bot.dom.getParentNodeInComposedDom(e);

if (parent instanceof ShadowRoot) {
if (parent.host.shadowRoot != parent) {
// There is a younger shadow root, which will take precedence over
// the shadow this element is in, thus this element won't be
// displayed.
return false;
} else {
parent = parent.host;
}
} while (elem && elem.nodeType != goog.dom.NodeType.ELEMENT);
return !parent || displayed(parent);
}

if (parent && (parent.nodeType == goog.dom.NodeType.DOCUMENT ||
parent.nodeType == goog.dom.NodeType.DOCUMENT_FRAGMENT)) {
return true;
}

return parent && displayed(parent);
};
} else {
// Any element with a display style equal to 'none' or that has an ancestor
Expand All @@ -601,7 +603,7 @@ bot.dom.isShown = function(elem, opt_ignoreOpacity) {
return false;
}
var parent = bot.dom.getParentElement(e);
return !parent || displayed(parent);
return parent && displayed(parent);
};
}
return bot.dom.isShown_(elem, !!opt_ignoreOpacity, displayed);
Expand Down Expand Up @@ -1260,12 +1262,19 @@ bot.dom.getOpacityNonIE_ = function(elem) {
*/
bot.dom.getParentNodeInComposedDom = function(node) {
var /**@type {Node}*/ parent = node.parentNode;
// Shadow DOM V0 (deprecated)
if (node.getDestinationInsertionPoints) {
var destinations = node.getDestinationInsertionPoints();
if (destinations.length > 0) {
parent = destinations[destinations.length - 1];
}
}
// Shadow DOM v1
if (parent.shadowRoot && node.assignedSlot !== undefined) {
// Can be null on purpose, meaning it has no parent as
// it hasn't yet been slotted
parent = node.assignedSlot;
}
return parent;
};

Expand Down Expand Up @@ -1359,8 +1368,10 @@ bot.dom.isNodeDistributedIntoShadowDom = function(node) {
elemOrText = /** @type {!Text} */ (node);
}
return elemOrText != null &&
elemOrText.getDestinationInsertionPoints &&
elemOrText.getDestinationInsertionPoints().length > 0;
(elemOrText.assignedSlot != null ||
(elemOrText.getDestinationInsertionPoints &&
elemOrText.getDestinationInsertionPoints().length > 0)
);
};


Expand Down
Loading

0 comments on commit ae758b0

Please sign in to comment.