diff --git a/js/utils/calcTextareaHeight.ts b/js/utils/calcTextareaHeight.ts index d72f47c8e6..282c402c3b 100644 --- a/js/utils/calcTextareaHeight.ts +++ b/js/utils/calcTextareaHeight.ts @@ -27,6 +27,11 @@ function calcTextareaHeight( minRows: LimitType = 1, maxRows: LimitType = null, ): CalculateStyleType { + // 验证元素有效性 - 修复 Bug #6028 + if (!targetElement || !targetElement.isConnected) { + return { height: 'auto' }; + } + if (!hiddenTextarea) { hiddenTextarea = document.createElement('textarea'); document.body.appendChild(hiddenTextarea); diff --git a/js/utils/helper.ts b/js/utils/helper.ts index d5e5fd26c4..f6f081a1a6 100644 --- a/js/utils/helper.ts +++ b/js/utils/helper.ts @@ -241,7 +241,21 @@ export function calculateNodeSize(targetElement: HTMLElement) { }; } - const style = window.getComputedStyle(targetElement); + // 验证元素有效性 - 修复 Bug #6028 + if (!targetElement + || !(targetElement instanceof Element) + || !targetElement.isConnected + || !document.contains(targetElement)) { + return { + paddingSize: 0, + borderSize: 0, + boxSizing: 'border-box', + sizingStyle: '', + }; + } + + try { + const style = window.getComputedStyle(targetElement); const boxSizing = style.getPropertyValue('box-sizing') || style.getPropertyValue('-moz-box-sizing') @@ -261,7 +275,15 @@ export function calculateNodeSize(targetElement: HTMLElement) { .map((name) => `${name}:${style.getPropertyValue(name)}`) .join(';'); - return { - paddingSize, borderSize, boxSizing, sizingStyle, - }; + return { + paddingSize, borderSize, boxSizing, sizingStyle, + }; + } catch (error) { + return { + paddingSize: 0, + borderSize: 0, + boxSizing: 'border-box', + sizingStyle: '', + }; + } }