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 timer;
// 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();
return;
@ -396,11 +396,8 @@
function init() {
docRootObserver = new MutationObserver(sortStyleElements);
Object.assign(docRootObserver, {start, stop});
if (!chrome.app) {
// compensate for Firefox missing a step when loading the tab in background
setTimeout(sortStyleElements);
}
}
function start({sort = false} = {}) {
if (sort && sortStyleMap()) {
sortStyleElements();
@ -434,26 +431,25 @@
}
}
function sortStyleElements() {
let expected = document.body || document.head;
if (!expected || sorting) {
let prevExpected = document.body || document.head;
if (!prevExpected || sorting) {
return;
}
for (const el of styleElements.values()) {
if (!isMovable(el)) {
continue;
}
let prev = el.previousElementSibling;
while (prev !== expected) {
if (prev && isSkippable(prev)) {
expected = prev;
prev = prev.nextElementSibling;
} else if (!moveAfter(el, expected)) {
return;
} else {
while (true) {
const next = prevExpected.nextElementSibling;
if (next && isSkippable(next)) {
prevExpected = next;
} else if (moveAfter(el, prevExpected)) {
prevExpected = el;
break;
} else {
return;
}
}
expected = el;
}
if (sorting) {
sorting = false;