From 7f23cd7f9586411a8ec01033d166040802837da9 Mon Sep 17 00:00:00 2001 From: zhangdi Date: Wed, 24 Apr 2024 17:21:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=AD=90=E5=BA=94?= =?UTF-8?q?=E7=94=A8document.querySelectorAll('script')=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E4=B8=BA0=20(#822)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/wujie-core/src/proxy.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/wujie-core/src/proxy.ts b/packages/wujie-core/src/proxy.ts index ab3e3af21..c49a4cb10 100644 --- a/packages/wujie-core/src/proxy.ts +++ b/packages/wujie-core/src/proxy.ts @@ -172,12 +172,13 @@ export function proxyGenerator( return ctx[propKey]?.apply(ctx, args); } // 二选一,优先shadowDom,除非采用array合并,排除base,防止对router造成影响 - return ( - target.apply(shadowRoot, args) || - (args[0] === "base" - ? null - : iframe.contentWindow[rawPropMap[propKey]].call(iframe.contentWindow.document, args[0])) - ); + const res = target.apply(shadowRoot, args); + if (res && (propKey === "querySelector" || res.length > 0)) { + return res; + } + return args[0] === "base" + ? null + : iframe.contentWindow[rawPropMap[propKey]].call(iframe.contentWindow.document, args[0]); }, }); } From 3eafb271bd826696398433d9cc0d190a1c354db4 Mon Sep 17 00:00:00 2001 From: yiludege Date: Sat, 14 Nov 2026 13:40:27 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(wujie-core):=20querySelectorAll('script?= =?UTF-8?q?')=20=E4=BB=8E=20js=20iframe=20=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 子应用脚本都在 js iframe 中执行,shadowRoot / 渲染 iframe 中没有可执行的 script。 与 getElementsByTagName('script') 保持一致,querySelectorAll('script') 改为从 js iframe 获取, 用原生 querySelectorAll 保证返回值仍是 NodeList;降级模式下 querySelector('script') 同样处理。 撤回对所有选择器的空结果兜底,其他选择器行为与之前一致,修复 querySelectorAll('base') 返回 null 的问题;补充单测。 Co-authored-by: Cursor --- .../__test__/unit/query-scripts.test.ts | 130 ++++++++++++++++++ packages/wujie-core/src/common.ts | 2 + packages/wujie-core/src/proxy.ts | 50 ++++++- 3 files changed, 175 insertions(+), 7 deletions(-) create mode 100644 packages/wujie-core/__test__/unit/query-scripts.test.ts diff --git a/packages/wujie-core/__test__/unit/query-scripts.test.ts b/packages/wujie-core/__test__/unit/query-scripts.test.ts new file mode 100644 index 000000000..37c3f6513 --- /dev/null +++ b/packages/wujie-core/__test__/unit/query-scripts.test.ts @@ -0,0 +1,130 @@ +/** + * 子应用的脚本在 js iframe 中执行,document.querySelectorAll("script") 应从 js iframe 获取, + * 与 getElementsByTagName("script") 保持一致;其他选择器行为不变。 + */ + +export {}; + +const { proxyGenerator, localGenerator } = require("../../src/proxy"); + +function createJsIframe(): HTMLIFrameElement { + const iframe = document.createElement("iframe"); + document.body.appendChild(iframe); + const iframeWindow = iframe.contentWindow as any; + const iframeDocument = iframe.contentDocument; + iframeWindow.__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR__ = iframeWindow.Document.prototype.querySelector; + iframeWindow.__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR_ALL__ = iframeWindow.Document.prototype.querySelectorAll; + iframeWindow.__WUJIE_RAW_DOCUMENT_HEAD__ = iframeDocument.head; + ["app-a", "app-b"].forEach((id) => { + const script = iframeDocument.createElement("script"); + script.id = id; + script.type = "text/plain"; + iframeDocument.head.appendChild(script); + }); + return iframe; +} + +/** 渲染 DOM:包含一个不会执行的模板 script、一个业务节点 */ +const RENDER_HTML = + 's'; + +const urlElement = Object.assign(document.createElement("a"), { href: "https://sub.example.com/app/" }); + +afterEach(() => { + document.body.innerHTML = ""; +}); + +describe("非降级 proxyDocument", () => { + let iframe: HTMLIFrameElement; + let proxyDocument: Document; + let shadowRoot: ShadowRoot; + + beforeEach(() => { + iframe = createJsIframe(); + const host = document.createElement("div"); + document.body.appendChild(host); + shadowRoot = host.attachShadow({ mode: "open" }); + const html = document.createElement("html"); + html.innerHTML = RENDER_HTML; + shadowRoot.appendChild(html); + (iframe.contentWindow as any).__WUJIE = { shadowRoot, proxyLocation: {} }; + proxyDocument = proxyGenerator( + iframe, + urlElement, + "https://main.example.com", + "https://sub.example.com" + ).proxyDocument; + }); + + const call = (method: "querySelector" | "querySelectorAll", selector: string) => + proxyDocument[method].call(iframe.contentDocument, selector); + + it("querySelectorAll('script') 返回 js iframe 中的脚本,且是可 forEach 的 NodeList", () => { + const res = call("querySelectorAll", "script"); + expect(res).toBeInstanceOf((iframe.contentWindow as any).NodeList); + const ids = []; + res.forEach((s) => ids.push(s.id)); + expect(ids).toEqual(["app-a", "app-b"]); + }); + + it("选择器大小写与首尾空格不影响", () => { + expect(call("querySelectorAll", " SCRIPT ").length).toBe(2); + }); + + it("其他选择器仍从 shadowRoot 获取", () => { + expect(call("querySelectorAll", ".s")[0]).toBe(shadowRoot.querySelector(".s")); + expect(call("querySelectorAll", "script[type='text/x-template']")[0].id).toBe("tpl"); + }); + + it("querySelectorAll('base') 在 shadowRoot 中查不到时返回空 NodeList", () => { + const res = call("querySelectorAll", "base"); + expect(res.length).toBe(0); + expect(typeof res.forEach).toBe("function"); + }); + + it("querySelector 行为不变:优先 shadowRoot", () => { + expect(call("querySelector", "script").id).toBe("tpl"); + }); + + it("非 iframe document 调用时不做处理", () => { + const res = proxyDocument.querySelectorAll.call(shadowRoot, "script"); + expect(res[0].id).toBe("tpl"); + }); +}); + +describe("降级 proxyDocument", () => { + let iframe: HTMLIFrameElement; + let proxyDocument: any; + let renderDocument: Document; + + beforeEach(() => { + iframe = createJsIframe(); + const renderIframe = document.createElement("iframe"); + document.body.appendChild(renderIframe); + renderDocument = renderIframe.contentDocument; + renderDocument.documentElement.innerHTML = RENDER_HTML; + (iframe.contentWindow as any).__WUJIE = { document: renderDocument, proxyLocation: {} }; + proxyDocument = localGenerator( + iframe, + urlElement, + "https://main.example.com", + "https://sub.example.com" + ).proxyDocument; + }); + + it("querySelectorAll('script') 返回 js iframe 中的脚本", () => { + const res = proxyDocument.querySelectorAll("script"); + expect(res).toBeInstanceOf((iframe.contentWindow as any).NodeList); + expect(Array.from(res).map((s: HTMLScriptElement) => s.id)).toEqual(["app-a", "app-b"]); + }); + + it("querySelector('script') 返回 js iframe 中的第一个脚本", () => { + expect(proxyDocument.querySelector("script").id).toBe("app-a"); + }); + + it("其他选择器仍从渲染 document 获取", () => { + expect(proxyDocument.querySelector(".s")).toBe(renderDocument.querySelector(".s")); + expect(proxyDocument.querySelectorAll(".s").length).toBe(1); + expect(proxyDocument.querySelector("#tpl")).toBe(renderDocument.querySelector("#tpl")); + }); +}); diff --git a/packages/wujie-core/src/common.ts b/packages/wujie-core/src/common.ts index 934ea98a7..eaa655dc2 100644 --- a/packages/wujie-core/src/common.ts +++ b/packages/wujie-core/src/common.ts @@ -61,6 +61,8 @@ export const documentProxyProperties = { "URL", "getElementsByTagName", "getElementById", + "querySelector", + "querySelectorAll", ], // 子应用需要手动修正的属性方法 diff --git a/packages/wujie-core/src/proxy.ts b/packages/wujie-core/src/proxy.ts index 7bd730b1d..1c8b99556 100644 --- a/packages/wujie-core/src/proxy.ts +++ b/packages/wujie-core/src/proxy.ts @@ -34,6 +34,19 @@ function locationHrefSet(iframe: HTMLIFrameElement, value: string, appHostPath: return true; } +function isScriptSelector(selector: unknown): boolean { + return typeof selector === "string" && selector.trim().toLowerCase() === "script"; +} + +/** + * 子应用脚本都在 js iframe 中执行,shadowRoot / 渲染 iframe 中没有可执行的 script。 + * 与 getElementsByTagName("script") 保持一致从 js iframe 获取; + * 用原生 querySelectorAll 而不是 document.scripts,保证返回值仍是 NodeList(scripts 是 HTMLCollection,没有 forEach)。 + */ +function queryIframeScripts(iframe: HTMLIFrameElement): NodeListOf { + return iframe.contentWindow.__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR_ALL__.call(iframe.contentDocument, "script"); +} + /** * 非降级情况下window、document、location代理 */ @@ -172,14 +185,16 @@ export function proxyGenerator( if (ctx !== iframe.contentDocument) { return ctx[propKey]?.apply(ctx, args); } - // 二选一,优先shadowDom,除非采用array合并,排除base,防止对router造成影响 - const res = target.apply(shadowRoot, args); - if (res && (propKey === "querySelector" || res.length > 0)) { - return res; + if (propKey === "querySelectorAll" && isScriptSelector(args[0])) { + return queryIframeScripts(iframe); } - return args[0] === "base" - ? null - : iframe.contentWindow[rawPropMap[propKey]].call(iframe.contentWindow.document, args[0]); + // 二选一,优先shadowDom,除非采用array合并,排除base,防止对router造成影响 + return ( + target.apply(shadowRoot, args) || + (args[0] === "base" + ? null + : iframe.contentWindow[rawPropMap[propKey]].call(iframe.contentWindow.document, args[0])) + ); }, }); } @@ -328,6 +343,27 @@ export function localGenerator( }; }, }, + querySelector: { + get() { + return function (...args) { + if (isScriptSelector(args[0])) { + return iframeRef.contentWindow.__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR__.call( + iframeRef.contentDocument, + "script" + ) as any; + } + return sandboxRef.document.querySelector.apply(sandboxRef.document, args) as any; + }; + }, + }, + querySelectorAll: { + get() { + return function (...args) { + if (isScriptSelector(args[0])) return queryIframeScripts(iframeRef) as any; + return sandboxRef.document.querySelectorAll.apply(sandboxRef.document, args) as any; + }; + }, + }, getElementById: { get() { return function (...args) {