Skip to content

Commit

Permalink
Extract to util
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Jul 22, 2021
1 parent 245eecd commit d4657d8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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( '.*<!--StartFragment-->', 's' );
const endReg = new RegExp( '<!--EndFragment-->.*', 's' );

return html.replace( startReg, '' ).replace( endReg, '' );
}

export function usePasteHandler( props ) {
const propsRef = useRef( props );
propsRef.current = props;
Expand Down Expand Up @@ -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();

Expand Down
14 changes: 14 additions & 0 deletions packages/block-editor/src/components/rich-text/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( '.*<!--StartFragment-->', 's' );
const endReg = new RegExp( '<!--EndFragment-->.*', 's' );

return html.replace( startReg, '' ).replace( endReg, '' );
}

/**
* Get the multiline tag based on the multiline prop.
*
Expand Down

0 comments on commit d4657d8

Please sign in to comment.