TinyMCE: wptextpattern: Handle unconverted inline patterns

Make sure the right text is matched when it already contains characters of the pattern.
Adds two more unit tests.

Fixes #37693.


Built from https://develop.svn.wordpress.org/trunk@39075


git-svn-id: http://core.svn.wordpress.org/trunk@39017 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Ella Iseulde Van Dorpe 2016-11-01 20:06:36 +00:00
parent 5c7aa3c5b8
commit 84c108db02
4 changed files with 35 additions and 24 deletions

View File

@ -15,15 +15,27 @@
return;
}
/**
* Escapes characters for use in a Regular Expression.
*
* @param {String} string Characters to escape
*
* @return {String} Escaped characters
*/
function escapeRegExp( string ) {
return string.replace( /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&' );
}
tinymce.PluginManager.add( 'wptextpattern', function( editor ) {
var VK = tinymce.util.VK;
var settings = editor.settings.wptextpattern || {};
var spacePatterns = [
var spacePatterns = settings.space || [
{ regExp: /^[*-]\s/, cmd: 'InsertUnorderedList' },
{ regExp: /^1[.)]\s/, cmd: 'InsertOrderedList' }
];
var enterPatterns = [
var enterPatterns = settings.enter || [
{ start: '##', format: 'h2' },
{ start: '###', format: 'h3' },
{ start: '####', format: 'h4' },
@ -33,7 +45,7 @@
{ regExp: /^(-){3,}$/, element: 'hr' }
];
var inlinePatterns = [
var inlinePatterns = settings.inline || [
{ start: '`', end: '`', format: 'code' }
];
@ -81,40 +93,39 @@
var format;
var zero;
// We need a non empty text node with an offset greater than zero.
if ( ! node || node.nodeType !== 3 || ! node.data.length || ! offset ) {
return;
}
// The ending character should exist in the patterns registered.
if ( tinymce.inArray( chars, node.data.charAt( offset - 1 ) ) === -1 ) {
return;
}
function findStart( node ) {
var i = inlinePatterns.length;
var offset;
var string = node.data.slice( 0, offset );
while ( i-- ) {
pattern = inlinePatterns[ i ];
offset = node.data.indexOf( pattern.end );
tinymce.each( inlinePatterns, function( p ) {
var regExp = new RegExp( escapeRegExp( p.start ) + '\\S+' + escapeRegExp( p.end ) + '$' );
var match = string.match( regExp );
if ( offset !== -1 ) {
return offset;
}
if ( ! match ) {
return;
}
}
startOffset = findStart( node );
endOffset = node.data.lastIndexOf( pattern.end );
// Don't allow pattern characters in the text.
if ( node.data.slice( match.index + p.start.length, offset - p.end.length ).indexOf( p.start.slice( 0, 1 ) ) !== -1 ) {
return;
}
if ( startOffset === endOffset || endOffset === -1 ) {
return;
}
startOffset = match.index;
endOffset = offset - p.end.length;
pattern = p;
if ( endOffset - startOffset <= pattern.start.length ) {
return;
}
return false;
} );
if ( node.data.slice( startOffset + pattern.start.length, endOffset ).indexOf( pattern.start.slice( 0, 1 ) ) !== -1 ) {
if ( ! pattern ) {
return;
}

View File

@ -1 +1 @@
!function(a,b){a.Env.ie&&a.Env.ie<9||a.PluginManager.add("wptextpattern",function(c){function d(){function d(a){for(var b,c=m.length;c--;)if(g=m[c],b=a.data.indexOf(g.end),-1!==b)return b}var e,f,g,h,j,k=c.selection.getRng(),l=k.startContainer,o=k.startOffset;l&&3===l.nodeType&&l.data.length&&o&&-1!==a.inArray(n,l.data.charAt(o-1))&&(e=d(l),f=l.data.lastIndexOf(g.end),e!==f&&-1!==f&&(f-e<=g.start.length||-1===l.data.slice(e+g.start.length,f).indexOf(g.start.slice(0,1))&&(h=c.formatter.get(g.format),h&&h[0].inline&&(c.undoManager.add(),c.undoManager.transact(function(){l.insertData(o,"\ufeff"),l=l.splitText(e),j=l.splitText(o-e),l.deleteData(0,g.start.length),l.deleteData(l.data.length-g.end.length,g.end.length),c.formatter.apply(g.format,{},l),c.selection.setCursorLocation(j,1)}),b(function(){i="space",c.once("selectionchange",function(){var a;j&&(a=j.data.indexOf("\ufeff"),-1!==a&&j.deleteData(a,a+1))})})))))}function e(a){var b,d=c.dom.getParent(a,"p");if(d){for(;(b=d.firstChild)&&3!==b.nodeType;)d=b;if(b)return b.data||(b=b.nextSibling&&3===b.nextSibling.nodeType?b.nextSibling:null),b}}function f(){var d,f,g=c.selection.getRng(),h=g.startContainer;h&&e(h)===h&&(d=h.parentNode,f=h.data,a.each(k,function(a){var e=f.match(a.regExp);if(e&&g.startOffset===e[0].length)return c.undoManager.add(),c.undoManager.transact(function(){h.deleteData(0,e[0].length),d.innerHTML||d.appendChild(document.createElement("br")),c.selection.setCursorLocation(d),c.execCommand(a.cmd)}),b(function(){i="space"}),!1}))}function g(){var d,f,g,j=c.selection.getRng(),k=j.startContainer,m=e(k),n=l.length;if(m){for(d=m.data;n--;)if(l[n].start){if(0===d.indexOf(l[n].start)){f=l[n];break}}else if(l[n].regExp&&l[n].regExp.test(d)){f=l[n];break}f&&(m===k&&a.trim(d)===f.start||c.once("keyup",function(){c.undoManager.add(),c.undoManager.transact(function(){f.format?(c.formatter.apply(f.format,{},m),m.replaceData(0,m.data.length,h(m.data.slice(f.start.length)))):f.element&&(g=m.parentNode&&m.parentNode.parentNode,g&&g.replaceChild(document.createElement(f.element),m.parentNode))}),b(function(){i="enter"})}))}}function h(a){return a?a.replace(/^\s+/,""):""}var i,j=a.util.VK,k=[{regExp:/^[*-]\s/,cmd:"InsertUnorderedList"},{regExp:/^1[.)]\s/,cmd:"InsertOrderedList"}],l=[{start:"##",format:"h2"},{start:"###",format:"h3"},{start:"####",format:"h4"},{start:"#####",format:"h5"},{start:"######",format:"h6"},{start:">",format:"blockquote"},{regExp:/^(-){3,}$/,element:"hr"}],m=[{start:"`",end:"`",format:"code"}],n=[];a.each(m,function(b){a.each((b.start+b.end).split(""),function(b){-1===a.inArray(n,b)&&n.push(b)})}),c.on("selectionchange",function(){i=null}),c.on("keydown",function(a){(i&&27===a.keyCode||"space"===i&&a.keyCode===j.BACKSPACE)&&(c.undoManager.undo(),a.preventDefault(),a.stopImmediatePropagation()),a.keyCode!==j.ENTER||j.modifierPressed(a)||g(),a.keyCode!==j.SPACEBAR||a.ctrlKey||a.metaKey||a.altKey?a.keyCode>47&&!(a.keyCode>=91&&a.keyCode<=93)&&b(d):b(f)},!0)})}(window.tinymce,window.setTimeout);
!function(a,b){function c(a){return a.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}a.Env.ie&&a.Env.ie<9||a.PluginManager.add("wptextpattern",function(d){function e(){var e,f,g,h,i,k=d.selection.getRng(),l=k.startContainer,m=k.startOffset;if(l&&3===l.nodeType&&l.data.length&&m&&-1!==a.inArray(p,l.data.charAt(m-1))){var n=l.data.slice(0,m);a.each(o,function(a){var b=new RegExp(c(a.start)+"\\S+"+c(a.end)+"$"),d=n.match(b);return d&&-1===l.data.slice(d.index+a.start.length,m-a.end.length).indexOf(a.start.slice(0,1))?(e=d.index,f=m-a.end.length,g=a,!1):void 0}),g&&(h=d.formatter.get(g.format),h&&h[0].inline&&(d.undoManager.add(),d.undoManager.transact(function(){l.insertData(m,"\ufeff"),l=l.splitText(e),i=l.splitText(m-e),l.deleteData(0,g.start.length),l.deleteData(l.data.length-g.end.length,g.end.length),d.formatter.apply(g.format,{},l),d.selection.setCursorLocation(i,1)}),b(function(){j="space",d.once("selectionchange",function(){var a;i&&(a=i.data.indexOf("\ufeff"),-1!==a&&i.deleteData(a,a+1))})})))}}function f(a){var b,c=d.dom.getParent(a,"p");if(c){for(;(b=c.firstChild)&&3!==b.nodeType;)c=b;if(b)return b.data||(b=b.nextSibling&&3===b.nextSibling.nodeType?b.nextSibling:null),b}}function g(){var c,e,g=d.selection.getRng(),h=g.startContainer;h&&f(h)===h&&(c=h.parentNode,e=h.data,a.each(m,function(a){var f=e.match(a.regExp);if(f&&g.startOffset===f[0].length)return d.undoManager.add(),d.undoManager.transact(function(){h.deleteData(0,f[0].length),c.innerHTML||c.appendChild(document.createElement("br")),d.selection.setCursorLocation(c),d.execCommand(a.cmd)}),b(function(){j="space"}),!1}))}function h(){var c,e,g,h=d.selection.getRng(),k=h.startContainer,l=f(k),m=n.length;if(l){for(c=l.data;m--;)if(n[m].start){if(0===c.indexOf(n[m].start)){e=n[m];break}}else if(n[m].regExp&&n[m].regExp.test(c)){e=n[m];break}e&&(l===k&&a.trim(c)===e.start||d.once("keyup",function(){d.undoManager.add(),d.undoManager.transact(function(){e.format?(d.formatter.apply(e.format,{},l),l.replaceData(0,l.data.length,i(l.data.slice(e.start.length)))):e.element&&(g=l.parentNode&&l.parentNode.parentNode,g&&g.replaceChild(document.createElement(e.element),l.parentNode))}),b(function(){j="enter"})}))}}function i(a){return a?a.replace(/^\s+/,""):""}var j,k=a.util.VK,l=d.settings.wptextpattern||{},m=l.space||[{regExp:/^[*-]\s/,cmd:"InsertUnorderedList"},{regExp:/^1[.)]\s/,cmd:"InsertOrderedList"}],n=l.enter||[{start:"##",format:"h2"},{start:"###",format:"h3"},{start:"####",format:"h4"},{start:"#####",format:"h5"},{start:"######",format:"h6"},{start:">",format:"blockquote"},{regExp:/^(-){3,}$/,element:"hr"}],o=l.inline||[{start:"`",end:"`",format:"code"}],p=[];a.each(o,function(b){a.each((b.start+b.end).split(""),function(b){-1===a.inArray(p,b)&&p.push(b)})}),d.on("selectionchange",function(){j=null}),d.on("keydown",function(a){(j&&27===a.keyCode||"space"===j&&a.keyCode===k.BACKSPACE)&&(d.undoManager.undo(),a.preventDefault(),a.stopImmediatePropagation()),a.keyCode!==k.ENTER||k.modifierPressed(a)||h(),a.keyCode!==k.SPACEBAR||a.ctrlKey||a.metaKey||a.altKey?a.keyCode>47&&!(a.keyCode>=91&&a.keyCode<=93)&&b(e):b(g)},!0)})}(window.tinymce,window.setTimeout);

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.7-beta1-39074';
$wp_version = '4.7-beta1-39075';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.