From 086dd256dc6c02ad249b0153633d68d05289f6bd Mon Sep 17 00:00:00 2001 From: Miguel Fonseca Date: Wed, 22 Nov 2017 11:22:35 +0000 Subject: [PATCH] Pasting: Respect plain-text pastes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When pasting plain-text-only clipboard data, use that plain-text data in the paste processing. This overrides the default behavior whereby an HTML-encoded counterpart of the plain text is used for processing. Previously, pasting the following text as a plain-text attachment: [gallery ids="1,2,3"] Would be processed as: [gallery ids="1,2,3"] Thus affecting the correct parsing of shortcodes. With this change, parsing of shortcodes works when pasting a plain-text object — like the one above — but also when pasting rich-text objects. For instance, copying a shortcode snippet from a rich-text page, even if not wrapped with a `
`, will be handled properly and yield a valid Gallery
block:

  [gallery ids="10,11,9"]
---
 blocks/editable/index.js | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/blocks/editable/index.js b/blocks/editable/index.js
index 69b43929c1b0d..c269afb53da3e 100644
--- a/blocks/editable/index.js
+++ b/blocks/editable/index.js
@@ -297,6 +297,9 @@ export default class Editable extends Component {
 		}
 
 		this.pastedPlainText = dataTransfer ? dataTransfer.getData( 'text/plain' ) : '';
+		this.isPlainTextPaste = ( dataTransfer &&
+			dataTransfer.types.length === 1 &&
+			dataTransfer.types[ 0 ] === 'text/plain' );
 	}
 
 	/**
@@ -308,8 +311,9 @@ export default class Editable extends Component {
 	 * @param {PrePasteProcessEvent} event The PrePasteProcess event as triggered by tinyMCE.
 	 */
 	onPastePreProcess( event ) {
+		const HTML = this.isPlainTextPaste ? this.pastedPlainText : event.content;
 		// Allows us to ask for this information when we get a report.
-		window.console.log( 'Received HTML:\n\n', this.pastedContent || event.content );
+		window.console.log( 'Received HTML:\n\n', this.pastedContent || HTML );
 		window.console.log( 'Received plain text:\n\n', this.pastedPlainText );
 
 		// There is a selection, check if a link is pasted.
@@ -345,7 +349,7 @@ export default class Editable extends Component {
 		}
 
 		const content = rawHandler( {
-			HTML: this.pastedContent || event.content,
+			HTML: this.pastedContent || HTML,
 			plainText: this.pastedPlainText,
 			mode,
 		} );