Fix exception thrown in mw.util.addPortletLink
authorKrinkle <krinkle@users.mediawiki.org>
Sun, 8 May 2011 20:48:20 +0000 (20:48 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Sun, 8 May 2011 20:48:20 +0000 (20:48 +0000)
* As of jQuery 1.4.4, Sizzle's query must be a string (it does a query.replace call before everything else). It fails otherwise.
* In REL1_17 and 1.17wmf1 this isn't a problem since jQuery 1.4.2 is there, in which Sizzle didn't fail on non-strings yet.
* Nonetheless this is undocumented support, we should make sure that the variable passed is a string on our side
* Adding a check for it in mw.util.addPortletLink's nextnode argument.
* Adding a test to mw.util.test that calls addPortletLink with three arguments, making nextnode implied undefined/null. This test returned "ERROR" before I made the fix in mw.util.addPortletLink.

Exact error: TypeError: Result of expression 'query' [undefined] is not an object.
Origin: line 4085 of jQuery/mediaWiki module load.
Line 4081: Sizzle = function( query, context, extra, seed ) {
Line 4085:     query = query.replace(/\=\s*([^'"\]]*)\s*\]/g, "='$1']");

resources/mediawiki.util/mediawiki.util.js
resources/mediawiki.util/mediawiki.util.test.js

index 39ceb77..dd53c43 100644 (file)
                                        this.updateTooltipAccessKeys( $link );
                                }
 
-                               // Append using DOM-element passing
-                               if ( nextnode && nextnode.parentNode == $ul[0] ) {
+                               // Where to put our node ?
+                               // - nextnode is a DOM element (before MW 1.17, in wikibits.js, this was the only option)
+                               if ( nextnode && nextnode.parentNode == $ul[0]  ) {
                                        $(nextnode).before( $item );
+
+                               // - nextnode is a CSS selector for jQuery
+                               } else if ( typeof nextnode == 'string' && $ul.find( nextnode ).length !== 0 ) {
+                                       $ul.find( nextnode ).eq( 0 ).before( $item );
+
+
+                               // If the jQuery selector isn't found within the <ul>,
+                               // or if nextnode was invalid or not passed at all,
+                               // then just append it at the end of the <ul> (this is the default behaviour)
                                } else {
-                                       // If the jQuery selector isn't found within the <ul>, just
-                                       // append it at the end
-                                       if ( $ul.find( nextnode ).length === 0 ) {
-                                               $ul.append( $item );
-                                       } else {
-                                               // Append using jQuery CSS selector
-                                               $ul.find( nextnode ).eq( 0 ).before( $item );
-                                       }
+                                       $ul.append( $item );
                                }
+                               
 
                                return $item[0];
                        }
index dcec29d..135e29e 100644 (file)
                                                mw.test.addTest( 'var a = mw.util.addPortletLink( "p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-rl" ); $(a).text();',
                                                        'MediaWiki.org (string)' );
 
+                                               mw.test.addTest( 'typeof mw.util.addPortletLink( "p-tb", "http://www.mediawiki.org/wiki/ResourceLoader/Default_modules", "Default modules", "t-rl", "All default modules present in MediaWiki" )',
+                                                       'object (string)' );
+
                                                mw.test.addTest( 'typeof mw.util.jsMessage',
                                                        'function (string)' );