x_component v0.1.0 X.Html View Source

Contains a set of functions to build a valid and safe HTML from X templates.

Link to this section Summary

Functions

Converts attr value into HTML-safe iodata

Converts given attrs into HTML-safe iodata

Merges given attrs and returns a list with key-value tuples

Converts given value into HTML-safe iodata

Link to this section Functions

Link to this function

attr_value_to_iodata(value, key \\ "")

View Source
attr_value_to_iodata(any(), String.t()) :: iodata()

Converts attr value into HTML-safe iodata:

iex> X.Html.attr_value_to_iodata("<test>")
[[[] | "&lt;"], "test" | "&gt;"]

"style" and "class" attr values are joined with a delimiter:

iex> X.Html.attr_value_to_iodata([{"color", "#fff"}, {"font", "small"}], "style")
[["color", ": ", "#fff"], "; ", ["font", ": ", "small"]]
Link to this function

attrs_to_iodata(attrs)

View Source
attrs_to_iodata(map() | [{String.t(), any()}]) :: iodata()

Converts given attrs into HTML-safe iodata:

iex> X.Html.attrs_to_iodata(%{"demo" => true, "env" => "<test>"})
[["demo", '="', "true", '"'], 32, "env", '="', [[[] | "&lt;"], "test" | "&gt;"], '"']
Link to this function

merge_attrs(base_attrs, merge_attrs)

View Source
merge_attrs(any(), any()) :: [{String.t(), any()}]

Merges given attrs and returns a list with key-value tuples:

iex> X.Html.merge_attrs(%{demo: true, env: "test"}, [demo: false])
[{"env", "test"}, {"demo", false}]

It doesn't override "style" and "class" attributes from base_attrs but adds merged values into the list:

iex> X.Html.merge_attrs(
...>   %{style: [{"color", "#fff"}, {"size", 1}]},
...>   [style: [{"color", "#aaa"}, {"font", "test"}]]
...> )
[{"style", [{"size", 1}, {"color", "#aaa"}, {"font", "test"}]}]
Link to this function

to_safe_iodata(value)

View Source
to_safe_iodata(any()) :: iodata()

Converts given value into HTML-safe iodata:

iex> X.Html.to_safe_iodata("<test>")
[[[] | "&lt;"], "test" | "&gt;"]