From 967d1de7ecbfb5d533f0345fb69bd5f4de7a3778 Mon Sep 17 00:00:00 2001 From: Bronley Date: Sat, 30 Jan 2021 15:42:11 -0500 Subject: [PATCH] Fix xml parse error --- src/parser/SGParser.spec.ts | 14 ++++++++++++++ src/parser/SGParser.ts | 18 +++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/parser/SGParser.spec.ts b/src/parser/SGParser.spec.ts index 043f04801..144c154db 100644 --- a/src/parser/SGParser.spec.ts +++ b/src/parser/SGParser.spec.ts @@ -75,6 +75,20 @@ describe('SGParser', () => { `); }); + it('does not crash when missing tag name', () => { + const parser = new SGParser(); + parser.parse( + 'pkg:/components/ParentScene.xml', trim` + + + + < + + `); + //the test passes if the parser doesn't throw a runtime exception. + }); + + it('Adds error when an unexpected tag is found in xml', () => { const parser = new SGParser(); parser.parse( diff --git a/src/parser/SGParser.ts b/src/parser/SGParser.ts index 041a88e86..bf76cae1a 100644 --- a/src/parser/SGParser.ts +++ b/src/parser/SGParser.ts @@ -250,14 +250,18 @@ function mapElements(content: ContentCstNode, allow: string[], diagnostics: Diag if (element) { for (const entry of element) { const name = entry.children.Name?.[0]; - if (name && allow.includes(name.image)) { - tags.push(mapElement(entry, diagnostics)); + if (name?.image) { + if (allow.includes(name.image)) { + tags.push(mapElement(entry, diagnostics)); + } else { + //unexpected tag + diagnostics.push({ + ...DiagnosticMessages.xmlUnexpectedTag(name.image), + range: rangeFromTokens(name) + }); + } } else { - //unexpected tag - diagnostics.push({ - ...DiagnosticMessages.xmlUnexpectedTag(name.image), - range: rangeFromTokens(name) - }); + //bad xml syntax... } } }