Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(proxy): protect the program when getElementById throws an exception error #693

Merged
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
2 changes: 2 additions & 0 deletions packages/wujie-core/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ export const WUJIE_TIPS_CSS_ERROR_REQUESTED = "样式请求出现错误";
export const WUJIE_TIPS_HTML_ERROR_REQUESTED = "html请求出现错误";
export const WUJIE_TIPS_REPEAT_RENDER = "无界组件短时间重复渲染了两次,可能存在性能问题请检查代码";
export const WUJIE_TIPS_NO_SCRIPT = "目标Script尚未准备好或已经被移除";
export const WUJIE_TIPS_GET_ELEMENT_BY_ID =
"不支持document.getElementById()传入特殊字符,请参考document.querySelector文档";
21 changes: 13 additions & 8 deletions packages/wujie-core/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { patchElementEffect, renderIframeReplaceApp } from "./iframe";
import { renderElementToContainer } from "./shadow";
import { pushUrlToWindow } from "./sync";
import { documentProxyProperties, rawDocumentQuerySelector } from "./common";
import { WUJIE_TIPS_RELOAD_DISABLED } from "./constant";
import { WUJIE_TIPS_RELOAD_DISABLED, WUJIE_TIPS_GET_ELEMENT_BY_ID } from "./constant";
import {
getTargetValue,
anchorElementGenerator,
Expand Down Expand Up @@ -146,13 +146,18 @@ export function proxyGenerator(
if (ctx !== iframe.contentDocument) {
return ctx[propKey]?.apply(ctx, args);
}
return (
target.call(shadowRoot, `[id="${args[0]}"]`) ||
iframe.contentWindow.__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR__.call(
iframe.contentWindow.document,
`#${args[0]}`
)
);
try {
return (
target.call(shadowRoot, `[id="${args[0]}"]`) ||
iframe.contentWindow.__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR__.call(
iframe.contentWindow.document,
`#${args[0]}`
)
);
} catch (error) {
warn(WUJIE_TIPS_GET_ELEMENT_BY_ID);
return null;
}
},
});
}
Expand Down