From ce513bc776f9ca11f4ca8914e923ecb75eff16aa Mon Sep 17 00:00:00 2001 From: Stefan Lisper Date: Sat, 16 Jun 2018 09:56:01 +0200 Subject: [PATCH] Use pull request #11167 from MoarCoding/develop for v6.5.0 2bfd3e0c4 fixed bug where aria-controls is not updated with multiple ids 3e6e35cb6 Removed old code 6437acf2b fix: improve id detection/replacment for Toggler `aria-controls` attribute Co-Authored-By: Nicolas Coden Signed-off-by: Nicolas Coden --- js/foundation.toggler.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/js/foundation.toggler.js b/js/foundation.toggler.js index 5373cecb4..f1f67930c 100644 --- a/js/foundation.toggler.js +++ b/js/foundation.toggler.js @@ -3,6 +3,7 @@ import $ from 'jquery'; import { Motion } from './foundation.util.motion'; import { Plugin } from './foundation.core.plugin'; +import { RegExpEscape } from './foundation.core.utils'; import { Triggers } from './foundation.util.triggers'; /** @@ -55,13 +56,20 @@ class Toggler extends Plugin { this.className = input[0] === '.' ? input.slice(1) : input; } - // Add ARIA attributes to triggers - var id = this.$element[0].id; - $(`[data-open="${id}"], [data-close="${id}"], [data-toggle="${id}"]`) - .attr({ - 'aria-controls': id, - 'aria-expanded': this.$element.is(':hidden') ? false : true - }); + // Add ARIA attributes to triggers: + var id = this.$element[0].id, + $triggers = $(`[data-open~="${id}"], [data-close~="${id}"], [data-toggle~="${id}"]`); + + // - aria-expanded: according to the element visibility. + $triggers.attr('aria-expanded', !this.$element.is(':hidden')); + // - aria-controls: adding the element id to it if not already in it. + $triggers.each((index, trigger) => { + const $trigger = $(trigger); + const controls = $trigger.attr('aria-controls') || ''; + + const containsId = new RegExp(`\\b${RegExpEscape(id)}\\b`).test(controls); + if (!containsId) $trigger.attr('aria-controls', controls ? `${controls} ${id}` : id); + }); } /** -- 2.47.3