-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Floki.traverse_and_update/2. Fix #206.
- Loading branch information
1 parent
6dd9435
commit a1275e5
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule Floki.Traversal do | ||
@moduledoc false | ||
|
||
def traverse_and_update(text, _fun) when is_binary(text), do: text | ||
|
||
def traverse_and_update([head | tail], fun) do | ||
case traverse_and_update(head, fun) do | ||
nil -> traverse_and_update(tail, fun) | ||
mapped_head -> [mapped_head | traverse_and_update(tail, fun)] | ||
end | ||
end | ||
|
||
def traverse_and_update([], _fun), do: [] | ||
|
||
def traverse_and_update({elem, attrs, children}, fun) do | ||
mapped_children = traverse_and_update(children, fun) | ||
fun.({elem, attrs, mapped_children}) | ||
end | ||
|
||
def traverse_and_update({:comment, children}, fun) do | ||
fun.({:comment, children}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
defmodule Floki.TraversalTest do | ||
use ExUnit.Case, async: true | ||
|
||
describe "traverse_and_update/2" do | ||
test "with string returns string" do | ||
assert Floki.traverse_and_update("hello", fn tag -> tag end) == "hello" | ||
end | ||
|
||
test "with div and identity function returns div" do | ||
html = {"div", [], ["hello"]} | ||
|
||
assert Floki.traverse_and_update(html, fn tag -> tag end) == html | ||
end | ||
|
||
test "with div and div->p function returns p" do | ||
html = {"div", [], ["hello"]} | ||
|
||
assert Floki.traverse_and_update(html, fn {"div", attrs, children} -> | ||
{"p", attrs, children} | ||
end) == | ||
{"p", [], ["hello"]} | ||
end | ||
|
||
test "with style attribute and style->src function returns src attribute" do | ||
html = {"div", [{"style", "display: flex"}], ["hello"]} | ||
|
||
assert Floki.traverse_and_update(html, fn {elem, _attrs, children} -> | ||
{elem, [{"src", "http://example.test"}], children} | ||
end) == | ||
{"div", [{"src", "http://example.test"}], ["hello"]} | ||
end | ||
|
||
test "with text child and child replacer function returns tag with new child" do | ||
html = {"div", [], ["hello"]} | ||
|
||
assert Floki.traverse_and_update(html, fn {elem, attrs, _children} -> | ||
{elem, attrs, ["world"]} | ||
end) == | ||
{"div", [], ["world"]} | ||
end | ||
|
||
test "with div->span and span deleter function returns div without span" do | ||
html = {"div", [], [{"span", [], ["hello"]}]} | ||
|
||
assert Floki.traverse_and_update(html, fn | ||
{"span", _attrs, _children} -> nil | ||
tag -> tag | ||
end) == | ||
{"div", [], []} | ||
end | ||
|
||
test "with div,p and identity function returns div,p" do | ||
html = [{"div", [], ["hello"]}, {"p", [], ["world"]}] | ||
|
||
assert Floki.traverse_and_update(html, fn tag -> tag end) == html | ||
end | ||
end | ||
end |