From 64aedb9546c6b81c2505d2f36a46b8602c4042ff Mon Sep 17 00:00:00 2001 From: thzxx <1440196015@qq.com> Date: Sun, 9 Aug 2026 10:22:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20maxLength=20=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC=E4=B8=8D=E5=86=8D=E8=B5=8B=E5=80=BC=20-1=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B5=8F=E8=A7=88=E5=99=A8=20IndexSizeError?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - textarea.maxLength 的 setter 不接受负值(-1 仅能通过不设置来 保持),显式赋 -1 在真实浏览器抛出 IndexSizeError - 改为仅当 maxLength > 0 时才设置属性 - 补防回归测试(mock setter 抛错 + 环境无关断言; jsdom 默认 maxLength 为 0,浏览器为 -1) --- src/core.ts | 2 +- tests/core.test.ts | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/core.ts b/src/core.ts index 689f8c7..19be5c6 100644 --- a/src/core.ts +++ b/src/core.ts @@ -206,7 +206,7 @@ export class MarkdownEditor { editorInner.appendChild(gutter); const textarea = document.createElement('textarea'); textarea.className = 'me-textarea'; textarea.spellcheck = !!this.config.spellcheck; textarea.readOnly = !!this.config.readOnly; - textarea.maxLength = this.config.maxLength && this.config.maxLength > 0 ? this.config.maxLength : -1; + if (this.config.maxLength && this.config.maxLength > 0) textarea.maxLength = this.config.maxLength; textarea.style.tabSize = String(this.config.tabSize || 2); textarea.placeholder = this.config.placeholder || i18nT('placeholder'); textarea.setAttribute('aria-label', i18nT('edit')); diff --git a/tests/core.test.ts b/tests/core.test.ts index 993a1ee..cf3bd4b 100644 --- a/tests/core.test.ts +++ b/tests/core.test.ts @@ -2342,7 +2342,7 @@ describe('MarkdownEditor - v0.2.5 maxLength', () => { const c = document.createElement('div'); document.body.appendChild(c); const ed = new MarkdownEditor(c, {}); - expect(ed.textarea.maxLength).toBe(-1); + expect(ed.textarea.maxLength).toBeGreaterThanOrEqual(0); const long = 'x'.repeat(10000); ed.setValue(long); expect(ed.getValue().length).toBe(10000); @@ -2490,3 +2490,42 @@ describe('MarkdownEditor - v0.2.5 链式返回', () => { ed.destroy(); }); }); + +// ============ v0.2.5 复查:maxLength 默认值不触发浏览器 IndexSizeError ============ + +describe('MarkdownEditor - v0.2.5 maxLength 默认值', () => { + test('不配置 maxLength 时保持默认 -1 且不显式赋值', () => { + document.body.innerHTML = ''; + const c = document.createElement('div'); + document.body.appendChild(c); + // 模拟浏览器 setter 行为:负值抛 IndexSizeError + const textareaProto = HTMLTextAreaElement.prototype; + const originalDesc = Object.getOwnPropertyDescriptor(textareaProto, 'maxLength'); + const setter = originalDesc?.set; + let threw = false; + if (setter) { + Object.defineProperty(textareaProto, 'maxLength', { + configurable: true, + get: originalDesc.get, + set: function (v: any) { + if (v < 0) { threw = true; throw new DOMException('IndexSizeError', 'IndexSizeError'); } + setter.call(this, v); + }, + }); + } + const ed = new MarkdownEditor(c, {}); + expect(threw).toBe(false); + expect(ed.textarea.maxLength).toBeGreaterThanOrEqual(0); + ed.destroy(); + if (setter) Object.defineProperty(textareaProto, 'maxLength', originalDesc); + }); + + test('maxLength 配置为 0 时不触发 setter 报错', () => { + document.body.innerHTML = ''; + const c = document.createElement('div'); + document.body.appendChild(c); + const ed = new MarkdownEditor(c, { maxLength: 0 }); + expect(ed.textarea.maxLength).toBeGreaterThanOrEqual(0); + ed.destroy(); + }); +});