Skip to content

Commit

Permalink
Correctly escape backslashes in PO composer (#139)
Browse files Browse the repository at this point in the history
Fixes #138.
  • Loading branch information
maennchen authored Aug 22, 2024
1 parent cbf1170 commit 5f1dbaf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/expo/po/composer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,6 @@ defmodule Expo.PO.Composer do
defp escape_char(?\n), do: ~S(\n)
defp escape_char(?\t), do: ~S(\t)
defp escape_char(?\r), do: ~S(\r)
defp escape_char(?\\), do: <<?\\::utf8, ?\\::utf8>>
defp escape_char(char), do: <<char>>
end
39 changes: 39 additions & 0 deletions test/expo/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,45 @@ defmodule Expo.ParserTest do
end
end

describe "unescaping" do
test "unescapes special characters" do
assert [
%Message.Singular{
msgid: ["backslash"],
msgstr: [<<?\\::utf8>>]
},
%Message.Singular{
msgid: ["double backslash"],
msgstr: [<<?\\::utf8, ?\\::utf8>>]
},
%Message.Singular{
msgid: ["tab"],
msgstr: [<<?\t::utf8>>]
},
%Message.Singular{
msgid: ["newline"],
msgstr: [<<?\r::utf8, ?\n::utf8>>]
},
%Message.Singular{
msgid: ["quote"],
msgstr: [<<?"::utf8>>]
}
] =
parse(~S"""
msgid "backslash"
msgstr "\\"
msgid "double backslash"
msgstr "\\\\"
msgid "tab"
msgstr "\t"
msgid "newline"
msgstr "\r\n"
msgid "quote"
msgstr "\""
""")
end
end

defp parse(string) do
case PO.parse_string(string) do
{:ok, %Messages{messages: messages}} ->
Expand Down
6 changes: 5 additions & 1 deletion test/expo/po_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ defmodule Expo.POTest do
headers: [],
messages: [
%Message.Singular{msgid: [~s("quotes")], msgstr: [~s(foo "bar" baz)]},
%Message.Singular{msgid: [~s(new\nlines\r)], msgstr: [~s(and\ttabs)]}
%Message.Singular{msgid: [~s(new\nlines\r)], msgstr: [~s(and\ttabs)]},
%Message.Singular{msgid: [~s(backslashes\\)], msgstr: [~s(and \\\\ double backslashes)]}
]
}

Expand All @@ -427,6 +428,9 @@ defmodule Expo.POTest do
msgid "new\nlines\r"
msgstr "and\ttabs"
msgid "backslashes\\"
msgstr "and \\\\ double backslashes"
"""
end

Expand Down

0 comments on commit 5f1dbaf

Please sign in to comment.