Skip to content

Commit

Permalink
Avoid using Element.classList in DOM surveyor
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Apr 3, 2022
1 parent 3d6aead commit 1423330
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/js/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,6 @@ vAPI.DOMFilterer = class {
// Extract all classes/ids: these will be passed to the cosmetic
// filtering engine, and in return we will obtain only the relevant
// CSS selectors.
const reWhitespace = /\s/;

// https://github.com/gorhill/uBlock/issues/672
// http://www.w3.org/TR/2014/REC-html5-20141028/infrastructure.html#space-separated-tokens
Expand All @@ -1039,19 +1038,21 @@ vAPI.DOMFilterer = class {
queriedIds.add(s);
};

// https://github.com/uBlockOrigin/uBlock-issues/discussions/2076
// Performance: avoid using Element.classList
const classesFromNode = (node, out) => {
const s = node.className;
if ( typeof s !== 'string' || s.length === 0 ) { return; }
if ( reWhitespace.test(s) === false ) {
if ( queriedClasses.has(s) ) { return; }
out.push(s);
queriedClasses.add(s);
return;
}
for ( const s of node.classList.values() ) {
if ( queriedClasses.has(s) ) { continue; }
out.push(s);
queriedClasses.add(s);
const s = node.getAttribute('class');
if ( typeof s !== 'string' ) { return; }
const len = s.length;
for ( let beg = 0, end = 0, token = ''; beg < len; beg += 1 ) {
end = s.indexOf(' ', beg);
if ( end === beg ) { continue; }
if ( end === -1 ) { end = len; }
token = s.slice(beg, end);
beg = end;
if ( queriedClasses.has(token) ) { continue; }
out.push(token);
queriedClasses.add(token);
}
};

Expand Down

0 comments on commit 1423330

Please sign in to comment.