From 98cddda4b3bd3be1556827956fe7ae2301f7218e Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Sun, 7 Jun 2015 01:43:51 +0100
Subject: [PATCH] Issue #2472177 by Dom., nod_, drubb, mgifford: Collapsible
 fieldset have duplicated and wrong aria-expanded

---
 core/core.libraries.yml   |  1 +
 core/includes/form.inc    |  2 +-
 core/misc/collapse.js     |  6 +++++-
 core/misc/details-aria.js | 24 ++++++++++++++++++++++++
 4 files changed, 31 insertions(+), 2 deletions(-)
 create mode 100644 core/misc/details-aria.js

diff --git a/core/core.libraries.yml b/core/core.libraries.yml
index fc659c840180..dc714deda4ee 100644
--- a/core/core.libraries.yml
+++ b/core/core.libraries.yml
@@ -124,6 +124,7 @@ drupal.batch:
 drupal.collapse:
   version: VERSION
   js:
+    misc/details-aria.js: {}
     misc/collapse.js: {}
   dependencies:
     - core/jquery
diff --git a/core/includes/form.inc b/core/includes/form.inc
index 9190e314d5b3..01d9068d9c21 100644
--- a/core/includes/form.inc
+++ b/core/includes/form.inc
@@ -225,7 +225,7 @@ function template_preprocess_details(&$variables) {
     if (!empty($element['#attributes']['id'])) {
       $variables['summary_attributes']['aria-controls'] = $element['#attributes']['id'];
     }
-    $variables['summary_attributes']['aria-expanded'] = !empty($element['#attributes']['open']);
+    $variables['summary_attributes']['aria-expanded'] = !empty($element['#attributes']['open']) ? 'true' : 'false';
     $variables['summary_attributes']['aria-pressed'] = $variables['summary_attributes']['aria-expanded'];
   }
   $variables['title'] = (!empty($element['#title'])) ? $element['#title'] : '';
diff --git a/core/misc/collapse.js b/core/misc/collapse.js
index 6aa2e254b429..2d44e3f403b3 100644
--- a/core/misc/collapse.js
+++ b/core/misc/collapse.js
@@ -91,7 +91,11 @@
       else {
         $summaryPrefix.html(Drupal.t('Hide'));
       }
-      this.$node.attr('open', !isOpen);
+      // Delay setting the attribute to emulate chrome behavior and make
+      // details-aria.js work as expected with this polyfill.
+      setTimeout(function () {
+        this.$node.attr('open', !isOpen);
+      }.bind(this), 0);
     }
   });
 
diff --git a/core/misc/details-aria.js b/core/misc/details-aria.js
new file mode 100644
index 000000000000..b400b4fc7a1c
--- /dev/null
+++ b/core/misc/details-aria.js
@@ -0,0 +1,24 @@
+/**
+ * @file
+ * Add aria attribute handling for details and summary elements.
+ */
+
+(function ($, Drupal) {
+
+  "use strict";
+
+  Drupal.behaviors.detailsAria = {
+    attach: function () {
+      $('body').once('detailsAria').on('click.detailsAria', 'summary', function (event) {
+        var $summary = $(event.currentTarget);
+        var open = $(event.currentTarget.parentNode).attr('open') === 'open' ? 'false' : 'true';
+
+        $summary.attr({
+          'aria-expanded': open,
+          'aria-pressed': open
+        });
+      });
+    }
+  };
+
+})(jQuery, Drupal);
-- 
GitLab