Skip to content

Commit

Permalink
Merge pull request #289 from josevalim/jv-document-post-walk-and-trav…
Browse files Browse the repository at this point in the history
…erse-nodes

Document postwalk behaviour and html nodes
  • Loading branch information
philss committed Jun 24, 2020
2 parents 1bdc62d + 9ce651d commit a65ba3f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
17 changes: 11 additions & 6 deletions lib/floki.ex
Expand Up @@ -61,11 +61,13 @@ defmodule Floki do
inside a list.
"""

@type html_declaration :: {:pi, String.t(), [html_attribute()]}
@type html_comment :: {:comment, String.t()}
@type html_doctype :: {:doctype, String.t(), String.t(), String.t()}
@type html_attribute :: {String.t(), String.t()}
@type html_tag :: {String.t(), [html_attribute()], [html_tag() | String.t() | html_comment()]}
@type html_tree :: [html_comment() | html_doctype() | html_tag()]
@type html_node :: html_comment() | html_doctype() | html_tag() | html_declaration()
@type html_tree :: [html_node()]

@doc """
Parses a HTML Document from a String.
Expand Down Expand Up @@ -326,7 +328,8 @@ defmodule Floki do
Traverses and updates a HTML tree structure.
This function returns a new tree structure that is the result of applying the
given `fun` on all nodes.
given `fun` on all nodes. The tree is traversed in a post-walk fashion, where
the children are traversed before the parent.
The function `fun` receives a tuple with `{name, attributes, children}`, and
should either return a similar tuple or `nil` to delete the current node.
Expand All @@ -347,15 +350,17 @@ defmodule Floki do
{"div", [], []}
"""

@spec traverse_and_update(html_tree(), (html_tag() -> html_tag() | nil)) :: html_tree()
@spec traverse_and_update(html_tree(), (html_node() -> html_node() | nil)) :: html_tree()

defdelegate traverse_and_update(html_tree, fun), to: Floki.Traversal

@doc """
Traverses and updates a HTML tree structure with an accumulator.
This function returns a new tree structure and the final value of accumulator
which are the result of applying the given `fun` on all nodes.
which are the result of applying the given `fun` on all nodes. The tree is
traversed in a post-walk fashion, where the children are traversed before
the parent.
The function `fun` receives a tuple with `{name, attributes, children}` and
an accumulator, and should return a 2-tuple like `{new_node, new_acc}`, where
Expand Down Expand Up @@ -386,8 +391,8 @@ defmodule Floki do
@spec traverse_and_update(
html_tree(),
traverse_acc,
(html_tag(), traverse_acc -> {html_tag() | nil, traverse_acc})
) :: {html_tree(), traverse_acc}
(html_node(), traverse_acc -> {html_node() | nil, traverse_acc})
) :: {html_node(), traverse_acc}
when traverse_acc: any()

defdelegate traverse_and_update(html_tree, acc, fun), to: Floki.Traversal
Expand Down
3 changes: 0 additions & 3 deletions lib/floki/html_tree.ex
Expand Up @@ -35,9 +35,6 @@ defmodule Floki.HTMLTree do

def build(html_tuples) when is_list(html_tuples) do
reducer = fn
{:pi, _}, tree ->
tree

{:pi, _, _}, tree ->
tree

Expand Down
4 changes: 2 additions & 2 deletions lib/floki/raw_html.ex
Expand Up @@ -47,8 +47,8 @@ defmodule Floki.RawHTML do
defp build_raw_html([{:comment, comment} | tail], html, encoder),
do: build_raw_html(tail, [html, "<!--", comment | "-->"], encoder)

defp build_raw_html([{:pi, "xml", attrs} | tail], html, encoder) do
build_raw_html(tail, [html, "<?xml ", tag_attrs(attrs) | "?>"], encoder)
defp build_raw_html([{:pi, tag, attrs} | tail], html, encoder) do
build_raw_html(tail, [html, "<?", tag, " ", tag_attrs(attrs) | "?>"], encoder)
end

defp build_raw_html([{:doctype, type, public, system} | tail], html, encoder) do
Expand Down

0 comments on commit a65ba3f

Please sign in to comment.