From 245eecd7e23ee410aa3378afc7a7ac3aeafaa9d0 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 21 Jul 2021 19:25:57 +0100 Subject: [PATCH 1/4] Remove the whitespace before the fragments that are provided by windows See https://discuss.prosemirror.net/t/space-added-on-paste/1274/7 --- .../src/components/rich-text/use-paste-handler.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/block-editor/src/components/rich-text/use-paste-handler.js b/packages/block-editor/src/components/rich-text/use-paste-handler.js index 511a62300e36c0..97009764209eb2 100644 --- a/packages/block-editor/src/components/rich-text/use-paste-handler.js +++ b/packages/block-editor/src/components/rich-text/use-paste-handler.js @@ -21,6 +21,13 @@ import { filePasteHandler } from './file-paste-handler'; import { addActiveFormats, isShortcode } from './utils'; import { splitValue } from './split-value'; +function normalizePastedHtml( html ) { + const startReg = new RegExp( '.*', 's' ); + const endReg = new RegExp( '.*', 's' ); + + return html.replace( startReg, '' ).replace( endReg, '' ); +} + export function usePasteHandler( props ) { const propsRef = useRef( props ); propsRef.current = props; @@ -69,6 +76,8 @@ export function usePasteHandler( props ) { } } + html = normalizePastedHtml( html ); + event.preventDefault(); // Allows us to ask for this information when we get a report. From d4657d8776f9d8983cfe887d7aabfd022eea93ee Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 22 Jul 2021 11:58:19 +0100 Subject: [PATCH 2/4] Extract to util --- .../src/components/rich-text/use-paste-handler.js | 12 +++--------- .../block-editor/src/components/rich-text/utils.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/block-editor/src/components/rich-text/use-paste-handler.js b/packages/block-editor/src/components/rich-text/use-paste-handler.js index 97009764209eb2..71339cc64c3429 100644 --- a/packages/block-editor/src/components/rich-text/use-paste-handler.js +++ b/packages/block-editor/src/components/rich-text/use-paste-handler.js @@ -18,16 +18,9 @@ import { isURL } from '@wordpress/url'; * Internal dependencies */ import { filePasteHandler } from './file-paste-handler'; -import { addActiveFormats, isShortcode } from './utils'; +import { addActiveFormats, isShortcode, normalizeCopiedHtml } from './utils'; import { splitValue } from './split-value'; -function normalizePastedHtml( html ) { - const startReg = new RegExp( '.*', 's' ); - const endReg = new RegExp( '.*', 's' ); - - return html.replace( startReg, '' ).replace( endReg, '' ); -} - export function usePasteHandler( props ) { const propsRef = useRef( props ); propsRef.current = props; @@ -76,7 +69,8 @@ export function usePasteHandler( props ) { } } - html = normalizePastedHtml( html ); + // Remove OS-specific metadata appended within copied HTML text. + html = normalizeCopiedHtml( html ); event.preventDefault(); diff --git a/packages/block-editor/src/components/rich-text/utils.js b/packages/block-editor/src/components/rich-text/utils.js index f1385eda82e26f..d6fb8e00c34c2b 100644 --- a/packages/block-editor/src/components/rich-text/utils.js +++ b/packages/block-editor/src/components/rich-text/utils.js @@ -17,6 +17,20 @@ export function addActiveFormats( value, activeFormats ) { } } +/** + * Normalizes a given string of HTML to remove the Windows specific "Fragment" comments + * and any preceeding and trailing whitespace. + * + * @param {string} html the html to be normalized + * @return {string} the normalized html + */ +export function normalizeCopiedHtml( html ) { + const startReg = new RegExp( '.*', 's' ); + const endReg = new RegExp( '.*', 's' ); + + return html.replace( startReg, '' ).replace( endReg, '' ); +} + /** * Get the multiline tag based on the multiline prop. * From 838ce44c0afe80566fad89a22b7ac47a9b35a172 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Wed, 4 Aug 2021 09:45:50 +0100 Subject: [PATCH 3/4] Avoid creating public APIs for util and improve function naming for clarity As we're not sure about this fix let's not create a public API via utils. Instead revert to inlining. Also renaming the function to make it's purpose clearer. It's not a generic normalizing function so let's make it obvious this is Windows specific. --- .../components/rich-text/use-paste-handler.js | 20 ++++++++++++++++--- .../src/components/rich-text/utils.js | 14 ------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/block-editor/src/components/rich-text/use-paste-handler.js b/packages/block-editor/src/components/rich-text/use-paste-handler.js index 71339cc64c3429..4b685924e4865b 100644 --- a/packages/block-editor/src/components/rich-text/use-paste-handler.js +++ b/packages/block-editor/src/components/rich-text/use-paste-handler.js @@ -18,7 +18,7 @@ import { isURL } from '@wordpress/url'; * Internal dependencies */ import { filePasteHandler } from './file-paste-handler'; -import { addActiveFormats, isShortcode, normalizeCopiedHtml } from './utils'; +import { addActiveFormats, isShortcode } from './utils'; import { splitValue } from './split-value'; export function usePasteHandler( props ) { @@ -69,8 +69,8 @@ export function usePasteHandler( props ) { } } - // Remove OS-specific metadata appended within copied HTML text. - html = normalizeCopiedHtml( html ); + // Remove Windows-specific metadata appended within copied HTML text. + html = removeWindowsFragments( html ); event.preventDefault(); @@ -225,3 +225,17 @@ export function usePasteHandler( props ) { }; }, [] ); } + +/** + * Normalizes a given string of HTML to remove the Windows specific "Fragment" comments + * and any preceeding and trailing whitespace. + * + * @param {string} html the html to be normalized + * @return {string} the normalized html + */ +function removeWindowsFragments( html ) { + const startReg = new RegExp( '.*', 's' ); + const endReg = new RegExp( '.*', 's' ); + + return html.replace( startReg, '' ).replace( endReg, '' ); +} diff --git a/packages/block-editor/src/components/rich-text/utils.js b/packages/block-editor/src/components/rich-text/utils.js index d6fb8e00c34c2b..f1385eda82e26f 100644 --- a/packages/block-editor/src/components/rich-text/utils.js +++ b/packages/block-editor/src/components/rich-text/utils.js @@ -17,20 +17,6 @@ export function addActiveFormats( value, activeFormats ) { } } -/** - * Normalizes a given string of HTML to remove the Windows specific "Fragment" comments - * and any preceeding and trailing whitespace. - * - * @param {string} html the html to be normalized - * @return {string} the normalized html - */ -export function normalizeCopiedHtml( html ) { - const startReg = new RegExp( '.*', 's' ); - const endReg = new RegExp( '.*', 's' ); - - return html.replace( startReg, '' ).replace( endReg, '' ); -} - /** * Get the multiline tag based on the multiline prop. * From 6a0db13642c0f9bc92972dd235709397cbb11020 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 12 Aug 2021 12:12:13 +0100 Subject: [PATCH 4/4] Make both regexes literal --- .../src/components/rich-text/use-paste-handler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/rich-text/use-paste-handler.js b/packages/block-editor/src/components/rich-text/use-paste-handler.js index 4b685924e4865b..c12e84720649f8 100644 --- a/packages/block-editor/src/components/rich-text/use-paste-handler.js +++ b/packages/block-editor/src/components/rich-text/use-paste-handler.js @@ -234,8 +234,8 @@ export function usePasteHandler( props ) { * @return {string} the normalized html */ function removeWindowsFragments( html ) { - const startReg = new RegExp( '.*', 's' ); - const endReg = new RegExp( '.*', 's' ); + const startReg = /.*/s; + const endReg = /.*/s; return html.replace( startReg, '' ).replace( endReg, '' ); }