Skip to content

Commit

Permalink
Fix unwanted additional spaces added around pasted text on Windows (#…
Browse files Browse the repository at this point in the history
…33607)

* Remove the whitespace before the fragments that are provided by windows

See https://discuss.prosemirror.net/t/space-added-on-paste/1274/7

* Extract to util

* 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.

* Make both regexes literal
  • Loading branch information
getdave authored Aug 12, 2021
1 parent 1e50cab commit db73369
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ export function usePasteHandler( props ) {
}
}

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

event.preventDefault();

// Allows us to ask for this information when we get a report.
Expand Down Expand Up @@ -222,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 = /.*<!--StartFragment-->/s;
const endReg = /<!--EndFragment-->.*/s;

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

0 comments on commit db73369

Please sign in to comment.