Ticket #36585: 36585.2.diff
File 36585.2.diff, 5.6 KB (added by , 8 years ago) |
---|
-
tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
6 6 return; 7 7 } 8 8 9 function mceType(chr) { 10 var editor = tinymce.activeEditor, keyCode, charCode, evt, startElm, rng, startContainer, startOffset, textNode; 9 function mceType( chr, eventType ) { 10 var editor = tinymce.activeEditor, 11 doKeyDown = ! eventType || /\bkeydown\b/.test( eventType ), 12 doKeyPress = ! eventType || /\bkeypress\b/.test( eventType ), 13 doKeyUp = ! eventType || /\bkeyup\b/.test( eventType ), 14 keyCode, charCode, evt, 15 startElm, rng, startContainer, startOffset, 16 textNode; 11 17 12 18 function charCodeToKeyCode(charCode) { 13 19 var lookup = { … … 56 62 evt = evt || {keyCode: keyCode, charCode: charCode}; 57 63 58 64 startElm = editor.selection.getStart(); 59 fakeEvent(startElm, 'keydown', evt); 60 fakeEvent(startElm, 'keypress', evt); 65 if ( doKeyDown ) { 66 fakeEvent(startElm, 'keydown', evt); 67 } 68 if ( doKeyPress ) { 69 fakeEvent(startElm, 'keypress', evt); 70 } 61 71 62 if ( !evt.isDefaultPrevented()) {72 if ( ( doKeyDown || doKeyPress ) && ! evt.isDefaultPrevented() ) { 63 73 if (keyCode === 8) { 64 74 if (editor.getDoc().selection) { 65 75 rng = editor.getDoc().selection.createRange(); … … 109 119 } 110 120 } 111 121 112 fakeEvent(startElm, 'keyup', evt); 122 if ( doKeyUp ) { 123 fakeEvent( startElm, 'keyup', evt ); 124 } 113 125 } 114 126 115 127 function type() { … … 219 231 }, assert.async() ); 220 232 } ); 221 233 222 QUnit.test( 'Only transform when atthe cursor is at the start.', function( assert ) {234 QUnit.test( 'Only transform when the cursor is at the start.', function( assert ) { 223 235 editor.setContent( '<p>* test</p>' ); 224 236 editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 6 ); 225 237 … … 228 240 }, assert.async() ); 229 241 } ); 230 242 243 QUnit.test( 'Transform when space pressed along with another key. (1)', function( assert ) { 244 var done = assert.async(); 245 246 editor.setContent( '<p>*' ); 247 editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 1 ); 248 249 mceType( ' ', 'keydown,keypress' ); 250 mceType( 'a', 'keydown,keypress' ); 251 mceType( ' ', 'keyup' ); 252 mceType( 'a', 'keyup' ); 253 254 setTimeout( function() { 255 assert.equal( editor.getContent(), '<ul>\n<li>a</li>\n</ul>' ); 256 assert.equal( editor.selection.getRng().startOffset, 1 ); 257 done(); 258 } ); 259 } ); 260 261 QUnit.test( 'Transform when space pressed along with another key. (2)', function( assert ) { 262 var done = assert.async(); 263 264 editor.setContent( '<p>*' ); 265 editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 1 ); 266 267 mceType( ' ', 'keydown,keypress' ); 268 mceType( '0', 'keydown,keypress' ); 269 mceType( '0', 'keyup' ); 270 mceType( ' ', 'keyup' ); 271 272 setTimeout( function() { 273 assert.equal( editor.getContent(), '<ul>\n<li>0</li>\n</ul>' ); 274 assert.equal( editor.selection.getRng().startOffset, 1 ); 275 done(); 276 } ); 277 } ); 278 279 QUnit.test( 'Transform when space pressed along with another key, then backspace to undo.', function( assert ) { 280 editor.setContent( '<p>*' ); 281 editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 1 ); 282 283 mceType( ' ', 'keydown,keypress' ); 284 mceType( 'a', 'keydown,keypress' ); 285 mceType( ' ', 'keyup' ); 286 mceType( 'a', 'keyup' ); 287 288 type( '\b', function() { 289 assert.equal( editor.getContent(), '<p>* a</p>' ); 290 assert.equal( editor.selection.getRng().startOffset, 3 ); 291 }, assert.async() ); 292 } ); 293 231 294 QUnit.test( 'Backspace should undo the transformation.', function( assert ) { 232 295 editor.setContent( '<p>test</p>' ); 233 296 editor.selection.setCursorLocation(); -
src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
39 39 40 40 var canUndo; 41 41 var chars = []; 42 var isSpacePressed = false; 43 var keysPressedWithSpace = []; 42 44 43 45 tinymce.each( inlinePatterns, function( pattern ) { 44 46 tinymce.each( ( pattern.start + pattern.end ).split( '' ), function( c ) { … … 62 64 if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) { 63 65 enter(); 64 66 } 67 68 if ( event.keyCode === VK.SPACEBAR && ! event.ctrlKey && ! event.metaKey && ! event.altKey ) { 69 isSpacePressed = true; 70 } else if ( isSpacePressed && event.keyCode > 32 && event.keyCode < 127 ) { 71 keysPressedWithSpace.push( event.keyCode ); 72 } 65 73 }, true ); 66 74 67 75 editor.on( 'keyup', function( event ) { 68 76 if ( event.keyCode === VK.SPACEBAR && ! event.ctrlKey && ! event.metaKey && ! event.altKey ) { 69 77 space(); 78 isSpacePressed = false; 79 keysPressedWithSpace = []; 70 80 } else if ( event.keyCode > 47 && ! ( event.keyCode >= 91 && event.keyCode <= 93 ) ) { 71 81 inline(); 72 82 } … … 204 214 tinymce.each( spacePatterns, function( pattern ) { 205 215 var match = text.match( pattern.regExp ); 206 216 207 if ( ! match || rng.startOffset !== match[0].length ) {217 if ( ! match || rng.startOffset !== match[0].length + keysPressedWithSpace.length ) { 208 218 return; 209 219 } 210 220 … … 217 227 parent.appendChild( document.createElement( 'br' ) ); 218 228 } 219 229 220 editor.selection.setCursorLocation( parent ); 230 if ( keysPressedWithSpace.length === 0 ) { 231 editor.selection.setCursorLocation( parent ); 232 } 221 233 editor.execCommand( pattern.cmd ); 222 234 } ); 223 235