mediawiki.util: Fix addPortletLink to allow jQuery objects
authorMark Holmquist <mtraceur@member.fsf.org>
Thu, 1 Aug 2013 00:25:29 +0000 (17:25 -0700)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 1 Aug 2013 23:31:53 +0000 (01:31 +0200)
The docs already say jQuery is a supported type for nextnode, it just
wasn't yet checking for it in the function.

Change-Id: I0bdc7dacd24aac720b70c6fc0ca3154ceccd0ebb

RELEASE-NOTES-1.22
resources/mediawiki/mediawiki.util.js
tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js

index a06ca64..2142522 100644 (file)
@@ -172,6 +172,7 @@ production.
   too. Can be used whenever several multicast group could be interested by a
   specific purge.
 * (bug 25931) Add Special:RandomInCategory.
+* mediawiki.util: addPortletLink now supports passing a jQuery object as nextnode.
 
 === Bug fixes in 1.22 ===
 * Disable Special:PasswordReset when $wgEnableEmail is false. Previously one
index b482823..6f76839 100644 (file)
                                $link.attr( 'accesskey', accesskey );
                        }
 
+                       // nextnode is a DOM element (was the only option before MW 1.17, in wikibits.js)
+                       // so we make it a jQuery object!
+                       if ( nextnode && nextnode.nodeType ) {
+                               nextnode = $( nextnode );
+                       }
+
                        // Where to put our node ?
-                       // - nextnode is a DOM element (was the only option before MW 1.17, in wikibits.js)
-                       if ( nextnode && nextnode.parentNode === $ul[0] ) {
-                               $( nextnode ).before( $item );
+                       // - nextnode is a jQuery object that represents exactly one element
+                       if ( nextnode && nextnode.jquery && nextnode.length === 1 && nextnode[0].parentNode === $ul[0] ) {
+                               nextnode.before( $item );
 
                        // - nextnode is a CSS selector for jQuery
                        } else if ( typeof nextnode === 'string' && $ul.find( nextnode ).length !== 0 ) {
index 674f56a..e867369 100644 (file)
         * Previously, test elements where invisible to the selector since only
         * one element can have a given id.
         */
-       QUnit.test( 'addPortletLink', 10, function ( assert ) {
-               var pTestTb, pCustom, vectorTabs, tbRL, cuQuux, $cuQuux, tbMW, $tbMW, tbRLDM, caFoo;
+       QUnit.test( 'addPortletLink', 11, function ( assert ) {
+               var pTestTb, pCustom, vectorTabs, tbRL, cuQuux, $cuQuux, tbMW, $tbMW, tbRLDM, caFoo, addedAfter;
 
                pTestTb = '\
                <div class="portlet" id="p-test-tb">\
                );
 
                assert.equal( $tbMW.closest( '.portlet' ).attr( 'id' ), 'p-test-tb', 'Link was inserted within correct portlet' );
-               assert.equal( $tbMW.next().attr( 'id' ), 't-rl', 'Link is in the correct position (by passing nextnode)' );
+               assert.strictEqual( $tbMW.next()[0], tbRL, 'Link is in the correct position (by passing nextnode)' );
 
                cuQuux = mw.util.addPortletLink( 'p-test-custom', '#', 'Quux', null, 'Example [shift-x]', 'q' );
                $cuQuux = $( cuQuux );
 
                assert.strictEqual( $tbMW.find( 'span' ).length, 0, 'No <span> element should be added for porlets without vectorTabs class.' );
                assert.strictEqual( $( caFoo ).find( 'span' ).length, 1, 'A <span> element should be added for porlets with vectorTabs class.' );
+
+               addedAfter = mw.util.addPortletLink( 'p-test-tb', '#', 'After foo', 'post-foo', 'After foo', null, $( tbRL ) );
+               assert.strictEqual( $( addedAfter ).next()[0], tbRL, 'Link is in the correct position (by passing a jQuery object as nextnode)' );
        } );
 
        QUnit.test( 'jsMessage', 1, function ( assert ) {