@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.
package/dist/index.esm.js CHANGED
@@ -1704,7 +1704,12 @@ const createProxyDocument = (rawDocument, app) => {
1704
1704
  return rawDocument.querySelector.call(this, selectors);
1705
1705
  }
1706
1706
  // 返回当前应用程序容器中匹配选择器的元素,如果没有匹配的元素则返回 null
1707
- return app?.container?.querySelector(selectors) ?? null;
1707
+ try {
1708
+ return app?.container?.querySelector(selectors) ?? null;
1709
+ }
1710
+ catch {
1711
+ return null;
1712
+ }
1708
1713
  }
1709
1714
  /**
1710
1715
  * 重写了 Document 类的 querySelectorAll 方法
@@ -1730,7 +1735,7 @@ const createProxyDocument = (rawDocument, app) => {
1730
1735
  }
1731
1736
  function getElementsByTagName(key) {
1732
1737
  if (SPECIAL_ELEMENT_TAG.includes(key) || (!app?.showSourceCode && key.toLocaleLowerCase() === 'script')) {
1733
- return rawDocument.getElementsByName(key);
1738
+ return rawDocument.getElementsByTagName(key);
1734
1739
  }
1735
1740
  return querySelectorAllNew(key);
1736
1741
  }
@@ -2581,9 +2586,10 @@ class EntrySource {
2581
2586
  const nonceStr = randomUrl();
2582
2587
  const scriptInstance = new Script({
2583
2588
  async: false,
2584
- code: script.textContent,
2589
+ code: script.textContent.replace(/var\s+([^=,]+)=/gm, `window.$1 = `),
2585
2590
  defer: script.type === 'module',
2586
2591
  fromHtml: !needReplaceELement,
2592
+ initial: true,
2587
2593
  isModule: script.type === 'module',
2588
2594
  url: nonceStr,
2589
2595
  });
@@ -2597,36 +2603,64 @@ class EntrySource {
2597
2603
  }
2598
2604
  return { replace: script };
2599
2605
  }
2600
- collectScriptAndStyle(parent, app) {
2601
- const children = Array.from(parent.children);
2602
- children.length &&
2603
- children.forEach(child => {
2604
- this.collectScriptAndStyle(child, app);
2605
- });
2606
- children?.forEach(dom => {
2607
- if (dom instanceof HTMLLinkElement) {
2608
- this.collectLink(dom, parent);
2609
- }
2610
- else if (dom instanceof HTMLStyleElement) {
2611
- if (!dom.hasAttribute('exclude') && !dom.hasAttribute('ignore')) {
2612
- this.styles.set(randomUrl(), new Style({
2613
- code: dom.textContent || '',
2614
- fromHtml: true,
2615
- url: '',
2616
- }));
2617
- dom.remove();
2618
- }
2619
- }
2620
- else if (dom instanceof HTMLScriptElement) {
2621
- this.collectScript(dom, parent);
2622
- }
2623
- else if (dom instanceof HTMLMetaElement || dom instanceof HTMLTitleElement) {
2624
- parent.removeChild(dom);
2606
+ collectScriptAndStyle(parent) {
2607
+ const links = Array.from(parent.querySelectorAll('link'));
2608
+ links?.forEach(link => {
2609
+ this.collectLink(link, link.parentElement);
2610
+ });
2611
+ const styles = Array.from(parent.querySelectorAll('style'));
2612
+ styles?.forEach(style => {
2613
+ if (!style.hasAttribute('exclude') && !style.hasAttribute('ignore')) {
2614
+ this.styles.set(randomUrl(), new Style({
2615
+ code: style.textContent || '',
2616
+ fromHtml: true,
2617
+ url: '',
2618
+ }));
2619
+ style.remove();
2625
2620
  }
2626
- else if (dom instanceof HTMLImageElement && dom.hasAttribute('src')) {
2627
- dom.setAttribute('src', fillUpPath(dom.getAttribute('src'), this.url));
2621
+ });
2622
+ const scripts = Array.from(parent.querySelectorAll('script'));
2623
+ scripts?.forEach(script => {
2624
+ this.collectScript(script, script.parentElement);
2625
+ });
2626
+ const metas = Array.from(parent.querySelectorAll('meta'));
2627
+ metas?.forEach(meta => {
2628
+ meta.parentElement.removeChild(meta);
2629
+ });
2630
+ const imgs = Array.from(parent.querySelectorAll('img'));
2631
+ imgs?.forEach(img => {
2632
+ if (img.hasAttribute('src')) {
2633
+ img.setAttribute('src', fillUpPath(img.getAttribute('src'), this.url));
2628
2634
  }
2629
2635
  });
2636
+ // const children = Array.from(parent.children);
2637
+ // children?.forEach(dom => {
2638
+ // if (dom instanceof HTMLLinkElement) {
2639
+ // this.collectLink(dom, parent);
2640
+ // } else if (dom instanceof HTMLStyleElement) {
2641
+ // if (!dom.hasAttribute('exclude') && !dom.hasAttribute('ignore')) {
2642
+ // this.styles.set(
2643
+ // randomUrl(),
2644
+ // new Style({
2645
+ // code: dom.textContent || '',
2646
+ // fromHtml: true,
2647
+ // url: '',
2648
+ // }),
2649
+ // );
2650
+ // dom.remove();
2651
+ // }
2652
+ // } else if (dom instanceof HTMLScriptElement) {
2653
+ // this.collectScript(dom, parent);
2654
+ // } else if (dom instanceof HTMLMetaElement || dom instanceof HTMLTitleElement) {
2655
+ // parent.removeChild(dom);
2656
+ // } else if (dom instanceof HTMLImageElement && dom.hasAttribute('src')) {
2657
+ // dom.setAttribute('src', fillUpPath(dom.getAttribute('src')!, this.url));
2658
+ // }
2659
+ // });
2660
+ // children.length &&
2661
+ // children.forEach(child => {
2662
+ // this.collectScriptAndStyle(child as HTMLElement, app);
2663
+ // });
2630
2664
  }
2631
2665
  getScript(url) {
2632
2666
  return this.scripts.get(url);
@@ -2664,7 +2698,7 @@ class EntrySource {
2664
2698
  if (wrapElement.__BK_WEWEB_APP_KEY__)
2665
2699
  delete wrapElement.__BK_WEWEB_APP_KEY__;
2666
2700
  wrapElement.innerHTML = htmlStr.replace(/<\/?head>/gim, '').replace(/<\/?body>/i, '');
2667
- this.collectScriptAndStyle(wrapElement, app);
2701
+ this.collectScriptAndStyle(wrapElement);
2668
2702
  await excuteAppStyles(app, wrapElement);
2669
2703
  this.html = wrapElement;
2670
2704
  }