Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions packages/wujie-core/__test__/unit/query-scripts.test.ts
Original file line number Diff line number Diff line change
@@ -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 =
'<head><script type="text/x-template" id="tpl"></script></head><body><span class="s">s</span></body>';

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"));
});
});
2 changes: 2 additions & 0 deletions packages/wujie-core/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export const documentProxyProperties = {
"URL",
"getElementsByTagName",
"getElementById",
"querySelector",
"querySelectorAll",
],

// 子应用需要手动修正的属性方法
Expand Down
37 changes: 37 additions & 0 deletions packages/wujie-core/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLScriptElement> {
return iframe.contentWindow.__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR_ALL__.call(iframe.contentDocument, "script");
}

/**
* 非降级情况下window、document、location代理
*/
Expand Down Expand Up @@ -172,6 +185,9 @@ export function proxyGenerator(
if (ctx !== iframe.contentDocument) {
return ctx[propKey]?.apply(ctx, args);
}
if (propKey === "querySelectorAll" && isScriptSelector(args[0])) {
return queryIframeScripts(iframe);
}
// 二选一,优先shadowDom,除非采用array合并,排除base,防止对router造成影响
return (
target.apply(shadowRoot, args) ||
Expand Down Expand Up @@ -327,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) {
Expand Down
Loading