This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathvein.js
175 lines (147 loc) · 6.14 KB
/
vein.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* vein.js - version 0.3
*
* by Danny Povolotski ([email protected])
**/
!function (name, definition) {
if (typeof module != 'undefined') module.exports = definition()
else if (typeof define == 'function' && define.amd) define(name, definition)
else this[name] = definition()
}('vein', function () {
var vein = function(){};
// Kudos to: http://youmightnotneedjquery.com/
var extend = function(out) {
out = out || {};
for (var i = 1; i < arguments.length; i++) {
if (!arguments[i])
continue;
for (var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key))
out[key] = arguments[i][key];
}
}
return out;
};
var findOrDeleteBySelector = function(selector, stylesheet, css){
var matches = [],
rulesDelete = [],
rules = stylesheet[ document.all ? 'rules' : 'cssRules' ],
selectorCompare = selector.replace(/\s/g,''),
ri, rl;
// Since there could theoretically be multiple versions of the same rule,
// we will first iterate
for(ri = 0, rl = rules.length; ri < rl; ri++) {
if(
// regular style selector
(rules[ri].selectorText === selector) ||
// for media queries, remove spaces and see if the query matches
(rules[ri].type === 4 && rules[ri].cssText.replace(/\s/g,'').substring(0, selectorCompare.length) == selectorCompare)
) {
if(css === null) {
// If we set css to null, let's delete that ruleset altogether
rulesDelete.push(ri);
}
else {
// Otherwise - we push it into the matches array
matches.push(rules[ri]);
}
}
}
for (ri = 0, rl = rulesDelete.length; ri < rl; ri++) {
stylesheet.deleteRule(rulesDelete[ri]);
}
return matches;
};
var cssToString = function(css){
var cssArray = [];
for(var property in css) {
if (css.hasOwnProperty(property)) {
cssArray.push(property + ': ' + css[property] + ';');
}
}
var cssText = cssArray.join('');
return cssText;
};
// Get the stylesheet we use to inject stuff or create it if it doesn't exist yet
vein.getStylesheet = function() {
var self = this,
si, sl;
if(!self.element || !document.getElementById('vein')) {
self.element = document.createElement("style");
self.element.setAttribute('type', 'text/css');
self.element.setAttribute('id', 'vein');
document.getElementsByTagName("head")[0].appendChild(self.element);
self.stylesheet = self.element.sheet;
}
return self.stylesheet;
};
var getRulesFromStylesheet = function(stylesheet){
return stylesheet[ document.all ? 'rules' : 'cssRules' ];
}
var insertRule = function(selector, cssText, stylesheet){
var rules = getRulesFromStylesheet(stylesheet);
if(stylesheet.insertRule) {
// Supported by all modern browsers
stylesheet.insertRule(selector + '{' + cssText + '}', rules.length);
} else {
// Old IE compatability
stylesheet.addRule(selector, cssText, rules.length);
}
};
// Let's inject some CSS. We can supply an array (or string) of selectors, and an object
// with CSS value and property pairs.
vein.inject = function(selectors, css, options) {
options = extend({}, options);
var self = this,
stylesheet = options.stylesheet || self.getStylesheet(),
rules = getRulesFromStylesheet(stylesheet),
si, sl, query, matches, cssText, property, mi, ml, qi, ql;
if(typeof selectors === 'string') {
selectors = [selectors];
}
for(si = 0, sl = selectors.length; si < sl; si++) {
if(typeof selectors[si] === 'object' && stylesheet.insertRule){
for(query in selectors[si]) {
matches = findOrDeleteBySelector(query, stylesheet, css);
if(matches.length === 0){
cssText = cssToString(css);
for(qi = 0, ql = selectors[si][query].length; qi < ql; qi++) {
insertRule(query, selectors[si][query][qi] + '{' + cssText + '}', stylesheet);
}
} else {
for(mi = 0, ml = matches.length; mi < ml; mi++) {
self.inject(selectors[si][query], css, {stylesheet: matches[mi]});
}
}
}
} else {
matches = findOrDeleteBySelector(selectors[si], stylesheet, css);
// If all we wanted is to delete that ruleset, we're done here
if(css === null) return;
// If no rulesets have been found for the selector, we will create it below
if(matches.length === 0) {
cssText = cssToString(css);
insertRule(selectors[si], cssText, stylesheet);
}
// Otherwise, we're just going to modify the property
else {
for(mi = 0, ml = matches.length; mi < ml; mi++) {
for(property in css) {
if (css.hasOwnProperty(property)) {
// TODO: Implement priority
if(matches[mi].style.setProperty) {
matches[mi].style.setProperty(property, css[property], '');
} else {
//IE8
matches[mi].style.setAttribute(property, css[property], '');
}
}
}
}
}
}
}
return self;
};
return vein;
});