-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.rule.js
9 lines (9 loc) · 4.15 KB
/
jquery.rule.js
1
2
3
4
5
6
7
8
9
/*
* jQuery.Rule - Css Rules manipulation, the jQuery way.
* Copyright (c) 2007-2011 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
* Dual licensed under MIT and GPL.
* Date: 02/7/2011
* @author Ariel Flesler
* @version 1.0.2
*/
(function(f){var c=f('<style rel="alternate stylesheet" type="text/css" />').appendTo("head")[0],k=c.sheet?"sheet":"styleSheet",i=c[k],m=i.rules?"rules":"cssRules",g=i.deleteRule?"deleteRule":"removeRule",d=i.ownerNode?"ownerNode":"owningElement",e=/^([^{]+)\{([^}]*)\}/m,l=/([^:]+):([^;}]+)/;i.disabled=true;var j=f.rule=function(n,o){if(!(this instanceof j)){return new j(n,o)}this.sheets=j.sheets(o);if(n&&e.test(n)){n=j.clean(n)}if(typeof n=="object"&&!n.exec){b(this,n.get?n.get():n.splice?n:[n])}else{b(this,this.sheets.cssRules().get());if(n){return this.filter(n)}}return this};f.extend(j,{sheets:function(p){var n=p;if(typeof n!="object"){n=f.makeArray(document.styleSheets)}n=f(n).not(i);if(typeof p=="string"){n=n.ownerNode().filter(p).sheet()}return n},rule:function(n){if(n.selectorText){return["",n.selectorText,n.style.cssText]}return e.exec(n)},appendTo:function(q,n,o){switch(typeof n){case"string":n=this.sheets(n);case"object":if(n[0]){n=n[0]}if(n[k]){n=n[k]}if(n[m]){break}default:if(typeof q=="object"){return q}n=i}var t;if(!o&&(t=this.parent(q))){q=this.remove(q,t)}var s=this.rule(q);if(n.addRule){n.addRule(s[1],s[2]||";")}else{if(n.insertRule){n.insertRule(s[1]+"{"+s[2]+"}",n[m].length)}}return n[m][n[m].length-1]},remove:function(o,q){q=q||this.parent(o);if(q!=i){var n=q?f.inArray(o,q[m]):-1;if(n!=-1){o=this.appendTo(o,0,true);q[g](n)}}return o},clean:function(n){return f.map(n.split("}"),function(o){if(o){return j.appendTo(o+"}")}})},parent:function(o){if(typeof o=="string"||!f.browser.msie){return o.parentStyleSheet}var n;this.sheets().each(function(){if(f.inArray(o,this[m])!=-1){n=this;return false}});return n},outerText:function(n){return !n||!n.selectorText?"":[n.selectorText+"{","\t"+n.style.cssText,"}"].join("\n").toLowerCase()},text:function(o,n){if(n!==undefined){o.style.cssText=n}return !o?"":o.style.cssText.toLowerCase()}});j.fn=j.prototype={pushStack:function(n,p){var o=j(n,p||this.sheets);o.prevObject=this;return o},end:function(){return this.prevObject||j(0,[])},filter:function(n){var p;if(!n){n=/./}if(n.split){p=f.trim(n).toLowerCase().split(/\s*,\s*/);n=function(){var o=this.selectorText||"";return !!f.grep(o.toLowerCase().split(/\s*,\s*/),function(q){return f.inArray(q,p)!=-1}).length}}else{if(n.exec){p=n;n=function(){return p.test(this.selectorText)}}}return this.pushStack(f.grep(this,function(q,o){return n.call(q,o)}))},add:function(n,o){return this.pushStack(f.merge(this.get(),j(n,o)))},is:function(n){return !!(n&&this.filter(n).length)},not:function(p,o){p=j(p,o);return this.filter(function(){return f.inArray(this,p)==-1})},append:function(n){var p=this,o;f.each(n.split(/\s*;\s*/),function(r,q){if((o=l.exec(q))){p.css(o[1],o[2])}});return this},text:function(n){return !arguments.length?j.text(this[0]):this.each(function(){j.text(this,n)})},outerText:function(){return j.outerText(this[0])}};f.each({ownerNode:d,sheet:k,cssRules:m},function(n,o){var p=o==m;f.fn[n]=function(){return this.map(function(){return p?f.makeArray(this[o]):this[o]})}});f.fn.cssText=function(){return this.filter("link,style").eq(0).sheet().cssRules().map(function(){return j.outerText(this)}).get().join("\n")};f.each("remove,appendTo,parent".split(","),function(n,o){j.fn[o]=function(){var p=f.makeArray(arguments),q=this;p.unshift(0);return this.each(function(r){p[0]=this;q[r]=j[o].apply(j,p)||q[r]})}});f.each(("each,index,get,size,eq,slice,map,attr,andSelf,css,show,hide,toggle,queue,dequeue,stop,animate,fadeIn,fadeOut,fadeTo").split(","),function(n,o){j.fn[o]=f.fn[o]});function b(o,n){o.length=0;Array.prototype.push.apply(o,n)}var h=f.curCSS;f.curCSS=function(o,n){return("selectorText" in o)?o.style[n]||f.prop(o,n=="opacity"?1:0,"curCSS",0,n):h.apply(this,arguments)};j.cache={};var a=function(n){return function(p){var o=p.selectorText;if(o){arguments[0]=j.cache[o]=j.cache[o]||{}}return n.apply(f,arguments)}};f.data=a(f.data);f.removeData=a(f.removeData);f(window).unload(function(){f(i).cssRules().remove()})})(jQuery);