Validate document nodes before use (#493)

* Validate document.body before use in loadScripts

This also fixes an issue where reject wasn't being passed to loadScriptSentinel.

* Validate document nodes before use in _getSiteColor

* Validate document.body before use in _getViewport

* Validate document.body before use in setContentScale

* Validate document.body before use in docImposterCreate
This commit is contained in:
toasted-nutbread 2020-05-02 13:00:46 -04:00 committed by GitHub
parent 51032d1eca
commit c4ea9321dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 9 deletions

View File

@ -28,6 +28,9 @@ function docSetImposterStyle(style, propertyName, value) {
} }
function docImposterCreate(element, isTextarea) { function docImposterCreate(element, isTextarea) {
const body = document.body;
if (body === null) { return [null, null]; }
const elementStyle = window.getComputedStyle(element); const elementStyle = window.getComputedStyle(element);
const elementRect = element.getBoundingClientRect(); const elementRect = element.getBoundingClientRect();
const documentRect = document.documentElement.getBoundingClientRect(); const documentRect = document.documentElement.getBoundingClientRect();
@ -78,7 +81,7 @@ function docImposterCreate(element, isTextarea) {
} }
container.appendChild(imposter); container.appendChild(imposter);
document.body.appendChild(container); body.appendChild(container);
// Adjust size // Adjust size
const imposterRect = imposter.getBoundingClientRect(); const imposterRect = imposter.getBoundingClientRect();

View File

@ -162,7 +162,9 @@ class DisplayFloat extends Display {
} }
setContentScale(scale) { setContentScale(scale) {
document.body.style.fontSize = `${scale}em`; const body = document.body;
if (body === null) { return; }
body.style.fontSize = `${scale}em`;
} }
async getDocumentTitle() { async getDocumentTitle() {

View File

@ -389,8 +389,13 @@ class Popup {
_getSiteColor() { _getSiteColor() {
const color = [255, 255, 255]; const color = [255, 255, 255];
Popup._addColor(color, Popup._getColorInfo(window.getComputedStyle(document.documentElement).backgroundColor)); const {documentElement, body} = document;
Popup._addColor(color, Popup._getColorInfo(window.getComputedStyle(document.body).backgroundColor)); if (documentElement !== null) {
Popup._addColor(color, Popup._getColorInfo(window.getComputedStyle(documentElement).backgroundColor));
}
if (body !== null) {
Popup._addColor(color, Popup._getColorInfo(window.getComputedStyle(body).backgroundColor));
}
const dark = (color[0] < 128 && color[1] < 128 && color[2] < 128); const dark = (color[0] < 128 && color[1] < 128 && color[2] < 128);
return dark ? 'dark' : 'light'; return dark ? 'dark' : 'light';
} }
@ -575,10 +580,11 @@ class Popup {
} }
} }
const body = document.body;
return { return {
left: 0, left: 0,
top: 0, top: 0,
right: document.body.clientWidth, right: (body !== null ? body.clientWidth : 0),
bottom: window.innerHeight bottom: window.innerHeight
}; };
} }

View File

@ -31,8 +31,13 @@ const dynamicLoader = (() => {
} }
function loadScripts(urls) { function loadScripts(urls) {
return new Promise((resolve) => { return new Promise((resolve, reject) => {
const parent = document.body; const parent = document.body;
if (parent === null) {
reject(new Error('Missing body'));
return;
}
for (const url of urls) { for (const url of urls) {
const node = parent.querySelector(`script[src='${escapeCSSAttribute(url)}']`); const node = parent.querySelector(`script[src='${escapeCSSAttribute(url)}']`);
if (node !== null) { continue; } if (node !== null) { continue; }
@ -43,12 +48,11 @@ const dynamicLoader = (() => {
parent.appendChild(script); parent.appendChild(script);
} }
loadScriptSentinel(resolve); loadScriptSentinel(parent, resolve, reject);
}); });
} }
function loadScriptSentinel(resolve, reject) { function loadScriptSentinel(parent, resolve, reject) {
const parent = document.body;
const script = document.createElement('script'); const script = document.createElement('script');
const sentinelEventName = 'dynamicLoaderSentinel'; const sentinelEventName = 'dynamicLoaderSentinel';