pad.libre-service.eu-etherpad/src/static/js/cssmanager.js

73 lines
2 KiB
JavaScript
Raw Normal View History

2020-12-22 16:12:38 +01:00
'use strict';
/**
2013-06-14 19:37:41 +02:00
* 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.
* 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.
*/
exports.makeCSSManager = (browserSheet) => {
2020-12-22 16:12:38 +01:00
const browserRules = () => (browserSheet.cssRules || browserSheet.rules);
2011-07-07 19:59:34 +02:00
2020-12-22 16:12:38 +01:00
const browserDeleteRule = (i) => {
2011-03-26 14:10:41 +01:00
if (browserSheet.deleteRule) browserSheet.deleteRule(i);
else browserSheet.removeRule(i);
2020-12-22 16:12:38 +01:00
};
2011-07-07 19:59:34 +02:00
2020-12-22 16:12:38 +01:00
const 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-12-22 16:12:38 +01:00
};
2020-11-23 19:24:19 +01:00
const selectorList = [];
2011-03-26 14:10:41 +01:00
2020-12-22 16:12:38 +01:00
const indexOfSelector = (selector) => {
2020-11-23 19:24:19 +01:00
for (let i = 0; i < selectorList.length; i++) {
2020-12-22 16:12:38 +01:00
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-12-22 16:12:38 +01:00
};
2011-03-26 14:10:41 +01:00
2020-12-22 16:12:38 +01:00
const 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-12-22 16:12:38 +01:00
};
2011-03-26 14:10:41 +01:00
2020-12-22 16:12:38 +01:00
const 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);
}
2020-12-22 16:12:38 +01:00
};
2011-03-26 14:10:41 +01:00
2011-07-07 19:59:34 +02:00
return {
2020-11-23 19:24:19 +01:00
selectorStyle,
removeSelectorStyle,
2020-12-22 16:12:38 +01:00
info: () => `${selectorList.length}:${browserRules().length}`,
2011-07-07 19:59:34 +02:00
};
2020-12-22 16:12:38 +01:00
};