Skip to content

Commit

Permalink
core: save HTMLElement.p.matches function to avoid conflict (#6283)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored and paulirish committed Oct 19, 2018
1 parent 42091b2 commit dcccf6d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
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 @@ -1191,7 +1191,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 dcccf6d

Please sign in to comment.