aboutsummaryrefslogblamecommitdiffstats
path: root/data/lib/cssUtils.js
blob: da24aa23ebf4ac2e91aa3c3a9cb176149d4c644f (plain) (tree)












































































                                                                                   
/*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);
};