cssmanager: Refactor CSS manager creation to avoid race condition

Safari takes a while to initialize `document.styleSheets`, which
results in a race condition when loading the pad. Avoid the race
condition by accessing the CSSStyleSheet objects directly from the
HTMLStyleElement DOM objects.
This commit is contained in:
Richard Hansen 2021-03-19 20:50:01 -04:00 committed by John McLear
parent e2bfe2fd10
commit 3ad1d0a74f
5 changed files with 20 additions and 60 deletions

View file

@ -22,34 +22,7 @@
* limitations under the License.
*/
const makeCSSManager = (emptyStylesheetTitle, doc) => {
if (doc === true) {
doc = 'parent';
} else if (!doc) {
doc = 'inner';
}
const getSheetByTitle = (title) => {
let win;
if (doc === 'parent') {
win = window.parent.parent;
} else if (doc === 'inner') {
win = window;
} else if (doc === 'outer') {
win = window.parent;
} else {
throw new Error('Unknown dynamic style container');
}
for (const s of win.document.styleSheets) {
if (s.title === title) return s;
}
const err = new Error(`no sheet with title ${title} in doc ${doc}`)
console.error(err);
throw err;
};
const browserSheet = getSheetByTitle(emptyStylesheetTitle);
exports.makeCSSManager = (browserSheet) => {
const browserRules = () => (browserSheet.cssRules || browserSheet.rules);
const browserDeleteRule = (i) => {
@ -97,5 +70,3 @@ const makeCSSManager = (emptyStylesheetTitle, doc) => {
info: () => `${selectorList.length}:${browserRules().length}`,
};
};
exports.makeCSSManager = makeCSSManager;