Skip to content

Commit

Permalink
Add try/catch fallback to plain text for pasteHandler (#14044)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkevins authored and hypest committed Feb 22, 2019
1 parent e485688 commit 331b68d
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/editor/src/components/rich-text/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -309,7 +330,7 @@ export class RichText extends Component {
mode = 'AUTO';
}

const pastedContent = pasteHandler( {
const pastedContent = saferPasteHandler( {
HTML: pastedHtml,
plainText: pastedText,
mode,
Expand Down

0 comments on commit 331b68d

Please sign in to comment.