forked from dashbitco/lazy_html
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_test.exs
More file actions
214 lines (187 loc) · 6.11 KB
/
Copy pathtree_test.exs
File metadata and controls
214 lines (187 loc) · 6.11 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
defmodule LazyHTML.TreeTest do
use ExUnit.Case
doctest LazyHTML.Tree
describe "to_html/1" do
test "serializes tree as a valid html representation" do
html = """
<!-- Top comment --><html><head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Page</title>
</head>
<body>
<div id="root" class="layout">
Hello world
<!-- Inner comment -->
<p>
<span data-id="1">Hello</span>
<span data-id="2">world</span>
</p>
<img src="/assets/image.jpeg" alt="image"/>
<form>
<input class="input" value="" name="name"/>
</form>
<script>
console.log(1 && 2);
</script>
<style>
.parent > .child {
&:hover {
display: none;
}
}
</style>
& < > " ' € 🔥 🐈
<div class="& < > " ' € 🔥 🐈"></div>
</div>
</body></html>\
"""
tree = html |> LazyHTML.from_document() |> LazyHTML.to_tree()
assert LazyHTML.Tree.to_html(tree) == html
end
test "with :skip_whitespace_nodes" do
tree =
"""
<p>
<span> Hello </span>
<span> world </span>
</p>
"""
|> LazyHTML.from_fragment()
|> LazyHTML.to_tree()
assert LazyHTML.Tree.to_html(tree, skip_whitespace_nodes: true) ==
"<p><span> Hello </span><span> world </span></p>"
end
end
describe "traverse/3" do
test "does post-order traversal of the nodes and accumulates results" do
tree = [
{:comment, "Hello world"},
{"div", [{"class", "root"}],
[
{"span", [], ["Hello"]},
{:comment, "intersection"},
{"span", [], ["world"]}
]}
]
nodes = LazyHTML.Tree.traverse(tree, [], fn node, acc -> [node | acc] end)
assert nodes == [
{"div", [{"class", "root"}],
[{"span", [], ["Hello"]}, {:comment, "intersection"}, {"span", [], ["world"]}]},
{"span", [], ["world"]},
"world",
{:comment, "intersection"},
{"span", [], ["Hello"]},
"Hello",
{:comment, "Hello world"}
]
end
end
describe "postwalk/3" do
test "does post-order traversal of the nodes and accumulates results" do
tree = [
{:comment, "Hello world"},
{"div", [{"class", "root"}],
[
{"span", [], ["Hello"]},
{:comment, "intersection"},
{"span", [], ["world"]}
]}
]
{^tree, nodes} = LazyHTML.Tree.postwalk(tree, [], fn node, acc -> {node, [node | acc]} end)
assert nodes == [
{"div", [{"class", "root"}],
[{"span", [], ["Hello"]}, {:comment, "intersection"}, {"span", [], ["world"]}]},
{"span", [], ["world"]},
"world",
{:comment, "intersection"},
{"span", [], ["Hello"]},
"Hello",
{:comment, "Hello world"}
]
end
test "returns tree with nodes updated by the mapper function" do
tree = [
{:comment, "Hello world"},
{"div", [{"class", "root"}],
[
{"span", [], ["Hello"]},
{:comment, "intersection"},
{"span", [], ["world"]}
]}
]
{updated_tree, []} =
LazyHTML.Tree.postwalk(tree, [], fn
{tag, attrs, children}, acc ->
{{tag, [{"data-new", "true"} | attrs], ["new" | children]}, acc}
{:comment, content}, acc ->
{{:comment, content <> " [new]"}, acc}
text, acc when is_binary(text) ->
{text <> " [new]", acc}
end)
assert updated_tree == [
{:comment, "Hello world [new]"},
{"div", [{"data-new", "true"}, {"class", "root"}],
[
"new",
{"span", [{"data-new", "true"}], ["new", "Hello [new]"]},
{:comment, "intersection [new]"},
{"span", [{"data-new", "true"}], ["new", "world [new]"]}
]}
]
end
test "replaces nodes when mapper returns a list" do
tree = [
{"div", [], [{"span", [{"id", "1"}], ["Hello"]}]},
{"div", [], [{"span", [{"id", "2"}], ["World"]}]}
]
{updated_tree, []} =
LazyHTML.Tree.postwalk(tree, [], fn
{"span", [{"id", "1"}], _children}, acc ->
# Remove the node.
{[], acc}
{"span", [{"id", "2"}], _children}, acc ->
# Replace the node with two other nodes.
{[{"p", [], []}, {"p", [], []}], acc}
other, acc ->
{other, acc}
end)
assert updated_tree == [
{"div", [], []},
{"div", [], [{"p", [], []}, {"p", [], []}]}
]
end
end
describe "postwalk/2" do
test "returns tree with nodes updated by the mapper function" do
tree = [
{:comment, "Hello world"},
{"div", [{"class", "root"}],
[
{"span", [], ["Hello"]},
{:comment, "intersection"},
{"span", [], ["world"]}
]}
]
updated_tree =
LazyHTML.Tree.postwalk(tree, fn
{tag, attrs, children} ->
{tag, [{"data-new", "true"} | attrs], ["new" | children]}
{:comment, content} ->
{:comment, content <> " [new]"}
text when is_binary(text) ->
text <> " [new]"
end)
assert updated_tree == [
{:comment, "Hello world [new]"},
{"div", [{"data-new", "true"}, {"class", "root"}],
[
"new",
{"span", [{"data-new", "true"}], ["new", "Hello [new]"]},
{:comment, "intersection [new]"},
{"span", [{"data-new", "true"}], ["new", "world [new]"]}
]}
]
end
end
end