67 lines
2.1 KiB
JavaScript
67 lines
2.1 KiB
JavaScript
(function($) {
|
|
if ($.fn.xstyle) {
|
|
return;
|
|
}
|
|
|
|
// Escape regex chars with \
|
|
var escape = function(text) {
|
|
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
|
};
|
|
|
|
// For those who need them (< IE 9), add support for CSS functions
|
|
var isStyleFuncSupported = !!CSSStyleDeclaration.prototype.getPropertyValue;
|
|
if (!isStyleFuncSupported) {
|
|
CSSStyleDeclaration.prototype.getPropertyValue = function(a) {
|
|
return this.getAttribute(a);
|
|
};
|
|
CSSStyleDeclaration.prototype.setProperty = function(styleName, value, priority) {
|
|
console.log('set-priority', styleName, value, priority);
|
|
this.setAttribute(styleName, value);
|
|
var priority = typeof priority != 'undefined' ? priority : '';
|
|
if (priority != '') {
|
|
// Add priority manually
|
|
var rule = new RegExp(escape(styleName) + '\\s*:\\s*' + escape(value) +
|
|
'(\\s*;)?', 'gmi');
|
|
this.cssText =
|
|
this.cssText.replace(rule, styleName + ': ' + value + ' !' + priority + ';');
|
|
console.log(this.cssText);
|
|
}
|
|
};
|
|
CSSStyleDeclaration.prototype.removeProperty = function(a) {
|
|
return this.removeAttribute(a);
|
|
};
|
|
CSSStyleDeclaration.prototype.getPropertyPriority = function(styleName) {
|
|
var rule = new RegExp(escape(styleName) + '\\s*:\\s*[^\\s]*\\s*!important(\\s*;)?',
|
|
'gmi');
|
|
return rule.test(this.cssText) ? 'important' : '';
|
|
}
|
|
}
|
|
|
|
// The style function
|
|
$.fn.xstyle = function(styleName, value, priority) {
|
|
// DOM node
|
|
var node = this.get(0);
|
|
// Ensure we have a DOM node
|
|
if (typeof node == 'undefined') {
|
|
return this;
|
|
}
|
|
// CSSStyleDeclaration
|
|
var style = this.get(0).style;
|
|
// Getter/Setter
|
|
console.log('style', styleName, value, priority);
|
|
if (typeof styleName != 'undefined') {
|
|
if (typeof value != 'undefined') {
|
|
// Set style property
|
|
priority = typeof priority != 'undefined' ? priority : '';
|
|
style.setProperty(styleName, value, priority);
|
|
return this;
|
|
} else {
|
|
// Get style property
|
|
return style.getPropertyValue(styleName);
|
|
}
|
|
} else {
|
|
// Get CSSStyleDeclaration
|
|
return style;
|
|
}
|
|
};
|
|
})(jQuery); |