r75552 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75551‎ | r75552 | r75553 >
Date:15:35, 27 October 2010
Author:krinkle
Status:ok
Tags:
Comment:
Fixed unfinished line from r75275, commented addPortletLink() more, moved prototypes from mediawiki.util.js to mediawiki.js, added String.prototype.escapeRE()
Modified paths:
  • /trunk/phase3/resources/mediawiki.util/mediawiki.util.js (modified) (history)
  • /trunk/phase3/resources/mediawiki.util/mediawiki.util.test.js (modified) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki.util/mediawiki.util.test.js
@@ -69,8 +69,12 @@
7070 'function (string)');
7171 mw.test.addTest('\'mediawiki\'.ucFirst()',
7272 'Mediawiki (string)');
73 - mw.test.addTest('typeof $.fn.enableCheckboxShiftClick',
 73+ mw.test.addTest('typeof String.prototype.escapeRE',
7474 'function (string)');
 75+ mw.test.addTest('\'.st{e}$st\'.escapeRE()',
 76+ '\\.st\\{e\\}\\$st (string)');
 77+ mw.test.addTest('typeof $.fn.checkboxShiftClick',
 78+ 'function (string)');
7579 mw.test.addTest('typeof mw.util.rawurlencode',
7680 'function (string)');
7781 mw.test.addTest('mw.util.rawurlencode(\'Test: A&B/Here\')',
Index: trunk/phase3/resources/mediawiki.util/mediawiki.util.js
@@ -26,13 +26,6 @@
2727 this.tooltipAccessKeyPrefix = 'alt-shift-';
2828 }
2929
30 - // Prototype enhancements
31 - if (typeof String.prototype.ucFirst === 'undefined') {
32 - String.prototype.ucFirst = function () {
33 - return this.substr(0, 1).toUpperCase() + this.substr(1, this.length);
34 - };
35 - }
36 -
3730 // Any initialisation after the DOM is ready
3831 $(function () {
3932
@@ -62,7 +55,7 @@
6356 *
6457 * @param String str string to be encoded
6558 */
66 - 'rawurlencode' : function (str) {
 59+ 'rawurlencode' : function( str ) {
6760 str = (str + '').toString();
6861 return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28')
6962 .replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/~/g, '%7E');
@@ -75,7 +68,7 @@
7669 *
7770 * @param String str string to be encoded
7871 */
79 - 'wikiUrlencode' : function (str) {
 72+ 'wikiUrlencode' : function( str ) {
8073 return this.rawurlencode(str).replace(/%20/g, '_').replace(/%3A/g, ':').replace(/%2F/g, '/');
8174 },
8275
@@ -84,7 +77,7 @@
8578 *
8679 * @param String str pagename to link to
8780 */
88 - 'getWikilink' : function (str) {
 81+ 'getWikilink' : function( str ) {
8982 return wgServer + wgArticlePath.replace('$1', this.wikiUrlencode(str));
9083 },
9184
@@ -94,7 +87,7 @@
9588 *
9689 * @param Mixed v the variable to check for empty ness
9790 */
98 - 'isEmpty' : function (v) {
 91+ 'isEmpty' : function( v ) {
9992 var key;
10093 if (v === "" || v === 0 || v === "0" || v === null || v === false || typeof v === 'undefined') {
10194 return true;
@@ -119,7 +112,7 @@
120113 * @param String param paramter name
121114 * @param String url url to search through (optional)
122115 */
123 - 'getParamValue' : function (param, url) {
 116+ 'getParamValue' : function( param, url ) {
124117 url = url ? url : document.location.href;
125118 var re = new RegExp('[^#]*[&?]' + param + '=([^&#]*)'); // Get last match, stop at hash
126119 var m = re.exec(url);
@@ -135,7 +128,7 @@
136129 * @param String str text to escape
137130 * @param Bool quotes if true escapes single and double quotes aswell (by default false)
138131 */
139 - 'htmlEscape' : function (str, quotes) {
 132+ 'htmlEscape' : function( str, quotes ) {
140133 str = $('<div/>').text(str).html();
141134 if (typeof quotes === 'undefined') {
142135 quotes = false;
@@ -151,7 +144,7 @@
152145 *
153146 * @param String str text to unescape
154147 */
155 - 'htmlUnescape' : function (str) {
 148+ 'htmlUnescape' : function( str ) {
156149 return $('<div/>').html(str).text();
157150 },
158151
@@ -170,19 +163,19 @@
171164 *
172165 * @param Mixed nodeList jQuery object, or array of elements
173166 */
174 - 'updateTooltipAccessKeys' : function (nodeList) {
 167+ 'updateTooltipAccessKeys' : function( nodeList ) {
175168 var $nodes;
176169 if (nodeList instanceof jQuery) {
177170 $nodes = nodeList;
178171 } else if (nodeList) {
179172 $nodes = $(nodeList);
180173 } else {
181 - // Rather than scanning all links, just
182 - $('#column-one a, #mw-head a, #mw-panel a, #p-logo a');
 174+ // Rather than scanning all links, just the elements that contain the relevant links
 175+ this.updateTooltipAccessKeys( $('#column-one a, #mw-head a, #mw-panel a, #p-logo a') );
183176
184177 // these are rare enough that no such optimization is needed
185 - this.updateTooltipAccessKeys($('input'));
186 - this.updateTooltipAccessKeys($('label'));
 178+ this.updateTooltipAccessKeys( $('input') );
 179+ this.updateTooltipAccessKeys( $('label') );
187180 return;
188181 }
189182
@@ -223,11 +216,13 @@
224217 *
225218 * @return Node the DOM node of the new item (a LI element, or A element for older skins) or null
226219 */
227 - 'addPortletLink' : function (portlet, href, text, id, tooltip, accesskey, nextnode) {
 220+ 'addPortletLink' : function( portlet, href, text, id, tooltip, accesskey, nextnode ) {
 221+
 222+ // Setup the anchor tag
228223 var $link = $('<a />').attr('href', href).text(text);
229224
230 - // Some skins don't have portlets
231 - // Just add it to the bottom of their 'sidebar' element ignoring the specified portlet target
 225+ // Some skins don't have any portlets
 226+ // just add it to the bottom of their 'sidebar' element as a fallback
232227 switch (skin) {
233228 case 'standard' :
234229 case 'cologneblue' :
@@ -236,30 +231,41 @@
237232 case 'nostalgia' :
238233 $("#searchform").before($link).before(' &#124; ');
239234 return $link.get(0);
240 - default : // chick, modern, monobook, myskin, simple, vector...
 235+ default : // Skins like chick, modern, monobook, myskin, simple, vector...
241236
 237+ // Select the specified portlet
242238 var $portlet = $('#' + portlet);
243239 if ($portlet.length === 0) {
244240 return null;
245241 }
 242+ // Select the first (most likely only) unordered list inside the portlet
246243 var $ul = $portlet.find('ul').eq(0);
 244+
 245+ // If it didn't have an unordered list yet, create it
247246 if ($ul.length === 0) {
 247+ // If there's no <div> inside, append it to the portlet directly
248248 if ($portlet.find('div').length === 0) {
249 - $portlet.append('<ul />');
 249+ $portlet.append('<ul/>');
250250 } else {
251 - $portlet.find('div').eq(-1).append('<ul />');
 251+ // otherwise if there's a div (such as div.body or div.pBody) append the <ul> to last (most likely only) div
 252+ $portlet.find('div').eq(-1).append('<ul/>');
252253 }
 254+ // Select the created element
253255 $ul = $portlet.find('ul').eq(0);
254256 }
 257+ // Just in case..
255258 if ($ul.length === 0) {
256259 return null;
257260 }
258261
259 - // unhide portlet if it was hidden before
 262+ // Unhide portlet if it was hidden before
260263 $portlet.removeClass('emptyPortlet');
261264
 265+ // Wrap the anchor tag in a <span> and create a list item for it
 266+ // and back up the selector to the list item
262267 var $item = $link.wrap('<li><span /></li>').parent().parent();
263268
 269+ // Implement the properties passed to the function
264270 if (id) {
265271 $item.attr('id', id);
266272 }
Index: trunk/phase3/resources/mediawiki/mediawiki.js
@@ -22,6 +22,25 @@
2323 }
2424
2525 /*
 26+ * Prototype enhancements
 27+ */
 28+
 29+// Capitalize the first character of the given string
 30+if ( typeof String.prototype.ucFirst === 'undefined' ) {
 31+ String.prototype.ucFirst = function() {
 32+ return this.substr(0, 1).toUpperCase() + this.substr(1, this.length);
 33+ };
 34+}
 35+
 36+// Escape all RegExp special characters such that the result can be safely used
 37+// in a RegExp as a literal.
 38+if ( typeof String.prototype.escapeRE === 'undefined' ) {
 39+ String.prototype.escapeRE = function() {
 40+ return this.replace (/([\\{}()|.?*+^$\[\]])/g, "\\$1");
 41+ };
 42+}
 43+
 44+/*
2645 * Core MediaWiki JavaScript Library
2746 */
2847

Follow-up revisions

RevisionCommit summaryAuthorDate
r76320As per r 75486 CR comments, no prototyping in mw core....krinkle18:13, 8 November 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r75275adding begin of mediaWiki.util and loading it by default + trailing whitespac...krinkle17:24, 23 October 2010

Status & tagging log