From 4527b2cbb462da2178ee4e4e2c584d813c408ffe Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Thu, 2 Apr 2015 21:10:28 +0000 Subject: [PATCH] Press This: prevent pasting of HTML in the title H2. Insert the clipboard text instead. Fixes #31768. git-svn-id: https://develop.svn.wordpress.org/trunk@31987 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/js/press-this.js | 37 +++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/wp-admin/js/press-this.js b/src/wp-admin/js/press-this.js index fb05f94d1d..2f1a60254b 100644 --- a/src/wp-admin/js/press-this.js +++ b/src/wp-admin/js/press-this.js @@ -529,12 +529,45 @@ } }).on( 'keyup', function() { saveAlert = true; - }).on( 'paste', function() { + }).on( 'paste', function( event ) { + var text, range, + clipboard = event.originalEvent.clipboardData || window.clipboardData; + + if ( clipboard ) { + try{ + text = clipboard.getData( 'Text' ) || clipboard.getData( 'text/plain' ); + + if ( text ) { + text = $.trim( text.replace( /\s+/g, ' ' ) ); + + if ( window.getSelection ) { + range = window.getSelection().getRangeAt(0); + + if ( range ) { + if ( ! range.collapsed ) { + range.deleteContents(); + } + + range.insertNode( document.createTextNode( text ) ); + } + } else if ( document.selection ) { + range = document.selection.createRange(); + + if ( range ) { + range.text = text; + } + } + } + } catch ( er ) {} + + event.preventDefault(); + } + saveAlert = true; setTimeout( function() { $titleField.text( getTitleText() ); - }, 100 ); + }, 50 ); }); if ( $titleField.text() || $titleField.html() ) {