-
Notifications
You must be signed in to change notification settings - Fork 0
/
syringe.js
353 lines (296 loc) · 10.2 KB
/
syringe.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**
* syringe.js
* @version 0.4.0
* https://github.com/noeldelgado/Syringe.js
* Licensed under the MIT lincese
*/
/* globals CSSRule */
(function () {
'use strict';
var Syringe = {
config: {
prefixedProperties: null
},
style: null,
sheet: null,
rules: null,
buffer: {},
_level: 0,
inject: function inject(options) {
if (this.style === null) {
Syringe._createStyleSheet();
}
// Reset variables for iteration
this._level = 0;
this.buffer.selectors = [];
this.buffer.blocks = [];
// Handle rules to be injected
this._iterate(options);
// Inject rules
return this._insertRules();
},
removeAll: function removeAll() {
while(this.rules.length) {
if (this.sheet.deleteRule) {
this.sheet.deleteRule(this.rules.length - 1);
} else {
this.sheet.removeRule(this.rules.length - 1);
}
}
/* IE8 at-rule */
if (this.sheet.imports) {
while(this.sheet.imports.length) {
this.sheet.removeImport(this.sheet.imports.length - 1);
}
}
this._removeStyleSheet();
},
remove: function remove(selector) {
var rules, rulesLength, selectorsLength, j;
if (!selector) {return false;}
rules = this.rules;
rulesLength = rules.length;
selectorsLength = selector.length;
if (typeof selector === "string") {
while(rulesLength--) {
this._deleteRule(rules[rulesLength], rulesLength, selector);
}
}
if (selector instanceof Array) {
while(rulesLength--) {
for (j = 0; j < selectorsLength; j++) {
this._deleteRule(rules[rulesLength], rulesLength, selector[j]);
}
}
}
if (this.rules.length === 0) {
this._removeStyleSheet();
}
},
_deleteRule: function _deleteRule(rule, index, selector) {
var isKeyframe, isAtRule, isMedia, isFontFace, selectorNoSpaces;
if (!rule) {return;}
if (this.sheet.deleteRule) {
isKeyframe = this._re.keyframe.exec(selector);
isAtRule = this._re.atRule.exec(selector);
isMedia = this._re.media.exec(selector);
isFontFace = this._re.fontFace.exec(selector);
if (rule.type === 1 && (rule.selectorText === selector)) { // CSSRule.STYLE_RULE
this.sheet.deleteRule(index);
} else if (rule.type === 3 && isAtRule) { // CSSRule.IMPORT_RULE
if (rule.href === isAtRule[1]) {
this.sheet.deleteRule(index);
}
} else if (rule.type === 4 && isMedia) { // CSSRule.MEDIA_RULE
selectorNoSpaces = selector.replace(/\s/g, '');
if (rule.cssText.replace(/\s/g, '').substring(0, selectorNoSpaces.length) === selectorNoSpaces) {
this.sheet.deleteRule(index);
}
} else if (rule.type === 5 && isFontFace) { // CSSFontFaceRule
this.sheet.deleteRule(index);
} else if (rule.type === 7 && isKeyframe) { // CSSRule.KEYFRAMES_RULE
if (rule.name === isKeyframe[2]) {
this.sheet.deleteRule(index);
}
}
return;
}
// IE-8
if (rule.selectorText.toLowerCase() === selector) {
this.sheet.removeRule(index);
} else {
isAtRule = this._re.atRule.exec(selector);
if (isAtRule && this.sheet.imports) {
for (var i = 0; i< this.sheet.imports.length; i++) {
if (this.sheet.imports[i].href.indexOf(isAtRule[1]) > 0) {
this.sheet.removeImport(i);
}
}
}
}
},
_iterate: function iterate(obj) {
var selector, lastSelector, property;
for (selector in obj) {
if (obj.hasOwnProperty(selector)) {
lastSelector = (this.buffer.selectors.length - 1);
if (typeof obj[selector] === "object") {
// Handle rule blocks
this._level++;
if (this._level === 1) {
this.buffer.selectors.push(selector);
lastSelector = this.buffer.selectors.length - 1;
} else {
this._addToBlock(lastSelector, selector + "{");
}
if (this._isEmptyObject(obj[selector])) {
this._level--;
this.buffer.blocks[lastSelector] = "";
} else {
this._iterate(obj[selector]);
if (this._level > 1) {
this.buffer.blocks[lastSelector] += "}";
}
this._level--;
}
} else {
// handle declarations
property = selector;
if (this._re.camelCase.test(selector) === true) {
property = this._toDash(selector);
}
if (this.config.prefixedProperties) {
if (lastSelector < 0) { // empty ruleset, special case for @import rule
lastSelector = 0;
this.buffer.selectors.push("");
}
this._addToBlock(lastSelector, this._getPrefixedDeclarations(property, obj[selector]));
} else {
this._addToBlock(lastSelector, this._getDeclaration(property, obj[selector]));
}
}
}
}
},
_insertRules: function _insertRules() {
var i, blocksLength, rulesLength, result, selector, block, isMedia, isKeyframe, keyframeName;
blocksLength = this.buffer.blocks.length;
rulesLength = this.rules.length;
result = "";
for (i = 0; i < blocksLength; i++) {
selector = this.buffer.selectors[i];
block = this.buffer.blocks[i];
isMedia = this._re.media.exec(selector);
isKeyframe = this._re.keyframe.exec(selector);
keyframeName= "";
// @import at-rule
if (selector === "") {
result += block;
if (this.sheet.insertRule) {
this.sheet.insertRule(block, rulesLength);
} else if (this.sheet.addImport) { // IE8-
this.sheet.addImport(this._re.atRule.exec(block)[1]);
}
continue;
}
// @keyframes
if (isKeyframe) {
keyframeName = " " + isKeyframe[2];
if (!!window.CSSRule) {
if (CSSRule.KEYFRAMES_RULE) {
selector = "@keyframes" + keyframeName;
} else if (CSSRule.WEBKIT_KEYFRAMES_RULE) {
selector = "@-webkit-keyframes" + keyframeName;
} else if (CSSRule.MOZ_KEYFRAMES_RULE) {
selector = "@-moz-keyframes" + keyframeName;
} else {
selector = "";
}
} else {
selector = "";
}
}
// media querie, IE8 validation
if (isMedia) {
if ((window.matchMedia === undefined && window.msMatchMedia === undefined) &&
(window.styleMedia === undefined && window.media === undefined)) {
selector = "";
}
}
if (selector && block) {
if (this.sheet.insertRule) {
this.sheet.insertRule(selector + "{" + block + "}", rulesLength);
} else {
this.sheet.addRule(selector, block, 0);
}
}
result += selector + "{" + block + "}";
selector = block = isKeyframe = keyframeName = null;
}
return result;
},
_addToBlock: function _addToBlock(index, value) {
if (this.buffer.blocks[index] === undefined) {
this.buffer.blocks[index] = "";
}
this.buffer.blocks[index] += value;
},
_getDeclaration: function _getDeclaration(property, value) {
var declaration;
if (property === "@import") {
declaration = (property + " " + value);
} else {
declaration = (property + ":" + value);
}
return declaration + ";";
},
_getPrefixedDeclarations: function _getPrefixedDeclarations(property, value) {
var result, index, prefixedProps, prefixedPropsLength;
if (this.config.prefixedProperties[property]) {
result = "";
prefixedProps = this._getPrefixedSelector(property);
prefixedPropsLength = prefixedProps.length;
for (index = 0; index < prefixedPropsLength; index++) {
result += this._getDeclaration(prefixedProps[index], value);
}
return result;
}
return this._getDeclaration(property, value);
},
_getPrefixedSelector: function _getPrefixedSelector(selector) {
var result = []
, prefixes = this.config.prefixedProperties[selector]
, index = 0
, prefixesLength = prefixes.length;
for (; index < prefixesLength; index++) {
result.push('-' + prefixes[index] + '-' + selector);
}
result.push(selector);
return result;
},
_re: {
camelCase: new RegExp("[A-Z]\\w+"),
keyframe: new RegExp("@(.*)?keyframes (.*)"),
atRule: new RegExp("@import url\\((.*)\\)"),
media: new RegExp("@media"),
fontFace: new RegExp("@font-face")
},
_toDash: function _toDash(string) {
return string.replace(/([A-Z])/g, function (letter) {
return '-' + letter.toLowerCase();
});
},
_isEmptyObject: function _isEmptyObject(obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
return false;
}
}
return true;
},
_removeStyleSheet: function _removeStyleSheet() {
// The complete style element must be deleted to
// update the style settings in Firefox and Opera
this.style.parentNode.removeChild(this.style);
this.style = null;
},
_createStyleSheet: function _createStyleSheet() {
this.style = document.createElement('style');
this.style.setAttribute("type", "text/css");
document.getElementsByTagName("head")[0].appendChild(this.style);
if (this.style.sheet === undefined) {
this.sheet = this.style.styleSheet;
} else {
this.sheet = this.style.sheet;
}
if (this.sheet.cssRules) {
this.rules = this.sheet.cssRules;
} else {
this.rules = this.sheet.rules;
}
}
};
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Syringe;
} else { window.Syringe = Syringe; }
})();