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: save native HTMLElement.prototype.matches function #6283

Merged
merged 1 commit into from
Oct 19, 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
17 changes: 17 additions & 0 deletions lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@
</head>
<body>

<!-- Various sites like to assign HTMLElements a custom `matches` property. See issue #5934 -->

<!-- EmbeddedContent will process these elements -->
<object id="5934a"></object>
<object id="5934b"></object>

<script>
// Ensure gatherers still work when individual elements override '.matches'
document.getElementById("5934a").matches = "blahblah";
Object.defineProperty(document.getElementById("5934b"), 'matches', {
value: "blahblah"
});

// Ensure gatherers still work when the prototype is messed with
HTMLElement.prototype.matches = { value: "blahblah" };
</script>

<div>
<h2>Do better web tester page</h2>
<span>Hi there!</span>
Expand Down
3 changes: 2 additions & 1 deletion lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,8 @@ class Driver {
async cacheNatives() {
await this.evaluateScriptOnNewDocument(`window.__nativePromise = Promise;
window.__nativeError = Error;
window.__nativeURL = URL;`);
window.__nativeURL = URL;
window.__ElementMatches = Element.prototype.matches;`);
Copy link
Member

Choose a reason for hiding this comment

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

👍👍 on the __ElementMatches instead of modifying the prototype

}

/**
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/lib/page-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function getElementsInDocument(selector) {
/** @param {NodeListOf<Element>} nodes */
const _findAllElements = nodes => {
for (let i = 0, el; el = nodes[i]; ++i) {
if (!selector || el.matches(selector)) {
if (!selector || window.__ElementMatches.call(el, selector)) {
results.push(el);
}
// If the element has a shadow root, dig deeper.
Expand Down