shadowstyles
Version:
Isolate CSS similar to Shadow DOM
4 lines • 9.53 kB
JavaScript
/*! shadowStyles 0.0.1 2015-05-08
License: MIT
Author: Ben Green <ben@latenightsketches.com> (http://github.com/numtel) */
Function.prototype.bind=Function.prototype.bind||function(a){var b=this;return function(){return b.apply(a,arguments)}},"undefined"==typeof WeakMap&&!function(){var a=Object.defineProperty,b=Date.now()%1e9,c=function(){this.name="__st"+(1e9*Math.random()>>>0)+(b++ +"__")};c.prototype={set:function(b,c){var d=b[this.name];d&&d[0]===b?d[1]=c:a(b,this.name,{value:[b,c],writable:!0})},get:function(a){var b;return(b=a[this.name])&&b[0]===a?b[1]:void 0},"delete":function(a){this.set(a,void 0)}},window.WeakMap=c}(),function(a){function b(a){u.push(a),t||(t=!0,q(d))}function c(a){return window.ShadowDOMPolyfill&&window.ShadowDOMPolyfill.wrapIfNeeded(a)||a}function d(){t=!1;var a=u;u=[],a.sort(function(a,b){return a.uid_-b.uid_});var b=!1;a.forEach(function(a){var c=a.takeRecords();e(a),c.length&&(a.callback_(c,a),b=!0)}),b&&d()}function e(a){a.nodes_.forEach(function(b){var c=p.get(b);c&&c.forEach(function(b){b.observer===a&&b.removeTransientObservers()})})}function f(a,b){for(var c=a;c;c=c.parentNode){var d=p.get(c);if(d)for(var e=0;e<d.length;e++){var f=d[e],g=f.options;if(c===a||g.subtree){var h=b(g);h&&f.enqueue(h)}}}}function g(a){this.callback_=a,this.nodes_=[],this.records_=[],this.uid_=++v}function h(a,b){this.type=a,this.target=b,this.addedNodes=[],this.removedNodes=[],this.previousSibling=null,this.nextSibling=null,this.attributeName=null,this.attributeNamespace=null,this.oldValue=null}function i(a){var b=new h(a.type,a.target);return b.addedNodes=a.addedNodes.slice(),b.removedNodes=a.removedNodes.slice(),b.previousSibling=a.previousSibling,b.nextSibling=a.nextSibling,b.attributeName=a.attributeName,b.attributeNamespace=a.attributeNamespace,b.oldValue=a.oldValue,b}function j(a,b){return w=new h(a,b)}function k(a){return x?x:(x=i(w),x.oldValue=a,x)}function l(){w=x=void 0}function m(a){return a===x||a===w}function n(a,b){return a===b?a:x&&m(a)?x:null}function o(a,b,c){this.observer=a,this.target=b,this.options=c,this.transientObservedNodes=[]}var p=new WeakMap,q=window.msSetImmediate;if(!q){var r=[],s=String(Math.random());window.addEventListener("message",function(a){if(a.data===s){var b=r;r=[],b.forEach(function(a){a()})}}),q=function(a){r.push(a),window.postMessage(s,"*")}}var t=!1,u=[],v=0;g.prototype={observe:function(a,b){if(a=c(a),!b.childList&&!b.attributes&&!b.characterData||b.attributeOldValue&&!b.attributes||b.attributeFilter&&b.attributeFilter.length&&!b.attributes||b.characterDataOldValue&&!b.characterData)throw new SyntaxError;var d=p.get(a);d||p.set(a,d=[]);for(var e,f=0;f<d.length;f++)if(d[f].observer===this){e=d[f],e.removeListeners(),e.options=b;break}e||(e=new o(this,a,b),d.push(e),this.nodes_.push(a)),e.addListeners()},disconnect:function(){this.nodes_.forEach(function(a){for(var b=p.get(a),c=0;c<b.length;c++){var d=b[c];if(d.observer===this){d.removeListeners(),b.splice(c,1);break}}},this),this.records_=[]},takeRecords:function(){var a=this.records_;return this.records_=[],a}};var w,x;o.prototype={enqueue:function(a){var c=this.observer.records_,d=c.length;if(c.length>0){var e=c[d-1],f=n(e,a);if(f)return void(c[d-1]=f)}else b(this.observer);c[d]=a},addListeners:function(){this.addListeners_(this.target)},addListeners_:function(a){var b=this.options;b.attributes&&a.addEventListener("DOMAttrModified",this,!0),b.characterData&&a.addEventListener("DOMCharacterDataModified",this,!0),b.childList&&a.addEventListener("DOMNodeInserted",this,!0),(b.childList||b.subtree)&&a.addEventListener("DOMNodeRemoved",this,!0)},removeListeners:function(){this.removeListeners_(this.target)},removeListeners_:function(a){var b=this.options;b.attributes&&a.removeEventListener("DOMAttrModified",this,!0),b.characterData&&a.removeEventListener("DOMCharacterDataModified",this,!0),b.childList&&a.removeEventListener("DOMNodeInserted",this,!0),(b.childList||b.subtree)&&a.removeEventListener("DOMNodeRemoved",this,!0)},addTransientObserver:function(a){if(a!==this.target){this.addListeners_(a),this.transientObservedNodes.push(a);var b=p.get(a);b||p.set(a,b=[]),b.push(this)}},removeTransientObservers:function(){var a=this.transientObservedNodes;this.transientObservedNodes=[],a.forEach(function(a){this.removeListeners_(a);for(var b=p.get(a),c=0;c<b.length;c++)if(b[c]===this){b.splice(c,1);break}},this)},handleEvent:function(a){switch(a.stopImmediatePropagation(),a.type){case"DOMAttrModified":var b=a.attrName,c=a.relatedNode.namespaceURI,d=a.target,e=new j("attributes",d);e.attributeName=b,e.attributeNamespace=c;var g=a.attrChange===MutationEvent.ADDITION?null:a.prevValue;f(d,function(a){return!a.attributes||a.attributeFilter&&a.attributeFilter.length&&-1===a.attributeFilter.indexOf(b)&&-1===a.attributeFilter.indexOf(c)?void 0:a.attributeOldValue?k(g):e});break;case"DOMCharacterDataModified":var d=a.target,e=j("characterData",d),g=a.prevValue;f(d,function(a){return a.characterData?a.characterDataOldValue?k(g):e:void 0});break;case"DOMNodeRemoved":this.addTransientObserver(a.target);case"DOMNodeInserted":var h,i,d=a.relatedNode,m=a.target;"DOMNodeInserted"===a.type?(h=[m],i=[]):(h=[],i=[m]);var n=m.previousSibling,o=m.nextSibling,e=j("childList",d);e.addedNodes=h,e.removedNodes=i,e.previousSibling=n,e.nextSibling=o,f(d,function(a){return a.childList?e:void 0})}l()}},a.JsMutationObserver=g,a.MutationObserver||(a.MutationObserver=g)}(this),function(){"use strict";var a="css-negate",b="shadow",c=5,d="undefined"!=typeof module?require("mutation-observer"):window.MutationObserver,e=[],f=!1,g=function(a){e.push(a),h()};g.nativeSupport=function(){try{document.querySelector("::shadow")}catch(a){return!1}return!0}();var h=function(){if(f)for(;e.length;){var a=e.shift(),b=s(a);k(b)}};window.addEventListener("load",function(){f=!0,window.unwrap||(window.unwrap=function(a){return a}),h()},!0);var i=new d(function(c){c.forEach(function(c){var d=[];"attributes"===c.type&&c.attributeName!==b&&c.attributeName!==a?d=d.concat(p(c.target)):"childList"===c.type&&Array.prototype.forEach.call(c.addedNodes,function(b){"#text"===b.nodeName||b.getAttribute(a)||(i.observe(b,{attributes:!0,childList:!0}),b.setAttribute(a,""),d=d.concat(p(b)),b.addEventListener_&&b.addEventListener_("DOMNodeInserted",function(a){k(d)},!1))}),k(l(d))})}),j=new d(function(a){a.forEach(function(a){var b=Array.prototype.slice.call(a.target.querySelectorAll("[shadow] *"),0);k(b)})}),k=function(d){d.length&&Array.prototype.forEach.call(document.styleSheets,function(e){o(e,function(e,f,g){var h=e.selectorText.split(",");h.forEach(function(i,j){i=i.trim();var l=i.replace(v.pseudoClass,"");Array.prototype.forEach.call(d,function(d){var m=d.getAttribute(a)||"";if(d.shadowRoot&&k(d.shadowRoot.querySelectorAll("*")),n(d,l)){var o=i.lastIndexOf("["+b+"]"),p=q(d);if(o>-1&&!p.shadowRoot){p.host&&(p=p.host);var r=i.indexOf(" ",o),s=r>-1?i.substr(0,r):i;if(n(p,s))return}if(-1===o&&p&&p.shadowRoot)return;var w=i.match(v.bufferAttr);if(w)w=w[2]||w[3];else{w=u(c),i=t(i,w),h[j]=i;var x=e.cssText.substr(e.selectorText.length);g.insertRule(h.join(", ")+x,f+1),g.deleteRule(f),e=g.cssRules[f]}-1===m.indexOf(w)&&(m+=w,d.setAttribute(a,m))}})})})})},l=function(a){return a.reduce(function(a,b){return a.indexOf(b)<0&&a.push(b),a},[])},m=function(){for(var a=["matches","msMatchesSelector","webkitMatchesSelector","mozMatchesSelector","oMatchesSelector"],b=0;b<a.length;b++)if(a[b]in document.documentElement)return document.documentElement[a[b]]}(),n=function(a,b){try{var c=m.call(unwrap(a),b)}catch(d){return}return c},o=function(a,b){a.cssRules&&Array.prototype.forEach.call(a.cssRules,function(c,d){1===c.type?b(c,d,a):c.cssRules?o(c,b):c.styleSheet&&o(c.styleSheet,b)})},p=function(a,b){var c=b?a:q(a),d=Array.prototype.slice.call(c.querySelectorAll("*"),0);return c.host?d=d.concat(Array.prototype.slice.call(c.host.querySelectorAll("*"),0)):c.shadowRoot&&(d=d.concat(Array.prototype.slice.call(c.shadowRoot.querySelectorAll("*"),0))),d},q=function(a){for(;a.parentNode;){if(window.ShadowRoot&&a.parentNode instanceof ShadowRoot)return a.parentNode;if(a.hasAttribute(b))return a;a=a.parentNode}},r=function(a){for(var b=[];a.parentNode;)b.push(a.parentNode),a=a.parentNode;return b},s=function(c){var d=[],e=[];return"string"==typeof c?d=document.querySelectorAll(c):c instanceof Array?c.forEach(function(a){e=e.concat(s(a))}):d=[c],Array.prototype.forEach.call(d,function(c){var d=c.shadowRoot||c;i.observe(d,{attributes:!0,childList:!0}),c.setAttribute(b,""),r(c).forEach(function(c){c.addEventListener_?c.addEventListener_("DOMAttrModified",function(e){if(e.target===unwrap(c)&&e.attrName!==a&&e.attrName!==b){var f=p(d,!0);k(f)}},!0):j.observe(c,{attributes:!0})});var f=p(d,!0);Array.prototype.forEach.call(f,function(b){i.observe(b,{attributes:!0,childList:!0}),b.setAttribute(a,""),e.push(b)})}),e},t=function(b,c){var d=b.lastIndexOf(":"),e=b.lastIndexOf(" "),f=":not(["+a+'*="'+c+'"])';if(-1===d)return b+f;for(var g=b.lastIndexOf(":",d-1);g>e;)d=g,g=b.lastIndexOf(":",g-1);return b.substr(0,d)+f+b.substr(d)},u=function(a){for(var b="",c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789",d=0;a>d;d++)b+=c.charAt(Math.floor(Math.random()*c.length));return b},v={bufferAttr:new RegExp(":not\\(\\["+a+"\\*=(\"([^\"]+)\"|'([^']+)')\\]\\)"),pseudoClass:new RegExp("(:after|:before|::after|::before|:hover|:active|:focus|:checked|:valid|:invalid)","gi")};"undefined"!=typeof module?module.exports=g:document.shadowStyles=g}();