Skip to content

Commit 0680554

Browse files
authored
Simplify HTMLTree.build/1 by avoiding redundant build_tree calls (#683)
Text and comment nodes are leaf nodes and don't need the recursive build_tree function. This change simplifies the code and provides a small speedup during tree construction for these node types.
1 parent 418250a commit 0680554

1 file changed

Lines changed: 2 additions & 26 deletions

File tree

lib/floki/html_tree.ex

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,37 +74,13 @@ defmodule Floki.HTMLTree do
7474
root_id = IDSeeder.seed(node_ids)
7575
root_node = %Text{content: text, node_id: root_id}
7676

77-
root_nodes_ids = [root_id | root_nodes_ids]
78-
node_ids = [root_id | node_ids]
79-
80-
{node_ids, nodes} =
81-
build_tree(
82-
node_ids,
83-
nodes,
84-
[],
85-
root_node,
86-
[]
87-
)
88-
89-
{root_nodes_ids, node_ids, nodes}
77+
{[root_id | root_nodes_ids], [root_id | node_ids], [{root_id, root_node} | nodes]}
9078

9179
{:comment, comment}, {root_nodes_ids, node_ids, nodes} ->
9280
root_id = IDSeeder.seed(node_ids)
9381
root_node = %Comment{content: comment, node_id: root_id}
9482

95-
root_nodes_ids = [root_id | root_nodes_ids]
96-
node_ids = [root_id | node_ids]
97-
98-
{node_ids, nodes} =
99-
build_tree(
100-
node_ids,
101-
nodes,
102-
[],
103-
root_node,
104-
[]
105-
)
106-
107-
{root_nodes_ids, node_ids, nodes}
83+
{[root_id | root_nodes_ids], [root_id | node_ids], [{root_id, root_node} | nodes]}
10884

10985
_, state ->
11086
state

0 commit comments

Comments
 (0)