Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure IDs on collapsible navigation are unique #160

Merged
merged 2 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- [Pull request #160: Make sure IDs on collapsible navigation are unique](https://github.com/alphagov/tech-docs-gem/pull/160)

## 2.0.9

### Fixes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Another nested nested page
---

# Another nested nested page
13 changes: 9 additions & 4 deletions lib/assets/javascripts/_modules/collapsible-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@
continue
}
$topLevelItem.addClass('collapsible')
$listing.addClass('collapsible__body')
.attr('id', id)
.attr('aria-expanded', 'false')
var arrayOfIds = []
$listing.each(function (i) {
var uniqueId = id + '-' + i
arrayOfIds.push(uniqueId)
$(this).addClass('collapsible__body')
.attr('id', uniqueId)
.attr('aria-expanded', 'false')
})
$heading.addClass('collapsible__heading')
.after('<button class="collapsible__toggle" aria-controls="' + id + '"><span class="collapsible__toggle-label">Expand ' + $heading.text() + '</span><span class="collapsible__toggle-icon" aria-hidden="true"></button>')
.after('<button class="collapsible__toggle" aria-controls="' + arrayOfIds.join(' ') + '"><span class="collapsible__toggle-label">Expand ' + $heading.text() + '</span><span class="collapsible__toggle-icon" aria-hidden="true"></button>')
$topLevelItem.on('click', '.collapsible__toggle', function (e) {
e.preventDefault()
var $parent = $(this).parent()
Expand Down
41 changes: 41 additions & 0 deletions spec/javascripts/collapsible-navigation-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
describe('Collapsible navigation', function () {
'use strict'

var module
var $navigation

beforeEach(function () {
module = new GOVUK.Modules.CollapsibleNavigation()
$navigation = $(
'<nav id="toc" class="js-toc-list toc__list" aria-labelledby="toc-heading" data-module="collapsible-navigation">' +
'<ul>' +
'<li>' +
'<a href="/nested-page/"><span>Nested page</span></a>' +
'<ul>' +
'<li>' +
'<a href="/nested-page/another-nested-page/#another-nested-page"><span>Another nested page</span></a>' +
'</li>' +
'</ul>' +
'<ul>' +
'<li>' +
'<a href="/nested-page/another-nested-nested-page/#another-nested-nested-page"><span>Another nested nested page</span></a>' +
'</li>' +
'</ul>' +
'</li>' +
'</ul>' +
'</nav>')
module.start($navigation)
})

it('sanitizes headings to unique IDs correctly', function () {
$navigation.find('ul > li > ul').each(function (i) {
expect($(this)[0].id).toEqual('toc-nested-page-' + i)
})
})

it('aria-controls on the button lists all the nested IDs', function () {
$navigation.find('button').each(function (i) {
expect($navigation.find('button').attr('aria-controls')).toEqual('toc-nested-page-0 toc-nested-page-1')
})
})
})