diff --git a/src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js b/src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js index 582b42965b..8343e632b9 100644 --- a/src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js +++ b/src/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js @@ -53,7 +53,7 @@ } } ); - function firstNode( node ) { + function firstTextNode( node ) { var parent = editor.dom.getParent( node, 'p' ), child; @@ -74,7 +74,11 @@ } if ( ! child.data ) { - child = child.nextSibling; + if ( child.nextSibling && child.nextSibling.nodeType === 3 ) { + child = child.nextSibling; + } else { + child = null; + } } return child; @@ -86,7 +90,7 @@ parent, text; - if ( ! node || firstNode( node ) !== node ) { + if ( ! node || firstTextNode( node ) !== node ) { return; } @@ -123,11 +127,9 @@ } function watchEnter() { - var selection = editor.selection, - rng = selection.getRng(), - offset = rng.startOffset, + var rng = editor.selection.getRng(), start = rng.startContainer, - node = firstNode( start ), + node = firstTextNode( start ), i = enterPatterns.length, text, pattern; @@ -148,12 +150,8 @@ return; } - if ( node === start ) { - if ( tinymce.trim( text ) === pattern.start ) { - return; - } - - offset = Math.max( 0, offset - pattern.start.length ); + if ( node === start && tinymce.trim( text ) === pattern.start ) { + return; } refNode = node; @@ -166,7 +164,7 @@ editor.undoManager.transact( function() { editor.formatter.apply( refPattern.format, {}, refNode ); - refNode.deleteData( 0, refPattern.start.length ); + refNode.replaceData( 0, refNode.data.length, tinymce.trim( refNode.data.slice( refPattern.start.length ) ) ); } ); // We need to wait for native events to be triggered. diff --git a/tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js b/tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js index 2f72339b8d..3644c621cb 100644 --- a/tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js +++ b/tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js @@ -150,6 +150,7 @@ editor.selection.setCursorLocation( editor.$( 'p' )[0].firstChild, 8 ); type( '\n', function() { + assert.equal( editor.$( 'h3' )[0].firstChild.data, 'test' ); assert.equal( editor.getContent(), '

test

\n

 

' ); }, assert.async() ); } );