@webcomponents/html-imports
Version:
29 lines (26 loc) • 9.14 kB
JavaScript
(function(){/*
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
;(function(t){function B(a,b){if("function"===typeof window.CustomEvent)return new CustomEvent(a,b);var c=document.createEvent("CustomEvent");c.initCustomEvent(a,!!b.bubbles,!!b.cancelable,b.detail);return c}function p(a){if(x)return a.ownerDocument!==document?a.ownerDocument:null;var b=a.__importDoc;if(!b&&a.parentNode){b=a.parentNode;if("function"===typeof b.closest)b=b.closest("link[rel=import]");else for(;!u(b)&&(b=b.parentNode););a.__importDoc=b}return b}function G(a){var b=m(document,
"link[rel=import]:not([import-dependency])"),c=b.length;c?h(b,function(d){return q(d,function(){0===--c&&a()})}):a()}function C(a){function b(){"loading"!==document.readyState&&document.body&&(document.removeEventListener("readystatechange",b),a())}document.addEventListener("readystatechange",b);b()}function D(a){C(function(){return G(function(){return a&&a()})})}function q(a,b){if(a.__loaded)b&&b();else if("script"===a.localName&&!a.src||"style"===a.localName&&!a.firstChild||"style"===a.localName&&
"http://www.w3.org/2000/svg"===a.namespaceURI)a.__loaded=!0,b&&b();else{var c=function(d){a.removeEventListener(d.type,c);a.__loaded=!0;b&&b()};a.addEventListener("load",c);"style"===a.localName&&(y||v)||a.addEventListener("error",c)}}function u(a){return a.nodeType===Node.ELEMENT_NODE&&"link"===a.localName&&"import"===a.rel}function k(){var a=this;this.g={};this.h=0;this.l=new MutationObserver(function(b){return a.K(b)});this.l.observe(document.head,{childList:!0,subtree:!0});this.loadImports(document)}
function E(a){h(m(a,"template"),function(b){h(m(b.content,'script:not([type]),script[type="application/javascript"],script[type="text/javascript"],script[type="module"]'),function(c){var d=document.createElement("script");h(c.attributes,function(f){return d.setAttribute(f.name,f.value)});d.textContent=c.textContent;c.parentNode.replaceChild(d,c)});E(b.content)})}function m(a,b){return a.childNodes.length?a.querySelectorAll(b):H}function h(a,b,c){var d=a?a.length:0,f=c?-1:1;for(c=c?d-1:0;c<d&&0<=c;c+=
f)b(a[c],c)}var n=document.createElement("link"),x="import"in n,H=n.querySelectorAll("*"),z=null;!1==="currentScript"in document&&Object.defineProperty(document,"currentScript",{get:function(){return z||("complete"!==document.readyState?document.scripts[document.scripts.length-1]:null)},configurable:!0});var I=/(url\()([^)]*)(\))/g,J=/(@import[\s]+(?!url\())([^;]*)(;)/g,K=/(<link[^>]*)(rel=['|"]?stylesheet['|"]?[^>]*>)/g,l={H:function(a,b){a.href&&a.setAttribute("href",l.i(a.getAttribute("href"),
b));a.src&&a.setAttribute("src",l.i(a.getAttribute("src"),b));if("style"===a.localName){var c=l.B(a.textContent,b,I);a.textContent=l.B(c,b,J)}},B:function(a,b,c){return a.replace(c,function(d,f,e,g){d=e.replace(/["']/g,"");b&&(d=l.i(d,b));return f+"'"+d+"'"+g})},i:function(a,b){if(void 0===l.j){l.j=!1;try{var c=new URL("b","http://a");c.pathname="c%20d";l.j="http://a/c%20d"===c.href}catch(d){}}if(l.j)return(new URL(a,b)).href;c=l.G;c||(c=document.implementation.createHTMLDocument("temp"),l.G=c,c.o=
c.createElement("base"),c.head.appendChild(c.o),c.m=c.createElement("a"));c.o.href=b;c.m.href=a;return c.m.href||a}},F={async:!0,load:function(a,b,c){if(a)if(a.match(/^data:/)){a=a.split(",");var d=a[1];d=-1<a[0].indexOf(";base64")?atob(d):decodeURIComponent(d);b(d)}else{var f=new XMLHttpRequest;f.open("GET",a,F.async);f.onload=function(){var e,g=null!=(e=f.responseURL||f.getResponseHeader("Location"))?e:void 0;g&&0===g.indexOf("/")&&(g=(location.origin||location.protocol+"//"+location.host)+g);e=
f.response||f.responseText;304===f.status||0===f.status||200<=f.status&&300>f.status?b(e,g):c(e)};f.send()}else c("error: href must be specified")}},y=/Trident/.test(navigator.userAgent),v=/Edge\/\d./i.test(navigator.userAgent);k.prototype.loadImports=function(a){var b=this;a=m(a,"link[rel=import]");h(a,function(c){return b.u(c)})};k.prototype.u=function(a){var b=this,c=a.href;if(void 0!==this.g[c]){var d=this.g[c];d&&d.__loaded&&(a.__import=d,this.s(a))}else this.h++,this.g[c]="pending",F.load(c,
function(f,e){f=b.C(f,e||c);b.g[c]=f;b.h--;b.loadImports(f);b.v()},function(){b.g[c]=null;b.h--;b.v()})};k.prototype.C=function(a,b){var c=this;if(!a)return document.createDocumentFragment();if(y||v)a=a.replace(K,function(e,g,w){return-1===e.indexOf("type=")?g+" type=import-disable "+w:e});var d=document.createElement("template");d.innerHTML=a;if(d.content)a=d.content,E(a);else for(a=document.createDocumentFragment();d.firstChild;)a.appendChild(d.firstChild);if(d=a.querySelector("base"))b=l.i(d.getAttribute("href"),
b),d.removeAttribute("href");d=m(a,'link[rel=import],link[rel=stylesheet][href][type=import-disable],style:not([type]),link[rel=stylesheet][href]:not([type]),script:not([type]),script[type="application/javascript"],script[type="text/javascript"],script[type="module"]');var f=0;h(d,function(e){q(e);l.H(e,b);if("style"===e.localName&&c.A(e)){var g=c.I(e);q(g);e.parentNode.replaceChild(g,e);e=g}e.setAttribute("import-dependency","");if("script"===e.localName&&!e.src&&e.textContent){if("module"===e.type)throw Error("Inline module scripts are not supported in HTML Imports.");
e.setAttribute("src","data:text/javascript;charset=utf-8,"+encodeURIComponent(e.textContent+("\n//# sourceURL="+b+(f?"-"+f:"")+".js\n")));e.textContent="";f++}});return a};k.prototype.A=function(a){return v&&-1<a.textContent.indexOf("@import")};k.prototype.I=function(a){var b=a.ownerDocument.createElement("style");b.textContent=a.textContent;h(a.attributes,function(c){return b.setAttribute(c.name,c.value)});return b};k.prototype.v=function(){var a=this;if(!this.h){this.l.disconnect();this.flatten(document);
var b=!1,c=!1,d=function(){c&&b&&(a.loadImports(document),a.h||(a.l.observe(document.head,{childList:!0,subtree:!0}),a.J()))};this.F(function(){c=!0;d()});this.D(function(){b=!0;d()})}};k.prototype.flatten=function(a){var b=this;a=m(a,"link[rel=import]");h(a,function(c){var d=b.g[c.href];(c.__import=d)&&d.nodeType===Node.DOCUMENT_FRAGMENT_NODE&&(b.g[c.href]=c,c.readyState="loading",c.__import=c,b.flatten(d),c.appendChild(d))})};k.prototype.D=function(a){function b(f){if(f<d){var e=c[f],g=document.createElement("script");
e.removeAttribute("import-dependency");h(e.attributes,function(w){return g.setAttribute(w.name,w.value)});z=g;e.parentNode.replaceChild(g,e);q(g,function(){z=null;b(f+1)})}else a()}var c=m(document,"script[import-dependency]"),d=c.length;b(0)};k.prototype.F=function(a){var b=this,c=m(document,"style[import-dependency],link[rel=stylesheet][import-dependency]"),d=c.length;if(d){var f=(y||v)&&!!document.querySelector("link[rel=stylesheet][href][type=import-disable]");h(c,function(e){f&&b.A(e)&&e.ownerDocument.defaultView!==
window.top&&(e.__loaded=!0);q(e,function(){e.removeAttribute("import-dependency");0===--d&&a()});if(f&&e.parentNode!==document.head){var g=document.createElement(e.localName);g.__appliedElement=e;g.setAttribute("type","import-placeholder");e.parentNode.insertBefore(g,e.nextSibling);for(g=p(e);g&&p(g);)g=p(g);g.parentNode!==document.head&&(g=null);document.head.insertBefore(e,g);e.removeAttribute("type")}})}else a()};k.prototype.J=function(){var a=this,b=m(document,"link[rel=import]");h(b,function(c){return a.s(c)},
!0)};k.prototype.s=function(a){a.__loaded||(a.__loaded=!0,a.import&&(a.import.readyState="complete"),a.dispatchEvent(B(a.import?"load":"error",{bubbles:!1,cancelable:!1,detail:void 0})))};k.prototype.K=function(a){var b=this;h(a,function(c){return h(c.addedNodes,function(d){d&&d.nodeType===Node.ELEMENT_NODE&&(u(d)?b.u(d):b.loadImports(d))})})};var A=null;if(x)n=m(document,"link[rel=import]"),h(n,function(a){a.import&&"loading"===a.import.readyState||(a.__loaded=!0)}),n=function(a){a=a.target;u(a)&&
(a.__loaded=!0)},document.addEventListener("load",n,!0),document.addEventListener("error",n,!0);else{var r=Object.getOwnPropertyDescriptor(Node.prototype,"baseURI");Object.defineProperty((!r||r.configurable?Node:Element).prototype,"baseURI",{get:function(){var a=u(this)?this:p(this);return a?a.href:r&&r.get?r.get.call(this):(document.querySelector("base")||window.location).href},configurable:!0,enumerable:!0});Object.defineProperty(HTMLLinkElement.prototype,"import",{get:function(){return this.__import||
null},configurable:!0,enumerable:!0});C(function(){A=new k})}D(function(){return document.dispatchEvent(B("HTMLImportsLoaded",{cancelable:!0,bubbles:!0,detail:void 0}))});t.useNative=x;t.whenReady=D;t.importForElement=p;t.loadImports=function(a){A&&A.loadImports(a)}})(window.HTMLImports=window.HTMLImports||{});}).call(this);
//# sourceMappingURL=html-imports.min.js.map