From adc6f3a26f55eb696071b459452326a5ef40fef5 Mon Sep 17 00:00:00 2001
From: Koji Ishii
Date: Sat, 11 Nov 2023 15:58:14 +0900
Subject: [PATCH] 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., ``.)
These test cases are from #355.
Fixes #361.
---
.../java/com/google/budoux/HTMLProcessor.java | 4 ++
.../com/google/budoux/HTMLProcessorTest.java | 58 ++++++++++++++-----
2 files changed, 49 insertions(+), 13 deletions(-)
diff --git a/java/src/main/java/com/google/budoux/HTMLProcessor.java b/java/src/main/java/com/google/budoux/HTMLProcessor.java
index 5797967d..b63a6db3 100644
--- a/java/src/main/java/com/google/budoux/HTMLProcessor.java
+++ b/java/src/main/java/com/google/budoux/HTMLProcessor.java
@@ -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()));
}
}
diff --git a/java/src/test/java/com/google/budoux/HTMLProcessorTest.java b/java/src/test/java/com/google/budoux/HTMLProcessorTest.java
index 123ba018..fe5cd39d 100644
--- a/java/src/test/java/com/google/budoux/HTMLProcessorTest.java
+++ b/java/src/test/java/com/google/budoux/HTMLProcessorTest.java
@@ -27,15 +27,19 @@
/** Unit tests for {@link HTMLProcessor}. */
@RunWith(JUnit4.class)
public class HTMLProcessorTest {
+ String pre = "";
+ String post = "";
+
+ private String wrap(String input) {
+ return this.pre + input + this.post;
+ }
@Test
public void testResolveWithSimpleTextInput() {
List phrases = Arrays.asList("abc", "def");
String html = "abcdef";
String result = HTMLProcessor.resolve(phrases, html, "");
- assertEquals(
- "abcdef",
- result);
+ assertEquals(this.wrap("abcdef"), result);
}
@Test
@@ -44,20 +48,32 @@ public void testResolveWithStandardHTMLInput() {
String html = "abcdef";
String result = HTMLProcessor.resolve(phrases, html, "");
assertEquals(
- "abcdef",
+ this.wrap("abcdef"),
result);
}
+ @Test
+ public void testResolveWithImg() {
+ List phrases = Arrays.asList("abc", "def");
+ String html = "
abcdef";
+ String result = HTMLProcessor.resolve(phrases, html, "");
+ assertEquals(this.wrap("
abcdef"), result);
+ }
+
+ @Test
+ public void testResolveWithUnpairedClose() {
+ List phrases = Arrays.asList("abc", "def");
+ String html = "abcdef
";
+ String result = HTMLProcessor.resolve(phrases, html, "");
+ assertEquals(this.wrap("abcdef"), result);
+ }
+
@Test
public void testResolveWithNodesToSkip() {
List phrases = Arrays.asList("abc", "def", "ghi");
String html = "afghi";
String result = HTMLProcessor.resolve(phrases, html, "");
- assertEquals(
- "afghi",
- result);
+ assertEquals(this.wrap("afghi"), result);
}
@Test
@@ -65,9 +81,26 @@ public void testResolveWithNodesBreakBeforeSkip() {
List phrases = Arrays.asList("abc", "def", "ghi", "jkl");
String html = "abcdefghijkl";
String result = HTMLProcessor.resolve(phrases, html, "");
+ assertEquals(this.wrap("abcdefghijkl"), result);
+ }
+
+ @Test
+ public void testResolveWithAfterSkip() {
+ List phrases = Arrays.asList("abc", "def", "ghi", "jkl");
+ String html = "abcdefghijkl";
+ String result = HTMLProcessor.resolve(phrases, html, "");
+ assertEquals(
+ this.wrap("abcdefghijkl"),
+ result);
+ }
+
+ @Test
+ public void testResolveWithAfterSkipWithImg() {
+ List phrases = Arrays.asList("abc", "def", "ghi", "jkl");
+ String html = "abcd
efghijkl";
+ String result = HTMLProcessor.resolve(phrases, html, "");
assertEquals(
- "abcdefghijkl",
+ this.wrap("abcd
efghijkl"),
result);
}
@@ -76,8 +109,7 @@ public void testResolveWithNothingToSplit() {
List phrases = Arrays.asList("abcdef");
String html = "abcdef";
String result = HTMLProcessor.resolve(phrases, html, "");
- assertEquals(
- "abcdef", result);
+ assertEquals(this.wrap("abcdef"), result);
}
@Test