Skip to content

Commit 5f1dbaf

Browse files
authored
Correctly escape backslashes in PO composer (#139)
Fixes #138.
1 parent cbf1170 commit 5f1dbaf

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

lib/expo/po/composer.ex

+1
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,6 @@ defmodule Expo.PO.Composer do
135135
defp escape_char(?\n), do: ~S(\n)
136136
defp escape_char(?\t), do: ~S(\t)
137137
defp escape_char(?\r), do: ~S(\r)
138+
defp escape_char(?\\), do: <<?\\::utf8, ?\\::utf8>>
138139
defp escape_char(char), do: <<char>>
139140
end

test/expo/parser_test.exs

+39
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,45 @@ defmodule Expo.ParserTest do
415415
end
416416
end
417417

418+
describe "unescaping" do
419+
test "unescapes special characters" do
420+
assert [
421+
%Message.Singular{
422+
msgid: ["backslash"],
423+
msgstr: [<<?\\::utf8>>]
424+
},
425+
%Message.Singular{
426+
msgid: ["double backslash"],
427+
msgstr: [<<?\\::utf8, ?\\::utf8>>]
428+
},
429+
%Message.Singular{
430+
msgid: ["tab"],
431+
msgstr: [<<?\t::utf8>>]
432+
},
433+
%Message.Singular{
434+
msgid: ["newline"],
435+
msgstr: [<<?\r::utf8, ?\n::utf8>>]
436+
},
437+
%Message.Singular{
438+
msgid: ["quote"],
439+
msgstr: [<<?"::utf8>>]
440+
}
441+
] =
442+
parse(~S"""
443+
msgid "backslash"
444+
msgstr "\\"
445+
msgid "double backslash"
446+
msgstr "\\\\"
447+
msgid "tab"
448+
msgstr "\t"
449+
msgid "newline"
450+
msgstr "\r\n"
451+
msgid "quote"
452+
msgstr "\""
453+
""")
454+
end
455+
end
456+
418457
defp parse(string) do
419458
case PO.parse_string(string) do
420459
{:ok, %Messages{messages: messages}} ->

test/expo/po_test.exs

+5-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,8 @@ defmodule Expo.POTest do
417417
headers: [],
418418
messages: [
419419
%Message.Singular{msgid: [~s("quotes")], msgstr: [~s(foo "bar" baz)]},
420-
%Message.Singular{msgid: [~s(new\nlines\r)], msgstr: [~s(and\ttabs)]}
420+
%Message.Singular{msgid: [~s(new\nlines\r)], msgstr: [~s(and\ttabs)]},
421+
%Message.Singular{msgid: [~s(backslashes\\)], msgstr: [~s(and \\\\ double backslashes)]}
421422
]
422423
}
423424

@@ -427,6 +428,9 @@ defmodule Expo.POTest do
427428
428429
msgid "new\nlines\r"
429430
msgstr "and\ttabs"
431+
432+
msgid "backslashes\\"
433+
msgstr "and \\\\ double backslashes"
430434
"""
431435
end
432436

0 commit comments

Comments
 (0)