Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add shadow dom v1 support to DOM atoms #5762

Merged
merged 9 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 53 additions & 40 deletions javascript/atoms/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,45 +565,40 @@ bot.dom.isShown_ = function(elem, ignoreOpacity, parentsDisplayedFn) {
* @return {boolean} Whether or not the element is visible.
*/
bot.dom.isShown = function(elem, opt_ignoreOpacity) {
var displayed;

if (bot.dom.IS_SHADOW_DOM_ENABLED) {
// Any element with a display style equal to 'none' or that has an ancestor
// with display style equal to 'none' is not shown.
displayed = function(e) {
if (bot.dom.getEffectiveStyle(e, 'display') == 'none') {
/**
* Determines whether an element or its parents have `display: none` set
* @param {!Node} e the element
* @return {boolean}
*/
function displayed(e) {
if (bot.dom.isElement(e)) {
var elem = /** @type {!Element} */ (e);
if (bot.dom.getEffectiveStyle(elem, '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;
}
} while (elem && elem.nodeType != goog.dom.NodeType.ELEMENT);
return !parent || displayed(parent);
};
} else {
// Any element with a display style equal to 'none' or that has an ancestor
// with display style equal to 'none' is not shown.
displayed = function(e) {
if (bot.dom.getEffectiveStyle(e, 'display') == 'none') {
}

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

if (bot.dom.IS_SHADOW_DOM_ENABLED && (parent instanceof ShadowRoot)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jleyba take a look at this now. so we no longer need an if/else as the parent node traversal already checks if shadow dom v0 or v1 is supported.

and this means now we automatically get the document/shadowRoot assertion further down and disconnected elements are falsey.

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;
}
var parent = bot.dom.getParentElement(e);
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);
}

return bot.dom.isShown_(elem, !!opt_ignoreOpacity, displayed);
};

Expand Down Expand Up @@ -1260,12 +1255,22 @@ bot.dom.getOpacityNonIE_ = function(elem) {
*/
bot.dom.getParentNodeInComposedDom = function(node) {
var /**@type {Node}*/ parent = node.parentNode;

// 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
return node.assignedSlot ? node.assignedSlot.parentNode : null;
}

// Shadow DOM V0 (deprecated)
if (node.getDestinationInsertionPoints) {
var destinations = node.getDestinationInsertionPoints();
if (destinations.length > 0) {
parent = destinations[destinations.length - 1];
return destinations[destinations.length - 1];
}
}

return parent;
};

Expand All @@ -1289,16 +1294,22 @@ bot.dom.appendVisibleTextLinesFromNodeInComposedDom_ = function(
} else if (bot.dom.isElement(node)) {
var castElem = /** @type {!Element} */ (node);

if (bot.dom.isElement(node, 'CONTENT')) {
if (bot.dom.isElement(node, 'CONTENT') || bot.dom.isElement(node, 'SLOT')) {
var parentNode = node;
while (parentNode.parentNode) {
parentNode = parentNode.parentNode;
}
if (parentNode instanceof ShadowRoot) {
// If the element is <content> and we're inside a shadow DOM then just
// If the element is <content> and we're inside a shadow DOM then just
// append the contents of the nodes that have been distributed into it.
var contentElem = /** @type {!Object} */ (node);
goog.array.forEach(contentElem.getDistributedNodes(), function(node) {
var shadowChildren;
if (bot.dom.isElement(node, 'CONTENT')) {
shadowChildren = contentElem.getDistributedNodes();
} else {
shadowChildren = contentElem.assignedNodes();
}
goog.array.forEach(shadowChildren, function(node) {
bot.dom.appendVisibleTextLinesFromNodeInComposedDom_(
node, lines, shown, whitespace, textTransform);
});
Expand Down Expand Up @@ -1353,8 +1364,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