Skip to content

Commit

Permalink
fix: handle \r in windows
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr committed Jun 5, 2024
1 parent 0363a9e commit a5ea8d4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/org/omegat/util/TmxEscapingWriterFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static class EscapeWriter extends Writer {
q['<'] = 1;
q['>'] = 1;
q['&'] = 1;
q['\r'] = (byte) (Platform.isWindows ? 0 : 1);
QUOTABLE_TEXT_CHARS = q;
}

Expand Down
3 changes: 2 additions & 1 deletion test/src/org/omegat/util/TMXWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
**************************************************************************/
package org.omegat.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -178,7 +179,7 @@ public void testEOLwrite() throws Exception {
text.append(buffer, 0, len);
}
}
assertTrue(text.toString().contains("tar" + eol + "get"));
assertThat(text.toString()).as("Preserve EOL mark in text.").contains("tar" + eol + "get");

final List<String> trs = new ArrayList<>();
load(null, trs, true, false);
Expand Down
58 changes: 54 additions & 4 deletions test/src/org/omegat/util/TmxEscapingWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,81 @@ public void setUp() throws UnsupportedEncodingException {
writer = factory.createEscapingWriterFor(outputStream, null);
}

/**
* Test basic characters, as-is.
*
* @throws IOException I/O error happened.
*/
@Test
public void escapeBasic() throws IOException {
writer.write("Hello world!\n");
writer.flush();
assertThat(outputStream.toString()).isEqualTo("Hello world!\n");
}

/**
* Test signs to be escaped.
* @throws IOException I/O error happened.
*/
@Test
public void escapeToEntities() throws IOException {
writer.write("\"<escape>\"");
writer.write("'<escape>\"");
writer.flush();
assertThat(outputStream.toString()).isEqualTo("\"&lt;escape&gt;\"");
assertThat(outputStream.toString())
.as("Check escape of < & and > signs and as-is for single/double quote")
.isEqualTo("'&lt;escape&gt;\"");
}

/**
* Test escape of NBSP, no-escape.
* @throws IOException I/O error happened.
*/
@Test
public void testNBSP() throws IOException {
writer.write("\u00a0");
writer.flush();
assertThat(outputStream.toString())
.as("Check NBSP is not escaped.")
.isEqualTo("\u00a0");
}

/**
* Test Control character No-Break-Here, escape.
*
* @throws IOException I/O error happened.
*/
@Test
public void testNBH() throws IOException {
writer.write("\u0083");
writer.flush();
assertThat(outputStream.toString())
.as("Check NO_BREAK_HERE control character to be escaped")
.isEqualTo("&#x83;");
}

/**
* Test emoji and flag, surrogate pair, escape.
* @throws IOException I/O error happened.
*/
@Test
public void testSurrogatePair() throws IOException {
writer.write("\uD83D\uDE00");
writer.flush();
assertThat(outputStream.toString()).isEqualTo("\uD83D\uDE00");
assertThat(outputStream.toString())
.as("Check emoji and flag that requires surrogate pair for encode.")
.isEqualTo("\uD83D\uDE00");
}

/**
* Test Invalid character, BOM flag, escape.
* @throws IOException I/O error happened.
*/
@Test
public void testInvalidChar() throws IOException {
writer.write((char) 0xFFFE);
writer.flush();
assertThat(outputStream.toString()).isEqualToIgnoringCase("&#xfffe;");
assertThat(outputStream.toString())
.as("check BOM mark to be escaped when appeared in TEXT.")
.isEqualToIgnoringCase("&#xfffe;");
}
}

0 comments on commit a5ea8d4

Please sign in to comment.