Skip to content

Instantly share code, notes, and snippets.

@think49
Last active June 7, 2017 04:05
Show Gist options
  • Save think49/953107 to your computer and use it in GitHub Desktop.
Save think49/953107 to your computer and use it in GitHub Desktop.
cssstyledeclaration-removeproperty.js, cssstyledeclaration-setproperty.js : IE8- で動作する CSSStyleDeclaration#removeProperty, CSSStyleDeclaration#setProperty
/**
* cssstyledeclaration-removeproperty.js
* define CSSStyleDeclaration.prototype.removeProperty for IE.
*
* @version 1.0.4
* @author think49
* @url https://gist.github.com/953107
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License)
* @see <a href="http://dev.w3.org/csswg/cssom/#the-cssstyledeclaration-interface">5.5.1. The CSSStyleDeclaration Interface - CSSOM</a>
* @see <a href="http://msdn.microsoft.com/en-us/library/dd347052%28v=vs.85%29.aspx">CSSStyleDeclaration Prototype (attribute) - MSDN</a>
*/
if ((typeof CSSStyleDeclaration === 'function' || typeof CSSStyleDeclaration === 'object') &&
!('removeProperty' in CSSStyleDeclaration.prototype)) {
(function (String, callbackfn) {
this.removeProperty = function (property) {
var value;
if (arguments.length < 1) {
return;
}
property = String(property);
if (property === 'float') {
if ('cssFloat' in this) {
property = 'cssFloat';
} else if ('styleFloat' in this) {
property = 'styleFloat';
}
} else {
property = property.replace(/-([a-z])/g, callbackfn);
}
if (!(property in this)) {
return;
}
value = this[property];
this[property] = '';
return typeof value === 'string' ? value : '';
};
}).call(CSSStyleDeclaration.prototype, String, function (subString, capture1) { return capture1.toUpperCase(); });
}
/**
* cssstyledeclaration-setproperty.js
* define CSSStyleDeclaration.prototype.setproperty for IE.
*
* @version 1.0.2
* @author think49
* @url https://gist.github.com/953107
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License)
* @see <a href="http://dev.w3.org/csswg/cssom/#the-cssstyledeclaration-interface">5.5.1. The CSSStyleDeclaration Interface - CSSOM</a>
* @see <a href="http://msdn.microsoft.com/en-us/library/dd347052%28v=vs.85%29.aspx">CSSStyleDeclaration Prototype (attribute) - MSDN</a>
*/
if ((typeof CSSStyleDeclaration === 'function' || typeof CSSStyleDeclaration === 'object') &&
!('setProperty' in CSSStyleDeclaration.prototype)) {
(function (String, RegExp, callbackfn) {
function removeDeclarationByProperty (cssText, property) {
var reg = /^\s*([^\s:]+)\s*:(?:[^\u0022\u0027;]*(?:\u0022[^\u0022]*\u0022|\u0027[^\u0027]*\u0027))*[^;]*(?:;|$)$/;
function callbackfn (token, p1) {
if (!reg.test(token)) {
throw new Error('ParseError');
}
return p1 === property ? '' : token;
}
cssText = cssText.replace(/\s*([^\s:]+)\s*:(?:[^\u0022\u0027;]*(?:\u0022[^\u0022]*\u0022|\u0027[^\u0027]*\u0027))*[^;]*(?:;|$)|[\s\S]/g, callbackfn);
property = reg = callbackfn = null;
return cssText;
}
this.setProperty = function (property, value, priority) {
var styleProperty;
if (arguments.length < 2) {
return;
}
styleProperty = property = String(property);
if (property === 'float') {
if ('cssFloat' in this) {
styleProperty = 'cssFloat';
} else if ('styleFloat' in this) {
styleProperty = 'styleFloat';
}
} else {
styleProperty = property.replace(/-([a-z])/g, callbackfn);
}
if (!(styleProperty in this)) {
return;
}
value = String(value);
if (arguments.length < 3 || String(priority).toLowerCase() !== 'important') {
this[styleProperty] = value;
} else {
this.cssText = removeDeclarationByProperty(this.cssText, property) + ';' + property + ':' + value + ' !important;';
}
};
}).call(CSSStyleDeclaration.prototype, String, RegExp, function (subString, capture1) { return capture1.toUpperCase(); });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment