Skip to content

Commit

Permalink
Pasting: Fix performance regression due to removeWindowsFragments (#4…
Browse files Browse the repository at this point in the history
…1907)

* Pasting: Fix performance regression due to removeWindowsFragments

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.

* removeWindowsFragments: bail early if no StartFragment found
  • Loading branch information
mcsf authored Jun 23, 2022
1 parent 79501fc commit 8df4bcb
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,29 @@ 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 );
} else {
// No point looking for EndFragment
return html;
}

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 8df4bcb

Please sign in to comment.