Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/quill/src/core/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,13 @@ function convertHTML(
}
if (blot instanceof TextBlot) {
const escapedText = escapeText(blot.value().slice(index, index + length));
return escapedText.replaceAll(' ', ' ');
// A single space between words renders fine as-is; only a run of two or
// more needs non-breaking spaces, since HTML would otherwise collapse
// the run down to one space and change the text's meaning.
return escapedText.replace(
/ {2,}/g,
(spaces) => ' '.repeat(spaces.length - 1) + ' ',
);
}
if (blot instanceof ParentBlot) {
// TODO fix API
Expand Down
16 changes: 16 additions & 0 deletions packages/quill/test/unit/core/quill.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,22 @@ describe('Quill', () => {
'"el"',
);
});

test('keeps a single space between words as a plain space', () => {
const quill = new Quill(
createContainer('<p>a text with multiple spaces</p>'),
);
expect(quill.getSemanticHTML()).toMatchInlineSnapshot(
`"<p>a text with multiple spaces</p>"`,
);
});

test('preserves a run of consecutive spaces with non-breaking spaces', () => {
const quill = new Quill(createContainer('<p>a&nbsp;&nbsp; b</p>'));
expect(quill.getSemanticHTML()).toMatchInlineSnapshot(
`"<p>a&nbsp;&nbsp; b</p>"`,
);
});
});

describe('setText()', () => {
Expand Down