Skip to content

Commit b122cf8

Browse files
committed
HtmlUtils properly escapes single quotes as well
1 parent 183523d commit b122cf8

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

org.springframework.web/src/main/java/org/springframework/web/util/HtmlCharacterEntityReferences.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2005 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,8 @@
3838
*/
3939
class HtmlCharacterEntityReferences {
4040

41+
private static final String PROPERTIES_FILE = "HtmlCharacterEntityReferences.properties";
42+
4143
static final char REFERENCE_START = '&';
4244

4345
static final String DECIMAL_REFERENCE_START = "&#";
@@ -49,12 +51,9 @@ class HtmlCharacterEntityReferences {
4951
static final char CHAR_NULL = (char) -1;
5052

5153

52-
private static final String PROPERTIES_FILE = "HtmlCharacterEntityReferences.properties";
53-
54-
5554
private final String[] characterToEntityReferenceMap = new String[3000];
5655

57-
private final Map entityReferenceToCharacterMap = new HashMap(252);
56+
private final Map<String, Character> entityReferenceToCharacterMap = new HashMap<String, Character>(252);
5857

5958

6059
/**
@@ -63,7 +62,7 @@ class HtmlCharacterEntityReferences {
6362
public HtmlCharacterEntityReferences() {
6463
Properties entityReferences = new Properties();
6564

66-
// Load refeence definition file.
65+
// Load reference definition file
6766
InputStream is = HtmlCharacterEntityReferences.class.getResourceAsStream(PROPERTIES_FILE);
6867
if (is == null) {
6968
throw new IllegalStateException(
@@ -82,7 +81,7 @@ public HtmlCharacterEntityReferences() {
8281
"Failed to parse reference definition file [HtmlCharacterEntityReferences.properties]: " + ex.getMessage());
8382
}
8483

85-
// Parse reference definition properites.
84+
// Parse reference definition properties
8685
Enumeration keys = entityReferences.propertyNames();
8786
while (keys.hasMoreElements()) {
8887
String key = (String) keys.nextElement();
@@ -96,6 +95,7 @@ public HtmlCharacterEntityReferences() {
9695
}
9796
}
9897

98+
9999
/**
100100
* Return the number of supported entity references.
101101
*/
@@ -128,7 +128,7 @@ public String convertToReference(char character) {
128128
* Return the char mapped to the given entityReference or -1.
129129
*/
130130
public char convertToCharacter(String entityReference) {
131-
Character referredCharacter = (Character) this.entityReferenceToCharacterMap.get(entityReference);
131+
Character referredCharacter = this.entityReferenceToCharacterMap.get(entityReference);
132132
if (referredCharacter != null) {
133133
return referredCharacter.charValue();
134134
}

org.springframework.web/src/main/resources/org/springframework/web/util/HtmlCharacterEntityReferences.properties

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# A complete description of the HTML 4.0 character set can be found at:
33
# http://www.w3.org/TR/html4/charset.html
44

5-
65
# Character entity references for ISO 8859-1 characters
76

87
160 = nbsp
@@ -102,7 +101,6 @@
102101
254 = thorn
103102
255 = yuml
104103

105-
106104
# Character entity references for symbols, mathematical symbols, and Greek letters
107105

108106
402 = fnof
@@ -230,11 +228,11 @@
230228
9829 = hearts
231229
9830 = diams
232230

233-
234231
# Character entity references for markup-significant and internationalization characters
235232

236233
34 = quot
237234
38 = amp
235+
39 = #39
238236
60 = lt
239237
62 = gt
240238
338 = OElig
@@ -265,4 +263,3 @@
265263
8249 = lsaquo
266264
8250 = rsaquo
267265
8364 = euro
268-

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,31 +16,36 @@
1616

1717
package org.springframework.web.util;
1818

19-
import junit.framework.TestCase;
19+
import org.junit.Test;
20+
21+
import static org.junit.Assert.*;
2022

2123
/**
2224
* @author Alef Arendsen
2325
* @author Martin Kersten
2426
* @author Rick Evans
2527
*/
26-
public class HtmlUtilsTests extends TestCase {
28+
public class HtmlUtilsTests {
2729

30+
@Test
2831
public void testHtmlEscape() {
29-
String unescaped = "\"This is a quote";
32+
String unescaped = "\"This is a quote'";
3033
String escaped = HtmlUtils.htmlEscape(unescaped);
31-
assertEquals("&quot;This is a quote", escaped);
34+
assertEquals("&quot;This is a quote&#39;", escaped);
3235
escaped = HtmlUtils.htmlEscapeDecimal(unescaped);
33-
assertEquals("&#34;This is a quote", escaped);
36+
assertEquals("&#34;This is a quote&#39;", escaped);
3437
escaped = HtmlUtils.htmlEscapeHex(unescaped);
35-
assertEquals("&#x22;This is a quote", escaped);
38+
assertEquals("&#x22;This is a quote&#x27;", escaped);
3639
}
3740

41+
@Test
3842
public void testHtmlUnescape() {
39-
String escaped = "&quot;This is a quote";
43+
String escaped = "&quot;This is a quote&#39;";
4044
String unescaped = HtmlUtils.htmlUnescape(escaped);
41-
assertEquals(unescaped, "\"This is a quote");
45+
assertEquals(unescaped, "\"This is a quote'");
4246
}
4347

48+
@Test
4449
public void testEncodeIntoHtmlCharacterSet() {
4550
assertNull("A null string should be converted to a null string",
4651
HtmlUtils.htmlEscape(null));
@@ -66,6 +71,7 @@ public void testEncodeIntoHtmlCharacterSet() {
6671
"&#977;", HtmlUtils.htmlEscapeDecimal("" + (char) 977));
6772
}
6873

74+
@Test
6975
public void testDecodeFromHtmlCharacterSet() {
7076
assertNull("A null string should be converted to a null string",
7177
HtmlUtils.htmlUnescape(null));

0 commit comments

Comments
 (0)