Skip to content

Commit

Permalink
Editor: Strip newlines via onChange value assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jul 6, 2016
1 parent 3c7789d commit 1213941
Showing 1 changed file with 1 addition and 33 deletions.
34 changes: 1 addition & 33 deletions client/post-editor/editor-title/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default React.createClass( {
return;
}

this.setTitle( event.target.value );
this.setTitle( event.target.value.replace( /[\r\n]+/g, ' ' ) );
this.props.onChange( event );
},

Expand All @@ -94,37 +94,6 @@ export default React.createClass( {
this.onChange( event );
},

stripPasteNewlines( event ) {
// `window.clipboardData` deprecated as of Microsoft Edge
// See: https://msdn.microsoft.com/library/ms535220(v=vs.85).aspx
const clipboard = event.clipboardData || window.clipboardData;
if ( ! clipboard ) {
return;
}

event.preventDefault();

let text = clipboard.getData( 'Text' ) || clipboard.getData( 'text/plain' );
if ( ! text ) {
return;
}

// Replace any newline characters with a single space
text = text.replace( /[\r\n]+/g, ' ' );

// Splice trimmed text into current title selection
const { value, selectionStart, selectionEnd } = event.target;
const valueChars = value.split( '' );
valueChars.splice( selectionStart, selectionEnd - selectionStart, text );
const newTitle = valueChars.join( '' );

// To preserve caret location, we track intended selection point to be
// restored after next render pass
this.selectionStart = selectionStart + text.length;

this.setTitle( newTitle );
},

preventEnterKeyPress( event ) {
if ( 'Enter' === event.key || 13 === event.charCode ) {
event.preventDefault();
Expand Down Expand Up @@ -154,7 +123,6 @@ export default React.createClass( {
placeholder={ this.translate( 'Title' ) }
onChange={ this.onChange }
onBlur={ this.onBlur }
onPaste={ this.stripPasteNewlines }
onKeyPress={ this.preventEnterKeyPress }
autoFocus={ isNew && ! isMobile() }
value={ post ? post.title : '' }
Expand Down

0 comments on commit 1213941

Please sign in to comment.