1
- import { visitElements , parseTemplate , replaceStartTag , replaceEndTag } from './tree-traversal' ;
1
+ import {
2
+ addAttribute ,
3
+ visitElements ,
4
+ parseTemplate ,
5
+ replaceStartTag ,
6
+ replaceEndTag ,
7
+ } from './tree-traversal' ;
2
8
3
9
function runTagNameDuplicationTest ( html : string , result : string ) : void {
4
10
visitElements (
@@ -13,6 +19,13 @@ function runTagNameDuplicationTest(html: string, result: string): void {
13
19
expect ( html ) . toBe ( result ) ;
14
20
}
15
21
22
+ function runAddAttributeTest ( html : string , result : string ) : void {
23
+ visitElements ( parseTemplate ( html ) . nodes , undefined , node => {
24
+ html = addAttribute ( html , node , 'attr' , 'val' ) ;
25
+ } ) ;
26
+ expect ( html ) . toBe ( result ) ;
27
+ }
28
+
16
29
describe ( '#visitElements' , ( ) => {
17
30
describe ( 'tag name replacements' , ( ) => {
18
31
it ( 'should handle basic cases' , async ( ) => {
@@ -76,4 +89,25 @@ describe('#visitElements', () => {
76
89
) ;
77
90
} ) ;
78
91
} ) ;
92
+
93
+ describe ( 'add attribute tests' , ( ) => {
94
+ it ( 'should handle single element' , async ( ) => {
95
+ runAddAttributeTest ( '<a></a>' , '<a attr="val"></a>' ) ;
96
+ } ) ;
97
+
98
+ it ( 'should handle multiple unnested' , async ( ) => {
99
+ runAddAttributeTest ( '<a></a><b></b>' , '<a attr="val"></a><b attr="val"></b>' ) ;
100
+ } ) ;
101
+
102
+ it ( 'should handle multiple nested' , async ( ) => {
103
+ runAddAttributeTest ( '<a><b></b></a>' , '<a attr="val"><b attr="val"></b></a>' ) ;
104
+ } ) ;
105
+
106
+ it ( 'should handle multiple nested and unnested' , async ( ) => {
107
+ runAddAttributeTest (
108
+ '<a><b></b><c></c></a>' ,
109
+ '<a attr="val"><b attr="val"></b><c attr="val"></c></a>' ,
110
+ ) ;
111
+ } ) ;
112
+ } ) ;
79
113
} ) ;
0 commit comments