forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree-traversal.spec.ts
122 lines (108 loc) · 3.11 KB
/
tree-traversal.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import {
addAttribute,
visitElements,
parseTemplate,
replaceStartTag,
replaceEndTag,
} from './tree-traversal';
function runTagNameDuplicationTest(html: string, result: string): void {
visitElements(
parseTemplate(html).nodes,
node => {
html = replaceEndTag(html, node, node.name.repeat(2));
},
node => {
html = replaceStartTag(html, node, node.name.repeat(2));
},
);
expect(html).toBe(result);
}
function runAddAttributeTest(html: string, result: string): void {
visitElements(parseTemplate(html).nodes, undefined, node => {
html = addAttribute(html, node, 'attr', 'val');
});
expect(html).toBe(result);
}
describe('#visitElements', () => {
describe('tag name replacements', () => {
it('should handle basic cases', async () => {
runTagNameDuplicationTest('<a></a>', '<aa></aa>');
});
it('should handle multiple same line', async () => {
runTagNameDuplicationTest('<a></a><b></b>', '<aa></aa><bb></bb>');
});
it('should handle multiple same line nested', async () => {
runTagNameDuplicationTest('<a><b></b></a>', '<aa><bb></bb></aa>');
});
it('should handle multiple same line nested and unnested', async () => {
runTagNameDuplicationTest('<a><b></b><c></c></a>', '<aa><bb></bb><cc></cc></aa>');
});
it('should handle multiple multi-line', async () => {
runTagNameDuplicationTest(
`
<a></a>
<b></b>
`,
`
<aa></aa>
<bb></bb>
`,
);
});
it('should handle multiple multi-line nested', async () => {
runTagNameDuplicationTest(
`
<a>
<b></b>
</a>
`,
`
<aa>
<bb></bb>
</aa>
`,
);
});
it('should handle multiple multi-line nested and unnested', async () => {
runTagNameDuplicationTest(
`
<a>
<b></b>
<c></c>
</a>
`,
`
<aa>
<bb></bb>
<cc></cc>
</aa>
`,
);
});
});
describe('add attribute tests', () => {
it('should handle single element', async () => {
runAddAttributeTest('<a></a>', '<a attr="val"></a>');
});
it('should handle multiple unnested', async () => {
runAddAttributeTest('<a></a><b></b>', '<a attr="val"></a><b attr="val"></b>');
});
it('should handle multiple nested', async () => {
runAddAttributeTest('<a><b></b></a>', '<a attr="val"><b attr="val"></b></a>');
});
it('should handle multiple nested and unnested', async () => {
runAddAttributeTest(
'<a><b></b><c></c></a>',
'<a attr="val"><b attr="val"></b><c attr="val"></c></a>',
);
});
it('should handle adding multiple attrs to a single element', async () => {
let html = '<a></a>';
visitElements(parseTemplate(html).nodes, undefined, node => {
html = addAttribute(html, node, 'attr1', 'val1');
html = addAttribute(html, node, 'attr2', 'val2');
});
expect(html).toBe('<a attr2="val2" attr1="val1"></a>');
});
});
});