-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcssjs.js
executable file
·160 lines (148 loc) · 4.84 KB
/
cssjs.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Original work by David Tang
* https://github.com/Box9/cssjs
* MIT Licensed (http://www.opensource.org/licenses/mit-license.php)
*/
var cssjs = (function(undefined) {
var adjSelAttrRgx = /((?:\.|#)[^\.\s#]+)((?:\.|#)[^\.\s#]+)/g;
function getRules(sheet, selector) {
var rules = sheet.cssRules || sheet.rules || [];
var results = [];
// Browsers report selectors in lowercase - TODO check how true this is cross-browser
selector = selector.toLowerCase();
for (var i = 0; i < rules.length; i++) {
// IE8 will split comma-delimited selectors into multiple rules, breaking our matching
var selectorText = rules[i].selectorText;
// Note - certain rules (e.g. @rules) don't have selectorText
if (selectorText && (selectorText == selector || selectorText == swapAdjSelAttr(selector))) {
results.push({
sheet: sheet,
index: i,
style: rules[i].style
});
}
}
return results;
}
function addRule(sheet, selector) {
var rules = sheet.cssRules || sheet.rules || [];
var index = rules.length;
if (sheet.insertRule) {
sheet.insertRule(selector + ' { }', index);
} else if (sheet.addRule) {
sheet.addRule(selector, null, index);
}
return {
sheet: sheet,
index: index,
style: rules[index].style
};
};
function removeRule(rule) {
var sheet = rule.sheet;
if (sheet.deleteRule) {
sheet.deleteRule(rule.index);
} else if (sheet.removeRule) {
sheet.removeRule(rule.index);
}
}
function extend(dest, src) {
for (var key in src) {
if (!src.hasOwnProperty(key))
continue;
dest[key] = src[key];
}
return dest;
}
function aggregateStyles(rules) {
var aggregate = {};
for (var i = 0; i < rules.length; i++) {
extend(aggregate, declaredProperties(rules[i].style));
}
return aggregate;
}
function declaredProperties(style) {
var declared = {};
for (var i = 0; i < style.length; i++) {
declared[style[i]] = style[style[i]];
}
return declared;
}
// IE9 stores rules with attributes (classes or ID's) adjacent in the opposite order as defined
// causing them to not be found, so this method swaps [#|.]sel1[#|.]sel2 to become [#|.]sel2[#|.]sel1
function swapAdjSelAttr(selector) {
var swap = '';
var lastIndex = 0;
while ((match = adjSelAttrRgx.exec(selector)) != null) {
if (match[0] === '')
break;
swap += selector.substring(lastIndex, match.index);
swap += selector.substr(match.index + match[1].length, match[2].length);
swap += selector.substr(match.index, match[1].length);
lastIndex = match.index + match[0].length;
}
swap += selector.substr(lastIndex);
return swap;
};
var CSSjs = function(doc) {
this.doc = doc;
this.head = this.doc.head || this.doc.getElementsByTagName('head')[0];
this.sheets = this.doc.styleSheets || [];
};
CSSjs.prototype = {
get: function(selector) {
return this.defaultSheet ? aggregateStyles(getRules(this.defaultSheet, selector)) : {};
},
getAll: function(selector) {
var properties = {};
for (var i = 0; i < this.sheets.length; i++) {
extend(properties, aggregateStyles(getRules(this.sheets[i], selector)));
}
return properties;
},
set: function(selector, properties) {
if (!this.defaultSheet) {
this.defaultSheet = this._createSheet();
}
var rules = getRules(this.defaultSheet, selector);
if (!rules.length) {
rules = [addRule(this.defaultSheet, selector)];
}
for (var i = 0; i < rules.length; i++) {
extend(rules[i].style, properties);
}
},
remove: function(selector) {
if (!this.defaultSheet)
return;
var rules = getRules(this.defaultSheet, selector);
for (var i = 0; i < rules.length; i++) {
removeRule(rules[i]);
}
return rules.length;
},
_createSheet: function() {
var styleNode = this.doc.createElement('style');
styleNode.type = 'text/css';
styleNode.rel = 'stylesheet';
this.head.appendChild(styleNode);
return this._getSheetForNode(styleNode);
},
_getSheetForNode: function(node) {
for (var i = 0; i < this.sheets.length; i++) {
if (node === this._getNodeForSheet(this.sheets[i])) {
return this.sheets[i];
}
}
return null;
},
_getNodeForSheet: function(sheet) {
return sheet.ownerNode || sheet.owningElement;
}
};
var exports = new CSSjs(document);
exports.forDocument = function(doc) {
return new CSSjs(doc);
};
return exports;
})();