From 9a96539b491a65cbcbef4d768c4d41e708907c0f Mon Sep 17 00:00:00 2001 From: Zihua Li Date: Tue, 30 Jan 2024 14:50:45 +0800 Subject: [PATCH] Fix Quill.getText not respecting length (#3979) --- CHANGELOG.md | 1 + packages/quill/src/core/quill.ts | 4 +-- packages/quill/test/unit/core/quill.spec.ts | 37 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c186f3ad4f..c5c7f949e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Fix IME not working correctly in Safari. - **Clipboard** Support paste as plain text. +- Fix `Quill.getText()` not respecting `length` parameter. # 2.0.0-beta.1 diff --git a/packages/quill/src/core/quill.ts b/packages/quill/src/core/quill.ts index a23a914c8f..df5992e69f 100644 --- a/packages/quill/src/core/quill.ts +++ b/packages/quill/src/core/quill.ts @@ -499,7 +499,7 @@ class Quill { length?: number, ) { if (typeof index === 'number') { - length = this.getLength() - index; + length = length ?? this.getLength() - index; } // @ts-expect-error [index, length] = overload(index, length); @@ -513,7 +513,7 @@ class Quill { length?: number, ): string { if (typeof index === 'number') { - length = this.getLength() - index; + length = length ?? this.getLength() - index; } // @ts-expect-error [index, length] = overload(index, length); diff --git a/packages/quill/test/unit/core/quill.spec.ts b/packages/quill/test/unit/core/quill.spec.ts index a38bbd1eb9..c6359d5bb0 100644 --- a/packages/quill/test/unit/core/quill.spec.ts +++ b/packages/quill/test/unit/core/quill.spec.ts @@ -506,6 +506,43 @@ describe('Quill', () => { `); }); + test('works when provide index and length', () => { + const quill = new Quill(createContainer('

Welcome

')); + expect(quill.getText(2, 3)).toMatchInlineSnapshot(` + "lco" + `); + }); + + test('works with range', () => { + const quill = new Quill(createContainer('

Welcome

')); + expect(quill.getText({ index: 1, length: 2 })).toMatchInlineSnapshot( + '"el"', + ); + }); + }); + + describe('getSemanticHTML()', () => { + test('return all html by default', () => { + const quill = new Quill(createContainer('

Welcome

')); + expect(quill.getSemanticHTML()).toMatchInlineSnapshot(` + "

Welcome

" + `); + }); + + test('works when only provide index', () => { + const quill = new Quill(createContainer('

Welcome

')); + expect(quill.getSemanticHTML(2)).toMatchInlineSnapshot(` + "lcome" + `); + }); + + test('works when provide index and length', () => { + const quill = new Quill(createContainer('

Welcome

')); + expect(quill.getSemanticHTML(2, 3)).toMatchInlineSnapshot(` + "lco" + `); + }); + test('works with range', () => { const quill = new Quill(createContainer('

Welcome

')); expect(quill.getText({ index: 1, length: 2 })).toMatchInlineSnapshot(