-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Changes from 7 commits
b792946
3db00d0
ae758b0
914f185
4ccbd05
de818f1
4c0c8a3
a34a2a7
8da6a65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove the /**
* @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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be
Since this function should only be called when checking the visibility of a parent, we're assuming the DOM is well-formed and There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
return false; | ||
} | ||
|
||
var parent = bot.dom.getParentNodeInComposedDom(e); | ||
|
||
if (bot.dom.IS_SHADOW_DOM_ENABLED && (parent instanceof ShadowRoot)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}; | ||
|
||
|
@@ -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; | ||
}; | ||
|
||
|
@@ -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); | ||
}); | ||
|
@@ -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) | ||
); | ||
}; | ||
|
||
|
||
|
There was a problem hiding this comment.
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 truthyThere was a problem hiding this comment.
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 ofelem
, looks like a bug.There was a problem hiding this comment.
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.