Skip to content

Commit

Permalink
fix(parser): properly set initial inXML state based on root ns
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Nov 28, 2023
1 parent ef97e8b commit 47ea285
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/compiler-core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,10 @@ export function baseParse(input: string, options?: ParserOptions): RootNode {
? ParseMode.SFC
: ParseMode.BASE

tokenizer.inXML =
currentOptions.ns === Namespaces.SVG ||
currentOptions.ns === Namespaces.MATH_ML

const delimiters = options?.delimiters
if (delimiters) {
tokenizer.delimiterOpen = toCharCodes(delimiters[0])
Expand Down
15 changes: 15 additions & 0 deletions packages/compiler-dom/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,5 +481,20 @@ describe('DOM parser', () => {
expect(elementForieng.ns).toBe(Namespaces.SVG)
expect(element.ns).toBe(Namespaces.HTML)
})

test('correct XML handling with root ns', () => {
// when root ns is an XML namespace, there should be no special content
// treatment for <script>, <style>, <textarea> etc.
const ast = parse('<script><g/><g/></script>', {
...parserOptions,
ns: Namespaces.SVG
})
const elementSvg = ast.children[0] as ElementNode
// should parse as nodes instead of text
expect(elementSvg.children).toMatchObject([
{ type: NodeTypes.ELEMENT, tag: 'g' },
{ type: NodeTypes.ELEMENT, tag: 'g' }
])
})
})
})

0 comments on commit 47ea285

Please sign in to comment.