File tree Expand file tree Collapse file tree
main/java/org/commonmark/internal/util
test/java/org/commonmark/test Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ));
Original file line number Diff line number Diff line change @@ -19,6 +19,19 @@ public void nullCharacterEntityShouldBeReplaced() {
1919 assertRendering ("foo�bar" , "<p>foo\uFFFD bar</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 ("�" , "<p>\uFFFD </p>\n " );
27+ assertRendering ("�" , "<p>\uFFFD </p>\n " );
28+ assertRendering ("�" , "<p>\uFFFD </p>\n " );
29+ assertRendering ("before�after" , "<p>before\uFFFD after</p>\n " );
30+ // Code points outside the surrogate range are not affected
31+ assertRendering ("😀" , "<p>\uD83D \uDE00 </p>\n " );
32+ assertRendering ("" , "<p>\uDBFF \uDFFF </p>\n " );
33+ }
34+
2235 @ Test
2336 public void crLfAsLineSeparatorShouldBeParsed () {
2437 assertRendering ("foo\r \n bar" , "<p>foo\n bar</p>\n " );
You can’t perform that action at this time.
0 commit comments