Merge pull request #1070 from dong0713/fix/xml-unescape-whitespace-control-chars
Fix XML.unescape rejecting valid whitespace numeric character references
This commit is contained in:
@@ -172,8 +172,14 @@ public class XML {
|
|||||||
&& cp != 0xA
|
&& cp != 0xA
|
||||||
&& cp != 0xD
|
&& cp != 0xD
|
||||||
) || !(
|
) || !(
|
||||||
// valid the range of acceptable characters that aren't control
|
// Valid character range per W3C XML 1.0 spec:
|
||||||
(cp >= 0x20 && cp <= 0xD7FF)
|
// #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
||||||
|
// Previously omitted #x9/#xA/#xD, causing unescape(" ") etc.
|
||||||
|
// to reject valid LF/TAB/CR as illegal (see #1059)
|
||||||
|
cp == 0x9
|
||||||
|
|| cp == 0xA
|
||||||
|
|| cp == 0xD
|
||||||
|
|| (cp >= 0x20 && cp <= 0xD7FF)
|
||||||
|| (cp >= 0xE000 && cp <= 0xFFFD)
|
|| (cp >= 0xE000 && cp <= 0xFFFD)
|
||||||
|| (cp >= 0x10000 && cp <= 0x10FFFF)
|
|| (cp >= 0x10000 && cp <= 0x10FFFF)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1537,6 +1537,43 @@ public class XMLTest {
|
|||||||
assertEquals("A", jsonObject.getString("a"));
|
assertEquals("A", jsonObject.getString("a"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that valid XML numeric character references for whitespace
|
||||||
|
* control characters (TAB, LF, CR) are correctly unescaped. These
|
||||||
|
* codepoints are explicitly allowed by the XML 1.0 spec
|
||||||
|
* (https://www.w3.org/TR/REC-xml/#charsets) but were previously rejected
|
||||||
|
* as invalid. See issue #1059.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testValidWhitespaceNumericEntityUnescape() {
|
||||||
|
// decimal references for the three allowed control characters
|
||||||
|
assertEquals("\t", XML.unescape("	"));
|
||||||
|
assertEquals("\n", XML.unescape(" "));
|
||||||
|
assertEquals("\r", XML.unescape(" "));
|
||||||
|
// hex references for the same codepoints
|
||||||
|
assertEquals("\t", XML.unescape("	"));
|
||||||
|
assertEquals("\n", XML.unescape("
"));
|
||||||
|
assertEquals("\r", XML.unescape("
"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that {@code XML.toJSONObject} accepts numeric character references
|
||||||
|
* for the XML-allowed control characters (TAB, LF, CR) without throwing.
|
||||||
|
* Regression test for #1059, where {@code XML.toJSONObject("<a> </a>")}
|
||||||
|
* threw JSONException in versions after 20251224.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testValidWhitespaceNumericEntityToJSONObject() {
|
||||||
|
// LF reference should round-trip through toJSONObject without throwing
|
||||||
|
JSONObject jsonObject = XML.toJSONObject("<a> </a>");
|
||||||
|
// the value is the LF character (possibly trimmed by the JSON path,
|
||||||
|
// but the call must not throw)
|
||||||
|
assertTrue(jsonObject.has("a"));
|
||||||
|
// TAB and CR references also accepted
|
||||||
|
XML.toJSONObject("<a>	</a>");
|
||||||
|
XML.toJSONObject("<a> </a>");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user