SyntaxValidator rejects <?xml-stylesheet?> at the start and PIs with a tab/newline after the target

Open Beginner friendly
#23 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, node.js
Domain
backend, testing

Research direction

Start with XmlDeclarationValidator.validate and SyntaxValidator._readPI, then find the existing tests for XML declarations and processing instructions. Add regression cases for the two PI forms described and verify both validate successfully, including targets separated from PI data by spaces, tabs, CR, or LF.

Written by the indexing model from the issue text.

Description

SyntaxValidator (fast-xml-validator 1.4.4, master c19b98a) rejects two kinds of well-formed XML that contain processing instructions.

1. A document that starts with a PI whose name begins with xml (e.g. xml-stylesheet) is read as an XML declaration
import { SyntaxValidator } from 'fast-xml-validator';

SyntaxValidator.validate(`<?xml-stylesheet type="text/xsl" href="feed.xsl"?><rss/>`);
// throws: XML declaration contains unknown attribute "type". Allowed: version, encoding, standalone.

SyntaxValidator.validate(`<?xmlfoo?><r/>`);
// throws: XML declaration contains invalid content: "foo".

Expected: both are valid (libxml2 / expat accept them). The same PI after a real <?xml version="1.0"?> line already validates, so this only hits feeds and SVGs that have no declaration.

Cause: XmlDeclarationValidator.validate uses xmlData.startsWith('<?xml'). The declaration is '<?xml' VersionInfo ... and VersionInfo starts with whitespace, so the next character has to be whitespace (or ? for the existing <?xml?> error path), e.g.

const hasDecl = xmlData.startsWith('<?xml') && (isWhiteSpace(xmlData[5]) || xmlData[5] === '?');
2. Tab, CR or LF after the PI target is taken as part of the target
SyntaxValidator.validate('<?pi\tdata?><r/>');
// throws: Processing instruction target "pi	data" is not a valid XML Name.

SyntaxValidator.validate('<r><?xml-stylesheet\n  href="a.xsl"?></r>');
// throws: Processing instruction target "xml-stylesheet\n" is not a valid XML Name.

Expected: valid. PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' and S is space, tab, CR or LF.

Cause: SyntaxValidator._readPI ends the target only on ' ' or '?':

if (!nameValidated && (ch === ' ' || ch === '?')) {

Using isWhiteSpace(ch) || ch === '?' (already imported from util.js) fixes it.

I checked both against expat on the inputs above. Happy to send a PR with specs if you want one.

Dominant language
JavaScript
Stars
3
Forks
2
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.