-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSS.js
56 lines (51 loc) · 1.45 KB
/
CSS.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
var CSS = function() {
if (!(this instanceof CSS)) return new CSS();
var el = document.createElement("style"), s, r;
document.getElementsByTagName('head')[0].appendChild(el);
s = el.sheet || el.styleSheet;
this._r = r = s.cssRules || s.rules;
this._add = (s.addRule) ? (function(c, d) { s.addRule(c, d.replace(/[\{\}]/g, "")); }) : (function(c, d) { s.insertRule(c + "{" + d.replace(/[\{\}]/g, "") + "}", r.length); });
this._remove = (s.removeRule) ? (function(i) { s.removeRule(i); }) : (function(i) { s.deleteRule(i); });
};
CSS.prototype._getSelectorText = function(s) { //IE는 selectorText중 태그명을 대문자로 만든다.
this.add(s, "foo:bar");
var i = this._r.length - 1,
ret = this._r[i].selectorText;
this._remove(i);
return ret;
};
CSS.prototype._find = function(s) {
var a = this._r, i, l = a.length,
ret = [];
for (i = 0; i < l; i++) {
if (s === "*" || a[i].selectorText === this._getSelectorText(s)) {
ret.push({ index : i, rule : a[i] });
}
}
return ret;
};
CSS.prototype.add = function(s, d) {
this._add(s, d);
return this;
};
CSS.prototype.remove = function(s) {
switch (typeof s) {
case "string":
var found = this._find(s), i;
for (i = found.length - 1; i >= 0; i--) {
this._remove(found[i].index);
}
break;
case "number":
this._remove(s);
break;
}
return this;
};
CSS.prototype.reset = function() {
var a = this._r;
for (var i = a.length - 1; i >= 0; i--) {
this._remove(i);
}
return this;
};