diff --git a/packages/wujie-core/src/constant.ts b/packages/wujie-core/src/constant.ts index 99cffb81..0f7fcd54 100644 --- a/packages/wujie-core/src/constant.ts +++ b/packages/wujie-core/src/constant.ts @@ -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文档"; diff --git a/packages/wujie-core/src/proxy.ts b/packages/wujie-core/src/proxy.ts index b545d9f2..ab3e3af2 100644 --- a/packages/wujie-core/src/proxy.ts +++ b/packages/wujie-core/src/proxy.ts @@ -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, @@ -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; + } }, }); }