Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] Fix errors by collapsed white spaces and <br> #367

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions java/src/main/java/com/google/budoux/HTMLProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ public void head(Node node, int depth) {
.map(attribute -> " " + attribute)
.collect(Collectors.joining(""));
final String nodeName = node.nodeName();
if (skipNodes.contains(nodeName.toUpperCase(Locale.ENGLISH))) {
final String upperNodeName = nodeName.toUpperCase(Locale.ENGLISH);
if (upperNodeName.equals("BR")) {
// Match jsoup `Element.wholeText()` returning `\n` for `<br>`.
// Assume phrasesJoined.charAt(scanIndex) == '\n'.
scanIndex++;
} else if (skipNodes.contains(upperNodeName)) {
if (!toSkip && phrasesJoined.charAt(scanIndex) == SEP) {
output.append(separator);
scanIndex++;
Expand All @@ -110,6 +115,7 @@ public void head(Node node, int depth) {
for (int i = 0; i < data.length(); i++) {
char c = data.charAt(i);
if (c != phrasesJoined.charAt(scanIndex)) {
// Assume phrasesJoined.charAt(scanIndex) == SEP.
if (!toSkip) {
output.append(separator);
}
Expand Down Expand Up @@ -169,6 +175,6 @@ public static String resolve(List<String> phrases, String html, String separator
* @return the text content.
*/
public static String getText(String html) {
return Jsoup.parseBodyFragment(html).text();
return Jsoup.parseBodyFragment(html).wholeText();
}
}
24 changes: 24 additions & 0 deletions java/src/test/java/com/google/budoux/HTMLProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,34 @@ public void testResolveWithNothingToSplit() {
assertEquals(this.wrap("abcdef"), result);
}

@Test
public void testResolveBR() {
String html = " 1 <br> 2 ";
String text = HTMLProcessor.getText(html);
assertEquals(" 1 \n 2 ", text);
List<String> phrases = Arrays.asList(" 1 \n 2 ");
String result = HTMLProcessor.resolve(phrases, html, "<wbr>");
assertEquals(this.wrap(" 1 <br> 2 "), result);
}

@Test
public void testGetText() {
String html = "Hello <button><b>W</b>orld</button>!";
String result = HTMLProcessor.getText(html);
assertEquals("Hello World!", result);
}

@Test
public void testGetTextWhiteSpace() {
String html = " H e ";
String result = HTMLProcessor.getText(html);
assertEquals(" H e ", result);
}

@Test
public void testGetTextWhiteSpaceAcrossElements() {
String html = "<div> 1 </div><div> 2 </div>";
String result = HTMLProcessor.getText(html);
assertEquals(" 1 2 ", result);
}
}
8 changes: 8 additions & 0 deletions java/src/test/java/com/google/budoux/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ public void testTranslateHTMLString() {
+ " href=\"http://example.com\">xyz\u200ba</a>bc</span>",
result);
}

@Test
public void testNewline() {
Parser parser = Parser.loadDefaultJapaneseParser();
List<String> result = parser.parse(" 1 \n 2 ");
List<String> expected = Arrays.asList(" 1 \n 2 ");
assertEquals(expected, result);
}
}
Loading