Skip to content

Commit

Permalink
Merge pull request #4938 from alphagov/fix-safe-attributes
Browse files Browse the repository at this point in the history
Fix `govukAttributes` macro not outputting values passed from the `safe` filter
  • Loading branch information
querkmachine authored Apr 19, 2024
2 parents 1871898 + fe7a019 commit e1b5611
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ We've made fixes to GOV.UK Frontend in the following pull requests:
- [#4899: Remove indents from conditional reveals in radios and checkboxes](https://github.com/alphagov/govuk-frontend/pull/4899)
- [#4935: Fix password input button unexpectedly stretching](https://github.com/alphagov/govuk-frontend/pull/4935)
- [#4936: Fix skip link underline being removed when global styles are enabled](https://github.com/alphagov/govuk-frontend/pull/4936)
- [#4938: Fix `govukAttributes` macro not outputting values passed from the `safe` filter](https://github.com/alphagov/govuk-frontend/pull/4938)

## GOV.UK Frontend v5.3.0 (Feature release)

Expand Down
6 changes: 6 additions & 0 deletions packages/govuk-frontend/src/govuk/macros/attributes.njk
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
{#- Append attribute name/value pairs -#}
{%- if attributes is mapping %}
{% for name, value in attributes %}
{#- Detect if this is a `safe` filtered value. Just pass the value through if so. -#}
{#- https://github.com/alphagov/govuk-frontend/issues/4937 -#}
{% if value is mapping and not [undefined, null].includes(value.val) and value.length %}
{% set value = value.val %}
{% endif %}

{#- Set default attribute options -#}
{% set options = value if value is mapping else {
value: value,
Expand Down
19 changes: 19 additions & 0 deletions packages/govuk-frontend/src/govuk/macros/attributes.unit.test.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { renderMacro } from '@govuk-frontend/lib/components'
import nunjucks from 'nunjucks'

describe('attributes.njk', () => {
describe('govukAttributes', () => {
Expand Down Expand Up @@ -262,5 +263,23 @@ describe('attributes.njk', () => {

expect(attributes).toBe('')
})

it('outputs values that are passed from the `safe` filter', () => {
// Set up a tiny Nunjucks environment, just so we can get the native `safe` filter
const nunjucksEnv = nunjucks.configure([])

const attributes = renderMacro(
'govukAttributes',
'govuk/macros/attributes.njk',
{
context: {
'data-text': 'Testing',
'data-safe-text': nunjucksEnv.getFilter('safe')('Testing')
}
}
)

expect(attributes).toBe(' data-text="Testing" data-safe-text="Testing"')
})
})
})

0 comments on commit e1b5611

Please sign in to comment.