-
Notifications
You must be signed in to change notification settings - Fork 15
/
scoper.min.js
1 lines (1 loc) · 2.06 KB
/
scoper.min.js
1
function scoperInit(){var e=document.createElement("style");e.appendChild(document.createTextNode("")),document.head.appendChild(e),e.sheet.insertRule("body { visibility: hidden; }",0)}function scoper(e,t){var o=new RegExp("([^\r\n,{}]+)(,(?=[^}]*{)|s*{)","g");return e=e.replace(o,function(e,o,n){return o.match(/^\s*(@media|@.*keyframes|to|from|@font-face|1?[0-9]?[0-9])/)?o+n:(o.match(/:scope/)&&(o=o.replace(/([^\s]*):scope/,function(e,t){return""===t?"> *":"> "+t})),(o=o.replace(/^(\s*)/,"$1"+t+" "))+n)})}function scoperProcess(){var e=document.body.querySelectorAll("style[scoped]");if(0!==e.length){for(var t=document.head||document.getElementsByTagName("head")[0],o=0;o<e.length;o++){var n=e[o],r=n.innerHTML;if(r&&"BODY"!==n.parentElement.nodeName){var d="scoper-"+o,c="#"+d,s=document.createElement("span");s.id=d;var a=n.parentNode;a.parentNode.replaceChild(s,a),s.appendChild(a),n.parentNode.removeChild(n);var l=document.createElement("style");l.setAttribute("data-scoped-style-for",d);var i=scoper(r,c);l.styleSheet?l.styleSheet.cssText=i:l.appendChild(document.createTextNode(i)),t.appendChild(l)}}document.getElementsByTagName("body")[0].style.visibility="visible"}else document.getElementsByTagName("body")[0].style.visibility="visible"}function scoperReset(){for(var e=document.head.querySelectorAll("style[data-scoped-style-for]"),t=0;t<e.length;t++){var o=e[t],n=o.getAttribute("data-scoped-style-for"),r=document.getElementById(n);if(r){var d=o.innerHTML.replace("#"+n+" ",""),c=r.parentNode,s=r.childNodes[0];c.replaceChild(s,r);var a=document.createElement("style");a.setAttribute("scoped","true"),a.styleSheet?a.styleSheet.cssText=d:a.appendChild(document.createTextNode(d)),s.appendChild(a)}o.parentNode.removeChild(o)}}function scoperRestart(){scoperReset(),scoperProcess()}!function(){"use strict";"undefined"==typeof document||"scoped"in document.createElement("style")||(scoperInit(),"complete"===document.readyState||"loaded"===document.readyState?scoperProcess():document.addEventListener("DOMContentLoaded",scoperProcess))}(),"undefined"!=typeof exports&&(exports.scoper=scoper);