@blueking/bk-weweb 0.0.2-5.beta.3 → 0.0.2-5.beta.4

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.
@@ -1819,7 +1819,12 @@ const createProxyDocument = (rawDocument, app) => {
1819
1819
  return rawDocument.querySelector.call(this, selectors);
1820
1820
  }
1821
1821
  // 返回当前应用程序容器中匹配选择器的元素,如果没有匹配的元素则返回 null
1822
- return app?.container?.querySelector(selectors) ?? null;
1822
+ try {
1823
+ return app?.container?.querySelector(selectors) ?? null;
1824
+ }
1825
+ catch {
1826
+ return null;
1827
+ }
1823
1828
  }
1824
1829
  /**
1825
1830
  * 重写了 Document 类的 querySelectorAll 方法
@@ -1845,7 +1850,7 @@ const createProxyDocument = (rawDocument, app) => {
1845
1850
  }
1846
1851
  function getElementsByTagName(key) {
1847
1852
  if (SPECIAL_ELEMENT_TAG.includes(key) || (!app?.showSourceCode && key.toLocaleLowerCase() === 'script')) {
1848
- return rawDocument.getElementsByName(key);
1853
+ return rawDocument.getElementsByTagName(key);
1849
1854
  }
1850
1855
  return querySelectorAllNew(key);
1851
1856
  }
@@ -2603,9 +2608,10 @@ class EntrySource {
2603
2608
  const nonceStr = randomUrl();
2604
2609
  const scriptInstance = new Script({
2605
2610
  async: false,
2606
- code: script.textContent,
2611
+ code: script.textContent.replace(/var\s+([^=,]+)=/gm, `window.$1 = `),
2607
2612
  defer: script.type === 'module',
2608
2613
  fromHtml: !needReplaceELement,
2614
+ initial: true,
2609
2615
  isModule: script.type === 'module',
2610
2616
  url: nonceStr,
2611
2617
  });
@@ -2619,36 +2625,64 @@ class EntrySource {
2619
2625
  }
2620
2626
  return { replace: script };
2621
2627
  }
2622
- collectScriptAndStyle(parent, app) {
2623
- const children = Array.from(parent.children);
2624
- children.length &&
2625
- children.forEach(child => {
2626
- this.collectScriptAndStyle(child, app);
2627
- });
2628
- children?.forEach(dom => {
2629
- if (dom instanceof HTMLLinkElement) {
2630
- this.collectLink(dom, parent);
2631
- }
2632
- else if (dom instanceof HTMLStyleElement) {
2633
- if (!dom.hasAttribute('exclude') && !dom.hasAttribute('ignore')) {
2634
- this.styles.set(randomUrl(), new Style({
2635
- code: dom.textContent || '',
2636
- fromHtml: true,
2637
- url: '',
2638
- }));
2639
- dom.remove();
2640
- }
2641
- }
2642
- else if (dom instanceof HTMLScriptElement) {
2643
- this.collectScript(dom, parent);
2644
- }
2645
- else if (dom instanceof HTMLMetaElement || dom instanceof HTMLTitleElement) {
2646
- parent.removeChild(dom);
2628
+ collectScriptAndStyle(parent) {
2629
+ const links = Array.from(parent.querySelectorAll('link'));
2630
+ links?.forEach(link => {
2631
+ this.collectLink(link, link.parentElement);
2632
+ });
2633
+ const styles = Array.from(parent.querySelectorAll('style'));
2634
+ styles?.forEach(style => {
2635
+ if (!style.hasAttribute('exclude') && !style.hasAttribute('ignore')) {
2636
+ this.styles.set(randomUrl(), new Style({
2637
+ code: style.textContent || '',
2638
+ fromHtml: true,
2639
+ url: '',
2640
+ }));
2641
+ style.remove();
2647
2642
  }
2648
- else if (dom instanceof HTMLImageElement && dom.hasAttribute('src')) {
2649
- dom.setAttribute('src', fillUpPath(dom.getAttribute('src'), this.url));
2643
+ });
2644
+ const scripts = Array.from(parent.querySelectorAll('script'));
2645
+ scripts?.forEach(script => {
2646
+ this.collectScript(script, script.parentElement);
2647
+ });
2648
+ const metas = Array.from(parent.querySelectorAll('meta'));
2649
+ metas?.forEach(meta => {
2650
+ meta.parentElement.removeChild(meta);
2651
+ });
2652
+ const imgs = Array.from(parent.querySelectorAll('img'));
2653
+ imgs?.forEach(img => {
2654
+ if (img.hasAttribute('src')) {
2655
+ img.setAttribute('src', fillUpPath(img.getAttribute('src'), this.url));
2650
2656
  }
2651
2657
  });
2658
+ // const children = Array.from(parent.children);
2659
+ // children?.forEach(dom => {
2660
+ // if (dom instanceof HTMLLinkElement) {
2661
+ // this.collectLink(dom, parent);
2662
+ // } else if (dom instanceof HTMLStyleElement) {
2663
+ // if (!dom.hasAttribute('exclude') && !dom.hasAttribute('ignore')) {
2664
+ // this.styles.set(
2665
+ // randomUrl(),
2666
+ // new Style({
2667
+ // code: dom.textContent || '',
2668
+ // fromHtml: true,
2669
+ // url: '',
2670
+ // }),
2671
+ // );
2672
+ // dom.remove();
2673
+ // }
2674
+ // } else if (dom instanceof HTMLScriptElement) {
2675
+ // this.collectScript(dom, parent);
2676
+ // } else if (dom instanceof HTMLMetaElement || dom instanceof HTMLTitleElement) {
2677
+ // parent.removeChild(dom);
2678
+ // } else if (dom instanceof HTMLImageElement && dom.hasAttribute('src')) {
2679
+ // dom.setAttribute('src', fillUpPath(dom.getAttribute('src')!, this.url));
2680
+ // }
2681
+ // });
2682
+ // children.length &&
2683
+ // children.forEach(child => {
2684
+ // this.collectScriptAndStyle(child as HTMLElement, app);
2685
+ // });
2652
2686
  }
2653
2687
  getScript(url) {
2654
2688
  return this.scripts.get(url);
@@ -2686,7 +2720,7 @@ class EntrySource {
2686
2720
  if (wrapElement.__BK_WEWEB_APP_KEY__)
2687
2721
  delete wrapElement.__BK_WEWEB_APP_KEY__;
2688
2722
  wrapElement.innerHTML = htmlStr.replace(/<\/?head>/gim, '').replace(/<\/?body>/i, '');
2689
- this.collectScriptAndStyle(wrapElement, app);
2723
+ this.collectScriptAndStyle(wrapElement);
2690
2724
  await excuteAppStyles(app, wrapElement);
2691
2725
  this.html = wrapElement;
2692
2726
  }