Skip to content

Commit

Permalink
Pasting: Fix performance regression due to removeWindowsFragments
Browse files Browse the repository at this point in the history
Fixes #41826.

removeWindowsFragments was running two String#replace with regular
expressions made computationally expensive due to the use of `.*` and
the `s / dotAll` flag, resulting in severe performance degradations when
handling larger strings of HTML.

The solution is to manually trim the strings via a combination of
String#indexOf and String#substring.
  • Loading branch information
mcsf committed Jun 23, 2022
1 parent caba4dc commit 03f76c0
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,26 @@ export function usePasteHandler( props ) {
}

/**
* Normalizes a given string of HTML to remove the Windows specific "Fragment" comments
* and any preceeding and trailing whitespace.
* Normalizes a given string of HTML to remove the Windows-specific "Fragment"
* comments and any preceeding and trailing content.
*
* @param {string} html the html to be normalized
* @return {string} the normalized html
*/
function removeWindowsFragments( html ) {
const startReg = /.*<!--StartFragment-->/s;
const endReg = /<!--EndFragment-->.*/s;
const startStr = '<!--StartFragment-->';
const startIdx = html.indexOf( startStr );
if ( startIdx > -1 ) {
html = html.substring( startIdx + startStr.length );
}

const endStr = '<!--EndFragment-->';
const endIdx = html.indexOf( endStr );
if ( endIdx > -1 ) {
html = html.substring( 0, endIdx );
}

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

/**
Expand Down

0 comments on commit 03f76c0

Please sign in to comment.