Skip to content

Commit 8553f97

Browse files
committed
Merge HtmlCharacterEntityDecoderTests into HtmlUtilsTests
See gh-35711 (cherry picked from commit 0342cd0)
1 parent 030dace commit 8553f97

File tree

2 files changed

+40
-68
lines changed

2 files changed

+40
-68
lines changed

spring-web/src/test/java/org/springframework/web/util/HtmlCharacterEntityDecoderTests.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

spring-web/src/test/java/org/springframework/web/util/HtmlUtilsTests.java

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121
import static org.assertj.core.api.Assertions.assertThat;
2222

2323
/**
24+
* Tests for {@link HtmlUtils}.
25+
*
2426
* @author Alef Arendsen
2527
* @author Martin Kersten
2628
* @author Rick Evans
2729
*/
2830
class HtmlUtilsTests {
2931

3032
@Test
31-
void testHtmlEscape() {
33+
void htmlEscape() {
3234
String unescaped = "\"This is a quote'";
3335
String escaped = HtmlUtils.htmlEscape(unescaped);
3436
assertThat(escaped).isEqualTo(""This is a quote'");
@@ -39,14 +41,7 @@ void testHtmlEscape() {
3941
}
4042

4143
@Test
42-
void testHtmlUnescape() {
43-
String escaped = ""This is a quote'";
44-
String unescaped = HtmlUtils.htmlUnescape(escaped);
45-
assertThat(unescaped).isEqualTo("\"This is a quote'");
46-
}
47-
48-
@Test
49-
void testEncodeIntoHtmlCharacterSet() {
44+
void htmlEscapeIntoHtmlCharacterSet() {
5045
assertThat(HtmlUtils.htmlEscape("")).as("An empty string should be converted to an empty string").isEmpty();
5146
assertThat(HtmlUtils.htmlEscape("A sentence containing no special characters.")).as("A string containing no special characters should not be affected").isEqualTo("A sentence containing no special characters.");
5247

@@ -60,12 +55,11 @@ void testEncodeIntoHtmlCharacterSet() {
6055
assertThat(HtmlUtils.htmlEscapeDecimal("" + (char) 977)).as("The special character 977 should be encoded to 'ϑ'").isEqualTo("ϑ");
6156
}
6257

63-
// SPR-9293
64-
@Test
65-
void testEncodeIntoHtmlCharacterSetFromUtf8() {
58+
@Test // SPR-9293
59+
void htmlEscapeIntoHtmlCharacterSetFromUtf8() {
6660
String utf8 = ("UTF-8");
67-
assertThat(HtmlUtils.htmlEscape("", utf8)).as("An empty string should be converted to an empty string")
68-
.isEmpty();
61+
62+
assertThat(HtmlUtils.htmlEscape("", utf8)).as("An empty string should be converted to an empty string").isEmpty();
6963
assertThat(HtmlUtils.htmlEscape("A sentence containing no special characters.")).as("A string containing no special characters should not be affected").isEqualTo("A sentence containing no special characters.");
7064

7165
assertThat(HtmlUtils.htmlEscape("< >", utf8)).as("'< >' should be encoded to '&lt; &gt;'").isEqualTo("&lt; &gt;");
@@ -75,7 +69,38 @@ void testEncodeIntoHtmlCharacterSetFromUtf8() {
7569
}
7670

7771
@Test
78-
void testDecodeFromHtmlCharacterSet() {
72+
void htmlUnescape() {
73+
String escaped = "&quot;This is a quote&#39;";
74+
String unescaped = HtmlUtils.htmlUnescape(escaped);
75+
assertThat(unescaped).isEqualTo("\"This is a quote'");
76+
}
77+
78+
@Test
79+
void htmlUnescapeHandlesSupplementaryCharactersAsDecimal() {
80+
String expectedCharacter = "😀";
81+
String decimalEntity = "&#128512;";
82+
String actualResultFromDecimal = HtmlUtils.htmlUnescape(decimalEntity);
83+
assertThat(actualResultFromDecimal).as("Decimal entity was not converted correctly.").isEqualTo(expectedCharacter);
84+
}
85+
86+
@Test
87+
void htmlUnescapeHandlesSupplementaryCharactersAsHexadecimal() {
88+
String expectedCharacter = "😀";
89+
String hexEntity = "&#x1F600;";
90+
String actualResultFromHex = HtmlUtils.htmlUnescape(hexEntity);
91+
assertThat(actualResultFromHex).as("Hexadecimal entity was not converted correctly.").isEqualTo(expectedCharacter);
92+
}
93+
94+
@Test
95+
void htmlUnescapeHandlesBasicEntities() {
96+
String input = "&lt;p&gt;Tom &amp; Jerry&#39;s &quot;Show&quot;&lt;/p&gt;";
97+
String expectedOutput = "<p>Tom & Jerry's \"Show\"</p>";
98+
String actualOutput = HtmlUtils.htmlUnescape(input);
99+
assertThat(actualOutput).as("Basic HTML entities were not unescaped correctly.").isEqualTo(expectedOutput);
100+
}
101+
102+
@Test
103+
void htmlUnescapeFromHtmlCharacterSet() {
79104
assertThat(HtmlUtils.htmlUnescape("")).as("An empty string should be converted to an empty string").isEmpty();
80105
assertThat(HtmlUtils.htmlUnescape("This is a sentence containing no special characters.")).as("A string containing no special characters should not be affected").isEqualTo("This is a sentence containing no special characters.");
81106

0 commit comments

Comments
 (0)