Fix JSHint errors in quicktags.js. Ignore deprecated API rather than marking it as exported globals.

props kovshenin.
fixes #26046.


git-svn-id: https://develop.svn.wordpress.org/trunk@26212 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Nacin 2013-11-15 14:25:29 +00:00
parent d5afb2c07c
commit 34eff3bf52
1 changed files with 79 additions and 53 deletions

View File

@ -1,3 +1,4 @@
/* global adminpage, wpActiveEditor, quicktagsL10n, wpLink, fullscreen, prompt */
/* /*
* Quicktags * Quicktags
* *
@ -21,14 +22,17 @@
// by Alex King // by Alex King
// http://www.alexking.org/ // http://www.alexking.org/
var QTags, edButtons = [], edCanvas, var QTags, edCanvas,
edButtons = [];
/* jshint ignore:start */
/** /**
* Back-compat * Back-compat
* *
* Define all former global functions so plugins that hack quicktags.js directly don't cause fatal errors. * Define all former global functions so plugins that hack quicktags.js directly don't cause fatal errors.
*/ */
edAddTag = function(){}, var edAddTag = function(){},
edCheckOpenTags = function(){}, edCheckOpenTags = function(){},
edCloseAllTags = function(){}, edCloseAllTags = function(){},
edInsertImage = function(){}, edInsertImage = function(){},
@ -65,16 +69,18 @@ function edInsertContent(bah, txt) {
* Added for back compatibility, use QTags.addButton() as it gives more flexibility like type of button, button placement, etc. * Added for back compatibility, use QTags.addButton() as it gives more flexibility like type of button, button placement, etc.
* @see QTags.addButton() * @see QTags.addButton()
*/ */
function edButton(id, display, tagStart, tagEnd, access, open) { function edButton(id, display, tagStart, tagEnd, access) {
return QTags.addButton( id, display, tagStart, tagEnd, access, '', -1 ); return QTags.addButton( id, display, tagStart, tagEnd, access, '', -1 );
} }
/* jshint ignore:end */
(function(){ (function(){
// private stuff is prefixed with an underscore // private stuff is prefixed with an underscore
var _domReady = function(func) { var _domReady = function(func) {
var t, i, DOMContentLoaded; var t, i, DOMContentLoaded, _tryReady;
if ( typeof jQuery != 'undefined' ) { if ( typeof jQuery !== 'undefined' ) {
jQuery(document).ready(func); jQuery(document).ready(func);
} else { } else {
t = _domReady; t = _domReady;
@ -105,16 +111,17 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
document.attachEvent('onreadystatechange', DOMContentLoaded); document.attachEvent('onreadystatechange', DOMContentLoaded);
window.attachEvent('onload', t.ready); window.attachEvent('onload', t.ready);
(function(){ _tryReady = function() {
try { try {
document.documentElement.doScroll("left"); document.documentElement.doScroll('left');
} catch(e) { } catch(e) {
setTimeout(arguments.callee, 50); setTimeout(_tryReady, 50);
return; return;
} }
t.ready(); t.ready();
})(); };
_tryReady();
} }
t.eventAttached = true; t.eventAttached = true;
@ -128,11 +135,12 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
zeroise = function(number) { zeroise = function(number) {
var str = number.toString(); var str = number.toString();
if ( str.length < 2 ) if ( str.length < 2 ) {
str = "0" + str; str = '0' + str;
}
return str; return str;
} };
return now.getUTCFullYear() + '-' + return now.getUTCFullYear() + '-' +
zeroise( now.getUTCMonth() + 1 ) + '-' + zeroise( now.getUTCMonth() + 1 ) + '-' +
@ -145,10 +153,11 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
qt; qt;
qt = QTags = function(settings) { qt = QTags = function(settings) {
if ( typeof(settings) == 'string' ) if ( typeof(settings) === 'string' ) {
settings = {id: settings}; settings = {id: settings};
else if ( typeof(settings) != 'object' ) } else if ( typeof(settings) !== 'object' ) {
return false; return false;
}
var t = this, var t = this,
id = settings.id, id = settings.id,
@ -156,15 +165,16 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
name = 'qt_' + id, name = 'qt_' + id,
tb, onclick, toolbar_id; tb, onclick, toolbar_id;
if ( !id || !canvas ) if ( !id || !canvas ) {
return false; return false;
}
t.name = name; t.name = name;
t.id = id; t.id = id;
t.canvas = canvas; t.canvas = canvas;
t.settings = settings; t.settings = settings;
if ( id == 'content' && typeof(adminpage) == 'string' && ( adminpage == 'post-new-php' || adminpage == 'post-php' ) ) { if ( id === 'content' && typeof(adminpage) === 'string' && ( adminpage === 'post-new-php' || adminpage === 'post-php' ) ) {
// back compat hack :-( // back compat hack :-(
edCanvas = canvas; edCanvas = canvas;
toolbar_id = 'ed_toolbar'; toolbar_id = 'ed_toolbar';
@ -185,8 +195,9 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
var target = e.target || e.srcElement, visible = target.clientWidth || target.offsetWidth, i; var target = e.target || e.srcElement, visible = target.clientWidth || target.offsetWidth, i;
// don't call the callback on pressing the accesskey when the button is not visible // don't call the callback on pressing the accesskey when the button is not visible
if ( !visible ) if ( !visible ) {
return; return;
}
// as long as it has the class ed_button, execute the callback // as long as it has the class ed_button, execute the callback
if ( / ed_button /.test(' ' + target.className + ' ') ) { if ( / ed_button /.test(' ' + target.className + ' ') ) {
@ -194,9 +205,10 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
t.canvas = canvas = document.getElementById(id); t.canvas = canvas = document.getElementById(id);
i = target.id.replace(name + '_', ''); i = target.id.replace(name + '_', '');
if ( t.theButtons[i] ) if ( t.theButtons[i] ) {
t.theButtons[i].callback.call(t.theButtons[i], target, canvas, t); t.theButtons[i].callback.call(t.theButtons[i], target, canvas, t);
} }
}
}; };
if ( tb.addEventListener ) { if ( tb.addEventListener ) {
@ -232,8 +244,9 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
defaults = ',strong,em,link,block,del,ins,img,ul,ol,li,code,more,close,'; defaults = ',strong,em,link,block,del,ins,img,ul,ol,li,code,more,close,';
for ( inst in t.instances ) { for ( inst in t.instances ) {
if ( inst == 0 ) if ( inst === 0 ) {
continue; continue;
}
ed = t.instances[inst]; ed = t.instances[inst];
canvas = ed.canvas; canvas = ed.canvas;
@ -244,34 +257,38 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
use = ''; use = '';
// set buttons // set buttons
if ( settings.buttons ) if ( settings.buttons ) {
use = ','+settings.buttons+','; use = ','+settings.buttons+',';
}
for ( i in edButtons ) { for ( i in edButtons ) {
if ( !edButtons[i] ) if ( !edButtons[i] ) {
continue; continue;
}
id = edButtons[i].id; id = edButtons[i].id;
if ( use && defaults.indexOf(','+id+',') != -1 && use.indexOf(','+id+',') == -1 ) if ( use && defaults.indexOf( ',' + id + ',' ) !== -1 && use.indexOf( ',' + id + ',' ) === -1 ) {
continue; continue;
}
if ( !edButtons[i].instance || edButtons[i].instance == inst ) { if ( !edButtons[i].instance || edButtons[i].instance === inst ) {
theButtons[id] = edButtons[i]; theButtons[id] = edButtons[i];
if ( edButtons[i].html ) if ( edButtons[i].html ) {
html += edButtons[i].html(name + '_'); html += edButtons[i].html(name + '_');
} }
} }
}
if ( use && use.indexOf(',fullscreen,') != -1 ) { if ( use && use.indexOf(',fullscreen,') !== -1 ) {
theButtons['fullscreen'] = new qt.FullscreenButton(); theButtons.fullscreen = new qt.FullscreenButton();
html += theButtons['fullscreen'].html(name + '_'); html += theButtons.fullscreen.html(name + '_');
} }
if ( 'rtl' == document.getElementsByTagName('html')[0].dir ) { if ( 'rtl' === document.getElementsByTagName('html')[0].dir ) {
theButtons['textdirection'] = new qt.TextDirectionButton(); theButtons.textdirection = new qt.TextDirectionButton();
html += theButtons['textdirection'].html(name + '_'); html += theButtons.textdirection.html(name + '_');
} }
ed.toolbar.innerHTML = html; ed.toolbar.innerHTML = html;
@ -309,8 +326,9 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
qt.addButton = function( id, display, arg1, arg2, access_key, title, priority, instance ) { qt.addButton = function( id, display, arg1, arg2, access_key, title, priority, instance ) {
var btn; var btn;
if ( !id || !display ) if ( !id || !display ) {
return; return;
}
priority = priority || 0; priority = priority || 0;
arg2 = arg2 || ''; arg2 = arg2 || '';
@ -324,12 +342,13 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
return; return;
} }
if ( priority == -1 ) // back-compat if ( priority === -1 ) { // back-compat
return btn; return btn;
}
if ( priority > 0 ) { if ( priority > 0 ) {
while ( typeof(edButtons[priority]) != 'undefined' ) { while ( typeof(edButtons[priority]) !== 'undefined' ) {
priority++ priority++;
} }
edButtons[priority] = btn; edButtons[priority] = btn;
@ -337,22 +356,24 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
edButtons[edButtons.length] = btn; edButtons[edButtons.length] = btn;
} }
if ( this.buttonsInitDone ) if ( this.buttonsInitDone ) {
this._buttonsInit(); // add the button HTML to all instances toolbars if addButton() was called too late this._buttonsInit(); // add the button HTML to all instances toolbars if addButton() was called too late
}
}; };
qt.insertContent = function(content) { qt.insertContent = function(content) {
var sel, startPos, endPos, scrollTop, text, canvas = document.getElementById(wpActiveEditor); var sel, startPos, endPos, scrollTop, text, canvas = document.getElementById(wpActiveEditor);
if ( !canvas ) if ( !canvas ) {
return false; return false;
}
if ( document.selection ) { //IE if ( document.selection ) { //IE
canvas.focus(); canvas.focus();
sel = document.selection.createRange(); sel = document.selection.createRange();
sel.text = content; sel.text = content;
canvas.focus(); canvas.focus();
} else if ( canvas.selectionStart || canvas.selectionStart == '0' ) { // FF, WebKit, Opera } else if ( canvas.selectionStart || canvas.selectionStart === '0' ) { // FF, WebKit, Opera
text = canvas.value; text = canvas.value;
startPos = canvas.selectionStart; startPos = canvas.selectionStart;
endPos = canvas.selectionEnd; endPos = canvas.selectionEnd;
@ -419,7 +440,7 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
var t = this, i = 0, ret = false; var t = this, i = 0, ret = false;
if ( ed.openTags ) { if ( ed.openTags ) {
while ( ret === false && i < ed.openTags.length ) { while ( ret === false && i < ed.openTags.length ) {
ret = ed.openTags[i] == t.id ? i : false; ret = ed.openTags[i] === t.id ? i : false;
i ++; i ++;
} }
} else { } else {
@ -434,10 +455,11 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
canvas.focus(); canvas.focus();
sel = document.selection.createRange(); sel = document.selection.createRange();
if ( sel.text.length > 0 ) { if ( sel.text.length > 0 ) {
if ( !t.tagEnd ) if ( !t.tagEnd ) {
sel.text = sel.text + t.tagStart; sel.text = sel.text + t.tagStart;
else } else {
sel.text = t.tagStart + sel.text + endTag; sel.text = t.tagStart + sel.text + endTag;
}
} else { } else {
if ( !t.tagEnd ) { if ( !t.tagEnd ) {
sel.text = t.tagStart; sel.text = t.tagStart;
@ -450,7 +472,7 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
} }
} }
canvas.focus(); canvas.focus();
} else if ( canvas.selectionStart || canvas.selectionStart == '0' ) { // FF, WebKit, Opera } else if ( canvas.selectionStart || canvas.selectionStart === '0' ) { // FF, WebKit, Opera
startPos = canvas.selectionStart; startPos = canvas.selectionStart;
endPos = canvas.selectionEnd; endPos = canvas.selectionEnd;
cursorPos = endPos; cursorPos = endPos;
@ -458,7 +480,7 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
l = v.substring(0, startPos); // left of the selection l = v.substring(0, startPos); // left of the selection
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 ) {
if ( !t.tagEnd ) { if ( !t.tagEnd ) {
canvas.value = l + i + t.tagStart + r; // insert self closing tags after the selection canvas.value = l + i + t.tagStart + r; // insert self closing tags after the selection
cursorPos += t.tagStart.length; cursorPos += t.tagStart.length;
@ -517,12 +539,13 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
button = ed.getButton(tbo[tbo.length - 1]); button = ed.getButton(tbo[tbo.length - 1]);
element = document.getElementById(ed.name + '_' + button.id); element = document.getElementById(ed.name + '_' + button.id);
if ( e ) if ( e ) {
button.callback.call(button, element, c, ed); button.callback.call(button, element, c, ed);
else } else {
button.closeTag(element, ed); button.closeTag(element, ed);
} }
} }
}
}; };
qt.CloseButton.prototype.callback = qt._close; qt.CloseButton.prototype.callback = qt._close;
@ -540,13 +563,14 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
qt.LinkButton.prototype.callback = function(e, c, ed, defaultValue) { qt.LinkButton.prototype.callback = function(e, c, ed, defaultValue) {
var URL, t = this; var URL, t = this;
if ( typeof(wpLink) != 'undefined' ) { if ( typeof(wpLink) !== 'undefined' ) {
wpLink.open(); wpLink.open();
return; return;
} }
if ( ! defaultValue ) if ( ! defaultValue ) {
defaultValue = 'http://'; defaultValue = 'http://';
}
if ( t.isOpen(ed) === false ) { if ( t.isOpen(ed) === false ) {
URL = prompt(quicktagsL10n.enterURL, defaultValue); URL = prompt(quicktagsL10n.enterURL, defaultValue);
@ -581,27 +605,29 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
}; };
qt.FullscreenButton.prototype = new qt.Button(); qt.FullscreenButton.prototype = new qt.Button();
qt.FullscreenButton.prototype.callback = function(e, c) { qt.FullscreenButton.prototype.callback = function(e, c) {
if ( !c.id || typeof(fullscreen) == 'undefined' ) if ( !c.id || typeof(fullscreen) === 'undefined' ) {
return; return;
}
fullscreen.on(); fullscreen.on();
}; };
qt.TextDirectionButton = function() { qt.TextDirectionButton = function() {
qt.Button.call(this, 'textdirection', quicktagsL10n.textdirection, '', quicktagsL10n.toggleTextdirection) qt.Button.call(this, 'textdirection', quicktagsL10n.textdirection, '', quicktagsL10n.toggleTextdirection);
}; };
qt.TextDirectionButton.prototype = new qt.Button(); qt.TextDirectionButton.prototype = new qt.Button();
qt.TextDirectionButton.prototype.callback = function(e, c) { qt.TextDirectionButton.prototype.callback = function(e, c) {
var isRTL = ( 'rtl' == document.getElementsByTagName('html')[0].dir ), var isRTL = ( 'rtl' === document.getElementsByTagName('html')[0].dir ),
currentDirection = c.style.direction; currentDirection = c.style.direction;
if ( ! currentDirection ) if ( ! currentDirection ) {
currentDirection = ( isRTL ) ? 'rtl' : 'ltr'; currentDirection = ( isRTL ) ? 'rtl' : 'ltr';
c.style.direction = ( 'rtl' == currentDirection ) ? 'ltr' : 'rtl';
c.focus();
} }
c.style.direction = ( 'rtl' === currentDirection ) ? 'ltr' : 'rtl';
c.focus();
};
// ensure backward compatibility // ensure backward compatibility
edButtons[10] = new qt.TagButton('strong','b','<strong>','</strong>','b'); edButtons[10] = new qt.TagButton('strong','b','<strong>','</strong>','b');
edButtons[20] = new qt.TagButton('em','i','<em>','</em>','i'), edButtons[20] = new qt.TagButton('em','i','<em>','</em>','i'),
@ -615,6 +641,6 @@ function edButton(id, display, tagStart, tagEnd, access, open) {
edButtons[100] = new qt.TagButton('li','li','\t<li>','</li>\n','l'), edButtons[100] = new qt.TagButton('li','li','\t<li>','</li>\n','l'),
edButtons[110] = new qt.TagButton('code','code','<code>','</code>','c'), edButtons[110] = new qt.TagButton('code','code','<code>','</code>','c'),
edButtons[120] = new qt.TagButton('more','more','<!--more-->','','t'), edButtons[120] = new qt.TagButton('more','more','<!--more-->','','t'),
edButtons[140] = new qt.CloseButton() edButtons[140] = new qt.CloseButton();
})(); })();