Ticket #33300: 33300.5.patch
File 33300.5.patch, 4.4 KB (added by , 9 years ago) |
---|
-
src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
26 26 { start: '#####', format: 'h5' }, 27 27 { start: '######', format: 'h6' }, 28 28 { start: '>', format: 'blockquote' }, 29 { regExp: /^\s*(?:(?:\* ?){3,}|(?:_ ?){3,}|(?:- ?){3,})\s*$/, element: 'hr' } 29 { regExp: /^\s*(?:(?:\* ?){3,}|(?:_ ?){3,}|(?:- ?){3,})\s*$/, element: 'hr' }, 30 { regExp: /^`{3,}$/, format: 'pre' } 30 31 ]; 31 32 32 33 var inlinePatterns = [ … … 60 61 } 61 62 62 63 if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) { 63 enter( );64 enter( event ); 64 65 } 65 66 }, true ); 66 67 … … 230 231 } ); 231 232 } 232 233 233 function enter( ) {234 function enter( event ) { 234 235 var rng = editor.selection.getRng(), 235 236 start = rng.startContainer, 237 offset = rng.startOffset, 236 238 node = firstTextNode( start ), 237 239 i = enterPatterns.length, 238 text, pattern, parent ;240 text, pattern, parent, match; 239 241 240 242 if ( ! node ) { 241 243 return; … … 250 252 break; 251 253 } 252 254 } else if ( enterPatterns[ i ].regExp ) { 253 if ( enterPatterns[ i ].regExp.test( text) ) {255 if ( match = text.match( enterPatterns[ i ].regExp ) ) { 254 256 pattern = enterPatterns[ i ]; 255 257 break; 256 258 } … … 265 267 return; 266 268 } 267 269 268 editor.once( 'keyup', function() {270 if ( match && pattern.format ) { 269 271 editor.undoManager.add(); 270 272 271 273 editor.undoManager.transact( function() { 272 if ( pattern.format ) { 273 editor.formatter.apply( pattern.format, {}, node ); 274 node.replaceData( 0, node.data.length, ltrim( node.data.slice( pattern.start.length ) ) ); 275 } else if ( pattern.element ) { 276 parent = node.parentNode && node.parentNode.parentNode; 274 editor.formatter.apply( pattern.format ); 277 275 278 if ( parent ) { 279 parent.replaceChild( document.createElement( pattern.element ), node.parentNode ); 280 } 276 parent = node.parentNode; 277 278 node.deleteData( 0, match[0].length ); 279 280 if ( ! parent.innerHTML ) { 281 parent.appendChild( document.createElement( 'br' ) ); 281 282 } 283 284 editor.selection.setCursorLocation( parent ); 282 285 } ); 283 286 284 287 // We need to wait for native events to be triggered. 285 288 setTimeout( function() { 286 289 canUndo = 'enter'; 287 290 } ); 288 } ); 291 292 event.preventDefault(); 293 event.stopImmediatePropagation(); 294 } else { 295 editor.once( 'keyup', function() { 296 editor.undoManager.add(); 297 298 editor.undoManager.transact( function() { 299 if ( pattern.format ) { 300 editor.formatter.apply( pattern.format, {}, node ); 301 302 if ( pattern.start ) { 303 node.replaceData( 0, node.data.length, ltrim( node.data.slice( pattern.start.length ) ) ); 304 } 305 } else if ( pattern.element ) { 306 parent = node.parentNode && node.parentNode.parentNode; 307 308 if ( parent ) { 309 parent.replaceChild( document.createElement( pattern.element ), node.parentNode ); 310 } 311 } 312 } ); 313 314 // We need to wait for native events to be triggered. 315 setTimeout( function() { 316 canUndo = 'enter'; 317 } ); 318 } ); 319 } 289 320 } 290 321 291 322 function ltrim( text ) { -
tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
311 311 }, assert.async() ); 312 312 } ); 313 313 314 QUnit.test( 'Horizontal Rule ', function( assert ) {314 QUnit.test( 'Horizontal Rule.', function( assert ) { 315 315 type( ' --- \n', function() { 316 316 assert.equal( editor.getContent(), '<hr />\n<p> </p>' ); 317 317 }, assert.async() ); 318 318 } ); 319 320 QUnit.test( 'Code block.', function( assert ) { 321 type( '```\ntest', function() { 322 assert.equal( editor.getContent(), '<pre>test</pre>' ); 323 }, assert.async() ); 324 } ); 325 326 QUnit.test( 'Code block with text.', function( assert ) { 327 editor.setContent( '<p>test</p>' ); 328 editor.selection.setCursorLocation(); 329 330 type( '```\n', function() { 331 assert.equal( editor.getContent(), '<pre>test</pre>' ); 332 }, assert.async() ); 333 } ); 319 334 } )( window.jQuery, window.QUnit, window.tinymce, window.setTimeout );