etherpad-lite/static/js/cssmanager.js

121 lines
2.8 KiB
JavaScript
Raw Normal View History

/**
* 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.
*/
2011-07-14 17:15:38 +02:00
function makeCSSManager(emptyStylesheetTitle, top)
2011-07-07 19:59:34 +02:00
{
2011-03-26 14:10:41 +01:00
2011-07-14 17:15:38 +02:00
function getSheetByTitle(title, top)
2011-07-07 19:59:34 +02:00
{
2011-07-14 17:15:38 +02:00
if(top)
2011-07-27 16:11:30 +02:00
var allSheets = window.parent.parent.document.styleSheets;
2011-07-14 17:15:38 +02:00
else
var allSheets = document.styleSheets;
2011-07-07 19:59:34 +02:00
for (var i = 0; i < allSheets.length; i++)
{
2011-03-26 14:10:41 +01:00
var s = allSheets[i];
2011-07-07 19:59:34 +02:00
if (s.title == title)
{
return s;
2011-03-26 14:10:41 +01:00
}
}
return null;
}
2011-07-07 19:59:34 +02:00
/*function getSheetTagByTitle(title) {
2011-03-26 14:10:41 +01:00
var allStyleTags = document.getElementsByTagName("style");
for(var i=0;i<allStyleTags.length;i++) {
var t = allStyleTags[i];
if (t.title == title) {
return t;
}
}
return null;
}*/
2011-07-14 17:15:38 +02:00
var browserSheet = getSheetByTitle(emptyStylesheetTitle, top);
2011-03-26 14:10:41 +01:00
//var browserTag = getSheetTagByTitle(emptyStylesheetTitle);
2011-07-07 19:59:34 +02:00
function browserRules()
{
return (browserSheet.cssRules || browserSheet.rules);
}
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
function browserInsertRule(i, selector)
{
if (browserSheet.insertRule) browserSheet.insertRule(selector + ' {}', i);
2011-03-26 14:10:41 +01:00
else browserSheet.addRule(selector, null, i);
}
var selectorList = [];
2011-07-07 19:59:34 +02:00
function indexOfSelector(selector)
{
for (var i = 0; i < selectorList.length; i++)
{
if (selectorList[i] == selector)
{
return i;
2011-03-26 14:10:41 +01:00
}
}
return -1;
}
2011-07-07 19:59:34 +02:00
function selectorStyle(selector)
{
2011-03-26 14:10:41 +01:00
var i = indexOfSelector(selector);
2011-07-07 19:59:34 +02:00
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;
}
2011-07-07 19:59:34 +02:00
function removeSelectorStyle(selector)
{
2011-03-26 14:10:41 +01:00
var i = indexOfSelector(selector);
2011-07-07 19:59:34 +02:00
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 {
selectorStyle: selectorStyle,
removeSelectorStyle: removeSelectorStyle,
info: function()
{
return selectorList.length + ":" + browserRules().length;
}
};
2011-03-26 14:10:41 +01:00
}