Skip to content

Commit

Permalink
core: save native HTMLElement.prototype.matches function to avoid con…
Browse files Browse the repository at this point in the history
…flict (#5934)
  • Loading branch information
connorjclark committed Oct 15, 2018
1 parent 2b12fc2 commit 03617b3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
}, 1500);
</script>

<object id="5934a"></object>
<object id="5934b"></object>
<script>
document.getElementById("5934a").matches = "See #5934";
Object.defineProperty(document.getElementById("5934b"), 'matches', {
value: "See #5934"
});
</script>

<!-- Note: these will only fail when using the static-server.js, which supports the ?delay=true param.
If you're using your own server, the resource will load instantly and the
stylesheets will be ignored for being below the threshold. -->
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;`);
}

/**
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

0 comments on commit 03617b3

Please sign in to comment.