Skip to content

Commit

Permalink
Add data-nosnippet to accordion 'show' text
Browse files Browse the repository at this point in the history
The data-nosnippet attribute stops the 'show' text from appearing in any Google Search text snippets.

We have successfully used a similar approach for the [cookie banner](#2192)
and on [GOV.UK Publishing components](alphagov/govuk_publishing_components#1185)
  • Loading branch information
hannalaakso committed Nov 1, 2021
1 parent c6dbd91 commit 75c8a2b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/govuk/components/accordion/accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ Accordion.prototype.constructHeaderMarkup = function ($headerWrapper, index) {
// Create container for show / hide icons and text.
var $showToggle = document.createElement('span')
$showToggle.classList.add(this.sectionShowHideToggleClass)
// Tell Google not to index the 'show' text as part of the heading
// For the snippet to work with JavaScript, it must be added before adding the page element to the
// page's DOM. See https://developers.google.com/search/docs/advanced/robots/robots_meta_tag#data-nosnippet-attr
$showToggle.setAttribute('data-nosnippet', '')
// Create an inner container to limit the width of the focus state
var $showToggleFocus = document.createElement('span')
$showToggleFocus.classList.add(this.sectionShowHideToggleFocusClass)
Expand Down
8 changes: 8 additions & 0 deletions src/govuk/components/accordion/accordion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ describe('/components/accordion', () => {
expect(ShowOrHideButtonText).toEqual('Hide this section')
})

it('should have a data-nosnippet attribute on the "Show / hide this section" container to hide it from search result snippets', async () => {
await page.goto(baseUrl + '/components/accordion/preview', { waitUntil: 'load' })

const dataNoSnippetAttribute = await page.evaluate(() => document.body.querySelector('.govuk-accordion__section-toggle').getAttribute('data-nosnippet'))

expect(dataNoSnippetAttribute).toEqual('')
})

describe('hidden text', () => {
it('should contain hidden text " this section" for assistive technology', async () => {
await page.goto(baseUrl + '/components/accordion/preview', { waitUntil: 'load' })
Expand Down

0 comments on commit 75c8a2b

Please sign in to comment.