aboutsummaryrefslogtreecommitdiffstats
path: root/data/cssUtils.js
blob: da24aa23ebf4ac2e91aa3c3a9cb176149d4c644f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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);
};