move style elements after head/body regardless of prior stuff

fixes #284
This commit is contained in:
tophf 2017-12-05 08:50:07 +03:00
parent edfca7bfed
commit 2b22494f3f

View File

@ -388,7 +388,7 @@
let sorting = false; let sorting = false;
let timer; let timer;
// allow any types of elements between ours, except for the following: // allow any types of elements between ours, except for the following:
const ORDERED_TAGS = ['head', 'body', 'style', 'link']; const ORDERED_TAGS = ['head', 'body', 'frameset', 'style', 'link'];
init(); init();
return; return;
@ -396,11 +396,8 @@
function init() { function init() {
docRootObserver = new MutationObserver(sortStyleElements); docRootObserver = new MutationObserver(sortStyleElements);
Object.assign(docRootObserver, {start, stop}); Object.assign(docRootObserver, {start, stop});
if (!chrome.app) {
// compensate for Firefox missing a step when loading the tab in background
setTimeout(sortStyleElements); setTimeout(sortStyleElements);
} }
}
function start({sort = false} = {}) { function start({sort = false} = {}) {
if (sort && sortStyleMap()) { if (sort && sortStyleMap()) {
sortStyleElements(); sortStyleElements();
@ -434,26 +431,25 @@
} }
} }
function sortStyleElements() { function sortStyleElements() {
let expected = document.body || document.head; let prevExpected = document.body || document.head;
if (!expected || sorting) { if (!prevExpected || sorting) {
return; return;
} }
for (const el of styleElements.values()) { for (const el of styleElements.values()) {
if (!isMovable(el)) { if (!isMovable(el)) {
continue; continue;
} }
let prev = el.previousElementSibling; while (true) {
while (prev !== expected) { const next = prevExpected.nextElementSibling;
if (prev && isSkippable(prev)) { if (next && isSkippable(next)) {
expected = prev; prevExpected = next;
prev = prev.nextElementSibling; } else if (moveAfter(el, prevExpected)) {
} else if (!moveAfter(el, expected)) { prevExpected = el;
return;
} else {
break; break;
} else {
return;
} }
} }
expected = el;
} }
if (sorting) { if (sorting) {
sorting = false; sorting = false;