Skip to content

Commit

Permalink
[Java] Stop emitting close tags if self-closing (#362)
Browse files Browse the repository at this point in the history
* Java: Stop emitting close tags if self-closing

This patch changes Java `HTMLProcessor` not to emit close tags if the tag is self-closing.

Also adds tests for:
* Unpaired close tags.
* Self-closing tags don't affect skip nodes (e.g., `<nobr>`.)
These test cases are from #355.

Fixes #361.
  • Loading branch information
kojiishi authored Nov 13, 2023
1 parent 2457c51 commit 14da897
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
4 changes: 4 additions & 0 deletions java/src/main/java/com/google/budoux/HTMLProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public void tail(Node node, int depth) {
}
// assume node instanceof Element;
toSkip = elementStack.pop();
Element element = (Element) node;
if (element.tag().isSelfClosing()) {
return;
}
output.append(String.format("</%s>", node.nodeName()));
}
}
Expand Down
60 changes: 43 additions & 17 deletions java/src/test/java/com/google/budoux/HTMLProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,57 +27,83 @@
/** Unit tests for {@link HTMLProcessor}. */
@RunWith(JUnit4.class)
public class HTMLProcessorTest {
String pre = "<span style=\"word-break: keep-all; overflow-wrap: anywhere;\">";
String post = "</span>";

private String wrap(String input) {
return this.pre + input + this.post;
}

@Test
public void testResolveWithSimpleTextInput() {
List<String> phrases = Arrays.asList("abc", "def");
String html = "abcdef";
String result = HTMLProcessor.resolve(phrases, html, "<wbr>");
assertEquals(
"<span style=\"word-break: keep-all; overflow-wrap: anywhere;\">abc<wbr>def</span>",
result);
assertEquals(this.wrap("abc<wbr>def"), result);
}

@Test
public void testResolveWithStandardHTMLInput() {
List<String> phrases = Arrays.asList("abc", "def");
String html = "ab<a href=\"http://example.com\">cd</a>ef";
String result = HTMLProcessor.resolve(phrases, html, "<wbr>");
assertEquals(
"<span style=\"word-break: keep-all; overflow-wrap: anywhere;\">ab<a"
+ " href=\"http://example.com\">c<wbr>d</a>ef</span>",
result);
assertEquals(this.wrap("ab<a href=\"http://example.com\">c<wbr>d</a>ef"), result);
}

@Test
public void testResolveWithImg() {
List<String> phrases = Arrays.asList("abc", "def");
String html = "<img>abcdef";
String result = HTMLProcessor.resolve(phrases, html, "<wbr>");
assertEquals(this.wrap("<img>abc<wbr>def"), result);
}

@Test
public void testResolveWithUnpairedClose() {
List<String> phrases = Arrays.asList("abc", "def");
String html = "abcdef</p>";
String result = HTMLProcessor.resolve(phrases, html, "<wbr>");
assertEquals(this.wrap("abc<wbr>def<p></p>"), result);
}

@Test
public void testResolveWithNodesToSkip() {
List<String> phrases = Arrays.asList("abc", "def", "ghi");
String html = "a<button>bcde</button>fghi";
String result = HTMLProcessor.resolve(phrases, html, "<wbr>");
assertEquals(
"<span style=\"word-break: keep-all; overflow-wrap:"
+ " anywhere;\">a<button>bcde</button>f<wbr>ghi</span>",
result);
assertEquals(this.wrap("a<button>bcde</button>f<wbr>ghi"), result);
}

@Test
public void testResolveWithNodesBreakBeforeSkip() {
List<String> phrases = Arrays.asList("abc", "def", "ghi", "jkl");
String html = "abc<nobr>defghi</nobr>jkl";
String result = HTMLProcessor.resolve(phrases, html, "<wbr>");
assertEquals(
"<span style=\"word-break: keep-all; overflow-wrap:"
+ " anywhere;\">abc<wbr><nobr>defghi</nobr><wbr>jkl</span>",
result);
assertEquals(this.wrap("abc<wbr><nobr>defghi</nobr><wbr>jkl"), result);
}

@Test
public void testResolveWithAfterSkip() {
List<String> phrases = Arrays.asList("abc", "def", "ghi", "jkl");
String html = "abc<nobr>def</nobr>ghijkl";
String result = HTMLProcessor.resolve(phrases, html, "<wbr>");
assertEquals(this.wrap("abc<wbr><nobr>def</nobr><wbr>ghi<wbr>jkl"), result);
}

@Test
public void testResolveWithAfterSkipWithImg() {
List<String> phrases = Arrays.asList("abc", "def", "ghi", "jkl");
String html = "abc<nobr>d<img>ef</nobr>ghijkl";
String result = HTMLProcessor.resolve(phrases, html, "<wbr>");
assertEquals(this.wrap("abc<wbr><nobr>d<img>ef</nobr><wbr>ghi<wbr>jkl"), result);
}

@Test
public void testResolveWithNothingToSplit() {
List<String> phrases = Arrays.asList("abcdef");
String html = "abcdef";
String result = HTMLProcessor.resolve(phrases, html, "<wbr>");
assertEquals(
"<span style=\"word-break: keep-all; overflow-wrap: anywhere;\">abcdef</span>", result);
assertEquals(this.wrap("abcdef"), result);
}

@Test
Expand Down

0 comments on commit 14da897

Please # to comment.