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 7 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
91 changes: 50 additions & 41 deletions javascript/atoms/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,43 +567,34 @@ bot.dom.isShown_ = function(elem, ignoreOpacity, parentsDisplayedFn) {
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') {
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i removed this loop because elem was never modified, so it would either run once or forever... guess we never ended up with this being as truthy

Copy link
Member

Choose a reason for hiding this comment

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

I suspect there should be parent instead of elem, looks like a bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we have some recursion below to deal with traversing the tree anyway, so i guess this removal is fine.

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') {
// 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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: remove the var displayed; declaration above and change this to:

/**
 * @param {?Node} e
 * @return {boolean}
 */
function displayed(e) {

The extra type annotations are annoying, but helps improve the compiler's accuracy in type checking

if (bot.dom.isElement(e) && bot.dom.getEffectiveStyle(e, 'display') == 'none') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be

!bot.dom.isElement(e) || bot.dom.getEffectiveStyle(e, 'display') == 'none'

Since this function should only be called when checking the visibility of a parent, we're assuming the DOM is well-formed and e will never be a text node.

Copy link
Contributor Author

@43081j 43081j Apr 23, 2018

Choose a reason for hiding this comment

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

i would rather keep it as is to be honest. if its an element, we check the style, otherwise we continue traversing. we shouldn't really be making assumptions on what can and can't be a parent, but instead just traversing what we are given IMO.
in cases when its not an element, we continue traversing. its very simple this way with no assumptions.

return false;
}

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 +1251,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 +1290,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 +1360,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