From b55d935241420e722b10f39234b16f2a4b2f8955 Mon Sep 17 00:00:00 2001 From: Jasmin Savard Date: Wed, 13 Dec 2023 13:59:18 -0500 Subject: [PATCH] Escape \r\n --- .../PoWriter.cs | 2 ++ .../PoWriterTests.cs | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/OrchardCoreContrib.PoExtractor/PoWriter.cs b/src/OrchardCoreContrib.PoExtractor/PoWriter.cs index 00b4621..29aa12b 100644 --- a/src/OrchardCoreContrib.PoExtractor/PoWriter.cs +++ b/src/OrchardCoreContrib.PoExtractor/PoWriter.cs @@ -104,6 +104,8 @@ private static string Escape(string text) var sb = new StringBuilder(text); sb.Replace("\\", "\\\\"); // \ -> \\ sb.Replace("\"", "\\\""); // " -> \" + sb.Replace("\r", "\\r"); + sb.Replace("\n", "\\n"); return sb.ToString(); } diff --git a/test/OrchardCoreContrib.PoExtractor.Tests/PoWriterTests.cs b/test/OrchardCoreContrib.PoExtractor.Tests/PoWriterTests.cs index f9a7bcc..c2cea9b 100644 --- a/test/OrchardCoreContrib.PoExtractor.Tests/PoWriterTests.cs +++ b/test/OrchardCoreContrib.PoExtractor.Tests/PoWriterTests.cs @@ -27,6 +27,27 @@ public void WriteRecord_WritesSingularLocalizableString() Assert.Equal($"msgstr \"\"", result[1]); } + [Fact] + public void WriteRecord_Escapes() + { + // Arrange + var localizableString = new LocalizableString + { + Text = "Computer \r\n" + }; + + // Act + using (var writer = new PoWriter(_stream)) + { + writer.WriteRecord(localizableString); + } + + // Assert + var result = ReadPoStream(); + Assert.Equal($"msgid \"Computer \\r\\n\"", result[0]); + Assert.Equal($"msgstr \"\"", result[1]); + } + [Fact] public void WriteRecord_WritesPluralLocalizableString() {