Skip to content

Commit 6d3e2b3

Browse files
Add LazyHTML.html_escape/1 (#14)
1 parent ceca1f9 commit 6d3e2b3

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/lazy_html.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,26 @@ defmodule LazyHTML do
481481
LazyHTML.NIF.tag(lazy_html)
482482
end
483483

484+
@doc ~S"""
485+
Escapes the given string to make a valid HTML text.
486+
487+
## Examples
488+
489+
iex> LazyHTML.html_escape("foo")
490+
"foo"
491+
492+
iex> LazyHTML.html_escape("<foo>")
493+
"&lt;foo&gt;"
494+
495+
iex> LazyHTML.html_escape("quotes: \" & \'")
496+
"quotes: &quot; &amp; &#39;"
497+
498+
"""
499+
@spec html_escape(String.t()) :: String.t()
500+
def html_escape(string) when is_binary(string) do
501+
LazyHTML.Tree.append_escaped(string, "")
502+
end
503+
484504
# Access
485505

486506
@impl true

lib/lazy_html/tree.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ defmodule LazyHTML.Tree do
133133
#
134134
# [1]: https://github.com/phoenixframework/phoenix_html/blob/v4.2.1/lib/phoenix_html/engine.ex#L29-L35
135135

136-
defp append_escaped(text, html) do
136+
@doc false
137+
def append_escaped(text, html) do
137138
append_escaped(text, text, 0, 0, html)
138139
end
139140

0 commit comments

Comments
 (0)