Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions lib/floki/finder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -341,33 +341,33 @@ defmodule Floki.Finder do
Map.get(tree.nodes, id)
end

defp get_sibling_ids_from([], _html_node), do: []

defp get_sibling_ids_from(ids, html_node) do
ids
|> Enum.reverse()
|> Enum.drop_while(fn id -> id != html_node.node_id end)
|> tl()
end

defp get_siblings(html_node, tree) do
parent = get_node(html_node.parent_node_id, tree)

ids =
if parent do
get_sibling_ids_from(parent.children_nodes_ids, html_node)
parent.children_nodes_ids
else
get_sibling_ids_from(Enum.reverse(tree.root_nodes_ids), html_node)
tree.root_nodes_ids
end

Enum.filter(ids, fn id ->
get_sibling_nodes(ids, html_node.node_id, tree, [])
end

defp get_sibling_nodes([id | _], id, _tree, acc), do: acc

defp get_sibling_nodes([id | rest], target_id, tree, acc) do
acc =
case get_node(id, tree) do
%HTMLNode{} -> true
_ -> false
%HTMLNode{} -> [id | acc]
_ -> acc
end
end)

get_sibling_nodes(rest, target_id, tree, acc)
end

defp get_sibling_nodes([], _target_id, _tree, _acc), do: []

# finds all descendant node ids recursively through the tree preserving the order
defp get_descendant_ids(node_id, tree) do
case get_node(node_id, tree) do
Expand Down
Loading