Don't reject a document that starts with a "<?xml…" processing instruction - #47
Merged
Merged
Conversation
…ction
`consumeXmlDeclaration` matched the 5-character literal `<?xml` and then
required whitespace, so a document beginning with a processing instruction
whose target merely starts with `xml` — `<?xml-stylesheet ?>`,
`<?xml-model ?>` — was forced down the XML-declaration path and rejected
with "Invalid XML declaration":
parseXml('<?xml-stylesheet type="text/xsl" href="a.xsl"?><a/>') // threw
Those are valid documents: an XML declaration is `'<?xml' S 'version' …`
(the `<?xml` must be followed by whitespace), whereas `xml-stylesheet` and
`xml-model` are distinct, well-formed PITargets, and `prolog ::= XMLDecl?
Misc*` (XML 1.0 §2.8) allows a processing instruction as the first item
when no declaration is present.
After consuming `<?xml`, if the next character is a name character this is
such a PI: rewind and let it be parsed as a processing instruction. `<?xml`
followed by whitespace (a real declaration) or by a non-name character
(e.g. `<?xml?>`, whose bare `xml` target is reserved) is unchanged.
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.
Problem
consumeXmlDeclarationmatches the 5-character literal<?xmland then requireswhitespace, so a document that begins with a processing instruction whose
target merely starts with
xmlis forced down the XML-declaration path andrejected:
These are well-formed documents, and both
<?xml-stylesheet?>and<?xml-model?>are extremely common in the wild (RSS/Atom, SVG, any XML+XSLT/CSS document). The
same PI parses fine when it isn't first:
Per XML 1.0:
'<?xml' VersionInfo …whereVersionInfo ::= S 'version' …— the
<?xmlmust be followed by whitespace.PITarget ::= Name - (('X'|'x')('M'|'m')('L'|'l'))subtracts only the exact3-letter name
xml;xml-stylesheet/xml-modelare distinct, valid Names.prolog ::= XMLDecl? Misc*andMisc ::= Comment | PI | S(§2.8) allow aprocessing instruction as the first item when no declaration is present.
The names-beginning-with-
xml-are-reserved note (§2.3) is a standardizationreservation, not a well-formedness constraint — processors must accept such names
(the same reason
xmlnsis legal).Fix
After consuming
<?xml, if the next character is a name character this is a PIwhose target begins with
xml: rewind and let it be parsed as a processinginstruction (the caller's
Misc*loop handles it).<?xmlfollowed bywhitespace (a real declaration) or by a non-name character (e.g.
<?xml?>, whosebare
xmltarget is reserved and has no valid interpretation) is unchanged.Test
Added a case asserting
<?xml-stylesheet?>at document start parses as a PI withthe root element following. Repointed the existing "invalid XML declaration" test
to
<?xml?>(still correctly rejected). Full suite green (1380 passing) at 100%coverage.