From 47a7575eaa1b65f0bdc2d8469fa0814b0cbfb0d6 Mon Sep 17 00:00:00 2001 From: Antoine Lambert Date: Wed, 11 Dec 2024 15:08:16 +0100 Subject: [PATCH] Fix parsing of xml-stylesheet as first processing instruction If xml-stylesheet is the first processing instruction in the document to parse, it was incorrectly parsed as a xml processing instruction. --- src/index.ts | 2 +- test/index.ts | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index b9dae14..0c64879 100644 --- a/src/index.ts +++ b/src/index.ts @@ -129,7 +129,7 @@ function parseDocument(): XmlParserResult { } function processingInstruction(matchDeclaration: boolean): XmlParserNodeWrapper|undefined { - const m = matchDeclaration ? match(/^<\?(xml)\s*/) : match(/^<\?([\w-:.]+)\s*/); + const m = matchDeclaration ? match(/^<\?(xml(-stylesheet)?)\s*/) : match(/^<\?([\w-:.]+)\s*/); if (!m) return; // tag diff --git a/test/index.ts b/test/index.ts index b4e240d..ca8dbbc 100644 --- a/test/index.ts +++ b/test/index.ts @@ -580,6 +580,30 @@ describe('XML Parser', function() { }); }); + it('should correctly parse xml-stylesheet as first processing instruction', function() { + const node = xmlParser(''); + + const root: XmlParserElementNode = { + type: 'Element', + name: 'foo', + attributes: {}, + children: [] + }; + + assert.deepEqual(node, { + declaration: { + name: 'xml-stylesheet', + type: 'ProcessingInstruction', + attributes: { + href: 'style.xsl', + type: 'text/xsl' + } + }, + root: root, + children: [root] + }); + }); + it('should support complex XML', function() { const node = xmlParser(`