From d4657d8776f9d8983cfe887d7aabfd022eea93ee Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Thu, 22 Jul 2021 11:58:19 +0100 Subject: [PATCH] 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 97009764209eb..71339cc64c342 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 f1385eda82e26..d6fb8e00c34c2 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. *