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
Owner
|
@Lyin-OTTF No files changed? |
Lyin-OTTF
force-pushed
the
fix/xml-serialize-escape-whitespace
branch
from
October 21, 2026 01:23
1cf10b5 to
3b93afd
Compare
|
Author
|
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. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



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.