forked from thingsinjars/jQuery-Scoped-CSS-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.scoped.js
executable file
·278 lines (241 loc) · 8.32 KB
/
jquery.scoped.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
/*
* jQuery Scoped CSS plugin
* This adds support for the CSS scoped attribute to limit a block of style declarations
* to a specific area of the HTML. You can also use @import and media filters in scoped blocks
* http://www.w3.org/TR/html5/semantics.html#the-style-element
*
* Simon Madine, 1 February 2011
*
* Use:
* Include this plugin file (minified, ideally) and call $.scoped() on load
*
* Limitations:
* - If you're using multiple nested declarations, Webkit might apply different inheritance
* specificity rules from the other engines. I don't know who's right.
* - Sometimes there are delays parsing externally loaded stylesheets (via @import) and they
* might get skipped. Not often but it happens.
*
* Notes:
* - If the browser natively supports <style scoped>, this will return without doing anything
* - Style elements really shouldn't have classes added to them. This functionality should
* probably use some kind of data attribute.
* - Currently, getElementStyles is hand-rolled and probably wrong.
*
* v0.6 2013-04-08
* Removed $.browser requirement and refactored a bit so it runs slightly faster and works with
* jQuery 1.9. Also checks for native scoped style support and exits early if available.
*
* v0.5 2011-01-30
* Sibling blocks work, most nested blocks work but some oddness in Webkit means that some
* styles don't inherit correctly when there are multiple nested declarations.
*
* v0.4 2011-01-29
* First jQuery plugin version. Works for most cases but gets confused when there are
* multiple scoped blocks affecting the same context (siblings).
*
*/
(function($) {
"use strict";
//Add this to the global jQuery object as we want to apply this once to the entire document
$.scoped = function() {
if ('scoped' in document.createElement('style')) {
return;
}
// To detect which method to use when setting styles
// IE9 pretends to use setProperty but doesn't
var styleSettable = true;
try {
document.createElement("div").style.setProperty("opacity", 0, "");
} catch (error) {
styleSettable = false;
}
scopedReset();
//Backup the original styles
backupBlocks();
//Go through once to add dependencies
var $style = $('style');
$style.each(function(index) {
var $this = $(this);
if (isScoped($this)) {
$this.addClass('this_is_' + index);
//Get all style blocks in this scope
//Including nested blocks
$this.parent().find('style').each(function() {
//Add a dependency class here to check for later
$(this).addClass('depends_on_' + index);
});
}
});
//Go through a second time to process the scopes
$style.each(function() {
var $this = $(this);
if (isScoped($this)) {
//Empty all scoped style blocks
//Except those that this context is dependant on
emptyBlocks($this);
var holdingArea = [];
//Read all styles and copy them to a holding area
var $parent = $this.parent(),
$all = $parent.find('*');
$all.add($parent).each(function() {
$(this).css('cssText', '');
if (this.nodeName !== 'STYLE') {
holdingArea.push(getStylesText(this));
}
});
//Copy all the styles back from the holding area onto the in-scope elements
$all.add($parent).each(function() {
var this_style, n;
if (this.nodeName !== 'STYLE') {
if (!$(this).data('originalInline')) {
$(this).data('originalInline', $(this).attr('style'));
}
this_style = holdingArea.shift();
if (typeof this_style === 'string') {
// Webkit, Gecko
$(this).css('cssText', this_style);
} else {
if (styleSettable) {
// Opera
for (n in this_style) {
if (n !== 'content' || this_style[n] !== 'none') {
try {
this.style.setProperty(n, this_style[n]);
} catch (err) {}
}
}
} else {
// IE
for (n in this_style) {
try {
if (this && this.style && n && this_style[n] && n !== '' && this_style[n] !== '') {
this.style[n] = this_style[n];
}
} catch (err) {}
}
}
}
}
});
//Put all other style blocks back
fillBlocks();
}
});
//Measurements done and styles applied, now clear styles from this style block
//This will stop them affecting out-of-scope elements
$('style').each(function(i, e) {
if (isScoped(e)) {
try {
e.innerHTML = '';
} catch (error) {
$(e).attr('disabled', 'disabled');
}
}
});
//Standard jQuery attribute selector $('style[scoped]') doesn't
//work with empty boolean attributes so this is used instead
function isScoped(styleBlock) {
return ($(styleBlock).attr('scoped') !== undefined);
}
//Save all style tag contents to a data object
function backupBlocks() {
$('style').each(function(i, e) {
if (isScoped(e)) {
var $e = $(e);
$e.data('original-style', $e.html());
}
});
}
//Each style block now has class="depends_on_1 depends_on_2"
//We switch off all the scoped style blocks not mentioned in that list
//The disabled attribute only works on IE but coincidentally,
//IE doesn't allow .html() on style blocks.
function emptyBlocks(currentBlock) {
$('style').each(function(i, e) {
if (isScoped(e)) {
if (!currentBlock.hasClass('depends_on_' + i)) {
try {
e.innerHTML = '';
} catch (error) {
$(e).attr('disabled', 'disabled');
}
}
}
});
}
//Put all the styles back to reset for the next loop
function fillBlocks() {
$('style').each(function(i, e) {
var $e = $(e);
if (isScoped(e)) {
try {
e.innerHTML = $e.data('original-style');
} catch (error) {
$(this).removeAttr('disabled');
}
}
});
}
//Update this bit with some jQuery magic later
function getStylesText(element) {
var temp, styles, n, key;
if (element.currentStyle) {
//We work with a style object in IE and Opera rather than the text
sleep(50); //50ms delay to allow for external stylesheet parsing.
return element.currentStyle;
}
//We extract and process the ComputedCSSStyleObject into text
temp = document.defaultView.getComputedStyle(element, null);
styles = '';
for (n in temp) {
if (parseInt(n, 10)) {
key = camelize(temp[n]);
if (temp[key] !== undefined) {
styles += temp[n] + ':' + temp[key] + ';\n';
}
}
}
return styles;
}
function scopedReset() {
var $style = $('style');
$style.each(function(i, styleBlock) {
var $styleBlock = $(styleBlock);
var $parent = $(this).parent(),
$all = $parent.find('*');
$all.add($parent).each(function() {
var $this = $(this);
if (this.nodeName !== 'STYLE') {
if ($this.data('scopedprocessed')) {
$this.attr('style', $this.data('originalInline') || '');
$this.data('scopedprocessed', true);
}
}
});
if (($styleBlock.attr('scoped') !== undefined)) {
try {
if ($styleBlock.data('scopedprocessed')) {
this.innerHTML = $styleBlock.data('original-style');
}
} catch (error) {
$styleBlock.removeAttr('disabled');
}
$styleBlock.data('scopedprocessed', true);
}
});
}
//from Prototype
function camelize(string) {
return string.replace(/-+(.)?/g, function(match, chr) {
return chr ? chr.toUpperCase() : '';
});
}
function sleep(ms) {
var dt = new Date();
dt.setTime(dt.getTime() + ms);
while (new Date().getTime() < dt.getTime()) {
$.noop();
}
}
};
})(jQuery);