-
Notifications
You must be signed in to change notification settings - Fork 9.6k
core(page-functions): don't try to clone a ShadowRoot #9079
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
Changes from 6 commits
a5f9343
5de9be9
bf80f17
f34fd58
51449a0
a1528b6
13d4753
29b209d
cbd0e48
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 |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| // @ts-nocheck | ||
| 'use strict'; | ||
|
|
||
| /* global window document Node */ | ||
| /* global window document Node ShadowRoot */ | ||
|
|
||
| /** | ||
| * Helper functions that are passed by `toString()` by Driver to be evaluated in target page. | ||
|
|
@@ -113,18 +113,24 @@ function getElementsInDocument(selector) { | |
| */ | ||
| /* istanbul ignore next */ | ||
| function getOuterHTMLSnippet(element, ignoreAttrs = []) { | ||
| const clone = element.cloneNode(); | ||
|
|
||
| ignoreAttrs.forEach(attribute =>{ | ||
| clone.removeAttribute(attribute); | ||
| }); | ||
|
|
||
| const reOpeningTag = /^[\s\S]*?>/; | ||
| const match = clone.outerHTML.match(reOpeningTag); | ||
| try { | ||
| if (ShadowRoot.prototype.isPrototypeOf(element) && element.host && element.localName !== 'a') { | ||
| element = element.host; | ||
| } | ||
|
|
||
| return (match && match[0]) || ''; | ||
| const clone = element.cloneNode(); | ||
| ignoreAttrs.forEach(attribute =>{ | ||
| clone.removeAttribute(attribute); | ||
|
Contributor
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. the types on this whole function get kind of broken if we aren't really accepting just
Maybe we should type it as It's still playing a little fast and loose with types (it doesn't help that the tsc built in types don't recognize |
||
| }); | ||
| const reOpeningTag = /^[\s\S]*?>/; | ||
| const match = clone.outerHTML.match(reOpeningTag); | ||
| return (match && match[0]) || ''; | ||
| } catch (_) { | ||
| return `<${element.localName}>`; | ||
|
Collaborator
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. can this entire range fail? if it's just the a comment in the catch block would be useful. |
||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Computes a memory/CPU performance benchmark index to determine rough device class. | ||
| * @see https://docs.google.com/spreadsheets/d/1E0gZwKsxegudkjJl8Fki_sOwHKpqgXwt8aBAfuUaB8A/edit?usp=sharing | ||
|
|
||
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.
nit:
reads a little simpler than the prototype check, but
non-nit: can we also add a comment to this line? Both to explain the shadow root case, but also to explain the
localNamecheck. I don't really understand it...ShadowRootdoesn't have alocalName, so by that point in the conditional won't the comparison always be true?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.
(also according to spec "Shadow roots’s associated
hostis never null.")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.
@patrickhulce since you added this line, what's the deal with the
element.localName !== 'a'check?Uh oh!
There was an error while loading. Please reload this page.
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.
This entire piece is straight up copied from the dom-size audit that filters this way, so you'd have to ask Eric as to why it was written this way :)
It indeed seemed like overkill and I thought about trying to extract this to a
isShadowRootfunction with a canonical version to do this or something but bringing in all the baggage ofpage-functionswith dependencies into this PR seemed unfair.I don't see why we can't just use
element instanceof ShadowRootpersonally, but I assumed Eric thought of something I didn't which is a pretty safe bet when it comes to our comparative web component/shadow dom knowledge 😆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.
:P I commited the suggested changes,
is there any other issue that we need to tackle?
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.
OK! After consulting the old texts (#2131), I think this was from adapting earlier code (which identified shadow roots just by looking for an
element.hostbut had to rule out anchors because they also have ahostproperty). That code was wrapped without modification in theShadowRoot.prototypecheck in that PR, which I believe was the error (but since it's always true, it doesn't cause any bad behavior).I'm going to remove based on the spec and my own testing saying it's fine. I'll take responsibility for any failures :)