Skip to content

Commit

Permalink
fix(proxy): protect the program when getElementById throws an excepti…
Browse files Browse the repository at this point in the history
…on error (#693)

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

* feat: 增加getElementById错误时警告文档

---------

Co-authored-by: chenzhaolong <chenzhaolong@ruqimobility.com>
  • Loading branch information
AzronChan and chenzhaolong authored Sep 19, 2023
1 parent 17179fb commit 37dc95b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
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

0 comments on commit 37dc95b

Please sign in to comment.