r75336 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75335‎ | r75336 | r75337 >
Date:19:32, 24 October 2010
Author:krinkle
Status:ok
Tags:
Comment:
added support for standard, cologneblue, nostalgia in mw.util.addPortletLink + updated the test suite
Modified paths:
  • /trunk/phase3/resources/mediawiki/mediawiki.util.js (modified) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.utiltest.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki/mediawiki.util.js
@@ -12,7 +12,6 @@
1313 if (this.initialised === false) {
1414 this.initialised = true;
1515
16 -
1716 // Set tooltipAccessKeyPrefix
1817 if (is_opera) {
1918 this.tooltipAccessKeyPrefix = 'shift-esc-';
@@ -52,7 +51,18 @@
5352
5453 // Any initialisation after the DOM is ready
5554 $(function () {
 55+
 56+ // Enable CheckboxShiftClick
5657 $('input[type=checkbox]:not(.noshiftselect)').enableCheckboxShiftClick();
 58+
 59+ // Fill bodyContant var
 60+ if ($('#bodyContent').length) {
 61+ mw.util.$content = $('#bodyContent');
 62+ } else if ($('#article').length) {
 63+ mw.util.$content = $('#article');
 64+ } else {
 65+ mw.util.$content = $('#content');
 66+ }
5767 });
5868
5969
@@ -200,6 +210,10 @@
201211 }
202212 });
203213 },
 214+
 215+ // jQuery object that refers to the page-content element
 216+ // Populated by init()
 217+ '$content' : null,
204218
205219
206220 /**
@@ -223,60 +237,74 @@
224238 * @param String accesskey accesskey to activate this link (one character, try to avoid conflicts. Use $('[accesskey=x').get() in the console to see if 'x' is already used.
225239 * @param mixed nextnode DOM node or jQuery-selector of the item that the new item should be added before, should be another item in the same list will be ignored if not the so
226240 *
227 - * @return Node the DOM node of the new item (a LI element) or null
 241+ * @return Node the DOM node of the new item (a LI element, or A element for older skins) or null
228242 */
229243 'addPortletLink' : function (portlet, href, text, id, tooltip, accesskey, nextnode) {
230 - var $portlet = $('#' + portlet);
231 - if ($portlet.length === 0) {
232 - return null;
233 - }
234 - var $ul = $portlet.find('ul').eq(0);
235 - if ($ul.length === 0) {
236 - if ($portlet.find('div').length === 0) {
237 - $portlet.append('<ul />');
238 - } else {
239 - $portlet.find('div').eq(-1).append('<ul />');
 244+ var $link = $('<a />').attr('href', href).text(text);
 245+
 246+ // Some skins don't have portlets
 247+ // Just add it to the bottom of their 'sidebar' element ignoring the specified portlet target
 248+ switch (skin) {
 249+ case 'standard' :
 250+ case 'cologneblue' :
 251+ $("#quickbar").append($link.after('<br />'));
 252+ return $link.get(0);
 253+ case 'nostalgia' :
 254+ $("#searchform").before($link).before(' &#124; ');
 255+ return $link.get(0);
 256+ default : // chick, modern, monobook, myskin, simple, vector...
 257+
 258+ var $portlet = $('#' + portlet);
 259+ if ($portlet.length === 0) {
 260+ return null;
240261 }
241 - $ul = $portlet.find('ul').eq(0);
242 - }
243 - if ($ul.length === 0) {
244 - return null;
245 - }
 262+ var $ul = $portlet.find('ul').eq(0);
 263+ if ($ul.length === 0) {
 264+ if ($portlet.find('div').length === 0) {
 265+ $portlet.append('<ul />');
 266+ } else {
 267+ $portlet.find('div').eq(-1).append('<ul />');
 268+ }
 269+ $ul = $portlet.find('ul').eq(0);
 270+ }
 271+ if ($ul.length === 0) {
 272+ return null;
 273+ }
 274+
 275+ // unhide portlet if it was hidden before
 276+ $portlet.removeClass('emptyPortlet');
 277+
 278+ var $item = $link.wrap('<li><span /></li>').parent().parent();
246279
247 - // unhide portlet if it was hidden before
248 - $portlet.removeClass('emptyPortlet');
249 -
250 - var $link = $('<a />').attr('href', href).text(text);
251 - var $item = $link.wrap('<li><span /></li>').parent().parent();
252 -
253 - if (id) {
254 - $item.attr('id', id);
255 - }
256 - if (accesskey) {
257 - $link.attr('accesskey', accesskey);
258 - tooltip += ' [' + accesskey + ']';
259 - }
260 - if (tooltip) {
261 - $link.attr('title', tooltip);
262 - }
263 - if (accesskey && tooltip) {
264 - this.updateTooltipAccessKeys($link);
265 - }
266 -
267 - // Append using DOM-element passing
268 - if (nextnode && nextnode.parentNode == $ul.get(0)) {
269 - $(nextnode).before($item);
270 - } else {
271 - // If the jQuery selector isn't found within the <ul>, just append it at the end
272 - if ($ul.find(nextnode).length === 0) {
273 - $ul.append($item);
 280+ if (id) {
 281+ $item.attr('id', id);
 282+ }
 283+ if (accesskey) {
 284+ $link.attr('accesskey', accesskey);
 285+ tooltip += ' [' + accesskey + ']';
 286+ }
 287+ if (tooltip) {
 288+ $link.attr('title', tooltip);
 289+ }
 290+ if (accesskey && tooltip) {
 291+ this.updateTooltipAccessKeys($link);
 292+ }
 293+
 294+ // Append using DOM-element passing
 295+ if (nextnode && nextnode.parentNode == $ul.get(0)) {
 296+ $(nextnode).before($item);
274297 } else {
275 - // Append using jQuery CSS selector
276 - $ul.find(nextnode).eq(0).before($item);
 298+ // If the jQuery selector isn't found within the <ul>, just append it at the end
 299+ if ($ul.find(nextnode).length === 0) {
 300+ $ul.append($item);
 301+ } else {
 302+ // Append using jQuery CSS selector
 303+ $ul.find(nextnode).eq(0).before($item);
 304+ }
277305 }
 306+
 307+ return $item.get(0);
278308 }
279 -
280 - return $item.get(0);
281309 }
282310
283311 };
Index: trunk/phase3/resources/mediawiki/mediawiki.utiltest.js
@@ -8,7 +8,6 @@
99 mediaWiki.test = {
1010
1111 /* Variables */
12 - '$bodyContent' : null,
1312 '$table' : null,
1413 'addedTests' : [],
1514
@@ -18,10 +17,14 @@
1918 * Adds a row to the test-table
2019 *
2120 * @param String code Code of the test to be executed
22 - * @param String result Expected result in 'var (vartype)' form.
 21+ * @param String result Expected result in 'var (vartype)' form
 22+ * @param String contain Important part of the result, if result is different but does contain this it will not return ERROR but PARTIALLY
2323 */
24 - 'addTest' : function (code, result) {
25 - this.addedTests.push([code, result]);
 24+ 'addTest' : function (code, result, contain) {
 25+ if (!contain) {
 26+ contain = result;
 27+ }
 28+ this.addedTests.push([code, result, contain]);
2629 this.$table.append('<tr><td>' + mw.util.htmlEscape(code) + '</td><td>' + mw.util.htmlEscape(result) + '<td></td></td><td>?</td></tr>');
2730 },
2831
@@ -36,8 +39,7 @@
3740 // Build page
3841 document.title = 'mediaWiki.util JavaScript Test - ' + wgSiteName;
3942 $('#firstHeading').text('mediaWiki.util JavaScript Test');
40 - mw.test.bodyContent = $('#bodyContent');
41 - mw.test.bodyContent.html(
 43+ mw.util.$content.html(
4244 '<p>Below is a list of tests to confirm proper functionality of the mediaWiki.util functions</p>' +
4345 '<hr />' +
4446 '<table id="mw-mwutiltest-table" class="wikitable sortable"><tr><th>Exec</th><th>Should return</th><th>Does return</th><th>Equal ?</th></tr></table>'
@@ -95,14 +97,17 @@
9698 'function (string)');
9799 mw.test.addTest('typeof mw.util.addPortletLink("p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-print")',
98100 'object (string)');
99 - mw.test.addTest('mw.util.addPortletLink("p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-print").outerHTML',
100 - '<li id="t-mworg"><span><a href="http://mediawiki.org/" accesskey="m" title="Go to MediaWiki.org [ctrl-alt-m]">MediaWiki.org</a></span></li> (string)');
 101+ mw.test.addTest('a = mw.util.addPortletLink("p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-print"); if(a){ a.outerHTML; }',
 102+ '<li id="t-mworg"><span><a href="http://mediawiki.org/" accesskey="m" title="Go to MediaWiki.org [ctrl-alt-m]">MediaWiki.org</a></span></li> (string)',
 103+ 'href="http://mediawiki.org/"');
101104
102105 // Run tests and compare results
103106 var exec,
104107 result,
105108 resulttype,
106109 numberoftests = 0,
 110+ numberofpasseds = 0,
 111+ numberofpartials = 0,
107112 numberoferrors = 0,
108113 $testrows;
109114 $testrows = mw.test.$table.find('tr');
@@ -111,13 +116,20 @@
112117
113118 exec = mw.test.addedTests[i][0];
114119 shouldreturn = mw.test.addedTests[i][1];
 120+ shouldcontain = mw.test.addedTests[i][2];
115121 doesreturn = eval(exec);
116122 doesreturn = doesreturn + ' (' + typeof doesreturn + ')';
117123 $thisrow = $testrows.eq(i + 1);
118124 $thisrow.find('> td').eq(2).text(doesreturn);
119125
120 - if (shouldreturn === doesreturn) {
121 - $thisrow.find('> td').eq(3).css('background', '#EFE').text('OK');
 126+ if (doesreturn.indexOf(shouldcontain) !== -1) {
 127+ if (doesreturn == shouldreturn){
 128+ $thisrow.find('> td').eq(3).css('background', '#EFE').text('OK');
 129+ numberofpasseds++;
 130+ } else {
 131+ $thisrow.find('> td').eq(3).css('background', '#FFE').html('<small>PARTIALLY</small>');
 132+ numberofpartials++;
 133+ }
122134 } else {
123135 $thisrow.find('> td').eq(3).css('background', '#FEE').text('ERROR');
124136 numberoferrors++;
@@ -125,7 +137,7 @@
126138
127139 })
128140 );
129 - mw.test.$table.before('<p><strong>Ran ' + numberoftests + ' tests. ' + numberoferrors + ' error(s). </p>');
 141+ mw.test.$table.before('<p><strong>Ran ' + numberoftests + ' tests. ' + numberofpasseds + ' passed test(s). ' + numberoferrors + ' error(s). ' + numberofpartials + ' partially passed test(s). </p>');
130142
131143 }
132144 });

Status & tagging log