Skip to content

Commit

Permalink
Fix Quill.getText not respecting length
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Jan 30, 2024
1 parent c82a61f commit 50746c4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions packages/quill/src/core/quill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions packages/quill/test/unit/core/quill.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,43 @@ describe('Quill', () => {
`);
});

test('works when provide index and length', () => {
const quill = new Quill(createContainer('<h1>Welcome</h1>'));
expect(quill.getText(2, 3)).toMatchInlineSnapshot(`
"lco"
`);
});

test('works with range', () => {
const quill = new Quill(createContainer('<h1>Welcome</h1>'));
expect(quill.getText({ index: 1, length: 2 })).toMatchInlineSnapshot(
'"el"',
);
});
});

describe('getSemanticHTML()', () => {
test('return all html by default', () => {
const quill = new Quill(createContainer('<h1>Welcome</h1>'));
expect(quill.getSemanticHTML()).toMatchInlineSnapshot(`
"<h1>Welcome</h1>"
`);
});

test('works when only provide index', () => {
const quill = new Quill(createContainer('<h1>Welcome</h1>'));
expect(quill.getSemanticHTML(2)).toMatchInlineSnapshot(`
"lcome"
`);
});

test('works when provide index and length', () => {
const quill = new Quill(createContainer('<h1>Welcome</h1>'));
expect(quill.getSemanticHTML(2, 3)).toMatchInlineSnapshot(`
"lco"
`);
});

test('works with range', () => {
const quill = new Quill(createContainer('<h1>Welcome</h1>'));
expect(quill.getText({ index: 1, length: 2 })).toMatchInlineSnapshot(
Expand Down

0 comments on commit 50746c4

Please sign in to comment.