Repository navigation
Conversation
XML.mustEscape() is shared by XML.escape() (serialization) and XMLTokener.unescapeEntity() (parsing), but the two callers need opposite answers for #x9/#xA/#xD. stleary#1070 added those codepoints to the valid-range clause to fix unescape("&stleary#10;"), which also stopped XML.toString() and JSONML.toString() from escaping literal TAB, LF and CR. Literal occurrences of these characters are subject to whitespace normalization by conforming XML parsers, so a value like "Lettuce\nTomato" loses its line breaks when the generated XML is read back by any standard parser. This regressed stleary#1092. Split the two concerns: mustEscape() returns true for every ISO control character again (serialization), and the new isValidXmlCharacter() checks the XML 1.0 character range for numeric character references during parsing. unescapeEntity() now validates with isValidXmlCharacter(), so &stleary#9;, &stleary#10; and &stleary#13; keep being accepted (stleary#1059/stleary#1070 behavior unchanged). Fixes stleary#1092
|
@Lyin-OTTF No files changed? |
1cf10b5 to
3b93afd
Compare
|
|
The first push came out empty. I built the tree with the Git Data API and reused the parent tree, so the changed files never made it into the commit - the tree was identical to master, which is what you saw. Force-pushed the same commit with the actual changes. The diff is there now: 3 files, +80/-20. Tests: 827 run, 0 failures, 0 errors, 6 skipped. The three new tests in XMLTest fail without the main-source change and pass with it. |
|
@Lyin-OTTF Looks like you have an undocumented behavior change. For example, the following test case fails with a parsing JSONException in the master branch, but executes successfully in your branch. |



Description
Fixes #1092.
XML.mustEscape()is shared by two callers that need opposite answers for TAB (0x9), LF (0xA) and CR (0xD):XML.escape()uses it to decide what to escape when serializing.XMLTokener.unescapeEntity()uses it to reject invalid numeric character references when parsing.#1070 added those three codepoints to the valid-range clause so that
unescape(" ")works again (#1059). A side effect was that the serialization path stopped escaping literal TAB/LF/CR:XML.toString()andJSONML.toString()now write them as literal characters. Conforming XML parsers normalize whitespace, so a value like"Lettuce\nTomato"loses its line breaks when the generated XML is read back by any standard parser — the attribute value comes back as"Lettuce Tomato"(or with collapsed whitespace), andXML.toJSONObject(XML.toString(o))does not return the original string.This PR splits the two concerns:
XML.mustEscape()again returns true for every ISO control character (serialization). Literal TAB/LF/CR are written as	, , , which was the behavior before Fix XML.unescape rejecting valid whitespace numeric character references #1070.XML.isValidXmlCharacter()checks the XML 1.0 character range (#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]).XMLTokener.unescapeEntity()validates numeric character references againstisValidXmlCharacter()instead, so	, and keep being accepted — the org.json.XML.unescape(" "); throws org.json.JSONException: Invalid numeric character reference: #1059/Fix XML.unescape rejecting valid whitespace numeric character references #1070 parsing behavior is unchanged.Tests
Three new tests in
XMLTest:testEscapesLiteralWhitespaceControlCharsOnSerialization—XML.toString()writes
/	/
for literal TAB/LF/CR. Fails on master.testWhitespaceControlCharsRoundTripThroughXml—XML.toJSONObject(XML.toString(o))returns the original value with the line break intact.testJSONMLToStringEscapesLiteralNewlines— same requirement forJSONML.toString().The existing #1059/#1070 regression tests (
testValidWhitespaceNumericEntityUnescape,testValidWhitespaceNumericEntityToJSONObject) still pass.