View Source Premailex.Util (Premailex v0.3.19)

Module that contains utility functions.

Summary

Functions

Traverses tree searching for needle, and will call provided function on any occurances.

Traverse all trees in array searching for needle, and will call function with element and number times needle has been found so far.

Traverses tree until first match for needle.

Types

@type html_tree() :: Premailex.HTMLParser.html_tree()
@type needle() :: binary() | html_tree() | :comment

Functions

Link to this function

traverse(html, needles, fun)

View Source
@spec traverse(html_tree(), needle(), function()) ::
  html_tree() | {:halt, html_tree()}

Traverses tree searching for needle, and will call provided function on any occurances.

If the function returns {:halt, any}, traverse will stop, and result will be {:halt, html_tree}.

Examples

iex> Premailex.Util.traverse({"div", [], [{"p", [], ["First paragraph"]}, {"p", [], ["Second paragraph"]}]}, "p", fn {name, attrs, _children} -> {name, attrs, ["Updated"]} end)
{"div", [], [{"p", [], ["Updated"]}, {"p", [], ["Updated"]}]}

iex> Premailex.Util.traverse({"div", [], [{"p", [], ["First paragraph"]}, {"p", [], ["Second paragraph"]}]}, {"p", [], ["Second paragraph"]}, fn {name, attrs, _children} -> {name, attrs, ["Updated"]} end)
{"div", [], [{"p", [], ["First paragraph"]}, {"p", [], ["Updated"]}]}

iex> Premailex.Util.traverse({"div", [], [{:comment, "This is a comment"}, {"p", [], ["Paragraph"]}]}, :comment, fn {:comment, _comment} -> {:comment, "Updated"} end)
{"div", [], [{:comment, "Updated"}, {"p", [], ["Paragraph"]}]}
Link to this function

traverse_reduce(children, needle, fun)

View Source
@spec traverse_reduce(list(), needle(), function()) :: {html_tree(), integer()}

Traverse all trees in array searching for needle, and will call function with element and number times needle has been found so far.

Examples

iex> Premailex.Util.traverse_reduce([{"p", [], ["First paragraph"]}, {"p", [], ["Second paragraph"]}], "p", fn({name, attrs, _children}, acc) -> {name, attrs, ["Updated " <> to_string(acc)]} end)
{[{"p", [], ["Updated 0"]}, {"p", [], ["Updated 1"]}], 2}
Link to this function

traverse_until_first(html, needle, fun)

View Source
@spec traverse_until_first(html_tree(), needle(), function()) :: html_tree()

Traverses tree until first match for needle.

Examples

iex> Premailex.Util.traverse_until_first({"div", [], [{"p", [], ["First paragraph"]}, {"p", [], ["Second paragraph"]}]}, "p", fn {name, attrs, _children} -> {name, attrs, ["Updated"]} end)
{"div", [], [{"p", [], ["Updated"]}, {"p", [], ["Second paragraph"]}]}