Skip to content

Commit 47dd577

Browse files
authored
Merge pull request #474 from simpleqt/fix/surrogate-character-reference
Replace numeric character references to surrogates with U+FFFD
2 parents 3e1f83b + 7cb9ff6 commit 47dd577

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎commonmark/src/main/java/org/commonmark/internal/util/Html5Entities.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public static String entityToString(String input) {
3030

3131
try {
3232
int codePoint = Integer.parseInt(value, base);
33-
if (codePoint == 0) {
33+
// The spec says that invalid Unicode code points are replaced by the REPLACEMENT
34+
// CHARACTER. The surrogate range is not a set of valid code points.
35+
if (codePoint == 0
36+
|| (codePoint >= Character.MIN_SURROGATE
37+
&& codePoint <= Character.MAX_SURROGATE)) {
3438
return "\uFFFD";
3539
}
3640
return new String(Character.toChars(codePoint));

‎commonmark/src/test/java/org/commonmark/test/SpecialInputTest.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ public void nullCharacterEntityShouldBeReplaced() {
1919
assertRendering("foo&#0;bar", "<p>foo\uFFFDbar</p>\n");
2020
}
2121

22+
@Test
23+
public void surrogateCharacterReferenceShouldBeReplaced() {
24+
// The reference is well-formed, but a surrogate is not a valid Unicode code point, so it
25+
// must be replaced by U+FFFD (same as cmark and commonmark.js)
26+
assertRendering("&#xD800;", "<p>\uFFFD</p>\n");
27+
assertRendering("&#55296;", "<p>\uFFFD</p>\n");
28+
assertRendering("&#xDFFF;", "<p>\uFFFD</p>\n");
29+
assertRendering("before&#xD800;after", "<p>before\uFFFDafter</p>\n");
30+
// Code points outside the surrogate range are not affected
31+
assertRendering("&#x1F600;", "<p>\uD83D\uDE00</p>\n");
32+
assertRendering("&#x10FFFF;", "<p>\uDBFF\uDFFF</p>\n");
33+
}
34+
2235
@Test
2336
public void crLfAsLineSeparatorShouldBeParsed() {
2437
assertRendering("foo\r\nbar", "<p>foo\nbar</p>\n");

0 commit comments

Comments
 (0)