Jdlrobson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/327137 )

Change subject: Test and correct behaviour of getSectionId
......................................................................

Test and correct behaviour of getSectionId

Bug: T146394
Change-Id: I36c231c13f8939ad50b9c3d0fb1621b6b7ea0021
---
M resources/mobile.startup/Skin.js
M tests/qunit/mobile.startup/test_Skin.js
2 files changed, 74 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/37/327137/1

diff --git a/resources/mobile.startup/Skin.js b/resources/mobile.startup/Skin.js
index 65e3183..e4038c5 100644
--- a/resources/mobile.startup/Skin.js
+++ b/resources/mobile.startup/Skin.js
@@ -7,28 +7,28 @@
        /**
         * Get the id of the section $el belongs to.
         * @param {jQuery.Object} $el
-        * @return {string}
+        * @return {string} which is empty if the element belongs to no section
         * @ignore
         */
        function getSectionId( $el ) {
-               var hSelector = 'h1,h2,h3,h4,h5,h6',
+               var id,
+                       hSelector = 'h1,h2,h3,h4,h5,h6',
+                       $parent = $el.parent(),
                        // e.g. matches Subheading in
                        // <h2>H</h2><div><h3 id="subheading">Subh</h3><a 
class="element"></a></div>
-                       id = $el.prevAll( hSelector ).eq( 0 )
-                               .find( '.mw-headline' ).attr( 'id' );
+                       $heading = $el.prevAll( hSelector ).eq( 0 );
 
-               // if there's no headline preceding the placeholder then it is 
inside a section
-               // and the id is of the collapsible heading preceding the 
section.
-               // e.g. matches heading in
-               // <div id="mw-content-text">
-               //   <h2 id="heading">Heading</h2>
-               //   <div><a class="element"></a></div>
-               // </div>
-               if ( id === undefined ) {
-                       id = $el.parents( '#mw-content-text > div' ).prevAll( 
hSelector ).eq( 0 )
-                               .find( '.mw-headline' ).attr( 'id' );
+               if ( $heading.length ) {
+                       id = $heading.find( '.mw-headline' ).attr( 'id' );
+                       if ( id ) {
+                               return id;
+                       }
                }
-               return id;
+               if ( $parent.length ) {
+                       return getSectionId( $parent );
+               } else {
+                       return '';
+               }
        }
 
        /**
@@ -368,6 +368,7 @@
                }
        } );
 
+       Skin.getSectionId = getSectionId;
        M.define( 'mobile.startup/Skin', Skin );
 
 }( mw.mobileFrontend, jQuery ) );
diff --git a/tests/qunit/mobile.startup/test_Skin.js 
b/tests/qunit/mobile.startup/test_Skin.js
index 9f2f4d8..02ae532 100644
--- a/tests/qunit/mobile.startup/test_Skin.js
+++ b/tests/qunit/mobile.startup/test_Skin.js
@@ -42,4 +42,62 @@
                } );
        } );
 
+       QUnit.test( '#getSectionId', 4, function ( assert ) {
+               var
+                       $el = $(
+                               [
+                                       '<div>',
+                                       '<h2><span class="mw-headline" 
id="heading">H</span></h2>',
+                                       '<div>',
+                                       '<h3><span class="mw-headline" 
id="subheading">Subh</span></h3>',
+                                       '<a class="element"></a>',
+                                       '</div>',
+                                       '</div>'
+                               ].join( '' )
+                       ),
+                       $elTwo = $(
+                               [
+                                       '<div>',
+                                       '<h2><span class="mw-headline" 
id="Notes_and_references">Notes and references</span></h2>',
+                                       '<div>',
+                                       '<h3 class="in-block"><span 
class="mw-headline" id="Notes">Notes</span></h3>',
+                                       '<div class="reflist"><a 
class="element"></a></div>',
+                                       '</div>',
+                                       '</div>'
+                               ].join( '' )
+                       ),
+                       $elThree = $(
+                               [
+                                       '<div id="mw-content-text">',
+                                       '<h2><span class="mw-headline" 
id="heading">Heading</span></h2>',
+                                       '<div><a class="element"></a></div>',
+                                       '</div>'
+                               ].join( '' )
+                       ),
+                       $elFour = $(
+                               [
+                                       '<div id="mw-content-text">',
+                                       '<div><a class="element"></a></div>'
+                               ].join( '' )
+                       );
+
+               assert.strictEqual(
+                       Skin.getSectionId( $el.find( '.element' ) ),
+                       'subheading'
+               );
+               assert.strictEqual(
+                       Skin.getSectionId( $elTwo.find( '.element' ) ),
+                       'Notes',
+                       'https://phabricator.wikimedia.org/T146394'
+               );
+               assert.strictEqual(
+                       Skin.getSectionId( $elThree.find( '.element' ) ),
+                       'heading'
+               );
+               assert.strictEqual(
+                       Skin.getSectionId( $elFour.find( '.element' ) ),
+                       ''
+               );
+       } );
+
 }( mw.mobileFrontend, jQuery ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/327137
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I36c231c13f8939ad50b9c3d0fb1621b6b7ea0021
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <jrob...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to