Ticket #33300: 33300.patch
File 33300.patch, 6.8 KB (added by , 9 years ago) |
---|
-
src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
12 12 */ 13 13 ( function( tinymce, setTimeout ) { 14 14 tinymce.PluginManager.add( 'wptextpattern', function( editor ) { 15 var VK = tinymce.util.VK, 16 spacePatterns = [ 17 { regExp: /^[*-]\s/, cmd: 'InsertUnorderedList' }, 18 { regExp: /^1[.)]\s/, cmd: 'InsertOrderedList' } 19 ], 20 enterPatterns = [ 21 { start: '##', format: 'h2' }, 22 { start: '###', format: 'h3' }, 23 { start: '####', format: 'h4' }, 24 { start: '#####', format: 'h5' }, 25 { start: '######', format: 'h6' }, 26 { start: '>', format: 'blockquote' } 27 ], 28 canUndo, refNode, refPattern; 15 var VK = tinymce.util.VK; 16 17 var spacePatterns = [ 18 { regExp: /^[*-]\s/, cmd: 'InsertUnorderedList' }, 19 { regExp: /^1[.)]\s/, cmd: 'InsertOrderedList' } 20 ]; 21 22 var enterPatterns = [ 23 { start: '##', format: 'h2' }, 24 { start: '###', format: 'h3' }, 25 { start: '####', format: 'h4' }, 26 { start: '#####', format: 'h5' }, 27 { start: '######', format: 'h6' }, 28 { start: '>', format: 'blockquote' } 29 ]; 30 31 var inlinePatterns = [ 32 { start: '*', end: '*', format: 'italic' }, 33 { start: '**', end: '**', format: 'bold' }, 34 { start: '_', end: '_', format: 'italic' }, 35 { start: '__', end: '__', format: 'bold' }, 36 { start: '`', end: '`', format: 'code' } 37 ]; 38 39 var canUndo; 40 var refNode; 41 var refPattern; 42 var chars = []; 43 var bookmark; 44 45 tinymce.each( inlinePatterns, function( pattern ) { 46 tinymce.each( ( pattern.start + pattern.end ).split( '' ), function( c ) { 47 if ( tinymce.inArray( chars, c ) === -1 ) { 48 chars.push( c ); 49 } 50 } ); 51 } ); 29 52 30 53 editor.on( 'selectionchange', function() { 31 54 canUndo = null; … … 48 71 space(); 49 72 } else if ( event.keyCode === VK.ENTER && ! VK.modifierPressed( event ) ) { 50 73 enter(); 74 } else if ( event.keyCode > 47 || ( event.keyCode >= 91 && event.keyCode <= 93 ) /* Ignore special keys. */ ) { 75 inline(); 51 76 } 52 77 } ); 53 78 79 function inline() { 80 var rng = editor.selection.getRng(); 81 var node = rng.startContainer; 82 var offset = rng.startOffset; 83 var startOffset; 84 var endOffset; 85 var pattern; 86 var format; 87 88 if ( node.nodeType !== 3 || ! node.data.length || ! offset ) { 89 return; 90 } 91 92 if ( tinymce.inArray( chars, node.data.charAt( offset - 1 ) ) === -1 ) { 93 return; 94 } 95 96 function findStart( node ) { 97 var i = inlinePatterns.length; 98 var offset; 99 100 while ( i-- ) { 101 pattern = inlinePatterns[ i ]; 102 offset = node.data.indexOf( pattern.end ); 103 104 if ( offset !== -1 ) { 105 return offset; 106 } 107 } 108 } 109 110 startOffset = findStart( node ); 111 endOffset = node.data.lastIndexOf( pattern.end ); 112 113 if ( startOffset === endOffset || endOffset === -1 ) { 114 return; 115 } 116 117 if ( endOffset - startOffset <= pattern.start.length ) { 118 return; 119 } 120 121 if ( node.data.slice( startOffset + pattern.start.length, endOffset ).indexOf( pattern.start.slice( 0, 1 ) ) !== -1 ) { 122 return; 123 } 124 125 format = editor.formatter.get( pattern.format ); 126 127 if ( format && format[0].inline ) { 128 editor.undoManager.add(); 129 130 editor.undoManager.transact( function() { 131 var hair; 132 133 bookmark = editor.selection.getBookmark(); 134 135 node.appendData( '\u200A' ); 136 137 node = node.splitText( startOffset ); 138 hair = node.splitText( offset - startOffset ); 139 140 node.deleteData( 0, pattern.start.length ); 141 node.deleteData( node.data.length - pattern.end.length, pattern.end.length ); 142 143 editor.formatter.apply( pattern.format, {}, node ); 144 145 hair.remove(); 146 } ); 147 148 // We need to wait for native events to be triggered. 149 setTimeout( function() { 150 canUndo = 'space'; 151 } ); 152 } 153 } 154 54 155 function firstTextNode( node ) { 55 156 var parent = editor.dom.getParent( node, 'p' ), 56 157 child; … … 157 258 } 158 259 159 260 function ltrim( text ) { 160 if ( text ) { 161 return text.replace( /^\s+/, '' ); 162 } 163 164 return ''; 261 return text ? text.replace( /^\s+/, '' ) : ''; 165 262 } 166 263 167 264 function enter() { -
tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js
14 14 '0': 48, '1': 49, '2': 50, '3': 51, '4': 52, '5': 53, '6': 54, '7': 55, '8': 56, '9': 57,'a': 65, 'b': 66, 'c': 67, 15 15 'd': 68, 'e': 69, 'f': 70, 'g': 71, 'h': 72, 'i': 73, 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78, 'o': 79, 'p': 80, 'q': 81, 16 16 'r': 82, 's': 83, 't': 84, 'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, ' ': 32, ',': 188, '-': 189, '.': 190, '/': 191, '\\': 220, 17 '[': 219, ']': 221, '\'': 222, ';': 186, '=': 187, ')': 41 17 '[': 219, ']': 221, '\'': 222, ';': 186, '=': 187, ')': 41, 18 '*': 48 // Anything will do. 18 19 }; 19 20 20 21 return lookup[String.fromCharCode(charCode)]; … … 277 278 assert.equal( editor.getContent(), '<p>### </p>\n<p> </p>' ); 278 279 }, assert.async() ); 279 280 } ); 281 282 QUnit.test( 'Inline: single.', function( assert ) { 283 type( '*test*', function() { 284 assert.equal( editor.getContent(), '<p><em>test</em></p>' ); 285 assert.equal( editor.selection.getRng().startOffset, 1 ); 286 assert.notEqual( editor.getContent( { format: 'raw' } ).indexOf( 'em><span' ), -1 ); 287 }, assert.async() ); 288 } ); 289 290 QUnit.test( 'Inline: double.', function( assert ) { 291 type( '**test**', function() { 292 assert.equal( editor.getContent(), '<p><strong>test</strong></p>' ); 293 assert.equal( editor.selection.getRng().startOffset, 1 ); 294 assert.notEqual( editor.getContent( { format: 'raw' } ).indexOf( 'strong><span' ), -1 ); 295 }, assert.async() ); 296 } ); 297 298 QUnit.test( 'Inline: after typing.', function( assert ) { 299 editor.setContent( '<p>test test test</p>' ); 300 editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 5 ); 301 302 type( '**', function() { 303 editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 11 ); 304 }, '**', function() { 305 assert.equal( editor.getContent(), '<p>test <strong>test</strong> test</p>' ); 306 assert.equal( editor.selection.getRng().startOffset, 1 ); 307 assert.notEqual( editor.getContent( { format: 'raw' } ).indexOf( 'strong><span' ), -1 ); 308 }, assert.async() ); 309 } ); 310 311 QUnit.test( 'Inline: no change.', function( assert ) { 312 type( '******', function() { 313 assert.equal( editor.getContent(), '<p>******</p>' ); 314 }, assert.async() ); 315 } ); 280 316 } )( window.jQuery, window.QUnit, window.tinymce, window.setTimeout );