[go: nahoru, domu]

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

core(page-functions): don't try to clone a ShadowRoot #9079

Merged
merged 9 commits into from
Jun 7, 2019
Prev Previous commit
Next Next commit
bug-fix: Avoid an excess DOM size error #9079
  • Loading branch information
NickolasBenakis committed May 29, 2019
commit 5de9be9b9258e5fbf759c348cb8e956940e4a8c2
22 changes: 12 additions & 10 deletions lighthouse-core/lib/page-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,20 @@ 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);

return (match && match[0]) || '';
try {
const clone = element.cloneNode();
ignoreAttrs.forEach(attribute =>{
clone.removeAttribute(attribute);
Copy link
Member

Choose a reason for hiding this comment

The 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 Element.

removeAttribute and outerHTML (and more or less localName) exist only on Element.

Maybe we should type it as @param {Element|ShadowRoot} element and (if we don't need that localName check) have the conditional be just if (element instanceof ShadowRoot) element = element.host; (with a comment somewhere explaining that this function handles the ShadowRoot case).

It's still playing a little fast and loose with types (it doesn't help that the tsc built in types don't recognize ShadowRoot as a global value), but it simplifies things a bit.

});
const reOpeningTag = /^[\s\S]*?>/;
const match = clone.outerHTML.match(reOpeningTag);
return (match && match[0]) || '';
} catch (_) {
return `<${element.localName}>`;
Copy link
Collaborator
@connorjclark connorjclark Jun 3, 2019

Choose a reason for hiding this comment

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

can this entire range fail? if it's just the clone, could you reduce the try/catch to just that part?

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
Expand Down