Liferay.Loader.define("frontend-js-spa-web@5.0.23/screen/HtmlScreen",["module","exports","require","frontend-js-web","../surface/Surface","../util/utils","./RequestScreen"],(function(e,t,r){window;{Object.defineProperty(t,"__esModule",{value:!0}),t.default=void 0;var n=r("frontend-js-web"),s=l(r("../surface/Surface")),a=r("../util/utils"),o=l(r("./RequestScreen"));function l(e){return e&&e.__esModule?e:{default:e}}class e extends o.default{constructor(){super(),this.metaTagsSelector="meta",this.titleSelector="title"}activate(){super.activate(),this.releaseVirtualDocument(),this.pendingStyles=null}allocateVirtualDocumentForContent(e){this.virtualDocument||(this.virtualDocument=document.createElement("html")),this.copyNodeAttributesFromContent_(e,this.virtualDocument),this.virtualDocument.innerHTML=e}appendStyleIntoDocument_(t){if(t.matches(e.selectors.stylesTemporary)&&this.pendingStyles.push(t),t.id){var r=document.getElementById(t.id);if(r)return void r.parentNode.insertBefore(t,r.nextSibling)}document.head.appendChild(t)}assertSameBodyIdInVirtualDocument(){var e=this.virtualDocument.querySelector("body");document.body.id||(document.body.id="senna_surface_"+(0,a.getUid)()),e&&(e.id=document.body.id)}copyNodeAttributesFromContent_(e,t){e=(e=e.replace(/[<]\s*html/gi,"<senna")).replace(/\/html\s*>/gi,"/senna>"),t.innerHTML=e;const r=t.querySelector("senna");r&&((0,a.clearNodeAttributes)(t),(0,a.copyNodeAttributes)(r,t))}disposeInternal(){this.disposePendingStyles(),super.disposeInternal()}disposePendingStyles(){this.pendingStyles&&this.pendingStyles.forEach((e=>e.remove()))}evaluateScripts(t){return this.evaluateTrackedResources_(n.runScriptsInElement,e.selectors.scripts,e.selectors.scriptsTemporary,e.selectors.scriptsPermanent).then((()=>super.evaluateScripts(t)))}evaluateStyles(t){return this.pendingStyles=[],this.evaluateTrackedResources_(a.runStylesInElement,e.selectors.styles,e.selectors.stylesTemporary,e.selectors.stylesPermanent,this.appendStyleIntoDocument_.bind(this)).then((()=>super.evaluateStyles(t)))}evaluateFavicon_(){const t=this.virtualQuerySelectorAll_(e.selectors.favicon),r=this.querySelectorAll_(e.selectors.favicon);return new Promise((e=>{r.forEach((e=>e.remove())),this.runFaviconInElement_(t).then((()=>e()))}))}evaluateTrackedResources_(t,r,s,a,o){var l=this.virtualQuerySelectorAll_(r),c=this.querySelectorAll_(s);this.querySelectorAll_(a).forEach((t=>{var r=this.getResourceKey_(t);r&&(e.permanentResourcesInDoc[r]=!0)}));var i=(0,n.buildFragment)();return l.forEach((t=>{var r=this.getResourceKey_(t);e.permanentResourcesInDoc[r]||i.appendChild(t),r&&t.matches(a)&&(e.permanentResourcesInDoc[r]=!0)})),new Promise((e=>{t(i,(()=>{c.forEach((e=>e.remove())),e()}),o)}))}flip(e){return super.flip(e).then((()=>{(0,a.clearNodeAttributes)(document.documentElement),(0,a.copyNodeAttributes)(this.virtualDocument,document.documentElement),this.evaluateFavicon_(),this.updateMetaTags_()}))}updateMetaTags_(){const e=this.querySelectorAll_("meta"),t=this.metas;e&&(e.forEach((e=>e.remove())),t&&t.forEach((e=>document.head.appendChild(e))))}getResourceKey_(e){return e.id||e.href||e.src||""}getSurfaceContent(e){var t=this.virtualDocument.querySelector("#"+e);if(t){var r=t.querySelector("#"+e+"-"+s.default.DEFAULT);return r?r.innerHTML:t.innerHTML}}getTitleSelector(){return this.titleSelector}load(e){return super.load(e).then((e=>(this.allocateVirtualDocumentForContent(e),this.resolveTitleFromVirtualDocument(),this.resolveMetaTagsFromVirtualDocument(),this.assertSameBodyIdInVirtualDocument(),e)))}runFaviconInElement_(e){return new Promise((t=>{e.forEach((e=>{e.href=e.href+"?q="+Math.random(),document.head.appendChild(e)})),t()}))}virtualQuerySelectorAll_(e){return Array.prototype.slice.call(this.virtualDocument.querySelectorAll(e))}querySelectorAll_(e){return Array.prototype.slice.call(document.querySelectorAll(e))}releaseVirtualDocument(){this.virtualDocument=null}resolveTitleFromVirtualDocument(){const e=this.virtualDocument.querySelector(this.titleSelector);e&&this.setTitle(e.textContent.trim())}resolveMetaTagsFromVirtualDocument(){const e=this.virtualQuerySelectorAll_(this.metaTagsSelector);e&&this.setMetas(e)}setTitleSelector(e){this.titleSelector=e}}const i=':not([rel="Shortcut Icon"]):not([rel="shortcut icon"]):not([rel="icon"]):not([href$="favicon.icon"])';e.selectors={favicon:'link[rel="Shortcut Icon"],link[rel="shortcut icon"],link[rel="icon"],link[href$="favicon.icon"]',scripts:"script[data-senna-track]",scriptsPermanent:'script[data-senna-track="permanent"]',scriptsTemporary:'script[data-senna-track="temporary"]',styles:"style[data-senna-track],link[data-senna-track]".concat(i),stylesPermanent:'style[data-senna-track="permanent"],link[data-senna-track="permanent"]'.concat(i),stylesTemporary:'style[data-senna-track="temporary"],link[data-senna-track="temporary"]'.concat(i)},e.permanentResourcesInDoc={};var c=e;t.default=c}}));
//# sourceMappingURL=HtmlScreen.js.map