From 004607e06aa965c0d7abbd694c0560dae649f54a Mon Sep 17 00:00:00 2001 From: Matthew Kevins Date: Fri, 22 Feb 2019 17:27:52 +1000 Subject: [PATCH] Add try/catch fallback to plain text for pasteHandler --- .../src/components/rich-text/index.native.js | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/editor/src/components/rich-text/index.native.js b/packages/editor/src/components/rich-text/index.native.js index f7b6e92c494125..5058712876a536 100644 --- a/packages/editor/src/components/rich-text/index.native.js +++ b/packages/editor/src/components/rich-text/index.native.js @@ -42,6 +42,27 @@ const unescapeSpaces = ( text ) => { return text.replace( / | /gi, ' ' ); }; +/** + * Calls {@link pasteHandler} with a fallback to plain text when HTML processing + * results in errors + * + * @param {Object} [options] The options to pass to {@link pasteHandler} + * + * @return {Array|string} A list of blocks or a string, depending on + * `handlerMode`. + */ +const saferPasteHandler = ( options ) => { + try { + return pasteHandler( options ); + } catch ( error ) { + window.console.log( 'Pasting HTML failed:', error ); + window.console.log( 'HTML:', options.HTML ); + window.console.log( 'Falling back to plain text.' ); + // fallback to plain text + return pasteHandler( { ...options, HTML: '' } ); + } +}; + const gutenbergFormatNamesToAztec = { 'core/bold': 'bold', 'core/italic': 'italic', @@ -308,7 +329,7 @@ export class RichText extends Component { mode = 'AUTO'; } - const pastedContent = pasteHandler( { + const pastedContent = saferPasteHandler( { HTML: pastedHtml, plainText: pastedText, mode,