Skip to content

Commit

Permalink
Pasting: Respect plain-text pastes
Browse files Browse the repository at this point in the history
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 `<pre>`, will be handled properly and yield a valid Gallery
block:

  <meta charset='utf-8'><span style="…">[gallery ids="10,11,9"]</span>
  • Loading branch information
mcsf committed Nov 28, 2017
1 parent 8d3e230 commit 086dd25
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -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,
} );
Expand Down

0 comments on commit 086dd25

Please sign in to comment.