Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Floki.filter_out/2 now can filter out nodes #283

Merged
merged 2 commits into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/floki.ex
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,9 @@ defmodule Floki do
iex> Floki.filter_out({"div", [], [{:comment, "comment"}, " text"]}, :comment)
{"div", [], [" text"]}

iex> Floki.filter_out({"div", [], ["text"]}, :text)
{"div", [], []}

"""

@spec filter_out(binary | html_tree, FilterOut.selector()) :: list
Expand Down
7 changes: 4 additions & 3 deletions lib/floki/filter_out.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ defmodule Floki.FilterOut do
# Helper functions for filtering out a specific element from the tree.

@type html_tree :: tuple | list
@type selector :: :comment | Finder.selector()
@type selector :: :comment | :text | Finder.selector()

@spec filter_out(html_tree, selector) :: tuple | list

def filter_out(html_tree, :comment) do
mapper(html_tree, :comment)
def filter_out(html_tree, type) when type in [:text, :comment] do
mapper(html_tree, type)
end

def filter_out(html_tree, selector) do
Expand Down Expand Up @@ -49,6 +49,7 @@ defmodule Floki.FilterOut do

defp filter({nodetext, _, _}, selector) when nodetext === selector, do: false
defp filter({nodetext, _}, selector) when nodetext === selector, do: false
defp filter(text, :text) when is_binary(text), do: false
defp filter(_, _), do: true

defp mapper(nodes, selector) when is_list(nodes) do
Expand Down
4 changes: 4 additions & 0 deletions test/floki/filter_out_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ defmodule Floki.FilterOutTest do
test "filter out filters script when it is the only node" do
assert Floki.FilterOut.filter_out({"script", [], ["wat"]}, "script") == []
end

test "filter out text nodes" do
assert Floki.FilterOut.filter_out({"p", [], ["test"]}, :text) == {"p", [], []}
end
end