1
1
import {
2
- addAttribute ,
2
+ updateAttribute ,
3
3
visitElements ,
4
4
parseTemplate ,
5
5
replaceStartTag ,
@@ -21,7 +21,28 @@ function runTagNameDuplicationTest(html: string, result: string): void {
21
21
22
22
function runAddAttributeTest ( html : string , result : string ) : void {
23
23
visitElements ( parseTemplate ( html ) . nodes , undefined , node => {
24
- html = addAttribute ( html , node , 'attr' , 'val' ) ;
24
+ html = updateAttribute ( html , node , 'add' , ( ) => 'val' ) ;
25
+ } ) ;
26
+ expect ( html ) . toBe ( result ) ;
27
+ }
28
+
29
+ function runRemoveAttributeTest ( html : string , result : string ) : void {
30
+ visitElements ( parseTemplate ( html ) . nodes , undefined , node => {
31
+ html = updateAttribute ( html , node , 'rm' , ( ) => null ) ;
32
+ } ) ;
33
+ expect ( html ) . toBe ( result ) ;
34
+ }
35
+
36
+ function runChangeAttributeTest ( html : string , result : string ) : void {
37
+ visitElements ( parseTemplate ( html ) . nodes , undefined , node => {
38
+ html = updateAttribute ( html , node , 'change' , old => ( old == ':(' ? ':)' : old ) ) ;
39
+ } ) ;
40
+ expect ( html ) . toBe ( result ) ;
41
+ }
42
+
43
+ function runClearAttributeTest ( html : string , result : string ) : void {
44
+ visitElements ( parseTemplate ( html ) . nodes , undefined , node => {
45
+ html = updateAttribute ( html , node , 'clear' , ( ) => '' ) ;
25
46
} ) ;
26
47
expect ( html ) . toBe ( result ) ;
27
48
}
@@ -92,39 +113,103 @@ describe('#visitElements', () => {
92
113
93
114
describe ( 'add attribute tests' , ( ) => {
94
115
it ( 'should handle single element' , async ( ) => {
95
- runAddAttributeTest ( '<a></a>' , '<a attr ="val"></a>' ) ;
116
+ runAddAttributeTest ( '<a></a>' , '<a add ="val"></a>' ) ;
96
117
} ) ;
97
118
98
119
it ( 'should handle multiple unnested' , async ( ) => {
99
- runAddAttributeTest ( '<a></a><b></b>' , '<a attr ="val"></a><b attr ="val"></b>' ) ;
120
+ runAddAttributeTest ( '<a></a><b></b>' , '<a add ="val"></a><b add ="val"></b>' ) ;
100
121
} ) ;
101
122
102
123
it ( 'should handle multiple nested' , async ( ) => {
103
- runAddAttributeTest ( '<a><b></b></a>' , '<a attr ="val"><b attr ="val"></b></a>' ) ;
124
+ runAddAttributeTest ( '<a><b></b></a>' , '<a add ="val"><b add ="val"></b></a>' ) ;
104
125
} ) ;
105
126
106
127
it ( 'should handle multiple nested and unnested' , async ( ) => {
107
128
runAddAttributeTest (
108
129
'<a><b></b><c></c></a>' ,
109
- '<a attr ="val"><b attr ="val"></b><c attr ="val"></c></a>' ,
130
+ '<a add ="val"><b add ="val"></b><c add ="val"></c></a>' ,
110
131
) ;
111
132
} ) ;
112
133
113
134
it ( 'should handle adding multiple attrs to a single element' , async ( ) => {
114
135
let html = '<a></a>' ;
115
136
visitElements ( parseTemplate ( html ) . nodes , undefined , node => {
116
- html = addAttribute ( html , node , 'attr1' , 'val1' ) ;
117
- html = addAttribute ( html , node , 'attr2' , 'val2' ) ;
137
+ html = updateAttribute ( html , node , 'attr1' , ( ) => 'val1' ) ;
138
+ html = updateAttribute ( html , node , 'attr2' , ( ) => 'val2' ) ;
118
139
} ) ;
119
140
expect ( html ) . toBe ( '<a attr2="val2" attr1="val1"></a>' ) ;
120
141
} ) ;
121
142
122
143
it ( 'should replace value of existing attribute' , async ( ) => {
123
- runAddAttributeTest ( '<a attr ="default"></a>' , '<a attr ="val"></a>' ) ;
144
+ runAddAttributeTest ( '<a add ="default"></a>' , '<a add ="val"></a>' ) ;
124
145
} ) ;
125
146
126
147
it ( 'should add value to existing attribute that does not have a value' , async ( ) => {
127
- runAddAttributeTest ( '<a attr></a>' , '<a attr="val"></a>' ) ;
148
+ runAddAttributeTest ( '<a add></a>' , '<a add="val"></a>' ) ;
149
+ } ) ;
150
+ } ) ;
151
+
152
+ describe ( 'remove attribute tests' , ( ) => {
153
+ it ( 'should remove attribute' , ( ) => {
154
+ runRemoveAttributeTest ( '<a rm="something"></a>' , '<a></a>' ) ;
155
+ } ) ;
156
+
157
+ it ( 'should remove empty attribute' , ( ) => {
158
+ runRemoveAttributeTest ( '<a rm></a>' , '<a></a>' ) ;
159
+ } ) ;
160
+
161
+ it ( 'should remove unquoted attribute' , ( ) => {
162
+ runRemoveAttributeTest ( '<a rm=3></a>' , '<a></a>' ) ;
163
+ } ) ;
164
+
165
+ it ( 'should remove value-less attribute' , ( ) => {
166
+ runRemoveAttributeTest ( '<a rm></a>' , '<a></a>' ) ;
167
+ } ) ;
168
+
169
+ it ( 'should not change element without attribute' , ( ) => {
170
+ runRemoveAttributeTest ( '<a></a>' , '<a></a>' ) ;
171
+ } ) ;
172
+
173
+ it ( 'should not remove other attributes' , ( ) => {
174
+ runRemoveAttributeTest (
175
+ `
176
+ <a
177
+ first="1"
178
+ rm="2"
179
+ last="3">
180
+ </a>
181
+ ` ,
182
+ `
183
+ <a
184
+ first="1"
185
+ last="3">
186
+ </a>
187
+ ` ,
188
+ ) ;
189
+ } ) ;
190
+ } ) ;
191
+
192
+ describe ( 'change attribute tests' , ( ) => {
193
+ it ( 'should change attribute with matching value' , ( ) => {
194
+ runChangeAttributeTest ( '<a change=":("></a>' , '<a change=":)"></a>' ) ;
195
+ } ) ;
196
+
197
+ it ( 'should not change attribute with non-matching value' , ( ) => {
198
+ runChangeAttributeTest ( '<a change="x"></a>' , '<a change="x"></a>' ) ;
199
+ } ) ;
200
+ } ) ;
201
+
202
+ describe ( 'clear attribute tests' , ( ) => {
203
+ it ( 'should clear attribute with value' , ( ) => {
204
+ runClearAttributeTest ( '<a clear="something"></a>' , '<a clear></a>' ) ;
205
+ } ) ;
206
+
207
+ it ( 'should preserve value-less attribute' , ( ) => {
208
+ runClearAttributeTest ( '<a clear></a>' , '<a clear></a>' ) ;
209
+ } ) ;
210
+
211
+ it ( 'should add attribute to element without it' , ( ) => {
212
+ runClearAttributeTest ( '<a></a>' , '<a clear></a>' ) ;
128
213
} ) ;
129
214
} ) ;
130
215
@@ -139,7 +224,7 @@ describe('#visitElements', () => {
139
224
` ,
140
225
`
141
226
<a
142
- attr ="val"
227
+ add ="val"
143
228
class="a"
144
229
aria-label="a"
145
230
aria-describedby="a"
@@ -157,8 +242,8 @@ describe('#visitElements', () => {
157
242
></a>
158
243
` ;
159
244
visitElements ( parseTemplate ( html ) . nodes , undefined , node => {
160
- html = addAttribute ( html , node , 'attr1' , 'val1' ) ;
161
- html = addAttribute ( html , node , 'attr2' , 'val2' ) ;
245
+ html = updateAttribute ( html , node , 'attr1' , ( ) => 'val1' ) ;
246
+ html = updateAttribute ( html , node , 'attr2' , ( ) => 'val2' ) ;
162
247
} ) ;
163
248
expect ( html ) . toBe ( `
164
249
<a
0 commit comments