livebook/test/livebook_web/ansi_test.exs
Frank Hunleth b1ceedc220
Ignore RFC 1468 switch to ASCII code sequence (#225)
This ignores "\e(B" which sometimes shows up even when the other RFC
1468 Japanese character set switch codes aren't used. This also updates
defmodifier so that it can be used with non-CSI escape codes (the
ones that start with '[').
2021-04-20 15:05:30 +02:00

102 lines
4.3 KiB
Elixir

defmodule LivebookWeb.ANSITest do
use ExUnit.Case, async: true
alias LivebookWeb.ANSI
describe "ansi_string_to_html/2" do
test "converts ANSI escape codes to span tags" do
assert ~s{<span style="color: var(--ansi-color-blue);">cat</span>} ==
ANSI.ansi_string_to_html("\e[34mcat\e[0m") |> Phoenix.HTML.safe_to_string()
assert ~s{<span style="background-color: var(--ansi-color-blue);">cat</span>} ==
ANSI.ansi_string_to_html("\e[44mcat\e[0m") |> Phoenix.HTML.safe_to_string()
assert ~s{<span style="font-weight: 600;">cat</span>} ==
ANSI.ansi_string_to_html("\e[1mcat\e[0m") |> Phoenix.HTML.safe_to_string()
assert ~s{<span style="text-decoration: underline;">cat</span>} ==
ANSI.ansi_string_to_html("\e[4mcat\e[0m") |> Phoenix.HTML.safe_to_string()
end
test "supports short reset sequence" do
assert ~s{<span style="color: var(--ansi-color-blue);">cat</span>} ==
ANSI.ansi_string_to_html("\e[34mcat\e[m") |> Phoenix.HTML.safe_to_string()
end
test "supports multiple escape codes at the same time" do
assert ~s{<span style="background-color: var(--ansi-color-red);color: var(--ansi-color-blue);">cat</span>} ==
ANSI.ansi_string_to_html("\e[34m\e[41mcat\e[0m") |> Phoenix.HTML.safe_to_string()
end
test "overriding a particular style property keeps the others" do
assert ~s{<span style="background-color: var(--ansi-color-red);">cat</span>} ==
ANSI.ansi_string_to_html("\e[34m\e[41m\e[39mcat\e[0m")
|> Phoenix.HTML.safe_to_string()
end
test "adjacent content with the same properties is wrapped in a single element" do
assert ~s{<span style="color: var(--ansi-color-blue);">coolcats</span>} ==
ANSI.ansi_string_to_html("\e[34mcool\e[0m\e[34mcats\e[0m")
|> Phoenix.HTML.safe_to_string()
end
test "modifiers have effect until reset" do
assert ~s{<span style="color: var(--ansi-color-blue);">cool</span><span style="color: var(--ansi-color-blue);text-decoration: underline;">cats</span>} ==
ANSI.ansi_string_to_html("\e[34mcool\e[4mcats\e[0m")
|> Phoenix.HTML.safe_to_string()
end
test "supports 8-bit rgb colors" do
assert ~s{<span style="color: rgb(51, 102, 153);">cat</span>} ==
ANSI.ansi_string_to_html("\e[38;5;67mcat\e[0m") |> Phoenix.HTML.safe_to_string()
end
test "supports 8-bit grayscale range" do
assert ~s{<span style="color: rgb(88, 88, 88);">cat</span>} ==
ANSI.ansi_string_to_html("\e[38;5;240mcat\e[0m") |> Phoenix.HTML.safe_to_string()
end
test "supports 8-bit well known colors" do
assert ~s{<span style="color: var(--ansi-color-red);">cat</span>} ==
ANSI.ansi_string_to_html("\e[38;5;1mcat\e[0m") |> Phoenix.HTML.safe_to_string()
end
test "ignores valid but irrelevant escape codes" do
assert ~s{cat} == ANSI.ansi_string_to_html("\e[H\e[1Acat") |> Phoenix.HTML.safe_to_string()
end
test "returns the whole string if on ANSI code detected" do
assert ~s{\e[300mcat} ==
ANSI.ansi_string_to_html("\e[300mcat") |> Phoenix.HTML.safe_to_string()
end
test "escapes HTML in the resulting string" do
assert ~s{&lt;div&gt;} == ANSI.ansi_string_to_html("<div>") |> Phoenix.HTML.safe_to_string()
end
test "given custom renderer uses it to generate HTML" do
div_renderer = fn
"", content -> content
style, content -> [~s{<div style="#{style}">}, content, ~s{</div>}]
end
assert ~s{<div style="color: var(--ansi-color-blue);">cat</div>} ==
ANSI.ansi_string_to_html("\e[34mcat\e[0m", renderer: div_renderer)
|> Phoenix.HTML.safe_to_string()
end
test "given custom renderer uses it for style-less text as well" do
div_renderer = fn _style, content ->
[~s{<div>}, content, ~s{</div>}]
end
assert ~s{<div>cat</div>} ==
ANSI.ansi_string_to_html("cat", renderer: div_renderer)
|> Phoenix.HTML.safe_to_string()
end
test "ignores RFC 1468 switch to ASCII" do
assert ~s{cat} == ANSI.ansi_string_to_html("\e(Bcat") |> Phoenix.HTML.safe_to_string()
end
end
end