From a7c5b9ba7685b4bfed732d8090508437737539aa Mon Sep 17 00:00:00 2001 From: Zihua Li <635902+luin@users.noreply.github.com> Date: Tue, 27 Aug 2024 21:47:19 +0800 Subject: [PATCH] Convert non-breaking spaces to regular spaces --- packages/quill/src/modules/clipboard.ts | 14 +++++++------- .../quill/test/unit/modules/clipboard.spec.ts | 18 +++++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/quill/src/modules/clipboard.ts b/packages/quill/src/modules/clipboard.ts index e4c3f755b5..21ce8c8267 100644 --- a/packages/quill/src/modules/clipboard.ts +++ b/packages/quill/src/modules/clipboard.ts @@ -624,6 +624,9 @@ function matchTable( return delta; } +const NBSP = '\u00a0'; +const SPACE_EXCLUDE_NBSP = `[^\\S${NBSP}]`; + function matchText(node: HTMLElement, delta: Delta, scroll: ScrollBlot) { // @ts-expect-error let text = node.data; @@ -639,12 +642,8 @@ function matchText(node: HTMLElement, delta: Delta, scroll: ScrollBlot) { ) { return delta; } - const replacer = (collapse: unknown, match: string) => { - const replaced = match.replace(/[^\u00a0]/g, ''); // \u00a0 is nbsp; - return replaced.length < 1 && collapse ? ' ' : replaced; - }; text = text.replace(/\r\n/g, ' ').replace(/\n/g, ' '); - text = text.replace(/\s\s+/g, replacer.bind(replacer, true)); // collapse whitespace + text = text.replace(new RegExp(`${SPACE_EXCLUDE_NBSP}{2,}`), ' '); // collapse whitespace if ( (node.previousSibling == null && node.parentElement != null && @@ -652,7 +651,7 @@ function matchText(node: HTMLElement, delta: Delta, scroll: ScrollBlot) { (node.previousSibling instanceof Element && isLine(node.previousSibling, scroll)) ) { - text = text.replace(/^\s+/, replacer.bind(replacer, false)); + text = text.replace(new RegExp(`^${SPACE_EXCLUDE_NBSP}+`), ''); } if ( (node.nextSibling == null && @@ -660,8 +659,9 @@ function matchText(node: HTMLElement, delta: Delta, scroll: ScrollBlot) { isLine(node.parentElement, scroll)) || (node.nextSibling instanceof Element && isLine(node.nextSibling, scroll)) ) { - text = text.replace(/\s+$/, replacer.bind(replacer, false)); + text = text.replace(new RegExp(`${SPACE_EXCLUDE_NBSP}+$`), ''); } + text = text.replaceAll(NBSP, ' '); } return delta.insert(text); } diff --git a/packages/quill/test/unit/modules/clipboard.spec.ts b/packages/quill/test/unit/modules/clipboard.spec.ts index 0ba7c159ed..7df30e7f7f 100644 --- a/packages/quill/test/unit/modules/clipboard.spec.ts +++ b/packages/quill/test/unit/modules/clipboard.spec.ts @@ -256,19 +256,23 @@ describe('Clipboard', () => { const html = '1 2'; const delta = createClipboard().convert({ html }); expect(delta).toEqual( - new Delta() - .insert('0\u00a0') - .insert('1', { bold: true }) - .insert('\u00a02'), + new Delta().insert('0 ').insert('1', { bold: true }).insert(' 2'), ); }); test('consecutive intentional whitespace', () => { const html = '  1  '; const delta = createClipboard().convert({ html }); - expect(delta).toEqual( - new Delta().insert('\u00a0\u00a01\u00a0\u00a0', { bold: true }), - ); + expect(delta).toEqual(new Delta().insert(' 1 ', { bold: true })); + }); + + test('intentional whitespace at line start/end', () => { + expect( + createClipboard().convert({ html: '

0  

  2

' }), + ).toEqual(new Delta().insert('0 \n 2')); + expect( + createClipboard().convert({ html: '

 2

' }), + ).toEqual(new Delta().insert('0 \n 2')); }); test('newlines between inline elements', () => {