Umherirrender has uploaded a new change for review. https://gerrit.wikimedia.org/r/94601
Change subject: mw.util.addPortletLink: Check length before access array index ...................................................................... mw.util.addPortletLink: Check length before access array index This avoids "TypeError: Cannot read property 'parentNode' of undefined", when the given nextNode is a jQuery object without elements. Adding a test for this case and for the case of unknown id, which internally results in a empty jQuery object. Bug: 56770 Change-Id: I56de9e6bc15449e294b538743baf0f0c1cc0abb1 --- M RELEASE-NOTES-1.23 M resources/mediawiki/mediawiki.util.js M tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js 3 files changed, 17 insertions(+), 3 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/01/94601/1 diff --git a/RELEASE-NOTES-1.23 b/RELEASE-NOTES-1.23 index 64c55f5..30ecf37 100644 --- a/RELEASE-NOTES-1.23 +++ b/RELEASE-NOTES-1.23 @@ -45,6 +45,7 @@ when the email address is already confirmed. Also, consistently use "confirmed", rather than "authenticated", when messaging whether or not the user has confirmed an email address. +* (bug 56770) mw.util.addPortletLink: Check length before access array index. === API changes in 1.23 === * (bug 54884) action=parse&prop=categories now indicates hidden and missing diff --git a/resources/mediawiki/mediawiki.util.js b/resources/mediawiki/mediawiki.util.js index cfc717f..be56de7 100644 --- a/resources/mediawiki/mediawiki.util.js +++ b/resources/mediawiki/mediawiki.util.js @@ -448,7 +448,7 @@ // nextnode is a DOM element (was the only option before MW 1.17, in wikibits.js) // or nextnode is a CSS selector for jQuery nextnode = $ul.find( nextnode ); - } else if ( !nextnode.jquery || nextnode[0].parentNode !== $ul[0] ) { + } else if ( !nextnode.jquery || ( nextnode.length > 0 && nextnode[0].parentNode !== $ul[0] ) ) { // Fallback $ul.append( $item ); return $item[0]; diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js index 2be8044..ba7ec7f 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.util.test.js @@ -141,8 +141,9 @@ * Previously, test elements where invisible to the selector since only * one element can have a given id. */ - QUnit.test( 'addPortletLink', 11, function ( assert ) { - var pTestTb, pCustom, vectorTabs, tbRL, cuQuux, $cuQuux, tbMW, $tbMW, tbRLDM, caFoo, addedAfter; + QUnit.test( 'addPortletLink', 13, function ( assert ) { + var pTestTb, pCustom, vectorTabs, tbRL, cuQuux, $cuQuux, tbMW, $tbMW, tbRLDM, + tbRLDMunknown, tbRLDMunknownJ, caFoo, addedAfter; pTestTb = '\ <div class="portlet" id="p-test-tb">\ @@ -223,6 +224,18 @@ 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)' ); + + // test case - unknown id as next node + tbRLDMunknown = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM', + 'Default modules', 't-rldm-unknown', 'List of all default modules ', 'd', '#t-rl-unknown' ); + + assert.equal( tbRLDMunknown, $( '#p-test-tb li:last' )[0], 'Invalid id as nextnode adds the portlet add end' ); + + // test case - empty jquery object as next node + tbRLDMunknownJ = mw.util.addPortletLink( 'p-test-tb', '//mediawiki.org/wiki/RL/DM', + 'Default modules', 't-rldm-unknown-j', 'List of all default modules ', 'd', $( '#t-rl-unknown-j' ) ); + + assert.equal( tbRLDMunknownJ, $( '#p-test-tb li:last' )[0], 'Empty jquery as nextnode adds the portlet add end' ); } ); QUnit.test( 'jsMessage', 1, function ( assert ) { -- To view, visit https://gerrit.wikimedia.org/r/94601 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I56de9e6bc15449e294b538743baf0f0c1cc0abb1 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Umherirrender <umherirrender_de...@web.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits