Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unwanted additional spaces added around pasted text on Windows #33607

Merged
merged 4 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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';

export function usePasteHandler( props ) {
Expand Down Expand Up @@ -69,6 +69,9 @@ export function usePasteHandler( props ) {
}
}

// Remove OS-specific metadata appended within copied HTML text.
html = normalizeCopiedHtml( html );

event.preventDefault();

// Allows us to ask for this information when we get a report.
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