-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.important.js
491 lines (446 loc) · 18.8 KB
/
jquery.important.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*!
* !important
* github.com/premasagar/important/
*
*//*
css !important manipulator (jQuery plugin)
by Premasagar Rose
dharmafly.com
license
opensource.org/licenses/mit-license.php
v0.2
*//*
creates methods
jQuery.important(boolean)
jQuery(elem).important(boolean)
jQuery(elem).isStyleNameSet()
jQuery(elem).isImportant()
and wraps the native jQuery CSS methods: css(), width(), height(),
allowing an optional last argument of boolean true, to pass the request through the !important function
use jQuery.important.noConflict() to revert back to the native jQuery methods, and returns the overriding methods
reference
http://www.w3.org/TR/CSS2/syndata.html#tokenization
*/
(function($){
'use strict';
// return a regular expression of a declaration, with the backreferences as the CSS property and the value
function find(property, rules){
var match = rules.match(regexDeclaration(property));
if (match){
// a bit inelegant: remove leading semicolon if present
match[0] = match[0].replace(/^;/, '');
}
return match;
}
function regexDeclaration(property){
return new RegExp('(?:^|\\s|;)(' + property + ')\\s*:\\s*([^;]*(?:;|$))', 'i');
}
// create CSS text from property & value, optionally inserting it into the supplied CSS rule
// e.g. declaration('width', '50%', 'margin:2em; width:auto;');
function cssDeclaration(property, value, rules, makeImportant, elem){ // if value === null, then remove from style; if style then merge with that
var oldDeclaration, newDeclaration;
rules = rules || '';
oldDeclaration = find(property, rules);
if (value === null){
newDeclaration = '';
}
else if (typeof value === 'string'){
newDeclaration = property + ': ' + value + ((makeImportant) ? ' !important;' : ';');
}
if (oldDeclaration){
if (typeof value === 'boolean'){
makeImportant = value;
newDeclaration = $.important(property + ': ' + oldDeclaration[2], makeImportant);
}
if (oldDeclaration[0][0] === " ") {
newDeclaration = " " + newDeclaration;
}
rules = rules.replace(oldDeclaration[0], newDeclaration);
}
else if (typeof newDeclaration !== 'undefined'){
rules = $.trim(rules);
if (rules !== ''){
if (rules.slice(-1) !== ';'){
rules += ';';
}
rules += ' ';
}
rules += newDeclaration;
}
return rules;
}
// Add !important to CSS rules if they don't already have it
function toImportant(rulesets, makeImportant){
// Cache regular expression
var re = toImportant.re;
if (!re){
re = toImportant.re =
/\s*(! ?important)?[\s\r\t\n]*;/g;
// TODO: Make this regexp handle missing semicolons at the end of a ruleset
}
if (makeImportant === false){
return rulesets.replace(re, ';');
}
return rulesets.replace(re, function($0, $1){
return $1 ? $0 : ' !important;';
});
}
function htmlStylesToImportant(html, makeImportant){
// Cache regular expression
var re = htmlStylesToImportant.re;
if (!re){
re = htmlStylesToImportant.re =
/(?=<style[^>]*>)([\w\W]*?)(?=<\/style>)/g;
}
return html.replace(re, function($0, rulesets){
return toImportant(rulesets, makeImportant);
});
}
var important = false;
var original = {};
var controller = {};
// TODO: other methods to be supported
/*,
show: function(){},
hide: function(){},
animate: function(){}
*/
$.each([ "css" ], function(index, method) {
original[method] = $.fn[method];
controller[method] = function() {
var args = $.makeArray(arguments);
var elem = $(this);
if (elem.length < 1) {
return original[method].apply(elem, args);
}
if (args.length < 1) {
// Invalid to have no arguments, but just pass it through to let jQuery do it's default handling
return original[method].apply(elem);
} else if (typeof args[0] === "object") {
if (typeof args[0].length === "number") {
// .css(propertyNames) --- GET
return original[method].apply(elem, args);
} else {
// .css(properties) --- SET
// .css(properties, important) --- SET
var returnValue = true;
var propertyNames = Object.keys(args[0]);
for (var i = 0; i < propertyNames.length; i++) {
var propertyName = propertyNames[i];
var propertyValue = args[0][propertyNames[i]];
var isImportantParam = args[1];
returnValue = (!returnValue) ? returnValue : applyStyleToElement(elem, propertyName,
propertyValue, (isImportantParam === true || important === true), method, [propertyName, propertyValue]);
}
return returnValue;
}
} else if (args.length === 1) {
// .css(propertyName) --- GET
return original[method].apply(elem, args);
} else if (args.length >= 2) {
// This is a setter, using one of the following formats:
// .css(name, value) --- SET
// .css(name, function) --- SET
// .css(name, value, important) --- SET
// .css(name, function, important) --- SET
var propertyName = args[0];
var propertyValue = args[1];
var isImportantParam = args[2];
return applyStyleToElement(elem, propertyName, propertyValue, (isImportantParam === true || important === true), method, [propertyName, propertyValue]);
} else {
// This is an invalid set of arguments, or there is a bug in our processing of
// the different ways .css() can be called so just call the original function
return original[method].apply(elem, args);
}
};
});
$.each([ "width", "height" ], function(index, method) {
original[method] = $.fn[method];
controller[method] = function() {
var args = $.makeArray(arguments);
var elem = $(this);
if (elem.length < 1) {
return original[method].apply(elem, args);
}
if (args.length < 1) {
// .width() --- GET
return original[method].apply(elem);
} else {
// .width(value) --- SET
// .width(function) --- SET
// .width(value, important) --- SET
// .width(function, important) --- SET
var propertyName = method;
var propertyValue = args[0];
var isImportantParam = args[1];
return applyStyleToElement(elem, propertyName, propertyValue, (isImportantParam === true || important === true), method, [propertyValue]);
}
};
});
// Taken from jQuery 1.10. The "ms-" conversion is because some ie styles have the leading "-"
function camelCaseStyleName(string) {
return string
.replace(/^-ms-/, "ms-")
.replace(/-([\da-z])/gi, function(all, letter) {
return letter.toUpperCase();
});
}
function applyStyleToElement(elem, propertyName, propertyValue, makeImportant, method, methodArgs) {
var complexStyles = [
{
baseStyle: "background",
subStyles: [ "background-attachment", "background-color", "background-image",
"background-position", "background-repeat" ]
},
{
baseStyle: "border-width",
subStyles: [ "border-top-width", "border-right-width", "border-bottom-width", "border-left-width" ]
},
{
baseStyle: "border-style",
subStyles: [ "border-top-style", "border-right-style", "border-bottom-style", "border-left-style" ]
},
{
baseStyle: "border-color",
subStyles: [ "border-top-color", "border-right-color", "border-bottom-color", "border-left-color" ]
},
{
baseStyle: "border",
subStyles: [ "border-width", "border-top-width", "border-right-width",
"border-bottom-width", "border-left-width", "border-top-style",
"border-right-style", "border-bottom-style", "border-left-style",
"border-top-color", "border-right-color", "border-bottom-color",
"border-left-color", "border-top", "border-right", "border-bottom",
"border-left", "border-style", "border-spacing", "border-color",
"border-collapse" ]
},
{
baseStyle: "font",
subStyles: [ "font-weight", "font-variant", "font-style", "font-size", "font-family" ]
},
{
baseStyle: "list-style",
subStyles: [ "list-style-type", "list-style-position", "list-style-image" ]
},
{
baseStyle: "margin",
subStyles: [ "margin-top", "margin-right", "margin-bottom", "margin-left" ]
},
{
baseStyle: "outline",
subStyles: [ "outline-width", "outline-style", "outline-color" ]
},
{
baseStyle: "padding",
subStyles: [ "padding-top", "padding-right", "padding-bottom", "padding-left" ]
}
];
var existingStylesWithImportant = [];
for (var i = 0; i < complexStyles.length; i++) {
if (camelCaseStyleName(propertyName) === camelCaseStyleName(complexStyles[i].baseStyle)) {
existingStylesWithImportant.push(complexStyles[i].baseStyle);
$(elem).important(complexStyles[i].baseStyle, false);
for (var j = 0; j < complexStyles[i].subStyles.length; j++) {
if (elem.isImportant(complexStyles[i].subStyles[j])) {
existingStylesWithImportant.push(complexStyles[i].subStyles[j]);
$(elem).important(complexStyles[i].subStyles[j], false);
}
}
}
for (var j = 0; j < complexStyles[i].subStyles.length; j++) {
if (camelCaseStyleName(propertyName) === camelCaseStyleName(complexStyles[i].subStyles[j])) {
if (elem.isImportant(complexStyles[i].subStyles[j])) {
existingStylesWithImportant.push(complexStyles[i].subStyles[j]);
$(elem).important(complexStyles[i].subStyles[j], false);
}
if (elem.isImportant(complexStyles[i].baseStyle)) {
existingStylesWithImportant.push(complexStyles[i].baseStyle);
$(elem).important(complexStyles[i].baseStyle, false);
}
break;
}
}
if ($.grep(complexStyles[i].subStyles, function(value) { return camelCaseStyleName(value) === camelCaseStyleName(propertyName); }).length > 0) {
if (elem.isStyleNameSet(complexStyles[i].baseStyle)) {
existingStylesWithImportant.push(complexStyles[i].baseStyle);
$.merge(existingStylesWithImportant, complexStyles[i].subStyles);
}
}
}
if (elem.isImportant(propertyName)) {
existingStylesWithImportant.push(propertyName);
$(elem).important(propertyName, false);
}
var retVal = original[method].apply(elem, methodArgs);
if (makeImportant === true) {
for (var i = 0; i < existingStylesWithImportant.length; i++) {
$(elem).important(existingStylesWithImportant[i], true);
}
$(elem).important(propertyName, true);
}
return retVal;
}
// Override the native jQuery methods with new methods
$.extend($.fn, controller);
// jQuery.important
$.important = $.extend(
function(){
var
args = $.makeArray(arguments),
makeImportant, cacheImportant;
if (typeof args[0] === 'string'){
if (typeof args[1] === 'undefined' || typeof args[1] === 'boolean'){
makeImportant = (args[1] !== false);
return (/<\w+.*>/).test(args[0]) ?
htmlStylesToImportant(args[0], makeImportant) :
toImportant(args[0], makeImportant);
}
}
// If a function is passed, then execute it while the !important flag is set to true
else if ($.isFunction(args[0])){
cacheImportant = important;
$.important.status = important = true;
args[0].call(this);
$.important.status = important = cacheImportant;
}
else if (typeof args[0] === 'undefined' || typeof args[0] === 'boolean'){
$.important.status = important = (args[0] !== false);
}
return important;
},
{
status: important,
// release native jQuery methods back to their original versions
noConflict: function(){
$.each(original, function(method, fn) {
$.fn[method] = fn;
});
},
declaration: cssDeclaration
}
);
$.fn.getElementStyle = function(cssProperty) {
if (typeof cssProperty !== 'string') {
return null;
}
var elem = $(this);
if (elem.length === 0) {
return null;
}
var existingProperty = find(cssProperty, elem.get(0).style.cssText);
if (existingProperty == null || existingProperty.length < 3) {
return null;
} else {
return existingProperty[2];
}
};
$.fn.isStyleNameSet = function(cssProperty) {
if (typeof cssProperty !== 'string') {
return null;
}
var elem = $(this);
if (elem.length === 0) {
return null;
}
var existingProperty = find(cssProperty, elem.get(0).style.cssText);
if (existingProperty == null) {
return null;
} else {
return true;
}
};
$.fn.isImportant = function(cssProperty) {
if (typeof cssProperty !== 'string') {
return null;
}
var elem = $(this);
if (elem.length === 0) {
return null;
}
var existingProperty = find(cssProperty, elem.get(0).style.cssText);
if (existingProperty == null) {
return null;
}
if (existingProperty.length < 3) {
return false;
} else {
var existingPropertyValue = $.trim(existingProperty[2]);
if (existingPropertyValue.lastIndexOf("!important;") + "!important;".length == existingPropertyValue.length) {
return true;
} else if (existingPropertyValue.lastIndexOf("!important") + "!important".length == existingPropertyValue.length) {
return true;
}
return false;
}
};
// jQuery(elem).important()
$.fn.important = function(method){
var
elem = $(this),
args = $.makeArray(arguments).concat(true),
nodeName = elem.data('nodeName'),
property, makeImportant, fn, oldStyleElem, newStyleInsert, newStyleInsertVerb;
// .css() is the default method, e.g. $(elem).important({border:'1px solid red'});
if (typeof method === 'undefined' || typeof method === 'boolean'){
// special behaviour for specific elements
if (!nodeName){
nodeName = elem.attr('nodeName').toLowerCase();
elem.data('nodeName', nodeName);
}
// style elements
if (nodeName === 'style'){
makeImportant = (method !== false);
elem.html(
toImportant(elem.html(), makeImportant)
);
var stylesheet = elem.attr('sheet');
if (stylesheet && stylesheet.cssRules){
$.each(stylesheet.cssRules, function(i, rule){
if (rule.type === CSSRule.STYLE_RULE){
rule.style.cssText = $.important(rule.style.cssText, makeImportant);
}
});
}
}
else {
elem.attr(
'style',
$.important(elem.attr('style'), method)
);
}
return elem;
}
else if (typeof method === 'object'){
args.unshift('css');
return elem.important.apply(this, args);
}
else if (typeof method === 'string'){
// switch the !important statement on or off for a particular property in an element's inline styles - but instead of elem.css(property), they should directly look in the style attribute
// e.g. $(elem).important('padding');
// e.g. $(elem).important('padding', false);
property = method;
makeImportant = (args[1] === true);
elem.attr(
'style',
cssDeclaration(property, makeImportant, elem.attr('style'))
);
}
// pass a function, which will be executed while the !important flag is set to true
/* e.g.
elem.important(function(){
$(this).css('height', 'auto');
});
*/
else if ($.isFunction(method)){
fn = method;
$.important.call(this, fn);
}
return elem;
};
}(jQuery));
/*
NOTES:
http://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertypriority
$('style')[0].sheet.cssRules[0].style.getPropertyPriority('color');
cssText on style object possible
*/