Skip to content

Commit

Permalink
feat: Floki.filter_out/2 now can filter out text nodes (#283)
Browse files Browse the repository at this point in the history
* feat: `Floki.filter_out/2` now can filter out nodes

* fix: use `is_binary/1` instead of `is_bitstring/1`

Strings in Elixir are binaries, not bitstrings.

Co-authored-by: Christian Kruse <cjk@defunct.ch>
  • Loading branch information
ckruse and Christian Kruse committed Jun 21, 2020
1 parent 0580ef7 commit 1bdc62d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/floki.ex
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,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

0 comments on commit 1bdc62d

Please sign in to comment.