@@ -57,6 +57,47 @@ describe('translate', () => {
57
57
expect ( translation ) . toBe ( 'Hallo <del>Name</del>' )
58
58
} )
59
59
60
+ it ( 'with global placeholder HTML escaping and enabled on parameter' , ( ) => {
61
+ const text = 'Hello {name}'
62
+ const translation = translate ( 'core' , text , { name : { value : '<del>Name</del>' , escape : true } } , undefined , { escape : true } )
63
+ expect ( translation ) . toBe ( 'Hallo <del>Name</del>' )
64
+ } )
65
+
66
+ it ( 'with global placeholder HTML escaping but disabled on parameter' , ( ) => {
67
+ const text = 'Hello {name}'
68
+ const translation = translate ( 'core' , text , { name : { value : '<del>Name</del>' , escape : false } } , undefined , { escape : true } )
69
+ expect ( translation ) . toBe ( 'Hallo <del>Name</del>' )
70
+ } )
71
+
72
+ it ( 'without global placeholder HTML escaping but enabled on parameter' , ( ) => {
73
+ const text = 'Hello {name}'
74
+ const translation = translate ( 'core' , text , { name : { value : '<del>Name</del>' , escape : true } } , undefined , { escape : false } )
75
+ expect ( translation ) . toBe ( 'Hallo <del>Name</del>' )
76
+ } )
77
+
78
+ it ( 'without global placeholder HTML escaping and disabled on parameter' , ( ) => {
79
+ const text = 'Hello {name}'
80
+ const translation = translate ( 'core' , text , { name : { value : '<del>Name</del>' , escape : false } } , undefined , { escape : false } )
81
+ expect ( translation ) . toBe ( 'Hallo <del>Name</del>' )
82
+ } )
83
+
84
+ it ( 'with global placeholder HTML escaping and invalid per-parameter escaping' , ( ) => {
85
+ const text = 'Hello {name}'
86
+ // @ts -expect-error We test calling it with an invalid value (missing)
87
+ const translation = translate ( 'core' , text , { name : { value : '<del>Name</del>' } } , undefined , { escape : true } )
88
+ // `escape` needs to be an boolean, otherwise we fallback to `false` to prevent security issues
89
+ // So in this case `undefined` is falsy but we still enforce escaping as we only accept `false`
90
+ expect ( translation ) . toBe ( 'Hallo <del>Name</del>' )
91
+ } )
92
+
93
+ it ( 'witout global placeholder HTML escaping and invalid per-parameter escaping' , ( ) => {
94
+ const text = 'Hello {name}'
95
+ // @ts -expect-error We test calling it with an invalid value
96
+ const translation = translate ( 'core' , text , { name : { value : '<del>Name</del>' , escape : 0 } } , undefined , { escape : false } )
97
+ // `escape` needs to be an boolean, otherwise we fallback to `false` to prevent security issues
98
+ expect ( translation ) . toBe ( 'Hallo <del>Name</del>' )
99
+ } )
100
+
60
101
it ( 'without placeholder XSS sanitizing' , ( ) => {
61
102
const text = 'Hello {name}'
62
103
const translation = translate ( 'core' , text , { name : '<img src=x onerror=alert(1)//>' } , undefined , { sanitize : false , escape : false } )
0 commit comments