Skip to content

Commit 84f41e5

Browse files
authored
Merge pull request #16762 from adalagandev/BAEL-5732_Unescape_HTML_Character_Entities_in_Java
BAEL-5732 Initial code changes for Unescaping html entities/chars.
2 parents 7d813df + 31416f9 commit 84f41e5

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

core-java-modules/core-java-string-operations-9/pom.xml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@
4444
<artifactId>commons-collections4</artifactId>
4545
<version>${apache.commons.collection.version}</version>
4646
</dependency>
47+
<dependency>
48+
<groupId>org.apache.commons</groupId>
49+
<artifactId>commons-text</artifactId>
50+
<version>${commons-text.version}</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.unbescape</groupId>
54+
<artifactId>unbescape</artifactId>
55+
<version>1.1.6.RELEASE</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.jsoup</groupId>
59+
<artifactId>jsoup</artifactId>
60+
<version>1.14.3</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.springframework</groupId>
64+
<artifactId>spring-web</artifactId>
65+
<version>6.1.6</version>
66+
</dependency>
67+
4768
</dependencies>
4869

4970
<build>
@@ -67,4 +88,4 @@
6788
<icu4j.version>74.1</icu4j.version>
6889
</properties>
6990

70-
</project>
91+
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.unescapehtml;
2+
3+
4+
import org.apache.commons.text.StringEscapeUtils;
5+
import org.jsoup.nodes.Entities;
6+
import org.junit.Assert;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.web.util.HtmlUtils;
9+
import org.unbescape.html.HtmlEscape;
10+
11+
public class HtmlUnescapeUnitTest {
12+
@Test
13+
public void givenAStringsWithHTMLCharacters_whenConvertedWithApacheCommons_thenUnescape(){
14+
String expectedQuote = "\"Hello\" Baeldung";
15+
String escapedQuote = "&quot;Hello&quot; Baeldung";
16+
Assert.assertEquals(expectedQuote, StringEscapeUtils.unescapeHtml4(escapedQuote));
17+
18+
String escapedStringsWithHtmlSymbol = "&lt;p&gt;&lt;strong&gt;Test sentence in bold type.&lt;/strong&gt;&lt;/p&gt;";
19+
String expectedStringsWithHtmlSymbol = "<p><strong>Test sentence in bold type.</strong></p>";
20+
Assert.assertEquals(expectedStringsWithHtmlSymbol, StringEscapeUtils.unescapeHtml4(escapedStringsWithHtmlSymbol));
21+
}
22+
@Test
23+
public void givenAStringsWithHTMLCharacters_whenConvertedWithSpringHtmlUtil_thenUnescape() {
24+
String expectedQuote = "\"Code smells\" -Martin Fowler";
25+
String escapedQuote = "&quot;Code smells&quot; -Martin Fowler";
26+
Assert.assertEquals(expectedQuote, HtmlUtils.htmlUnescape(escapedQuote));
27+
28+
String escapedStringsWithHtmlSymbol = "&lt;p&gt;Loren Ipsum is a popular paragraph.&lt;/p&gt;";
29+
String expectedStringsWithHtmlSymbol = "<p>Loren Ipsum is a popular paragraph.</p>";
30+
Assert.assertEquals(expectedStringsWithHtmlSymbol, HtmlUtils.htmlUnescape(escapedStringsWithHtmlSymbol));
31+
}
32+
33+
@Test
34+
public void givenAStringsWithHTMLCharacters_whenConvertedWithUnbescape_thenUnescape() {
35+
String expectedQuote = "\"Carpe diem\" -Horace";
36+
String escapedQuote = "&quot;Carpe diem&quot; -Horace";
37+
Assert.assertEquals(expectedQuote, HtmlEscape.unescapeHtml(escapedQuote));
38+
39+
String escapedStringsWithHtmlSymbol = "&lt;p&gt;&lt;em&gt;Pizza is a famous Italian food. Duh.&lt;/em&gt;&lt;/p&gt;";
40+
String expectedStringsWithHtmlSymbol = "<p><em>Pizza is a famous Italian food. Duh.</em></p>";
41+
Assert.assertEquals(expectedStringsWithHtmlSymbol, HtmlEscape.unescapeHtml(escapedStringsWithHtmlSymbol));
42+
}
43+
@Test
44+
public void givenAStringsWithHTMLCharacters_whenConvertedWithJsoup_thenUnescape() {
45+
String expectedQuote = "\"Jsoup\" is another strong library";
46+
String escapedQuote = "&quot;Jsoup&quot; is another strong library";
47+
Assert.assertEquals(expectedQuote, Entities.unescape(escapedQuote));
48+
49+
String escapedStringsWithHtmlSymbol = "&lt;p&gt;It simplifies working with real-world &lt;strong&gt;HTML&lt;/strong&gt; and &lt;strong&gt;XML&lt;/strong&gt;&lt;/p&gt;";
50+
String expectedStringsWithHtmlSymbol = "<p>It simplifies working with real-world <strong>HTML</strong> and <strong>XML</strong></p>";
51+
Assert.assertEquals(expectedStringsWithHtmlSymbol, Entities.unescape(escapedStringsWithHtmlSymbol));
52+
}
53+
}

spring-core-4/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,4 @@
8080
<commons-text.version>1.10.0</commons-text.version>
8181
</properties>
8282

83-
</project>
83+
</project>

0 commit comments

Comments
 (0)