@@ -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 & 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