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
This commit is contained in:
azaozz 2011-09-19 20:47:20 +00:00
parent bad1132fdf
commit 64da76d23e
3 changed files with 40 additions and 28 deletions

View File

@ -52,7 +52,7 @@ function edInsertContent(bah, txt) {
* @see QTags.addButton() * @see QTags.addButton()
*/ */
function edButton(id, display, tagStart, tagEnd, access, open) { 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 * 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. * 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 * To be able to add button(s) to Quicktags, your script should be enqueued as dependant
* should be enqueued as dependant on "quicktags" and outputted in the footer. If you are echoing JS * on "quicktags" and outputted in the footer. If you are echoing JS directly from PHP,
* directly from PHP, use add_action( 'admin_print_footer_scripts', 'output_my_js', 100 ) or add_action( 'wp_footer', 'output_my_js', 100 ) * 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: * Minimun required to add a button that calls an external function:
* QTags.addButton( 'my_id', 'my button', my_callback ); * QTags.addButton( 'my_id', 'my button', my_callback );
@ -315,30 +315,31 @@ edToolbar = function(){};
* *
* Minimun required to add a button that inserts a tag: * Minimun required to add a button that inserts a tag:
* QTags.addButton( 'my_id', 'my button', '<span>', '</span>' ); * QTags.addButton( 'my_id', 'my button', '<span>', '</span>' );
* QTags.addButton( 'my_id', 'my button', '<br />' );
* *
* @param id string required Button HTML ID * @param id string required Button HTML ID
* @param display string required Button's value="..." * @param display string required Button's value="..."
* @param arg1 string || function required Either a starting tag to be inserted like "<span>" or a callback that is executed when the button is pressed * @param arg1 string || function required Either a starting tag to be inserted like "<span>" or a callback that is executed when the button is clicked.
* @param arg2 string Ending tag like "</span>" * @param arg2 string Ending tag like "</span>"
* @param arg3 int Set to -1 if the inserted tag is self-closing * @param access_key string Access key for the button
* @param access string Access key for the button
* @param title string Button's title="..." * @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. * @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; var btn;
if ( !id || !display ) if ( !id || !display )
return; return;
priority = priority || 0; priority = priority || 0;
arg2 = arg2 || '';
if ( typeof(arg1) == 'function' ) { if ( typeof(arg1) === 'function' ) {
btn = new qt.Button(id, display, access, title); btn = new qt.Button(id, display, access_key, title);
btn.callback = arg1; btn.callback = arg1;
} else if ( arg1 && arg2 && typeof(arg1) == 'string' ) { } else if ( typeof(arg1) === 'string' ) {
btn = new qt.TagButton(id, display, arg1, arg2, access, arg3, title); btn = new qt.TagButton(id, display, arg1, arg2, access_key, title);
} else { } else {
return; return;
} }
@ -405,12 +406,11 @@ edToolbar = function(){};
qt.Button.prototype.callback = function(){}; qt.Button.prototype.callback = function(){};
// a button that inserts HTML tag // 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; var t = this;
qt.Button.call(t, id, display, access, title); qt.Button.call(t, id, display, access, title);
t.tagStart = tagStart; t.tagStart = tagStart;
t.tagEnd = tagEnd; t.tagEnd = tagEnd;
t.open = open;
}; };
qt.TagButton.prototype = new qt.Button(); qt.TagButton.prototype = new qt.Button();
qt.TagButton.prototype.openTag = function(e, ed) { qt.TagButton.prototype.openTag = function(e, ed) {
@ -453,9 +453,14 @@ edToolbar = function(){};
canvas.focus(); canvas.focus();
sel = document.selection.createRange(); sel = document.selection.createRange();
if ( sel.text.length > 0 ) { 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 { } else {
if ( t.isOpen(ed) === false || t.tagEnd === '' ) { if ( !endTag ) {
sel.text = t.tagStart;
} else if ( t.isOpen(ed) === false ) {
sel.text = t.tagStart; sel.text = t.tagStart;
t.openTag(element, ed); t.openTag(element, ed);
} else { } else {
@ -473,13 +478,18 @@ edToolbar = function(){};
r = v.substring(endPos, v.length); // right of the selection r = v.substring(endPos, v.length); // right of the selection
i = v.substring(startPos, endPos); // inside the selection i = v.substring(startPos, endPos); // inside the selection
if ( startPos != endPos ) { if ( startPos != endPos ) {
canvas.value = l + t.tagStart + i + endTag + r; if ( !endTag ) {
if ( endTag === '' ) { canvas.value = l + i + t.tagStart + r; // insert self closing tags after the selection
cursorPos = startPos; 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 { } 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; canvas.value = l + t.tagStart + r;
t.openTag(element, ed); t.openTag(element, ed);
cursorPos = startPos + t.tagStart.length; cursorPos = startPos + t.tagStart.length;
@ -494,10 +504,10 @@ edToolbar = function(){};
canvas.selectionStart = cursorPos; canvas.selectionStart = cursorPos;
canvas.selectionEnd = cursorPos; canvas.selectionEnd = cursorPos;
canvas.scrollTop = scrollTop; canvas.scrollTop = scrollTop;
} } else { // other browsers?
// other browsers if ( !endTag ) {
else { canvas.value += t.tagStart;
if ( t.isOpen(ed) !== false || t.tagEnd === '' ) { } else if ( t.isOpen(ed) !== false ) {
canvas.value += t.tagStart; canvas.value += t.tagStart;
t.openTag(element, ed); t.openTag(element, ed);
} else { } else {
@ -553,6 +563,8 @@ edToolbar = function(){};
element = document.getElementById(ed.name + '_' + button.id); element = document.getElementById(ed.name + '_' + button.id);
button.callback.call(button, element, c, ed); button.callback.call(button, element, c, ed);
} }
} else {
ed.canvas.focus();
} }
}; };

File diff suppressed because one or more lines are too long

View File

@ -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( '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( $scripts->add_script_data( 'quicktags', 'quicktagsL10n', array(
'wordLookup' => __('Enter a word to look up:'), 'wordLookup' => __('Enter a word to look up:'),
'dictionaryLookup' => esc_attr(__('Dictionary lookup')), 'dictionaryLookup' => esc_attr(__('Dictionary lookup')),