]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
fix(scrollspy): Spying on nested navs fails to activate for .nav-link's inside nav...
authorTroy Morehouse <troymore@nbnet.nb.ca>
Sun, 17 Sep 2017 07:30:37 +0000 (04:30 -0300)
committerJohann-S <johann.servoire@gmail.com>
Sun, 17 Sep 2017 07:30:37 +0000 (09:30 +0200)
* fix(scrollspy): Handle nested navs when nav-link inside nav-item

* [scrolspy] Find only child .nav-link's inside .nav-item

* [scrollspyt] Add tests for nested navs with nav-link inside nav-item

* fix troy-o's in test

js/src/scrollspy.js
js/tests/unit/scrollspy.js

index 70067c0b35be8f9ddebc354bf34c16f093b68cfa..e8f0d3101b59c1786c967ac8da15caea42e780cc 100644 (file)
@@ -54,6 +54,7 @@ const ScrollSpy = (() => {
     ACTIVE          : '.active',
     NAV_LIST_GROUP  : '.nav, .list-group',
     NAV_LINKS       : '.nav-link',
+    NAV_ITEMS       : '.nav-item',
     LIST_ITEMS      : '.list-group-item',
     DROPDOWN        : '.dropdown',
     DROPDOWN_ITEMS  : '.dropdown-item',
@@ -264,6 +265,8 @@ const ScrollSpy = (() => {
         // Set triggered links parents as active
         // With both <ul> and <nav> markup a parent is the previous sibling of any nav ancestor
         $link.parents(Selector.NAV_LIST_GROUP).prev(`${Selector.NAV_LINKS}, ${Selector.LIST_ITEMS}`).addClass(ClassName.ACTIVE)
+        // Handle special case when .nav-link is inside .nav-item
+        $link.parents(Selector.NAV_LIST_GROUP).prev(Selector.NAV_ITEMS).children(Selector.NAV_LINKS).addClass(ClassName.ACTIVE)
       }
 
       $(this._scrollElement).trigger(Event.ACTIVATE, {
index 96cf54eef0981b05ae54b2cd6722801ced9c96e8..7bdeb4a8d73b874f3c179b883529c8f5fa5123e2 100644 (file)
@@ -360,6 +360,47 @@ $(function () {
     testActiveElements()
   })
 
+
+  QUnit.test('should add the active class correctly when there are nested elements (nav nav-item markup)', function (assert) {
+    assert.expect(6)
+    var times = 0
+    var done = assert.async()
+    var navbarHtml = '<nav id="navigation" class="navbar">'
+      + '<ul class="nav">'
+      + '<li class="nav-item"><a id="a-1" class="nav-link" href="#div-1">div 1</a></li>'
+      + '<ul class="nav">'
+      + '<li class="nav-item"><a id="a-2" class="nav-link" href="#div-2">div 2</a></li>'
+      + '</ul>'
+      + '</ul>'
+      + '</nav>'
+
+    var contentHtml = '<div class="content" style="position: absolute; top: 0px; overflow: auto; height: 50px">'
+      + '<div id="div-1" style="padding: 0; margin: 0">'
+      + '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>'
+      + '</div>'
+      + '</div>'
+
+    $(navbarHtml).appendTo('#qunit-fixture')
+
+    var $content = $(contentHtml)
+      .appendTo('#qunit-fixture')
+      .bootstrapScrollspy({ offset: 0, target: '#navigation' })
+
+    function testActiveElements() {
+      if (++times > 3) { return done() }
+
+      $content.one('scroll', function () {
+        assert.ok($('#a-1').hasClass('active'), 'nav item for outer element has "active" class')
+        assert.ok($('#a-2').hasClass('active'), 'nav item for inner element has "active" class')
+        testActiveElements()
+      })
+
+      $content.scrollTop($content.scrollTop() + 10)
+    }
+
+    testActiveElements()
+  })
+
   QUnit.test('should add the active class correctly when there are nested elements (list-group markup)', function (assert) {
     assert.expect(6)
     var times = 0