Fix: change disabled state after the content is set (#701)

This commit is contained in:
eight 2019-06-01 17:10:44 +08:00 committed by narcolepticinsomniac
parent eb6888e44c
commit 60693e8c3f
2 changed files with 9 additions and 12 deletions

View File

@ -89,17 +89,18 @@ const APPLY = (() => {
// for getPreventDefault which got removed in FF59 https://bugzil.la/691151 // for getPreventDefault which got removed in FF59 https://bugzil.la/691151
const EVENT_NAME = chrome.runtime.id; const EVENT_NAME = chrome.runtime.id;
let ready; let ready;
return (el, content) => return (el, content, disabled) =>
checkPageScript().then(ok => { checkPageScript().then(ok => {
if (!ok) { if (!ok) {
const disabled = el.disabled;
el.textContent = content; el.textContent = content;
// https://github.com/openstyles/stylus/issues/693
el.disabled = disabled; el.disabled = disabled;
} else { } else {
const detail = pageObject({ const detail = pageObject({
method: 'setStyleContent', method: 'setStyleContent',
id: el.id, id: el.id,
content content,
disabled
}); });
window.dispatchEvent(new CustomEvent(EVENT_NAME, {detail})); window.dispatchEvent(new CustomEvent(EVENT_NAME, {detail}));
} }
@ -119,13 +120,12 @@ const APPLY = (() => {
const available = checkStyleApplied(); const available = checkStyleApplied();
if (available) { if (available) {
window.addEventListener(EVENT_NAME, function handler(e) { window.addEventListener(EVENT_NAME, function handler(e) {
const {method, id, content} = e.detail; const {method, id, content, disabled} = e.detail;
if (method === 'setStyleContent') { if (method === 'setStyleContent') {
const el = document.getElementById(id); const el = document.getElementById(id);
if (!el) { if (!el) {
return; return;
} }
const disabled = el.disabled;
el.textContent = content; el.textContent = content;
el.disabled = disabled; el.disabled = disabled;
} else if (method === 'orphan') { } else if (method === 'orphan') {

View File

@ -68,7 +68,7 @@ function createStyleInjector({compare, setStyleContent, onUpdate}) {
return update(style); return update(style);
} }
style.el = createStyle(style.id); style.el = createStyle(style.id);
const pending = setStyleContent(style.el, style.code); const pending = setStyleContent(style.el, style.code, !enabled);
table.set(style.id, style); table.set(style.id, style);
const nextIndex = list.findIndex(i => compare(i, style) > 0); const nextIndex = list.findIndex(i => compare(i, style) > 0);
if (nextIndex < 0) { if (nextIndex < 0) {
@ -78,8 +78,6 @@ function createStyleInjector({compare, setStyleContent, onUpdate}) {
document.documentElement.insertBefore(style.el, list[nextIndex].el); document.documentElement.insertBefore(style.el, list[nextIndex].el);
list.splice(nextIndex, 0, style); list.splice(nextIndex, 0, style);
} }
// disabled flag is read-only when not attached to a document
style.el.disabled = !enabled;
return pending; return pending;
} }
@ -110,7 +108,7 @@ function createStyleInjector({compare, setStyleContent, onUpdate}) {
oldEl.parentNode.insertBefore(style.el, oldEl.nextSibling); oldEl.parentNode.insertBefore(style.el, oldEl.nextSibling);
style.el.disabled = !enabled; style.el.disabled = !enabled;
} }
return setStyleContent(style.el, code) return setStyleContent(style.el, code, !enabled)
.then(() => oldEl && oldEl.remove()); .then(() => oldEl && oldEl.remove());
} }
@ -153,13 +151,12 @@ function createStyleInjector({compare, setStyleContent, onUpdate}) {
function sort() { function sort() {
list.sort(compare); list.sort(compare);
for (const style of list) { for (const style of list) {
// moving an element resets its 'disabled' state
const disabled = style.el.disabled;
// FIXME: do we need this? // FIXME: do we need this?
// const copy = document.importNode(el, true); // const copy = document.importNode(el, true);
// el.textContent += ' '; // invalidate CSSOM cache // el.textContent += ' '; // invalidate CSSOM cache
document.documentElement.appendChild(style.el); document.documentElement.appendChild(style.el);
style.el.disabled = disabled; // moving an element resets its 'disabled' state
style.el.disabled = !enabled;
} }
} }