-
Notifications
You must be signed in to change notification settings - Fork 10
/
jquery.stylesheet.js
426 lines (405 loc) · 14.8 KB
/
jquery.stylesheet.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/**
* jQuery plugin for adding, removing and making changes to CSS rules
*
* @author Vimal Aravindashan
* @version 0.3.7
* @licensed MIT license
*/
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
// Node/CommonJS
module.exports = factory;
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
var _ahref = $(document.createElement('a')), /**< <a> tag used for evaluating hrefs */
_styles = _ahref.prop('style'), /**< Collection of styles available on the host */
_sheet = function(s) {
return s.sheet || s.styleSheet;
}($('<style type="text/css">*{}</style>').appendTo('head')[0]), /**< StyleSheet for adding new rules*/
_rules = ('cssRules' in _sheet) ? 'cssRules' : 'rules', /**< Attribute name for rules collection in a stylesheet */
vendorPrefixes = ["Webkit", "O", "Moz", "ms"]; /**< Case sensitive list of vendor specific prefixes */
/**
* @function filterStyleSheet
* Filter a stylesheet based on accessibility and, ID or location
* @param {String} filter Filter to be applied. id or href of the style element can be used as filters.
* @param {CSSStyleSheet} styleSheet StyleSheet to be filtered
* @returns {Boolean} true if styleSheet matches the filter, false otherwise
*/
function filterStyleSheet(filter, styleSheet) {
try {
if(styleSheet[_rules]) {
filter = filter || '';
var node = $(styleSheet.ownerNode || styleSheet.owningElement);
return (filter === '') || (filter === '*') ||
('#'+(node.prop('id') || '') == filter) ||
((node.prop('href') || '') == _ahref.prop('href', filter).prop('href'));
} else {
return false;
}
} catch(e) {
return false;
}
}
/**
* @function parseSelector
* Splits a jQuery.stylesheet compatible selector into stylesheet filter and selector text
* @param {String} selector Selector text to be parsed
* @returns {Object} object with two properties 'styleSheet' and 'selectorText'
*/
function parseSelector(selector) {
var styleSheet = (/.*?{/.exec(selector) || ['{'])[0],
selectorText = /{.*}/g.exec(selector); //TODO: replace selector with dict object
if(selectorText === null) {
var parts = selector.split('{');
selectorText = '{'+parts[parts.length==1 ? 0 : 1].split('}')[0]+'}';
} else {
selectorText = selectorText[0];
}
return {
styleSheet: $.trim(styleSheet.substr(0, styleSheet.length-1)),
selectorText: normalizeSelector(selectorText.substr(1, selectorText.length-2))
};
}
/**
* @function normalizeSelector
* Normalizes selectorText to work cross-browser
* @param {String} selectorText selector string to normalize
* @returns {String} normalized selector string
*/
function normalizeSelector(selectorText) {
var selector = [], last, len;
last = _sheet[_rules].length;
insertRule.call(_sheet, selectorText, ';'); //NOTE: IE doesn't seem to mind ';' as non-empty
len = _sheet[_rules].length;
for(var i=len-1; i>=last; i--) {
selector.push(_sheet[_rules][i].selectorText);
deleteRule.call(_sheet, i);
}
return selector.reverse().join(', ');
}
/**
* @function matchSelector
* Matches given selector to selectorText of cssRule
* @param {CSSStyleRule} cssRule to match with
* @param {String} selectorText selector string to compare
* @param {Boolean} matchGroups when true, selector is matched in grouped style rules
* @returns true if selectorText of cssRule matches given selector, false otherwise
*/
function matchSelector(cssRule, selectorText, matchGroups) {
if($.type(cssRule.selectorText) !== 'string') {
return false;
}
if(cssRule.selectorText === selectorText) {
return true;
} else if (matchGroups === true) {
return $($.map(cssRule.selectorText.split(','), $.trim)).filter(function(i) {
return this.toString() === selectorText;
}).length > 0;
} else {
return false;
}
}
/**
* @function vendorPropName
* Vendor prefixed style property name.
* Based on similar function in jQuery library.
* @param {String} name camelCased CSS property name
* @returns {String} Vendor specific tag prefixed style name
* if found in styles, else passed name as-is
* @see vendorPrefixes
* @see _styles
*/
function vendorPropName(name) {
var titleName = name[0].toUpperCase() + name.slice(1),
styleName, i = vendorPrefixes.length;
while( --i ) {
styleName = vendorPrefixes[i] + titleName;
if(styleName in _styles) {
return styleName;
}
}
return name;
}
/**
* @function normalizeRule
* Normalizes the CSSStyleRule object to work better across browsers
* @param {CSSStyleRule} rule CSSStyleRule object to be normalized
* @param {StyleSheet} styleSheet parent stylesheet of the rule
* @returns {CSSStyleRule} normalized CSSStyleRule
*/
function normalizeRule(rule, styleSheet) {
//NOTE: this is experimental, however, it does have it's benefits
// for use with $.animate(), be sure to include jquery.stylesheet-animate.js as well
//TODO: move some of the defaults used here to user options
rule.ownerDocument = rule.ownerDocument || document; //XXX: Hack for jQuery.isHidden()
rule.nodeType = rule.nodeType || 1; //XXX: Hack for jQuery's defaultPrefilter()
rule.nodeName = rule.nodeName || 'DIV'; //XXX: Hack for jQuery's acceptData()
rule.parentNode = rule.parentNode || styleSheet.ownerNode || styleSheet.owningElement; //XXX: Hack for jQuery.contains()
rule.parentStyleSheet = rule.parentStyleSheet || styleSheet; //XXX: Fix for IE7
return rule;
}
/*
* Checking for 'instanceof CSSStyleRule' fails in IE7 but not in IE8, however, the call to normalizeRule() fails in both.
* So, we will define our custom CSSStyleRule class on all browsers where normalizeRule() fails.
*/
try {
normalizeRule(_sheet[_rules][0], _sheet);
$.support.nativeCSSStyleRule = true;
} catch(e) {
$.support.nativeCSSStyleRule = false;
CSSStyleRule = function(rule) {
$.extend(this, rule);
this.rule = rule; //XXX: deleteRule() requires the original object
this.currentStyle = rule.style; //XXX: Hack for jQuery's curCSS()/getStyles() for IE7
};
}
/**
* @function insertRule
* Cross-browser function for inserting rules
* @param {String} selector selectorText for the rule
* @param {String} css CSS property-value pair string
* @param {Number} index Index position to insert the string;
* defaults to end of rules collection
*/
function insertRule(selector, css, index) {
if(!selector || !css) {
return -1; //NOTE: IE does not like addRule(selector,'',index)
}
var self = this,
_insfn = self.insertRule ? function (selector, css, index) { this.insertRule(selector+'{'+css+'}', index); } : self.addRule;
index = index || this[_rules].length;
try {
return _insfn.call(self, selector, css, index);
} catch(e) {
$.each(selector.split(','), function(i, sel) {
_insfn.call(self, $.trim(sel), css);
});
return -1;
}
}
/**
* @function deleteRule
* Cross-browser function for deleting rules
* @param {Number|CSSStyleRule} Index of rule to be deleted, or
* reference to rule to be deleted from rules collection
*/
function deleteRule(rule) {
//NOTE: If we are using our custom CSSStyleRule, then CSSStyleRule.rule is the real style rule object
rule = (rule && rule.rule) ? rule.rule : rule;
if(!rule) {
return;
}
var self = this,
_delfn = self.deleteRule || self.removeRule;
if(!_delfn) { //NOTE: IE7 has issues with rule.parentStyleSheet, so we need to search for the parent stylesheet
$(document.styleSheets).each(function (i, styleSheet) {
if($(styleSheet[_rules]).filter(function() {return this === rule;}).length == 1) {
self = styleSheet;
_delfn = self.deleteRule || self.removeRule;
return false;
}
});
}
if($.type(rule) == 'number') {
_delfn.call(self, rule);
} else {
$.each(self[_rules], function (i, _rule) {
if(rule === _rule) {
_delfn.call(self, i);
return false;
}
});
}
}
/**
* jQuery.stylesheet
*
* Constructor/Factory method for initializing a jQuery.stylesheet object.
* Includes a short-cut to apply style changes immediately.
* @param {String} selector CSS rule selector text with optional stylesheet filter
* @param {String|Array|Object} name Name of style property to get/set.
* Also accepts array of property names and object of name/value pairs.
* @param {String} value If defined, then value of the style property
* is updated with it. Unused when name is an object map.
* @returns {jQuery.stylesheet|String|Object} A new jQuery.stylesheet object
* if name/value is not passed, or value of property or object of name/value pairs
*/
$.stylesheet = function (selector, name, value) {
if(!(this instanceof $.stylesheet)) {
return new $.stylesheet(selector, name, value);
}
this.init(selector);
return this.css(name, value);
};
$.extend($.stylesheet, {
/**
* @function jQuery.stylesheet.cssRules
* @param {String} selector CSS rule selector text with optional stylesheet filter
* @returns {Array} Array of CSSStyleRule objects that match the selector text
* and pass the stylesheet filter
*/
cssRules: function (selector) {
var rules = [],
filters = parseSelector(selector);
//NOTE: The stylesheet filter will be treated as case-sensitive
// The selectorText filter's case depends on the browser
$(document.styleSheets).each(function (i, styleSheet) {
if(filterStyleSheet(filters.styleSheet, styleSheet)) {
$.merge(rules, $(styleSheet[_rules]).filter(function() {
return matchSelector(this, filters.selectorText, filters.styleSheet === '*');
}).map(function() {
return normalizeRule($.support.nativeCSSStyleRule ? this : new CSSStyleRule(this), styleSheet);
}));
}
});
return rules.reverse();
},
/**
* @function jQuery.stylesheet.camelCase
* jQuery.camelCase is undocumented and could be removed at any point
* @param {String} str Hypenated string to be camelCased
* @returns {String} camelCased string
*/
camelCase: $.camelCase || function( str ) {
return str.replace(/-([\da-z])/g, function(a){return a.toUpperCase().replace('-','');});
},
/**
* Normalized CSS property names
* jQuery.cssProps is undocumented and could be removed at any point
*/
cssProps: $.cssProps || {},
/**
* @function jQuery.styesheet.cssStyleName
* @param {String} name Hypenated CSS property name
* @returns {String} camelCased or vendor specific name if found in host styles
*/
cssStyleName: function (name) {
if(name) {
var camelcasedName = $.camelCase(name);
if(camelcasedName in _styles) {
return camelcasedName;
} else if (($.cssProps[name] || ($.cssProps[name] = vendorPropName(camelcasedName))) in _styles) {
return $.cssProps[name];
}
}
}
});
$.stylesheet.fn = $.stylesheet.prototype = {
/**
* @function jQuery.stylesheet.fn.init
* Initializes a jQuery.stylesheet object.
* Selects a list of applicable CSS rules for given selector.
* @see jQuery.stylesheet.cssRules
* @param {String|Array|Object} selector CSS rule selector text(s)
* with optional stylesheet filter(s)
*/
init: function (selector) {
var rules = []; /**< Array of CSSStyleRule objects matching the selector initialized with */
switch($.type(selector)) {
case 'string':
rules = $.stylesheet.cssRules(selector);
break;
case 'array':
$.each(selector, function (idx, val) {
if($.type(val) === 'string') {
$.merge(rules, $.stylesheet.cssRules(val));
} else if(val instanceof CSSStyleRule) {
rules.push(val);
}
});
break;
case 'object':
if(selector instanceof CSSStyleRule) {
rules.push(val);
}
break;
}
$.extend(this, {
/**
* @function jQuery.stylesheet.rules
* @returns {Array} Copy of array of CSSStyleRule objects used
* by this instance of jQuery.stylesheet
*/
rules: function() {
return rules.slice();
},
/**
* @function jQuery.stylesheet.css()
* @param {String|Array|Object} name Name of style property to get/set.
* Also accepts array of property names and object of name/value pairs.
* @param {String} value If defined, then value of the style property
* is updated with it. Unused when name is an object map.
* @returns {jQuery.stylesheet|String|Object} A new jQuery.stylesheet object
* if name/value is not passed, or value of property or object of name/value pairs
*/
css: function (name, value) {
var self = this, styles = undefined;
switch($.type(name)) {
case 'null':
$.each(rules, function (idx, rule) {
deleteRule.call(rule.parentStyleSheet, rule);
});
//NOTE: Safari seems to replace the rules collection object on insert/delete
// Refresh our private collection to reflect the changes
rules = $.stylesheet.cssRules(selector);
return self;
case 'string':
var stylename = $.stylesheet.cssStyleName(name);
if(stylename) {
if(rules.length === 0 && value !== undefined) {
var filters = parseSelector(selector),
sheet = $(document.styleSheets).filter(function () {
return filterStyleSheet(filters.styleSheet, this);
});
sheet = (sheet && sheet.length == 1) ? sheet[0] : _sheet;
insertRule.call(sheet, filters.selectorText, name+':'+value+';');
//NOTE: See above note on Safari
// Also, IE has different behaviour for grouped selectors
rules = $.stylesheet.cssRules(selector);
styles = self;
} else {
$.each(rules, function (i, rule) {
if(rule.style[stylename] !== '') {
if(value !== undefined) {
rule.style[stylename] = value;
styles = self;
} else {
styles = rule.style[stylename];
}
return false;
}
});
if(styles === undefined && value !== undefined) {
rules[0].style[stylename] = value;
styles = self;
}
}
}
break;
case 'array':
styles = {};
$.each(name, function (idx, key) {
styles[key] = self.css(key, value);
});
if(value !== undefined) {
styles = self;
}
break;
case 'object':
$.each(name, function (key, val) {
self.css(key, val);
});
return self;
default: /*undefined*/
return self;
}
return styles;
}
});
}
};
}));
// vi:sw=2:ts=2