From cc3ebab592c685d9f5f1a330caba50231bad6898 Mon Sep 17 00:00:00 2001 From: Yi-Cyuan Chen Date: Sun, 7 Jul 2024 10:51:31 +0800 Subject: [PATCH] fixed closing tag issues --- src/index.ts | 2 +- test/index.ts | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index f4b7635..b9dae14 100644 --- a/src/index.ts +++ b/src/index.ts @@ -210,7 +210,7 @@ function element(matchRoot: boolean): XmlParserNodeWrapper throw new ParsingError('Failed to parse XML', `Closing tag not matching "${closingTag}"`); } } else { - match(/^<\/\s*[\w-:.\u00C0-\u00FF]+>/); + match(/^<\/[\w-:.\u00C0-\u00FF]+\s*>/); } return { diff --git a/test/index.ts b/test/index.ts index 478d57c..b4e240d 100644 --- a/test/index.ts +++ b/test/index.ts @@ -152,7 +152,7 @@ describe('XML Parser', function() { }); it('should support weird whitespace', function() { - const node = xmlParser('\n\nhello world'); + const node = xmlParser('\n\nhello world'); assert.deepEqual(node.root, { type: 'Element', name: 'foo', @@ -260,6 +260,16 @@ describe('XML Parser', function() { }); }); + it('should support closing tags with tailing whitespaces', function() { + const node = xmlParser(''); + assert.deepEqual(node.root, { + type: 'Element', + name: 'a', + attributes: {}, + children: [] + }); + }); + it('should support self-closing tags without attributes', function() { const node = xmlParser('foo bar'); assert.deepEqual(node.root, {