>, original, skip, len, acc) do
+ part = binary_part(original, skip, len)
+ escape_binary(rest, original, skip + len + 1, 0, [acc, part, unquote(replacement)])
+ end
+ end
+
+ defp escape_binary(<<_char, rest::binary>>, original, skip, len, acc) do
+ escape_binary(rest, original, skip, len + 1, acc)
+ end
+
+ defp escape_binary(<<>>, original, skip, len, acc) do
+ [acc | binary_part(original, skip, len)]
end
defp escape(iodata) when is_list(iodata) do
diff --git a/test/html_formatter_test.exs b/test/html_formatter_test.exs
index 2577321..d4fa86c 100644
--- a/test/html_formatter_test.exs
+++ b/test/html_formatter_test.exs
@@ -74,6 +74,17 @@ defmodule MakeupTest.Lexer.HTMLFormatterTest do
end
end
+ test "a binary value is escaped, preserving safe characters and utf8" do
+ value = "a < b & \"c\" é 日"
+
+ assert HTMLFormatter.format_as_binary([{:string, %{}, value}]) ==
+ ~S[] <>
+ ~S[] <>
+ ~S[a < b & "c" é 日] <>
+ ~S[] <>
+ ~S[
]
+ end
+
test "group ids are encoded before being written to data attributes" do
html =
HTMLFormatter.format_as_binary([