From 5b9f263ac1485c0ab18c2d42c5cd18746208bad4 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Wed, 23 Feb 2011 02:50:39 +0100 Subject: Moved .js files from data/ to data/lib/ so that we don't mix data and code. --- data/lib/cssUtils.js | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 data/lib/cssUtils.js (limited to 'data/lib/cssUtils.js') diff --git a/data/lib/cssUtils.js b/data/lib/cssUtils.js new file mode 100644 index 0000000..da24aa2 --- /dev/null +++ b/data/lib/cssUtils.js @@ -0,0 +1,77 @@ +/*global console: false */ +/*jslint onevar: false */ +// Released under the MIT/X11 license +// http://www.opensource.org/licenses/mit-license.php +"use strict"; + +/** + * get CSS style from all styles in the document with given name + * + * @param ruleName String with the identificator of the rule (the same + * used on the page itself) + * @param deleteFlag ??? + * @return ??? (exact type of the object returned FIXME) + * + * e.g., getCSSRule(".tramp") gives particular style + * from http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript + */ +var getCSSRule = exports.getCSSRule = function getCSSRule(ruleName, deleteFlag) { + ruleName=ruleName.toLowerCase(); // style rules are case insensitive + var foundRuleIdx = 0; + + Array.forEach(document.styleSheets, function (sheet) { + var ruleIdx = 0; + foundRule = Array.reduce(sheet.cssRules, function (ruleIdx, curRule, idx) { + if ((foundRuleIdx === 0) && (curRule. + selectorText.toLowerCase() == ruleName)) { + return idx; + } + return foundRuleIdx; + }); + if (foundRules > 0) { + if (deleteFlag === "delete") { + sheet.deleteRule(foundRuleIdx); + return true; + } + return sheet.cssRules[foundRuleIdx]; + } + }); + return false; // we found NOTHING! +}; + +/** + * + */ +exports.killCSSRule = function killCSSRule (ruleName) { + return getCSSRule(ruleName, "delete"); +}; + +/** + * + */ +exports.addCSSRule = function addCSSRule(ruleName, stylesheetTitle) { + var sheets = {}; + if (!getCSSRule(ruleName)) { + if (stylesheetTitle) { + sheets = Array.filter(document.styleSheets,function (sheet) { + return (sheet.title === stylesheetTitle); + }); + } else { + sheets = document.styleSheets; + } + sheets[0].insertRule(ruleName+' { }', 0); + } + return getCSSRule(ruleName); +}; + +/** + * + */ +exports.addCSSStylesheet = function addCSSStylesheet (StylesheetName) { + var cssNode = document.createElement("style"); + cssNode.type = 'text/css'; + cssNode.rel = 'stylesheet'; + cssNode.media = 'screen'; + cssNode.title = StylesheetName; + document.getElementsByTagName("head")[0].appendChild(cssNode); +}; -- cgit