fix(core): maxLength 默认值不再赋值 -1,修复浏览器 IndexSizeError
CI / test-parser (push) Successful in 9m26s
CI / test-core (push) Successful in 9m33s
CI / test-rest (push) Successful in 9m29s
CI / verify (18.x) (push) Successful in 9m50s
CI / verify (20.x) (push) Successful in 9m52s
CI / verify (24.x) (push) Successful in 9m45s

- textarea.maxLength 的 setter 不接受负值(-1 仅能通过不设置来
  保持),显式赋 -1 在真实浏览器抛出 IndexSizeError
- 改为仅当 maxLength > 0 时才设置属性
- 补防回归测试(mock setter 抛错 + 环境无关断言;
  jsdom 默认 maxLength 为 0,浏览器为 -1)
This commit is contained in:
2026-08-09 10:22:22 +08:00
parent d22380ac7a
commit 64aedb9546
2 changed files with 41 additions and 2 deletions
+40 -1
View File
@@ -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();
});
});