-
Notifications
You must be signed in to change notification settings - Fork 1
/
silk-style.js
64 lines (52 loc) · 1.44 KB
/
silk-style.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
(function() {
var sheet, isReady = false;
$(function() {
isReady = true;
sheet = document.createElement("style");
sheet.setAttribute("id", "silkStyleSheet");
sheet.setAttribute("type", "text/css");
document.head.appendChild(document.createElement("style"));
sheet = document.styleSheets[document.styleSheets.length-1];
});
function createRule(selector) {
sheet.insertRule(selector + '{}', sheet.cssRules.length);
return sheet.cssRules[sheet.cssRules.length-1];
}
function getSingletonRule(selector) {
for (var i = 0; i < sheet.cssRules.length; i++) {
if (sheet.cssRules.item(i).selectorText == selector)
return sheet.cssRules.item(i);
}
return createRule(selector);
}
$.fn.sss = function(declaration) {
var jq = this;
if (!isReady) {
$(function() { jq.sss(declaration) })
return jq
}
this.sssRule = getSingletonRule(this.selector);
if (declaration instanceof Function) {
var declarationFunc = declaration;
declaration = {};
declarationFunc.call(declaration);
}
for (var p in declaration) if (declaration.hasOwnProperty(p)) {
if (p == 'rules') {
for (var s in declaration[p]) if (declaration[p].hasOwnProperty(s)) {
$(this.selector + ' ' + s).sss(declaration[p][s]);
}
}
if (p[0] == '/') {
$(this.selector + p.replace(/^(\/w*)/, '')).sss(declaration[p]);
}
else if (p == 'forEach') {
this.each(declaration[p]);
}
else {
this.sssRule.style[p] = declaration[p];
}
}
return this
}
}())