Don't assume `document.documentElement` is non-null (#3857)

* Fix uBlockOrigin/uBlock-issues#1756

This PR fixes uBlockOrigin/uBlock-issues#1756.

* fix dom-inspector.js

* more explicit if statements

* these changes should also be safe
This commit is contained in:
Eli Grey 2021-10-14 06:08:08 -07:00 committed by GitHub
parent ebd8ab6037
commit 1285f78e05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 20 deletions

View File

@ -788,7 +788,7 @@
rmattr();
if ( /\bstay\b/.test(behavior) === false ) { return; }
const observer = new MutationObserver(mutationHandler);
observer.observe(document.documentElement, {
observer.observe(document, {
attributes: true,
attributeFilter: tokens,
childList: true,
@ -847,7 +847,7 @@
rmclass();
if ( /\bstay\b/.test(behavior) === false ) { return; }
const observer = new MutationObserver(mutationHandler);
observer.observe(document.documentElement, {
observer.observe(document, {
attributes: true,
attributeFilter: [ 'class' ],
childList: true,
@ -1555,7 +1555,7 @@
asyncTimer = window.requestAnimationFrame(cleanVideo);
};
var observer = new MutationObserver(cleanVideoAsync);
observer.observe(document.documentElement, { childList: true, subtree: true });
observer.observe(document, { childList: true, subtree: true });
})();

View File

@ -393,7 +393,7 @@ vAPI.SafeAnimationFrame = class {
const startMutationObserver = function() {
if ( domLayoutObserver !== undefined ) { return; }
domLayoutObserver = new MutationObserver(observerHandler);
domLayoutObserver.observe(document.documentElement, {
domLayoutObserver.observe(document, {
//attributeFilter: [ 'class', 'id' ],
//attributes: true,
childList: true,
@ -469,7 +469,7 @@ vAPI.injectScriptlet = function(doc, text) {
try {
script = doc.createElement('script');
script.appendChild(doc.createTextNode(text));
(doc.head || doc.documentElement).appendChild(script);
(doc.head || doc.documentElement || doc).appendChild(script);
} catch (ex) {
}
if ( script ) {
@ -1171,10 +1171,14 @@ vAPI.DOMFilterer = class {
// Look-up safe-only selectors to mitigate probability of
// html/body elements of erroneously being targeted.
const ids = [], classes = [];
idFromNode(document.documentElement, ids);
idFromNode(document.body, ids);
classesFromNode(document.documentElement, classes);
classesFromNode(document.body, classes);
if (document.documentElement !== null) {
idFromNode(document.documentElement, ids);
classesFromNode(document.documentElement, classes);
}
if (document.body !== null) {
idFromNode(document.body, ids);
classesFromNode(document.body, classes);
}
if ( ids.length !== 0 || classes.length !== 0 ) {
messaging.send('contentscript', {
what: 'retrieveGenericCosmeticSelectors',

View File

@ -247,7 +247,7 @@ const domLayout = (function() {
const getLayoutData = function() {
var layout = [];
var stack = [];
var node = document.documentElement;
var node = document;
var domNode;
var lvl = 0;
@ -264,15 +264,17 @@ const domLayout = (function() {
continue;
}
// sibling
if ( node.nextElementSibling === null ) {
do {
node = stack.pop();
if (node instanceof Element) {
if ( node.nextElementSibling === null ) {
do {
node = stack.pop();
if ( !node ) { break; }
lvl -= 1;
} while ( node.nextElementSibling === null );
if ( !node ) { break; }
lvl -= 1;
} while ( node.nextElementSibling === null );
if ( !node ) { break; }
}
node = node.nextElementSibling;
}
node = node.nextElementSibling;
}
return layout;
@ -547,7 +549,9 @@ const cosmeticFilterMapper = (function() {
const reset = function() {
roRedNodes.clear();
incremental(document.documentElement);
if (document.documentElement !== null) {
incremental(document.documentElement);
}
};
const shutdown = function() {
@ -812,7 +816,7 @@ const shutdown = function() {
domLayout.shutdown();
vAPI.MessagingConnection.disconnectFrom(loggerConnectionId);
window.removeEventListener('scroll', onScrolled, true);
document.documentElement.removeChild(pickerRoot);
pickerRoot.remove();
pickerRoot = svgRoot = null;
};
@ -977,7 +981,7 @@ pickerRoot.style.cssText = [
].join(' !important;\n');
pickerRoot.addEventListener('load', ev => { bootstrap(ev); });
document.documentElement.appendChild(pickerRoot);
(document.documentElement || document).appendChild(pickerRoot);
/******************************************************************************/