Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/lib/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,15 @@ export class Parser {
return false;
}

// `<?xml` begins an XML declaration only when it's immediately followed by
// whitespace. If the next character is a name character, this is a
// processing instruction whose target merely begins with "xml" (e.g.
// `<?xml-stylesheet ... ?>`); rewind and let it be parsed as a PI.
if (syntax.isNameChar(scanner.peek())) {
scanner.reset(startIndex);
return false;
}

if (!this.consumeWhitespace()) {
throw this.error('Invalid XML declaration');
}
Expand Down
6 changes: 3 additions & 3 deletions tests/lib/Parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ describe('Parser', () => {
describe('invalid XML declaration', () => {
it('throws an error', () => {
assert.throws(() => {
parseXml('<?xmlblah');
parseXml('<?xml?>');
}, {
column: 6,
excerpt: '<?xmlblah',
excerpt: '<?xml?>',
line: 1,
message: 'Invalid XML declaration (line 1, column 6)\n' +
' <?xmlblah\n' +
' <?xml?>\n' +
' ^\n',
pos: 5,
});
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/XmlProcessingInstruction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ describe('XmlProcessingInstruction', () => {
assert.strictEqual(JSON.stringify(root.children[0]), `{"type":"pi","name":"xml-stylesheet","content":"type=\\"text/xsl\\" href=\\"style.xsl\\""}`);
});

it('may begin the document when its target starts with "xml"', () => {
let doc = parseXml('<?xml-stylesheet type="text/xsl" href="style.xsl"?><root/>');
assert(doc.children[0] instanceof XmlProcessingInstruction);
assert.strictEqual(doc.children[0].name, 'xml-stylesheet');
assert.strictEqual(doc.root.name, 'root');
});

describe('constructor', () => {
it('defaults the value of `content` to an empty string if not provided', () => {
let pi = new XmlProcessingInstruction('foo');
Expand Down