From b7141c1e5ff3d621c3395e01b8bb884f0311f24c Mon Sep 17 00:00:00 2001 From: Zihua Li <635902+luin@users.noreply.github.com> Date: Tue, 27 Aug 2024 22:01:50 +0800 Subject: [PATCH] Convert regular spaces to non-breaking spaces when exporting to HTML --- packages/quill/src/core/editor.ts | 3 ++- packages/quill/test/unit/core/editor.spec.ts | 28 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/quill/src/core/editor.ts b/packages/quill/src/core/editor.ts index a19485840d..ed93366ed5 100644 --- a/packages/quill/src/core/editor.ts +++ b/packages/quill/src/core/editor.ts @@ -370,7 +370,8 @@ function convertHTML( return blot.html(index, length); } if (blot instanceof TextBlot) { - return escapeText(blot.value().slice(index, index + length)); + const escapedText = escapeText(blot.value().slice(index, index + length)); + return escapedText.replace(/\s/g, ' '); } if (blot instanceof ParentBlot) { // TODO fix API diff --git a/packages/quill/test/unit/core/editor.spec.ts b/packages/quill/test/unit/core/editor.spec.ts index 0c595332bb..2d47a1dec9 100644 --- a/packages/quill/test/unit/core/editor.spec.ts +++ b/packages/quill/test/unit/core/editor.spec.ts @@ -28,9 +28,11 @@ import { ColorClass } from '../../../src/formats/color.js'; import Quill from '../../../src/core.js'; import { normalizeHTML } from '../__helpers__/utils.js'; -const createEditor = (html: string) => { +const createEditor = (htmlOrContents: string | Delta) => { const container = document.createElement('div'); - container.innerHTML = normalizeHTML(html); + if (typeof htmlOrContents === 'string') { + container.innerHTML = normalizeHTML(htmlOrContents); + } document.body.appendChild(container); const quill = new Quill(container, { registry: createRegistry([ @@ -54,6 +56,9 @@ const createEditor = (html: string) => { SizeClass, ]), }); + if (typeof htmlOrContents !== 'string') { + quill.setContents(htmlOrContents); + } return quill.editor; }; @@ -1246,6 +1251,25 @@ describe('Editor', () => { ); }); + test('collapsible spaces', () => { + expect( + createEditor('

123 123 123

').getHTML( + 0, + 11, + ), + ).toEqual('123 123 123'); + + expect(createEditor(new Delta().insert('1 2\n')).getHTML(0, 5)).toEqual( + '1   2', + ); + + expect( + createEditor( + new Delta().insert(' 123', { bold: true }).insert('\n'), + ).getHTML(0, 5), + ).toEqual('  123'); + }); + test('mixed list', () => { const editor = createEditor( `