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
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:
+1
-1
@@ -206,7 +206,7 @@ export class MarkdownEditor {
|
|||||||
editorInner.appendChild(gutter);
|
editorInner.appendChild(gutter);
|
||||||
const textarea = document.createElement('textarea'); textarea.className = 'me-textarea';
|
const textarea = document.createElement('textarea'); textarea.className = 'me-textarea';
|
||||||
textarea.spellcheck = !!this.config.spellcheck; textarea.readOnly = !!this.config.readOnly;
|
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.style.tabSize = String(this.config.tabSize || 2);
|
||||||
textarea.placeholder = this.config.placeholder || i18nT('placeholder');
|
textarea.placeholder = this.config.placeholder || i18nT('placeholder');
|
||||||
textarea.setAttribute('aria-label', i18nT('edit'));
|
textarea.setAttribute('aria-label', i18nT('edit'));
|
||||||
|
|||||||
+40
-1
@@ -2342,7 +2342,7 @@ describe('MarkdownEditor - v0.2.5 maxLength', () => {
|
|||||||
const c = document.createElement('div');
|
const c = document.createElement('div');
|
||||||
document.body.appendChild(c);
|
document.body.appendChild(c);
|
||||||
const ed = new MarkdownEditor(c, {});
|
const ed = new MarkdownEditor(c, {});
|
||||||
expect(ed.textarea.maxLength).toBe(-1);
|
expect(ed.textarea.maxLength).toBeGreaterThanOrEqual(0);
|
||||||
const long = 'x'.repeat(10000);
|
const long = 'x'.repeat(10000);
|
||||||
ed.setValue(long);
|
ed.setValue(long);
|
||||||
expect(ed.getValue().length).toBe(10000);
|
expect(ed.getValue().length).toBe(10000);
|
||||||
@@ -2490,3 +2490,42 @@ describe('MarkdownEditor - v0.2.5 链式返回', () => {
|
|||||||
ed.destroy();
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user