@blueking/bk-weweb 0.0.31 → 0.0.32-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
var _e=null;function F(){return _e}function T(r){_e=r}var N=new Map,Fe=`const { ${["Array","ArrayBuffer","Boolean","constructor","DataView","Date","decodeURI","decodeURIComponent","encodeURI","encodeURIComponent","Error","escape","eval","EvalError","Float32Array","Float64Array","Function","hasOwnProperty","Infinity","Int16Array","Int32Array","Int8Array","isFinite","isNaN","isPrototypeOf","JSON","Map","Math","NaN","Number","Object","parseFloat","parseInt","Promise","propertyIsEnumerable","Proxy","RangeError","ReferenceError","Reflect","RegExp","Set","String","Symbol","SyntaxError","toLocaleString","toString","TypeError","Uint16Array","Uint32Array","Uint8Array","Uint8ClampedArray","undefined","unescape","URIError","valueOf","WeakMap","WeakSet"].join(",")} }= this;`,Q=()=>Fe,je=()=>{let r=Object.getOwnPropertyNames(window);for(let e of r)!N.has(e)&&e.match(/^[A-Z]/)&&typeof window[e]=="function"&&window[e].toString().includes("[native code]")&&N.set(e,!0)};je();var I="id";var K=(o=>(o.APP="app",o.CONFIG="config",o.INSTANCE="js",o))(K||{});var Ce=["System","__cjsWrapper",""],j=["__POWERED_BY_BK_WEWEB__","__BK_WEWEB_APP_KEY__","__BK_WEWEB_DATA__"],Me=["window","self","globalThis"],ee=["location","history"],Ye={__bk_pop_manager:!0,__bk_zIndex_manager:!0,i18n:!0},Te=Ye;var D=r=>Promise.resolve().then(r);function x(r){return r.startsWith("//")?`${location.protocol}${r}`:r.startsWith("http")?r:`${location.protocol}//${r}`}function h(r,e){if(!r||/^((((ht|f)tps?)|file):)?\/\//.test(r)||/^(data|blob):/.test(r))return r;let{origin:t,pathname:o}=new URL(x(e));return new URL(r,`${t}${o}`.replace(/\.(\w+)$/,"/")).toString()}function L(){return`inline-${W(16)}`}var Y=window.requestIdleCallback||(r=>{let e=Date.now();return setTimeout(()=>{r({didTimeout:!1,timeRemaining(){return Math.max(0,50-(Date.now()-e))}})},1)}),St=window.cancelIdleCallback||(r=>{clearTimeout(r)}),W=(r,e="abcdefghijklmnopqrstuvwxyz0123456789")=>{let t="";for(let o=0;o<r;o++)t+=e[Number.parseInt((Math.random()*e.length).toString(),10)];return t},J=r=>{if(!r||r.match(/\.js$/))return!1;let{pathname:e}=new URL(x(r));return!e.match(/\.js$/)};var te=["body","html","head"],oe=(r,e)=>{let t={};function o(d,f){let m=document.createElement("div");m.innerHTML=f;let b=Array.from(m.childNodes),B=e.container;switch(d){case"beforebegin":b.forEach(P=>B.host.parentNode?.insertBefore(P,B.host));break;case"afterbegin":b.reverse().forEach(P=>B.insertBefore(P,B.firstChild));break;case"beforeend":b.forEach(P=>B.appendChild(P));break;case"afterend":b.forEach(P=>B.host.parentNode?.insertBefore(P,B.host.nextSibling));break}}let n=new Proxy({},{get(d,f){if(e.container instanceof ShadowRoot){if(f==="insertAdjacentHTML")return o.bind(e.container);let b=e.container[f];if(typeof b=="function")return b.bind(e.container);if(b!==void 0)return b}let m=Reflect.get(r.body,f);return typeof m=="function"?m.bind(r.body):m},set(d,f,m){return e.container instanceof ShadowRoot?(e.container[f]=m,!0):(Reflect.set(r.body,f,m),!0)}});function i(d,f){let m=r.createElement(d,f);return m.__BK_WEWEB_APP_KEY__=e.appCacheKey,m}function c(d){if(d===n)return e.container instanceof ShadowRoot?e.container:r.body;if(te.includes(d))return e?.container instanceof ShadowRoot?e?.container:r.querySelector.call(this,d);try{return e?.container?.querySelector(d)??null}catch{return null}}function s(d){return te.includes(d)?e?.container instanceof ShadowRoot?e?.container:r.querySelector(d):e?.container?.querySelectorAll(d)??[]}function u(d){return c.call(r,`#${d}`)}function l(d){return s(`.${d}`)}function p(d){return te.includes(d)||!e?.showSourceCode&&d.toLocaleLowerCase()==="script"?r.getElementsByTagName(d):s(d)}function S(d){return s(`[name=${d}]`)}return new Proxy(t,{get(d,f){if(f==="createElement")return i.bind(r);if(f==="querySelector")return c.bind(r);if(f==="querySelectorAll")return s.bind(r);if(f==="getElementById")return u.bind(r);if(f==="getElementsByClassName")return l.bind(r);if(f==="getElementsByTagName")return p.bind(r);if(f==="getElementsByName")return S.bind(r);if(f==="body")return n;let m=Reflect.get(r,f);return typeof m=="function"?m.bind(r):m}})};function xe(){let{addEventListener:r,removeEventListener:e}=window.document,{addEventListener:t,removeEventListener:o}=window.document.body,n=new Map;document.addEventListener=function(c,s,u){let l=F();if(l?.keepAlive){let p=n.get(c)||[];n.set(c,[...p,s])}r.call(l?.container instanceof ShadowRoot?l.container:this,c,s,u)},document.body.addEventListener=document.addEventListener,document.removeEventListener=function(c,s,u){let l=F();if(l?.keepAlive){let p=n.get(c)||[];p.length&&p.some(S=>S===s)&&p.splice(p.indexOf(s),1)}e.call(l?.container instanceof ShadowRoot?l.container:this,c,s,u)},document.body.removeEventListener=document.removeEventListener;function i(){F()?.keepAlive&&n.values()&&Array.from(n.entries()).forEach(([s,u])=>{u?.forEach(l=>{document.removeEventListener.call(document,s,l)})}),document.addEventListener=r,document.body.addEventListener=t,document.removeEventListener=e,document.body.removeEventListener=o,n.clear()}return{resetDocumentAndBodyEvent:i}}var Je=/^class\b/,Ve=/^function\b\s[A-Z].*/,re=new WeakMap;function Ge(r){if(r.prototype?.constructor===r&&Object.getOwnPropertyNames(r.prototype).length>1)return!0;if(re.has(r))return re.get(r);let e=Ve.test(r.toString())||Je.test(r.toString());return re.set(r,e),e}var ne=new WeakMap;function ie(r,e){if(ne.has(e))return ne.get(e);if(typeof e=="function"&&!Ge(e)){let t=Function.prototype.bind.call(e,r);for(let o in e)t[o]=e[o];if(e.hasOwnProperty("prototype")&&!t.hasOwnProperty("prototype")&&Object.defineProperty(t,"prototype",{enumerable:!1,value:e.prototype,writable:!0}),typeof e.toString=="function"){let o=e.hasOwnProperty("toString")&&!t.hasOwnProperty("toString"),n=t.toString===Function.prototype.toString;if(o||n){let i=Object.getOwnPropertyDescriptor(o?e:Function.prototype,"toString");Object.defineProperty(t,"toString",{...i,...i?.get?null:{value:()=>e.toString()}})}}return ne.set(e,t),t}return e}function Le(r){let e=new Map,t=[],o=window,{addEventListener:n,clearInterval:i,removeEventListener:c,setInterval:s}=window;r.addEventListener=(l,p,S)=>{e.set(l,[...e.get(l)||[],p]),n.call(o,l,p,S)},r.removeEventListener=(l,p,S)=>{let d=e.get(l);if(d?.length){let f=d.indexOf(p);f>-1&&d.splice(f,1)}c.call(o,l,p,S)},r.setInterval=(l,p,...S)=>{let d=s.call(o,l,p,...S);return t.push(d),d},r.clearInterval=l=>{let p=t.indexOf(l);p>-1&&t.splice(p,1),i.call(o,l)};function u(){e.size&&(e.forEach((l,p)=>{l.forEach(S=>c.call(o,p,S))}),e.clear()),t.length&&t.forEach(l=>{i.call(o,l)})}return{resetWindowFunction:u}}var R=class{constructor(e){this.app=e;let t=new Set,o=window;this.rawWindow=o,this.rawDocument=oe(document,e);let n=Object.create({});n.__BK_WEWEB_APP_KEY__=e.appCacheKey,n.__POWERED_BY_BK_WEWEB__=!0,n.rawDocument=document,n.rawWindow=o,n.__proto__=Window,this.fakeWindow=n;let{resetWindowFunction:i}=Le(this.fakeWindow);this.resetWindowFunction=i,this.windowSymbolKey=`__${(e.name||e.appCacheKey).replace(/(-|,|:|~|'|")/gim,"_")}_${W(10)}__`,this.proxyWindow=new Proxy(this.fakeWindow,{defineProperty:(c,s,u)=>t.has(s)?Reflect.defineProperty(o,s,u):Reflect.defineProperty(c,s,u),deleteProperty:(c,s)=>Object.hasOwn(c,s)?(this.sameRawWindowKeySet.has(s)&&this.sameRawWindowKeySet.delete(s),this.inRawWindowKeySet.has(s)&&Reflect.deleteProperty(o,s),Reflect.deleteProperty(c,s)):!0,get:(c,s)=>{if(s===Symbol.unscopables||N.has(s))return o[s];if(Te[s])return this.fakeWindow[s];if(Me.includes(s))return this.proxyWindow;if(s==="document")return e.registerRunningApp(),this.rawDocument;if(s==="eval")return e.registerRunningApp(),eval;if(ee.includes(s)&&this.app instanceof A&&this.app.iframe&&this.app.scopeLocation)return this.app.iframe.contentWindow?.[s];if(s==="hasOwnProperty")return l=>Object.hasOwn(this.fakeWindow,l)||Object.hasOwn(o,l);if(s==="top"||s==="parent")return o===o.parent?this.proxyWindow:Reflect.get(o,s);if(s==="getComputedStyle")return(l,p)=>l instanceof Element?o.getComputedStyle(l,p):o.getComputedStyle(document.body,p);if(Reflect.has(c,s)||j.includes(s))return Reflect.get(c,s);let u=Reflect.get(o,s);return ie(o,u)},getOwnPropertyDescriptor:(c,s)=>{if(Object.hasOwn(c,s))return Object.getOwnPropertyDescriptor(c,s);if(Object.hasOwn(o,s)){t.add(s);let u=Object.getOwnPropertyDescriptor(o,s);return u&&!u.configurable&&(u.configurable=!0),u}},has:(c,s)=>N.has(s)||s in c||s in o,ownKeys:c=>Array.from(new Set(Reflect.ownKeys(o).concat(Reflect.ownKeys(c)))),set:(c,s,u)=>{if(this.active){if(ee.includes(s)&&this.app instanceof A&&this.app.iframe&&this.app.scopeLocation)return Reflect.set(this.app.iframe.contentWindow,s,u);if(s==="location")Reflect.set(o,s,u);else if(!Object.hasOwn(c,s)&&Object.hasOwn(o,s)&&!j.includes(s)){let l=Object.getOwnPropertyDescriptor(o,s),{configurable:p,enumerable:S,writable:d}=l;d&&(Object.defineProperty(c,s,{configurable:p,enumerable:S,value:u,writable:d}),this.sameRawWindowKeySet.add(s))}else Reflect.set(c,s,u),this.sameRawWindowKeySet.add(s);Ce.includes(s)&&!Reflect.has(o,s)&&!j.includes(s)&&(Reflect.set(o,s,u),this.inRawWindowKeySet.add(s))}return!0}}),o[this.windowSymbolKey]=this.proxyWindow}active=!1;inRawWindowKeySet=new Set;resetDocumentAndBodyEvent;resetWindowFunction;sameRawWindowKeySet=new Set;fakeWindow;proxyDocument;proxyWindow;rawDocument;rawWindow;windowSymbolKey;activated(e){if(!this.active){this.active=!0,this.rawDocument=oe(document,this.app),this.fakeWindow.__BK_WEWEB_DATA__=e??{};let{resetDocumentAndBodyEvent:t}=xe();this.resetDocumentAndBodyEvent=t}}deactivated(){if(this.active){this.active=!1,this.resetWindowFunction();for(let e of this.inRawWindowKeySet)Reflect.deleteProperty(window,e);this.inRawWindowKeySet.clear(),this.resetDocumentAndBodyEvent?.()}}};var qe=["currentTarget","srcElement","target"];function ve(r,e="custom"){return Object.defineProperties(new CustomEvent(e),qe.reduce((t,o)=>(t[o]={get(){return r}},t),{}))}function _(r){let e=ve(r,"load");if(typeof r.onload=="function"){r.onload(e);return}r.dispatchEvent(e)}function U(r){let e=ve(r,"error");if(typeof r.onerror=="function"){r.onerror(e);return}r.dispatchEvent(e)}var{appendChild:Be}=HTMLBodyElement.prototype;function Pe(r,e,t){if(e instanceof HTMLStyleElement){if(e.hasAttribute("exclude"))return document.createComment("\u3010bk-weweb\u3011style with exclude attribute is ignored");if(e.textContent&&!(t.container instanceof ShadowRoot)&&a.getBaseAppStyle(e.textContent))return document.createComment("\u3010bk-weweb\u3011style is effective in base app");if(!e.hasAttribute("ignore")){let o=new w({code:e.textContent||"",fromHtml:!1,url:""});t.source?.setStyle(L(),o),o.scopedStyleCSS(t,e)}return e}if(e instanceof HTMLLinkElement){let o=t.source?.collectLink(e,r,!0);return o?(o.style&&o.style.scopedLinkCSS(t,e),o.replace!==e?o.replace:e):e}if(e instanceof HTMLScriptElement){let o=t.source.collectScript(e,r,!0);if(!o)return e;if(o.script&&o.script.executeCode(t),o.replace!==e)return o.replace;if(t.scopeJs&&!e.getAttribute("src")&&!e.textContent){let n=new MutationObserver(()=>{if(e.getAttribute("src")){n.disconnect();let i=t.source.collectScript(e,r,!0);if(i?.replace&&Be.call(t.container,i.replace),J(e.getAttribute("src"))){t.container?.append(e);return}i?.script&&i.script.executeCode(t),e.remove()}else if(e.textContent){n.disconnect();let i=new y({async:!1,code:e.textContent,defer:e.type==="module",fromHtml:!1,isModule:e.type==="module"});t.source.scripts.set(L(),i);try{i.executeCode(t)}catch(c){console.error(c)}finally{!i.isModule&&_(e),e.remove()}}});return n.observe(e,{attributeFilter:["src"],childList:!0,subtree:!1}),document.createComment("\u3010bk-weweb\u3011dynamic script or module")}return e}return e}function k(r){return r instanceof HTMLScriptElement||r instanceof HTMLStyleElement||r instanceof HTMLLinkElement}function se(r,e,t){if(e.__BK_WEWEB_APP_KEY__){let o=a.getApp(e.__BK_WEWEB_APP_KEY__);if(o?.container){let n=Pe(r,e,o),i=k(e)&&!!o.keepAlive&&!(o.container instanceof ShadowRoot),c=i?document.head:o?.container;return E(n,o,i),t.call(c,n)}}return t.call(r,e)}function ce(r,e,t,o){if(e.__BK_WEWEB_APP_KEY__){let n=a.getApp(e.__BK_WEWEB_APP_KEY__);if(n?.container){let i=k(e)&&n.keepAlive&&!(n.container instanceof ShadowRoot),c=i?document.head:n?.container,s=Pe(r,e,n);return i&&E(s,n,i),t&&!c.contains(t)?Be.call(c,s):o.call(c,s,t)}}return o.call(r,e,t)}function E(r,e,t){return t&&e&&(r.__KEEP_ALIVE__=e.appCacheKey,r.setAttribute("data-from",e.name),r.setAttribute("data-keep-alive","true")),r.setAttribute?.("powered-by","bk-weweb"),r}function g(r,e={},t){return typeof t?.fetchSource=="function"?t.fetchSource(r,e).catch(()=>""):ae.fetchSource?ae.fetchSource(r,e):window.fetch(r,e).then(o=>o.text())}var w=class{code="";fromHtml;initial;prefetch=!1;preload=!1;scoped;scopedCode="";url;constructor({code:e,fromHtml:t,initial:o,prefetch:n,preload:i,url:c}){this.scoped=!1,this.code=e,this.prefetch=n??!1,this.preload=i??!1,this.url=c,this.fromHtml=t,this.initial=o??!1}commonScoped(e,t){if(t.scopeCss&&!(t.container instanceof ShadowRoot)){let o=new CSSStyleSheet({disabled:!0});o.replaceSync(e.textContent||this.code);let n=Array.from(o?.cssRules??[]),i=`#${t.name}`,c=this.scopeRule(n,i),s=this.resetUrlHost(c,t.url,this.url);e.textContent=s,this.scopedCode=s}else{let o=this.resetUrlHost(e.textContent||this.code||"",t.url,this.url);if(o&&t.container instanceof ShadowRoot){let n="";o.match(/@font-face\s*\{[^}]+\}/g)?.forEach(c=>{n+=`${c}
|
|
2
|
-
`});let i=t.sandBox?.rawDocument;if(i&&n){let c=i.createElement("style");c.setAttribute("type","text/css"),c.setAttribute("powered-by","bk-weweb"),c.textContent=n,i?.head?.append(c)}}e.textContent=o}this.scoped=!0}createStyleElement(){let e=document.createElement("style");return e.__BK_WEWEB_APP_KEY__&&(e.__BK_WEWEB_APP_KEY__=void 0),e}async executeCode(e){e.registerRunningApp();let t=this.createStyleElement();t.setAttribute("type","text/css"),t.textContent=this.code;try{this.code||await this.getCode(e),t=this.scopedStyleCSS(e,t),this.scoped=!0}catch(o){console.error("scoped style error",o)}return t}async getCode(e){if(this.code.length||!this.url)return this.code;let t="";return e?.source?.styles?.has(this.url)&&(t=e.source.styles.get(this.url)?.code||""),!t&&a.getCacheStyle(this.url)&&(t=a.getCacheStyle(this.url)?.code||""),t||(t=await g(this.url,{},e).catch(()=>"")),this.code=t,t}linkedBaseStyle(e,t){return!(t.container instanceof ShadowRoot)&&e.textContent&&a.getBaseAppStyle(e.textContent)?(e.textContent="",e.innerHTML="",e.setAttribute("linked-from-base","true"),!0):!1}resetPackRule(e,t,o){let n=this.scopeRule(Array.from(e.cssRules),t);return`@${o} ${e.conditionText} {${n}}`}resetUrlHost(e,t,o){let n=t;return e.replace(/url\(["']?([^)"']+)["']?\)/gm,(i,c)=>{if(/^(data|blob):/.test(c)||/^(https?:)?\/\//.test(c))return i;if(/^((\.\.?\/)|[^/])/.test(c)&&o){let s=o.split("/");s.pop(),n=x(`${s.join("/")}/`)}return`url("${h(c,n)}")`})}scopeRule(e,t){let o="";for(let n of e)switch(n.type){case 1:o+=this.scopeStyleRule(n,t);break;case 4:o+=this.resetPackRule(n,t,"media");break;case 12:o+=this.resetPackRule(n,t,"supports");break;default:o+=n.cssText;break}return o.replace(/^\s+/,"")}scopeStyleRule(e,t){let{cssText:o,selectorText:n}=e;if(/^((html[\s>~,]+body)|(html|body|:root))$/.test(n))return o.replace(/^((html[\s>~,]+body)|(html|body|:root))/,t);if(n==="*")return o.replace("*",`${t} *`);let i=/(^|\s+)((html[\s>~]+body)|(html|body|:root))(?=[\s>~]+|$)/;return o.replace(/^[\s\S]+{/,c=>c.replace(/(^|,)([^,]+)/g,(s,u,l)=>i.test(l)?s.replace(i,t):`${u} ${t} ${l.replace(/^\s*/,"")}`))}scopedLinkCSS(e,t){let o=this.createStyleElement();o.setAttribute("type","text/css");let n=!!e.keepAlive&&!(e.container instanceof ShadowRoot);E(o,e,n);let i=n?document.head:e.container;try{if(this.code)this.commonScoped(o,e),i?.prepend(o),t&&_(t);else if(t.getAttribute("href"))this.url=h(t.getAttribute("href"),e.url),this.getCode(e).then(()=>{this.scopedStyleCSS(e,o),t.remove(),i?.prepend(o),t&&_(t),this.scoped=!0});else{let c=new MutationObserver(()=>{t.href&&(c.disconnect(),this.url=h(t.getAttribute("href"),e.url),this.getCode(e).then(()=>{this.scopedStyleCSS(e,o),t.remove(),i?.prepend(o),t&&_(t),this.scoped=!0}))});c.observe(t,{attributeFilter:["href"],childList:!1,subtree:!1})}}catch{t&&U(t)}return o}scopedStyleCSS(e,t){let o=!!e.keepAlive&&!(e.container instanceof ShadowRoot);if(E(t,e,o),this.code||t.textContent){if(t.textContent&&(t.textContent="",t.innerHTML=""),this.linkedBaseStyle(t,e))return t;this.commonScoped(t,e)}else{let n=new MutationObserver(()=>{(t.textContent||t.sheet?.cssRules?.length)&&(n.disconnect(),this.linkedBaseStyle(t,e)||this.commonScoped(t,e))});n.observe(t,{attributes:!1,characterData:!0,childList:!0,subtree:!0})}return this.url&&t.setAttribute("origin-src",this.url),t}};async function V(r,e){let t=Array.from(r.source.styles.values()),o=[];for(let n of t)o.push(n.executeCode(r));await Promise.all(o).then(n=>{let i=e||r.container;r.keepAlive&&!(i instanceof ShadowRoot)?document.head.append(...n):i?.append(...n)})}var C=class{state="UNSET";appCacheKey;container;data;initSource;isPreLoad=!1;keepAlive;name;sandBox;scopeCss=!0;scopeJs=!1;showSourceCode=!0;source;url;fetchSource;constructor(e){this.name=e.id!==e.url?e.id:W(5),this.appCacheKey=e.id||this.name,this.url=e.url,this.container=e.container??void 0,this.scopeJs=e.scopeJs??!0,this.showSourceCode=e.showSourceCode??!0,this.scopeCss=e.scopeCss??!0,this.keepAlive=e.keepAlive??!1,this.data=e.data??{},this.initSource=e.initSource??[],this.scopeJs&&(this.sandBox=new R(this)),this.fetchSource=e.fetchSource}activated(e,t){if(this.isPreLoad=!1,this.state="ACTIVATED",this.container&&e){e instanceof Element&&e?.setAttribute(I,this.name);let o=document.createDocumentFragment();for(let i of Array.from(this.container.childNodes))o.appendChild(i);e.appendChild(o),this.container=e,this.sandBox?.activated();let n=this.source?.getScript(this.url);t?.(this,n?.exportInstance)}}deactivated(){this.state="DEACTIVATED",this.sandBox?.deactivated()}mount(e,t){this.isPreLoad=!1,this.container=e??this.container,this.state="MOUNTING",this.container instanceof HTMLElement&&this.container?.setAttribute(I,this.name),this.container.innerHTML="";let o=document.createElement("div"),n=`${this.name}-wrapper`;o.setAttribute("id",n),this.source?.styles.size&&V(this,this.container),this.container.appendChild(o),this.sandBox?.activated(),G(this).finally(()=>{this.state="MOUNTED";let i=this.source?.getScript(this.url);typeof i?.exportInstance?.render=="function"&&i.exportInstance.render(o,this.data),t?.(this,i?.exportInstance)})}onError(){this.state="ERROR"}onMount(){this.isPreLoad||(this.state="LOADED",this.mount())}registerRunningApp(){T(this),Promise.resolve().then(()=>T(null))}async start(){(!this.source||["ERROR","UNSET"].includes(this.status))&&(this.source=new v(this.url),await this.source.importEntry(this))}unmount(e){this.state="UNMOUNT",this.sandBox?.deactivated(),e&&a.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}set status(e){this.state=e}get status(){return this.state}};var q,$,z,y=class{async=!1;code="";defer=!1;exportInstance;fromHtml;initial;isModule=!1;scoped;url;constructor({async:e,code:t,defer:o,fromHtml:n,initial:i,isModule:c,url:s}){this.code=t,this.async=e,this.defer=o,this.isModule=c,this.url=s,this.scoped=!1,this.fromHtml=n??!1,this.initial=i??!1}async executeCode(e,t=!1){try{if(this.code||await this.getCode(e),e instanceof C){let n=e.scopeJs&&e.sandBox?.proxyWindow||window;Ze(n)}let o=this.code;if(o=this.transformCode(e),e.showSourceCode||this.isModule){let n=document.createElement("script");if(n.__BK_WEWEB_APP_KEY__&&(n.__BK_WEWEB_APP_KEY__=void 0),e.registerRunningApp(),this.executeSourceScript(n,o),t)return n;let i=!!e.keepAlive&&!(e.container instanceof ShadowRoot),c=i?document.head:e.container;E(n,e,i),c.appendChild(n)}else if(this.executeMemoryScript(e,o),t)return document.createComment("\u3010bk-weweb\u3011dynamic script");if(e instanceof C){let n=e.scopeJs&&e.sandBox?.proxyWindow||window,i=ze(n);i&&(this.exportInstance=n[i],e.scopeJs||delete n[i])}}catch(o){console.error("execute script code error",o)}}executeMemoryScript(e,t){try{let o=e instanceof A&&e.scopeLocation;e.registerRunningApp(),new Function("window","location","history",t)(e.sandBox.proxyWindow,o?e.iframe.contentWindow.location:window.location,o?e.iframe.contentWindow.history:window.history)}catch(o){console.error(o)}}executeSourceScript(e,t){this.isModule?(e.src=`${this.url}?key=${Date.now()}`,e.setAttribute("type","module")):e.textContent=t,this.url&&e.setAttribute("origin-src",this.url)}async getCode(e){if(this.code.length||!this.url)return this.code;let t="";return e?.source?.getScript(this.url)&&(t=e.source.getScript(this.url)?.code||""),!t&&a.getCacheScript(this.url)&&(t=a.getCacheScript(this.url)?.code||""),t||(t=await g(this.url,{},e).catch(o=>(console.error(`fetch script ${this.url} error`,o),""))),t=t.replace(/^"use\sstrict";$/gim,""),this.code=t,t}setCode(e){this.code=e}transformCode(e){let t=this.
|
|
3
|
-
`:"";return e.sandBox?this.isModule?` with(window.${e.sandBox.windowSymbolKey}){
|
|
2
|
+
`});let i=t.sandBox?.rawDocument;if(i&&n){let c=i.createElement("style");c.setAttribute("type","text/css"),c.setAttribute("powered-by","bk-weweb"),c.textContent=n,i?.head?.append(c)}}e.textContent=o}this.scoped=!0}createStyleElement(){let e=document.createElement("style");return e.__BK_WEWEB_APP_KEY__&&(e.__BK_WEWEB_APP_KEY__=void 0),e}async executeCode(e){e.registerRunningApp();let t=this.createStyleElement();t.setAttribute("type","text/css"),t.textContent=this.code;try{this.code||await this.getCode(e),t=this.scopedStyleCSS(e,t),this.scoped=!0}catch(o){console.error("scoped style error",o)}return t}async getCode(e){if(this.code.length||!this.url)return this.code;let t="";return e?.source?.styles?.has(this.url)&&(t=e.source.styles.get(this.url)?.code||""),!t&&a.getCacheStyle(this.url)&&(t=a.getCacheStyle(this.url)?.code||""),t||(t=await g(this.url,{},e).catch(()=>"")),this.code=t,t}linkedBaseStyle(e,t){return!(t.container instanceof ShadowRoot)&&e.textContent&&a.getBaseAppStyle(e.textContent)?(e.textContent="",e.innerHTML="",e.setAttribute("linked-from-base","true"),!0):!1}resetPackRule(e,t,o){let n=this.scopeRule(Array.from(e.cssRules),t);return`@${o} ${e.conditionText} {${n}}`}resetUrlHost(e,t,o){let n=t;return e.replace(/url\(["']?([^)"']+)["']?\)/gm,(i,c)=>{if(/^(data|blob):/.test(c)||/^(https?:)?\/\//.test(c))return i;if(/^((\.\.?\/)|[^/])/.test(c)&&o){let s=o.split("/");s.pop(),n=x(`${s.join("/")}/`)}return`url("${h(c,n)}")`})}scopeRule(e,t){let o="";for(let n of e)switch(n.type){case 1:o+=this.scopeStyleRule(n,t);break;case 4:o+=this.resetPackRule(n,t,"media");break;case 12:o+=this.resetPackRule(n,t,"supports");break;default:o+=n.cssText;break}return o.replace(/^\s+/,"")}scopeStyleRule(e,t){let{cssText:o,selectorText:n}=e;if(/^((html[\s>~,]+body)|(html|body|:root))$/.test(n))return o.replace(/^((html[\s>~,]+body)|(html|body|:root))/,t);if(n==="*")return o.replace("*",`${t} *`);let i=/(^|\s+)((html[\s>~]+body)|(html|body|:root))(?=[\s>~]+|$)/;return o.replace(/^[\s\S]+{/,c=>c.replace(/(^|,)([^,]+)/g,(s,u,l)=>i.test(l)?s.replace(i,t):`${u} ${t} ${l.replace(/^\s*/,"")}`))}scopedLinkCSS(e,t){let o=this.createStyleElement();o.setAttribute("type","text/css");let n=!!e.keepAlive&&!(e.container instanceof ShadowRoot);E(o,e,n);let i=n?document.head:e.container;try{if(this.code)this.commonScoped(o,e),i?.prepend(o),t&&_(t);else if(t.getAttribute("href"))this.url=h(t.getAttribute("href"),e.url),this.getCode(e).then(()=>{this.scopedStyleCSS(e,o),t.remove(),i?.prepend(o),t&&_(t),this.scoped=!0});else{let c=new MutationObserver(()=>{t.href&&(c.disconnect(),this.url=h(t.getAttribute("href"),e.url),this.getCode(e).then(()=>{this.scopedStyleCSS(e,o),t.remove(),i?.prepend(o),t&&_(t),this.scoped=!0}))});c.observe(t,{attributeFilter:["href"],childList:!1,subtree:!1})}}catch{t&&U(t)}return o}scopedStyleCSS(e,t){let o=!!e.keepAlive&&!(e.container instanceof ShadowRoot);if(E(t,e,o),this.code||t.textContent){if(t.textContent&&(t.textContent="",t.innerHTML=""),this.linkedBaseStyle(t,e))return t;this.commonScoped(t,e)}else{let n=new MutationObserver(()=>{(t.textContent||t.sheet?.cssRules?.length)&&(n.disconnect(),this.linkedBaseStyle(t,e)||this.commonScoped(t,e))});n.observe(t,{attributes:!1,characterData:!0,childList:!0,subtree:!0})}return this.url&&t.setAttribute("origin-src",this.url),t}};async function V(r,e){let t=Array.from(r.source.styles.values()),o=[];for(let n of t)o.push(n.executeCode(r));await Promise.all(o).then(n=>{let i=e||r.container;r.keepAlive&&!(i instanceof ShadowRoot)?document.head.append(...n):i?.append(...n)})}var C=class{state="UNSET";appCacheKey;container;data;initSource;isPreLoad=!1;keepAlive;name;sandBox;scopeCss=!0;scopeJs=!1;showSourceCode=!0;source;url;fetchSource;constructor(e){this.name=e.id!==e.url?e.id:W(5),this.appCacheKey=e.id||this.name,this.url=e.url,this.container=e.container??void 0,this.scopeJs=e.scopeJs??!0,this.showSourceCode=e.showSourceCode??!0,this.scopeCss=e.scopeCss??!0,this.keepAlive=e.keepAlive??!1,this.data=e.data??{},this.initSource=e.initSource??[],this.scopeJs&&(this.sandBox=new R(this)),this.fetchSource=e.fetchSource}activated(e,t){if(this.isPreLoad=!1,this.state="ACTIVATED",this.container&&e){e instanceof Element&&e?.setAttribute(I,this.name);let o=document.createDocumentFragment();for(let i of Array.from(this.container.childNodes))o.appendChild(i);e.appendChild(o),this.container=e,this.sandBox?.activated();let n=this.source?.getScript(this.url);t?.(this,n?.exportInstance)}}deactivated(){this.state="DEACTIVATED",this.sandBox?.deactivated()}mount(e,t){this.isPreLoad=!1,this.container=e??this.container,this.state="MOUNTING",this.container instanceof HTMLElement&&this.container?.setAttribute(I,this.name),this.container.innerHTML="";let o=document.createElement("div"),n=`${this.name}-wrapper`;o.setAttribute("id",n),this.source?.styles.size&&V(this,this.container),this.container.appendChild(o),this.sandBox?.activated(),G(this).finally(()=>{this.state="MOUNTED";let i=this.source?.getScript(this.url);typeof i?.exportInstance?.render=="function"&&i.exportInstance.render(o,this.data),t?.(this,i?.exportInstance)})}onError(){this.state="ERROR"}onMount(){this.isPreLoad||(this.state="LOADED",this.mount())}registerRunningApp(){T(this),Promise.resolve().then(()=>T(null))}async start(){(!this.source||["ERROR","UNSET"].includes(this.status))&&(this.source=new v(this.url),await this.source.importEntry(this))}unmount(e){this.state="UNMOUNT",this.sandBox?.deactivated(),e&&a.deleteApp(this.url),this.container.innerHTML="",this.container=void 0}set status(e){this.state=e}get status(){return this.state}};var q,$,z,y=class{async=!1;code="";defer=!1;exportInstance;fromHtml;initial;isModule=!1;scoped;url;constructor({async:e,code:t,defer:o,fromHtml:n,initial:i,isModule:c,url:s}){this.code=t,this.async=e,this.defer=o,this.isModule=c,this.url=s,this.scoped=!1,this.fromHtml=n??!1,this.initial=i??!1}async executeCode(e,t=!1){try{if(this.code||await this.getCode(e),e instanceof C){let n=e.scopeJs&&e.sandBox?.proxyWindow||window;Ze(n)}let o=this.code;if(o=this.transformCode(e),e.showSourceCode||this.isModule){let n=document.createElement("script");if(n.__BK_WEWEB_APP_KEY__&&(n.__BK_WEWEB_APP_KEY__=void 0),e.registerRunningApp(),this.executeSourceScript(n,o),t)return n;let i=!!e.keepAlive&&!(e.container instanceof ShadowRoot),c=i?document.head:e.container;E(n,e,i),c.appendChild(n)}else if(this.executeMemoryScript(e,o),t)return document.createComment("\u3010bk-weweb\u3011dynamic script");if(e instanceof C){let n=e.scopeJs&&e.sandBox?.proxyWindow||window,i=ze(n);i&&(this.exportInstance=n[i],e.scopeJs||delete n[i])}}catch(o){console.error("execute script code error",o)}}executeMemoryScript(e,t){try{let o=e instanceof A&&e.scopeLocation;e.registerRunningApp(),new Function("window","location","history",t)(e.sandBox.proxyWindow,o?e.iframe.contentWindow.location:window.location,o?e.iframe.contentWindow.history:window.history)}catch(o){console.error(o)}}executeSourceScript(e,t){this.isModule?(e.src=`${this.url}?key=${Date.now()}`,e.setAttribute("type","module")):e.textContent=t,this.url&&e.setAttribute("origin-src",this.url)}async getCode(e){if(this.code.length||!this.url)return this.code;let t="";return e?.source?.getScript(this.url)&&(t=e.source.getScript(this.url)?.code||""),!t&&a.getCacheScript(this.url)&&(t=a.getCacheScript(this.url)?.code||""),t||(t=await g(this.url,{},e).catch(o=>(console.error(`fetch script ${this.url} error`,o),""))),t=t.replace(/^"use\sstrict";$/gim,""),this.code=t,t}setCode(e){this.code=e}transformCode(e){let t="";return e.sandBox?this.isModule?` with(window.${e.sandBox.windowSymbolKey}){
|
|
4
3
|
;${this.code}
|
|
5
4
|
|
|
6
5
|
${t}
|