From 64da76d23ed903bd4be7d4c9c75c7353ed724f37 Mon Sep 17 00:00:00 2001 From: azaozz Date: Mon, 19 Sep 2011 20:47:20 +0000 Subject: [PATCH] Fix quicktags buttons fir self-closing tags, remove unneeded arg when adding a button, see #16695 git-svn-id: http://svn.automattic.com/wordpress/trunk@18725 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/js/quicktags.dev.js | 64 +++++++++++++++++++-------------- wp-includes/js/quicktags.js | 2 +- wp-includes/script-loader.php | 2 +- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/wp-includes/js/quicktags.dev.js b/wp-includes/js/quicktags.dev.js index c5681730c0..c0f01ef43a 100644 --- a/wp-includes/js/quicktags.dev.js +++ b/wp-includes/js/quicktags.dev.js @@ -52,7 +52,7 @@ function edInsertContent(bah, txt) { * @see QTags.addButton() */ function edButton(id, display, tagStart, tagEnd, access, open) { - return QTags.addButton( id, display, tagStart, tagEnd, open, access, '', -1 ); + return QTags.addButton( id, display, tagStart, tagEnd, access, '', -1 ); } /** @@ -305,9 +305,9 @@ edToolbar = function(){}; * Main API function for adding a button to Quicktags * * Adds qt.Button or qt.TagButton depending on the args. The first three args are always required. - * For TagButton the 4th or the 5th argument is also required. To be able to add button(s) to Quicktags, your script - * should be enqueued as dependant on "quicktags" and outputted in the footer. If you are echoing JS - * directly from PHP, use add_action( 'admin_print_footer_scripts', 'output_my_js', 100 ) or add_action( 'wp_footer', 'output_my_js', 100 ) + * To be able to add button(s) to Quicktags, your script should be enqueued as dependant + * on "quicktags" and outputted in the footer. If you are echoing JS directly from PHP, + * use add_action( 'admin_print_footer_scripts', 'output_my_js', 100 ) or add_action( 'wp_footer', 'output_my_js', 100 ) * * Minimun required to add a button that calls an external function: * QTags.addButton( 'my_id', 'my button', my_callback ); @@ -315,30 +315,31 @@ edToolbar = function(){}; * * Minimun required to add a button that inserts a tag: * QTags.addButton( 'my_id', 'my button', '', '' ); + * QTags.addButton( 'my_id', 'my button', '
' ); * * @param id string required Button HTML ID * @param display string required Button's value="..." - * @param arg1 string || function required Either a starting tag to be inserted like "" or a callback that is executed when the button is pressed + * @param arg1 string || function required Either a starting tag to be inserted like "" or a callback that is executed when the button is clicked. * @param arg2 string Ending tag like "" - * @param arg3 int Set to -1 if the inserted tag is self-closing - * @param access string Access key for the button + * @param access_key string Access key for the button * @param title string Button's title="..." * @param priority int Number representing the desired position of the button in the toolbar. 1 - 9 = first, 11 - 19 = second, 21 - 29 = third, etc. - * @return null This is needed for back-compat as the common method of adding a button was to manually add it to the buttons array + * @return mixed null or the button object that is needed for back-compat. The common method of adding a button was to manually add it to the buttons array. */ - qt.addButton = function( id, display, arg1, arg2, arg3, access, title, priority ) { + qt.addButton = function( id, display, arg1, arg2, access_key, title, priority ) { var btn; if ( !id || !display ) return; priority = priority || 0; + arg2 = arg2 || ''; - if ( typeof(arg1) == 'function' ) { - btn = new qt.Button(id, display, access, title); + if ( typeof(arg1) === 'function' ) { + btn = new qt.Button(id, display, access_key, title); btn.callback = arg1; - } else if ( arg1 && arg2 && typeof(arg1) == 'string' ) { - btn = new qt.TagButton(id, display, arg1, arg2, access, arg3, title); + } else if ( typeof(arg1) === 'string' ) { + btn = new qt.TagButton(id, display, arg1, arg2, access_key, title); } else { return; } @@ -405,12 +406,11 @@ edToolbar = function(){}; qt.Button.prototype.callback = function(){}; // a button that inserts HTML tag - qt.TagButton = function(id, display, tagStart, tagEnd, access, open, title) { + qt.TagButton = function(id, display, tagStart, tagEnd, access, title) { var t = this; qt.Button.call(t, id, display, access, title); t.tagStart = tagStart; t.tagEnd = tagEnd; - t.open = open; }; qt.TagButton.prototype = new qt.Button(); qt.TagButton.prototype.openTag = function(e, ed) { @@ -453,9 +453,14 @@ edToolbar = function(){}; canvas.focus(); sel = document.selection.createRange(); if ( sel.text.length > 0 ) { - sel.text = t.tagStart + sel.text + endTag; + if ( !endTag ) + sel.text = sel.text + t.tagStart; + else + sel.text = t.tagStart + sel.text + endTag; } else { - if ( t.isOpen(ed) === false || t.tagEnd === '' ) { + if ( !endTag ) { + sel.text = t.tagStart; + } else if ( t.isOpen(ed) === false ) { sel.text = t.tagStart; t.openTag(element, ed); } else { @@ -473,13 +478,18 @@ edToolbar = function(){}; r = v.substring(endPos, v.length); // right of the selection i = v.substring(startPos, endPos); // inside the selection if ( startPos != endPos ) { - canvas.value = l + t.tagStart + i + endTag + r; - if ( endTag === '' ) { - cursorPos = startPos; + if ( !endTag ) { + canvas.value = l + i + t.tagStart + r; // insert self closing tags after the selection + cursorPos += t.tagStart.length; + } else { + canvas.value = l + t.tagStart + i + endTag + r; + cursorPos += t.tagStart.length + endTag.length; } - cursorPos += t.tagStart.length + endTag.length; } else { - if ( t.isOpen(ed) === false || t.tagEnd === '' ) { + if ( !endTag ) { + canvas.value = l + t.tagStart + r; + cursorPos = startPos + t.tagStart.length; + } else if ( t.isOpen(ed) === false ) { canvas.value = l + t.tagStart + r; t.openTag(element, ed); cursorPos = startPos + t.tagStart.length; @@ -494,10 +504,10 @@ edToolbar = function(){}; canvas.selectionStart = cursorPos; canvas.selectionEnd = cursorPos; canvas.scrollTop = scrollTop; - } - // other browsers - else { - if ( t.isOpen(ed) !== false || t.tagEnd === '' ) { + } else { // other browsers? + if ( !endTag ) { + canvas.value += t.tagStart; + } else if ( t.isOpen(ed) !== false ) { canvas.value += t.tagStart; t.openTag(element, ed); } else { @@ -553,6 +563,8 @@ edToolbar = function(){}; element = document.getElementById(ed.name + '_' + button.id); button.callback.call(button, element, c, ed); } + } else { + ed.canvas.focus(); } }; diff --git a/wp-includes/js/quicktags.js b/wp-includes/js/quicktags.js index a6615c4e6e..4fbdcaa0dc 100644 --- a/wp-includes/js/quicktags.js +++ b/wp-includes/js/quicktags.js @@ -1 +1 @@ -var QTags,edButtons=[],edCanvas;function quicktags(a){return new QTags(a)}function edInsertContent(b,a){return QTags.insertContent(a)}function edButton(f,e,c,b,a,d){return QTags.addButton(f,e,c,b,d,a,"",-1)}var edAddTag=function(){},edCheckOpenTags=function(){},edCloseAllTags=function(){},edInsertImage=function(){},edInsertLink=function(){},edInsertTag=function(){},edLink=function(){},edQuickLink=function(){},edRemoveTag=function(){},edShowButton=function(){},edShowLinks=function(){},edSpell=function(){},edToolbar=function(){};(function(){var c=function(h){var g,f,e;if(typeof jQuery!="undefined"){jQuery(document).ready(h)}else{g=c;g.funcs=[];g.ready=function(){if(!g.isReady){g.isReady=true;for(f=0;f0){while(typeof(edButtons[m])!="undefined"){m++}edButtons[m]=f}else{edButtons[edButtons.length]=f}if(this.buttonsInitDone){this._buttonsInit()}};d.insertContent=function(h){var i,g,f,j,k,e=document.getElementById(wpActiveEditor);if(!e){return false}if(document.selection){e.focus();i=document.selection.createRange();i.text=h;e.focus()}else{if(e.selectionStart||e.selectionStart=="0"){k=e.value;g=e.selectionStart;f=e.selectionEnd;j=e.scrollTop;e.value=k.substring(0,g)+h+k.substring(f,k.length);e.focus();e.selectionStart=g+h.length;e.selectionEnd=g+h.length;e.scrollTop=j}else{e.value+=h;e.focus()}}return true};d.Button=function(i,g,e,h){var f=this;f.id=i;f.display=g;f.access=e;f.title=h||""};d.Button.prototype.html=function(f){var e=this.access?' accesskey="'+this.access+'"':"";return''};d.Button.prototype.callback=function(){};d.TagButton=function(l,j,g,f,e,h,k){var i=this;d.Button.call(i,l,j,e,k);i.tagStart=g;i.tagEnd=f;i.open=h};d.TagButton.prototype=new d.Button();d.TagButton.prototype.openTag=function(h,f){var g=this;if(!f.openTags){f.openTags=[]}if(g.tagEnd){f.openTags.push(g.id);h.value="/"+h.value}};d.TagButton.prototype.closeTag=function(j,f){var h=this,g=h.isOpen(f);if(g!==false){f.openTags.splice(g,1)}j.value=h.display};d.TagButton.prototype.isOpen=function(e){var h=this,g=0,f=false;if(e.openTags){while(f===false&&g0){g.text=w.tagStart+g.text+m}else{if(w.isOpen(q)===false||w.tagEnd===""){g.text=w.tagStart;w.openTag(p,q)}else{g.text=m;w.closeTag(p,q)}}j.focus()}else{if(j.selectionStart||j.selectionStart=="0"){s=j.selectionStart;f=j.selectionEnd;n=f;h=j.scrollTop;k=u.substring(0,s);e=u.substring(f,u.length);o=u.substring(s,f);if(s!=f){j.value=k+w.tagStart+o+m+e;if(m===""){n=s}n+=w.tagStart.length+m.length}else{if(w.isOpen(q)===false||w.tagEnd===""){j.value=k+w.tagStart+e;w.openTag(p,q);n=s+w.tagStart.length}else{j.value=k+m+e;n=s+m.length;w.closeTag(p,q)}}j.focus();j.selectionStart=n;j.selectionEnd=n;j.scrollTop=h}else{if(w.isOpen(q)!==false||w.tagEnd===""){j.value+=w.tagStart;w.openTag(p,q)}else{j.value+=m;w.closeTag(p,q)}j.focus()}}};d.SpellButton=function(){d.Button.call(this,"spell",quicktagsL10n.lookup,"",quicktagsL10n.dictionaryLookup)};d.SpellButton.prototype=new d.Button();d.SpellButton.prototype.callback=function(i,h,e){var k="",j,g,f;if(document.selection){h.focus();j=document.selection.createRange();if(j.text.length>0){k=j.text}}else{if(h.selectionStart||h.selectionStart=="0"){g=h.selectionStart;f=h.selectionEnd;if(g!=f){k=h.value.substring(g,f)}}}if(k===""){k=prompt(quicktagsL10n.wordLookup,"")}if(k!==null&&/^\w[\w ]*$/.test(k)){window.open("http://www.answers.com/"+encodeURIComponent(k))}};d.CloseButton=function(){d.Button.call(this,"close",quicktagsL10n.closeTags,"",quicktagsL10n.closeAllOpenTags)};d.CloseButton.prototype=new d.Button();d.CloseButton.prototype.callback=function(j,k,f){var h,g,i=f.openTags;if(i){while(i.length>0){h=f.getButton(i[i.length-1]);g=document.getElementById(f.name+"_"+h.id);h.callback.call(h,g,k,f)}}};d.closeAllTags=function(g){var e=this.getInstance(g),f=e.getButton("close");f.callback.call(f,"",e.canvas,e)};d.LinkButton=function(){d.TagButton.call(this,"link","link","","","a")};d.LinkButton.prototype=new d.TagButton();d.LinkButton.prototype.callback=function(j,k,h,g){var f,i=this;if(typeof(wpLink)!="undefined"){wpLink.open();return}if(!g){g="http://"}if(i.isOpen(h)===false){f=prompt(quicktagsL10n.enterURL,g);if(f){i.tagStart='';d.TagButton.prototype.callback.call(i,j,k,h)}}else{d.TagButton.prototype.callback.call(i,j,k,h)}};d.ImgButton=function(){d.TagButton.call(this,"img","img","","","m",-1)};d.ImgButton.prototype=new d.TagButton();d.ImgButton.prototype.callback=function(i,k,g,f){if(!f){f="http://"}var j=prompt(quicktagsL10n.enterImageURL,f),h;if(j){h=prompt(quicktagsL10n.enterImageDescription,"");this.tagStart=''+h+'';d.TagButton.prototype.callback.call(this,i,k,g)}};d.FullscreenButton=function(){d.Button.call(this,"fullscreen",quicktagsL10n.fullscreen,"f",quicktagsL10n.toggleFullscreen)};d.FullscreenButton.prototype=new d.Button();d.FullscreenButton.prototype.callback=function(f,g){if(g.id!="content"||typeof(fullscreen)=="undefined"){return}fullscreen.on()};edButtons[10]=new d.TagButton("strong","b","","","b");edButtons[20]=new d.TagButton("em","i","","","i"),edButtons[30]=new d.LinkButton(),edButtons[40]=new d.TagButton("block","b-quote","\n\n
","
\n\n","q"),edButtons[50]=new d.TagButton("del","del",'',"","d"),edButtons[60]=new d.TagButton("ins","ins",'',"","s"),edButtons[70]=new d.ImgButton(),edButtons[80]=new d.TagButton("ul","ul","
    \n","
\n\n","u"),edButtons[90]=new d.TagButton("ol","ol","
    \n","
\n\n","o"),edButtons[100]=new d.TagButton("li","li","\t
  • ","
  • \n","l"),edButtons[110]=new d.TagButton("code","code","","","c"),edButtons[120]=new d.TagButton("more","more","","","t",-1),edButtons[130]=new d.SpellButton(),edButtons[140]=new d.CloseButton()})(); \ No newline at end of file +var QTags,edButtons=[],edCanvas;function quicktags(a){return new QTags(a)}function edInsertContent(b,a){return QTags.insertContent(a)}function edButton(f,e,c,b,a,d){return QTags.addButton(f,e,c,b,a,"",-1)}var edAddTag=function(){},edCheckOpenTags=function(){},edCloseAllTags=function(){},edInsertImage=function(){},edInsertLink=function(){},edInsertTag=function(){},edLink=function(){},edQuickLink=function(){},edRemoveTag=function(){},edShowButton=function(){},edShowLinks=function(){},edSpell=function(){},edToolbar=function(){};(function(){var c=function(h){var g,f,e;if(typeof jQuery!="undefined"){jQuery(document).ready(h)}else{g=c;g.funcs=[];g.ready=function(){if(!g.isReady){g.isReady=true;for(f=0;f0){while(typeof(edButtons[h])!="undefined"){h++}edButtons[h]=g}else{edButtons[edButtons.length]=g}if(this.buttonsInitDone){this._buttonsInit()}};d.insertContent=function(h){var i,g,f,j,k,e=document.getElementById(wpActiveEditor);if(!e){return false}if(document.selection){e.focus();i=document.selection.createRange();i.text=h;e.focus()}else{if(e.selectionStart||e.selectionStart=="0"){k=e.value;g=e.selectionStart;f=e.selectionEnd;j=e.scrollTop;e.value=k.substring(0,g)+h+k.substring(f,k.length);e.focus();e.selectionStart=g+h.length;e.selectionEnd=g+h.length;e.scrollTop=j}else{e.value+=h;e.focus()}}return true};d.Button=function(i,g,e,h){var f=this;f.id=i;f.display=g;f.access=e;f.title=h||""};d.Button.prototype.html=function(f){var e=this.access?' accesskey="'+this.access+'"':"";return''};d.Button.prototype.callback=function(){};d.TagButton=function(k,i,g,f,e,j){var h=this;d.Button.call(h,k,i,e,j);h.tagStart=g;h.tagEnd=f};d.TagButton.prototype=new d.Button();d.TagButton.prototype.openTag=function(h,f){var g=this;if(!f.openTags){f.openTags=[]}if(g.tagEnd){f.openTags.push(g.id);h.value="/"+h.value}};d.TagButton.prototype.closeTag=function(j,f){var h=this,g=h.isOpen(f);if(g!==false){f.openTags.splice(g,1)}j.value=h.display};d.TagButton.prototype.isOpen=function(e){var h=this,g=0,f=false;if(e.openTags){while(f===false&&g0){if(!m){g.text=g.text+w.tagStart}else{g.text=w.tagStart+g.text+m}}else{if(!m){g.text=w.tagStart}else{if(w.isOpen(q)===false){g.text=w.tagStart;w.openTag(p,q)}else{g.text=m;w.closeTag(p,q)}}}j.focus()}else{if(j.selectionStart||j.selectionStart=="0"){s=j.selectionStart;f=j.selectionEnd;n=f;h=j.scrollTop;k=u.substring(0,s);e=u.substring(f,u.length);o=u.substring(s,f);if(s!=f){if(!m){j.value=k+o+w.tagStart+e;n+=w.tagStart.length}else{j.value=k+w.tagStart+o+m+e;n+=w.tagStart.length+m.length}}else{if(!m){j.value=k+w.tagStart+e;n=s+w.tagStart.length}else{if(w.isOpen(q)===false){j.value=k+w.tagStart+e;w.openTag(p,q);n=s+w.tagStart.length}else{j.value=k+m+e;n=s+m.length;w.closeTag(p,q)}}}j.focus();j.selectionStart=n;j.selectionEnd=n;j.scrollTop=h}else{if(!m){j.value+=w.tagStart}else{if(w.isOpen(q)!==false){j.value+=w.tagStart;w.openTag(p,q)}else{j.value+=m;w.closeTag(p,q)}}j.focus()}}};d.SpellButton=function(){d.Button.call(this,"spell",quicktagsL10n.lookup,"",quicktagsL10n.dictionaryLookup)};d.SpellButton.prototype=new d.Button();d.SpellButton.prototype.callback=function(i,h,e){var k="",j,g,f;if(document.selection){h.focus();j=document.selection.createRange();if(j.text.length>0){k=j.text}}else{if(h.selectionStart||h.selectionStart=="0"){g=h.selectionStart;f=h.selectionEnd;if(g!=f){k=h.value.substring(g,f)}}}if(k===""){k=prompt(quicktagsL10n.wordLookup,"")}if(k!==null&&/^\w[\w ]*$/.test(k)){window.open("http://www.answers.com/"+encodeURIComponent(k))}};d.CloseButton=function(){d.Button.call(this,"close",quicktagsL10n.closeTags,"",quicktagsL10n.closeAllOpenTags)};d.CloseButton.prototype=new d.Button();d.CloseButton.prototype.callback=function(j,k,f){var h,g,i=f.openTags;if(i){while(i.length>0){h=f.getButton(i[i.length-1]);g=document.getElementById(f.name+"_"+h.id);h.callback.call(h,g,k,f)}}else{f.canvas.focus()}};d.closeAllTags=function(g){var e=this.getInstance(g),f=e.getButton("close");f.callback.call(f,"",e.canvas,e)};d.LinkButton=function(){d.TagButton.call(this,"link","link","","
    ","a")};d.LinkButton.prototype=new d.TagButton();d.LinkButton.prototype.callback=function(j,k,h,g){var f,i=this;if(typeof(wpLink)!="undefined"){wpLink.open();return}if(!g){g="http://"}if(i.isOpen(h)===false){f=prompt(quicktagsL10n.enterURL,g);if(f){i.tagStart='';d.TagButton.prototype.callback.call(i,j,k,h)}}else{d.TagButton.prototype.callback.call(i,j,k,h)}};d.ImgButton=function(){d.TagButton.call(this,"img","img","","","m",-1)};d.ImgButton.prototype=new d.TagButton();d.ImgButton.prototype.callback=function(i,k,g,f){if(!f){f="http://"}var j=prompt(quicktagsL10n.enterImageURL,f),h;if(j){h=prompt(quicktagsL10n.enterImageDescription,"");this.tagStart=''+h+'';d.TagButton.prototype.callback.call(this,i,k,g)}};d.FullscreenButton=function(){d.Button.call(this,"fullscreen",quicktagsL10n.fullscreen,"f",quicktagsL10n.toggleFullscreen)};d.FullscreenButton.prototype=new d.Button();d.FullscreenButton.prototype.callback=function(f,g){if(g.id!="content"||typeof(fullscreen)=="undefined"){return}fullscreen.on()};edButtons[10]=new d.TagButton("strong","b","","","b");edButtons[20]=new d.TagButton("em","i","","","i"),edButtons[30]=new d.LinkButton(),edButtons[40]=new d.TagButton("block","b-quote","\n\n
    ","
    \n\n","q"),edButtons[50]=new d.TagButton("del","del",'',"","d"),edButtons[60]=new d.TagButton("ins","ins",'',"","s"),edButtons[70]=new d.ImgButton(),edButtons[80]=new d.TagButton("ul","ul","
      \n","
    \n\n","u"),edButtons[90]=new d.TagButton("ol","ol","
      \n","
    \n\n","o"),edButtons[100]=new d.TagButton("li","li","\t
  • ","
  • \n","l"),edButtons[110]=new d.TagButton("code","code","","","c"),edButtons[120]=new d.TagButton("more","more","","","t",-1),edButtons[130]=new d.SpellButton(),edButtons[140]=new d.CloseButton()})(); \ No newline at end of file diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index 8ee3d24ef8..7fe577834d 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -68,7 +68,7 @@ function wp_default_scripts( &$scripts ) { $scripts->add( 'sack', "/wp-includes/js/tw-sack$suffix.js", false, '1.6.1', 1 ); - $scripts->add( 'quicktags', "/wp-includes/js/quicktags$suffix.js", false, '20110902', 1 ); + $scripts->add( 'quicktags', "/wp-includes/js/quicktags$suffix.js", false, '20110919', 1 ); $scripts->add_script_data( 'quicktags', 'quicktagsL10n', array( 'wordLookup' => __('Enter a word to look up:'), 'dictionaryLookup' => esc_attr(__('Dictionary lookup')),