]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
Use pull request #11052 from DanielRuf/fix/toggler-aria-expanded-on-trigger-11049...
authorDaniel Ruf <daniel@daniel-ruf.de>
Sat, 16 Jun 2018 07:27:59 +0000 (09:27 +0200)
committerNicolas Coden <nicolas@ncoden.fr>
Sat, 16 Jun 2018 07:27:59 +0000 (09:27 +0200)
fbc69f10e fix: add aria-expanded to the toggler triggers
e84e7d119 fix: apply aria-expanded to the toggler triggers in _updateARIA
dd96a42ad tests: replace duplicate code in the toggler test

Signed-off-by: Nicolas Coden <nicolas@ncoden.fr>
js/foundation.toggler.js
test/javascript/components/toggler.js

index d8c5744ecab3a6875935723347039c4c498525e8..6427e2c95e761923cb2d1f393aa92f0649480ec0 100644 (file)
@@ -58,9 +58,10 @@ class Toggler extends Plugin {
     // Add ARIA attributes to triggers
     var id = this.$element[0].id;
     $(`[data-open="${id}"], [data-close="${id}"], [data-toggle="${id}"]`)
-      .attr('aria-controls', id);
-    // If the target is hidden, add aria-hidden
-    this.$element.attr('aria-expanded', this.$element.is(':hidden') ? false : true);
+      .attr({
+        'aria-controls': id,
+        'aria-expanded': this.$element.is(':hidden') ? false : true
+      });
   }
 
   /**
@@ -125,7 +126,11 @@ class Toggler extends Plugin {
   }
 
   _updateARIA(isOn) {
-    this.$element.attr('aria-expanded', isOn ? true : false);
+    var id = this.$element[0].id;
+    $(`[data-open="${id}"], [data-close="${id}"], [data-toggle="${id}"]`)
+      .attr({
+        'aria-expanded': isOn ? true : false
+      });
   }
 
   /**
index e55817b370e41d26b1bcc585cadfadbe2725dd86..7d90107950121e9b4f69084a35509a326bef268f 100644 (file)
@@ -7,6 +7,14 @@ describe('Toggler', function() {
     $html.remove();
   });
 
+  function appendTriggers() {
+    return $(`<div>
+      <a data-open="toggler">Open</a>
+      <a data-close="toggler">Close</a>
+      <a data-toggle="toggler">Toggle</a>
+    </div>`).appendTo('body')
+  }
+
   describe('constructor()', function() {
     it('stores the element and plugin options', function() {
       $html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
@@ -42,33 +50,29 @@ describe('Toggler', function() {
 
     it('adds Aria attributes to click triggers', function() {
       $html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
-      var $triggers = $(`<div>
-          <a data-open="toggler">Open</a>
-          <a data-close="toggler">Close</a>
-          <a data-toggle="toggler">Toggle</a>
-        </div>`).appendTo('body');
+      var $triggers = appendTriggers();
       plugin = new Foundation.Toggler($html, {});
 
-      $triggers.find('[data-open]').should.have.attr('aria-controls', 'toggler');
-      $triggers.find('[data-close]').should.have.attr('aria-controls', 'toggler');
-      $triggers.find('[data-toggle]').should.have.attr('aria-controls', 'toggler');
+      $triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-controls', 'toggler');
 
       $triggers.remove();
     });
 
     it('sets aria-expanded to true if the element is visible', function() {
       $html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
+      var $triggers = appendTriggers();
       plugin = new Foundation.Toggler($html, {});
 
-      $('#toggler').should.have.attr('aria-expanded', 'true');
+      $triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'true');
     });
 
     it('sets aria-expanded to false if the element is invisible', function() {
       var $css = $('<style>#toggler { display: none }</style>').appendTo('body');
       $html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
+      var $triggers = appendTriggers();
       plugin = new Foundation.Toggler($html, {});
 
-      $('#toggler').should.have.attr('aria-expanded', 'false');
+      $triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'false');
       $css.remove();
     });
   });
@@ -92,13 +96,14 @@ describe('Toggler', function() {
 
     it('updates aria-expanded after the class is toggled', function() {
       $html = $('<div id="toggler" data-toggler="class"></div>').appendTo('body');
+      var $triggers = appendTriggers();
       plugin = new Foundation.Toggler($html, {});
 
       plugin._toggleClass();
-      $('#toggler').should.have.attr('aria-expanded', 'true');
+      $triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'true');
 
       plugin._toggleClass();
-      $('#toggler').should.have.attr('aria-expanded', 'false');
+      $triggers.find('[data-open], [data-close], [data-toggle]').should.have.attr('aria-expanded', 'false');
     });
   });
 
@@ -107,11 +112,11 @@ describe('Toggler', function() {
     it('animates an invisible element in', function(done) {
       var $css = $('<style>#toggler { display: none; }</style>').appendTo('body');
       $html = $('<div id="toggler" data-toggler data-animate="fade-in fade-out"></div>').appendTo('body');
+
       plugin = new Foundation.Toggler($html, {});
 
       $html.on('on.zf.toggler', function() {
         $('#toggler').should.be.visible;
-        $('#toggler').should.have.attr('aria-expanded', 'true');
         $css.remove();
         done();
       });
@@ -119,13 +124,13 @@ describe('Toggler', function() {
       plugin._toggleAnimate();
     });
 
-    it('animates an visible element out', function(done) {
+    it('animates a visible element out', function(done) {
       $html = $('<div id="toggler" data-toggler data-animate="fade-in fade-out"></div>').appendTo('body');
+
       plugin = new Foundation.Toggler($html, {});
 
       $html.on('off.zf.toggler', function() {
         $('#toggler').should.be.hidden;
-        $('#toggler').should.have.attr('aria-expanded', 'false');
         done();
       });