2011-12-04 16:33:56 +01:00
|
|
|
/**
|
2013-06-14 19:37:41 +02:00
|
|
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
2011-12-04 16:33:56 +01:00
|
|
|
* This helps other people to understand this code better and helps them to improve it.
|
|
|
|
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
|
|
|
|
*/
|
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function makeCSSManager(emptyStylesheetTitle, doc) {
|
2020-11-23 19:24:19 +01:00
|
|
|
if (doc === true) {
|
2013-06-14 19:37:41 +02:00
|
|
|
doc = 'parent';
|
|
|
|
} else if (!doc) {
|
|
|
|
doc = 'inner';
|
|
|
|
}
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function getSheetByTitle(title) {
|
2020-11-23 19:24:19 +01:00
|
|
|
if (doc === 'parent') {
|
2013-06-14 19:37:41 +02:00
|
|
|
win = window.parent.parent;
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (doc === 'inner') {
|
2013-06-14 19:37:41 +02:00
|
|
|
win = window;
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (doc === 'outer') {
|
2013-06-14 19:37:41 +02:00
|
|
|
win = window.parent;
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
|
|
|
throw 'Unknown dynamic style container';
|
2013-06-14 19:37:41 +02:00
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
const allSheets = win.document.styleSheets;
|
2013-06-14 19:37:41 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
for (let i = 0; i < allSheets.length; i++) {
|
|
|
|
const s = allSheets[i];
|
|
|
|
if (s.title == title) {
|
2011-07-07 19:59:34 +02:00
|
|
|
return s;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2013-06-14 19:37:41 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const browserSheet = getSheetByTitle(emptyStylesheetTitle);
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function browserRules() {
|
2011-07-07 19:59:34 +02:00
|
|
|
return (browserSheet.cssRules || browserSheet.rules);
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function browserDeleteRule(i) {
|
2011-03-26 14:10:41 +01:00
|
|
|
if (browserSheet.deleteRule) browserSheet.deleteRule(i);
|
|
|
|
else browserSheet.removeRule(i);
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function browserInsertRule(i, selector) {
|
2020-11-23 19:24:19 +01:00
|
|
|
if (browserSheet.insertRule) browserSheet.insertRule(`${selector} {}`, i);
|
2011-03-26 14:10:41 +01:00
|
|
|
else browserSheet.addRule(selector, null, i);
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
const selectorList = [];
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function indexOfSelector(selector) {
|
2020-11-23 19:24:19 +01:00
|
|
|
for (let i = 0; i < selectorList.length; i++) {
|
|
|
|
if (selectorList[i] == selector) {
|
2011-07-07 19:59:34 +02:00
|
|
|
return i;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function selectorStyle(selector) {
|
2020-11-23 19:24:19 +01:00
|
|
|
let i = indexOfSelector(selector);
|
|
|
|
if (i < 0) {
|
2011-03-26 14:10:41 +01:00
|
|
|
// add selector
|
|
|
|
browserInsertRule(0, selector);
|
|
|
|
selectorList.splice(0, 0, selector);
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
return browserRules().item(i).style;
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function removeSelectorStyle(selector) {
|
2020-11-23 19:24:19 +01:00
|
|
|
const i = indexOfSelector(selector);
|
|
|
|
if (i >= 0) {
|
2011-03-26 14:10:41 +01:00
|
|
|
browserDeleteRule(i);
|
|
|
|
selectorList.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-07 19:59:34 +02:00
|
|
|
return {
|
2020-11-23 19:24:19 +01:00
|
|
|
selectorStyle,
|
|
|
|
removeSelectorStyle,
|
|
|
|
info() {
|
|
|
|
return `${selectorList.length}:${browserRules().length}`;
|
|
|
|
},
|
2011-07-07 19:59:34 +02:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2012-01-16 02:23:48 +01:00
|
|
|
|
|
|
|
exports.makeCSSManager = makeCSSManager;
|