Skip to content

Commit a1a0794

Browse files
Merge pull request #1766 from nhsuk/nested-attribute-tests
Align attributes macro JSON escaping with Jinja2
2 parents 864b928 + a103a2d commit a1a0794

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

packages/nhsuk-frontend/src/nhsuk/macros/attributes.njk

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,15 @@
117117
{% set valueEscaped = options.value %}
118118

119119
{#- Use escaped JSON for iterable values -#}
120+
{#- https://jinja.palletsprojects.com/en/stable/templates/#jinja-filters.tojson -#}
120121
{%- elif options.type === "array" and options.value is iterable %}
121-
{% set valueEscaped = options.value | dump %}
122+
{% set valueEscaped = options.value
123+
| dump
124+
| replace("<", "\\u003c")
125+
| replace(">", "\\u003e")
126+
| replace("&", "\\u0026")
127+
| replace("'", "\\u0027")
128+
%}
122129

123130
{#- Otherwise assume escaping is necessary -#}
124131
{#- https://github.com/alphagov/govuk-frontend/issues/4940 -#}

packages/nhsuk-frontend/src/nhsuk/macros/attributes.unit.test.mjs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,53 @@ describe('attributes.njk', () => {
5454
expect(attributes).toBe(' data-attributes=\'["value1","value2"]\'')
5555
})
5656

57+
it('renders array attributes values with escaping', () => {
58+
const attributes = nunjucks.renderMacro(
59+
'nhsukAttributes',
60+
'nhsuk/macros/attributes.njk',
61+
{
62+
context: {
63+
'data-quotes-double': {
64+
type: 'array',
65+
value: ['value "1"']
66+
},
67+
'data-quotes-single': {
68+
type: 'array',
69+
value: ["value '2'"]
70+
},
71+
'data-quotes-mixed': {
72+
type: 'array',
73+
value: ['value "\'3\'"']
74+
},
75+
'data-html-elements': {
76+
type: 'array',
77+
value: ['value <strong>4</strong>']
78+
},
79+
'data-html-entities': {
80+
type: 'array',
81+
value: ['value & 5']
82+
},
83+
'data-html-entities-double': {
84+
type: 'array',
85+
value: ['value &amp; 6']
86+
}
87+
}
88+
}
89+
)
90+
91+
// Note that single quotes are only used when rendering arrays that contain double quotes
92+
expect(attributes).toBe(
93+
[
94+
' data-quotes-double=\'["value \\"1\\""]\'',
95+
' data-quotes-single=\'["value \\u00272\\u0027"]\'',
96+
' data-quotes-mixed=\'["value \\"\\u00273\\u0027\\""]\'',
97+
' data-html-elements=\'["value \\u003cstrong\\u003e4\\u003c/strong\\u003e"]\'',
98+
' data-html-entities=\'["value \\u0026 5"]\'',
99+
' data-html-entities-double=\'["value \\u0026amp; 6"]\''
100+
].join('')
101+
)
102+
})
103+
57104
it('renders multiple attributes', () => {
58105
const attributes = nunjucks.renderMacro(
59106
'nhsukAttributes',
@@ -84,7 +131,14 @@ describe('attributes.njk', () => {
84131

85132
// Note that single quotes are only used when rendering arrays that contain double quotes
86133
expect(attributes).toBe(
87-
' data-attribute="value" data-second-attribute="second-value" data-third-attribute="third-value" data-fourth-attribute=\'["fourth-value"]\' data-fifth-attribute="[true]" data-sixth-attribute="[]"'
134+
[
135+
' data-attribute="value"',
136+
' data-second-attribute="second-value"',
137+
' data-third-attribute="third-value"',
138+
' data-fourth-attribute=\'["fourth-value"]\'',
139+
' data-fifth-attribute="[true]"',
140+
' data-sixth-attribute="[]"'
141+
].join('')
88142
)
89143
})
90144

0 commit comments

Comments
 (0)