lint: cssmanager.js
parent
d9f3bb0e39
commit
2929ba9894
|
@ -1,3 +1,5 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
||||||
* This helps other people to understand this code better and helps them to improve it.
|
* This helps other people to understand this code better and helps them to improve it.
|
||||||
|
@ -20,14 +22,15 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function makeCSSManager(emptyStylesheetTitle, doc) {
|
const makeCSSManager = (emptyStylesheetTitle, doc) => {
|
||||||
if (doc === true) {
|
if (doc === true) {
|
||||||
doc = 'parent';
|
doc = 'parent';
|
||||||
} else if (!doc) {
|
} else if (!doc) {
|
||||||
doc = 'inner';
|
doc = 'inner';
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSheetByTitle(title) {
|
const getSheetByTitle = (title) => {
|
||||||
|
let win;
|
||||||
if (doc === 'parent') {
|
if (doc === 'parent') {
|
||||||
win = window.parent.parent;
|
win = window.parent.parent;
|
||||||
} else if (doc === 'inner') {
|
} else if (doc === 'inner') {
|
||||||
|
@ -35,46 +38,44 @@ function makeCSSManager(emptyStylesheetTitle, doc) {
|
||||||
} else if (doc === 'outer') {
|
} else if (doc === 'outer') {
|
||||||
win = window.parent;
|
win = window.parent;
|
||||||
} else {
|
} else {
|
||||||
throw 'Unknown dynamic style container';
|
throw new Error('Unknown dynamic style container');
|
||||||
}
|
}
|
||||||
const allSheets = win.document.styleSheets;
|
const allSheets = win.document.styleSheets;
|
||||||
|
|
||||||
for (let i = 0; i < allSheets.length; i++) {
|
for (let i = 0; i < allSheets.length; i++) {
|
||||||
const s = allSheets[i];
|
const s = allSheets[i];
|
||||||
if (s.title == title) {
|
if (s.title === title) {
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
};
|
||||||
|
|
||||||
const browserSheet = getSheetByTitle(emptyStylesheetTitle);
|
const browserSheet = getSheetByTitle(emptyStylesheetTitle);
|
||||||
|
|
||||||
function browserRules() {
|
const browserRules = () => (browserSheet.cssRules || browserSheet.rules);
|
||||||
return (browserSheet.cssRules || browserSheet.rules);
|
|
||||||
}
|
|
||||||
|
|
||||||
function browserDeleteRule(i) {
|
const browserDeleteRule = (i) => {
|
||||||
if (browserSheet.deleteRule) browserSheet.deleteRule(i);
|
if (browserSheet.deleteRule) browserSheet.deleteRule(i);
|
||||||
else browserSheet.removeRule(i);
|
else browserSheet.removeRule(i);
|
||||||
}
|
};
|
||||||
|
|
||||||
function browserInsertRule(i, selector) {
|
const browserInsertRule = (i, selector) => {
|
||||||
if (browserSheet.insertRule) browserSheet.insertRule(`${selector} {}`, i);
|
if (browserSheet.insertRule) browserSheet.insertRule(`${selector} {}`, i);
|
||||||
else browserSheet.addRule(selector, null, i);
|
else browserSheet.addRule(selector, null, i);
|
||||||
}
|
};
|
||||||
const selectorList = [];
|
const selectorList = [];
|
||||||
|
|
||||||
function indexOfSelector(selector) {
|
const indexOfSelector = (selector) => {
|
||||||
for (let i = 0; i < selectorList.length; i++) {
|
for (let i = 0; i < selectorList.length; i++) {
|
||||||
if (selectorList[i] == selector) {
|
if (selectorList[i] === selector) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
};
|
||||||
|
|
||||||
function selectorStyle(selector) {
|
const selectorStyle = (selector) => {
|
||||||
let i = indexOfSelector(selector);
|
let i = indexOfSelector(selector);
|
||||||
if (i < 0) {
|
if (i < 0) {
|
||||||
// add selector
|
// add selector
|
||||||
|
@ -83,23 +84,21 @@ function makeCSSManager(emptyStylesheetTitle, doc) {
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
return browserRules().item(i).style;
|
return browserRules().item(i).style;
|
||||||
}
|
};
|
||||||
|
|
||||||
function removeSelectorStyle(selector) {
|
const removeSelectorStyle = (selector) => {
|
||||||
const i = indexOfSelector(selector);
|
const i = indexOfSelector(selector);
|
||||||
if (i >= 0) {
|
if (i >= 0) {
|
||||||
browserDeleteRule(i);
|
browserDeleteRule(i);
|
||||||
selectorList.splice(i, 1);
|
selectorList.splice(i, 1);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
selectorStyle,
|
selectorStyle,
|
||||||
removeSelectorStyle,
|
removeSelectorStyle,
|
||||||
info() {
|
info: () => `${selectorList.length}:${browserRules().length}`,
|
||||||
return `${selectorList.length}:${browserRules().length}`;
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.makeCSSManager = makeCSSManager;
|
exports.makeCSSManager = makeCSSManager;
|
||||||
|
|
Loading…
Reference in New Issue