import { baseParse as parse, NodeTypes, ElementNode, TextNode, ErrorCodes, ElementTypes, InterpolationNode } from '@vue/compiler-core' import { parserOptionsMinimal as parserOptions, DOMNamespaces } from '../src/parserOptionsMinimal' describe('DOM parser', () => { describe('Text', () => { test('textarea handles comments/elements as just text', () => { const ast = parse( '', parserOptions ) const element = ast.children[0] as ElementNode const text = element.children[0] as TextNode expect(text).toStrictEqual({ type: NodeTypes.TEXT, content: 'some
text
and', loc: { start: { offset: 10, line: 1, column: 11 }, end: { offset: 46, line: 1, column: 47 }, source: 'some
text
and' } }) }) test('textarea handles character references', () => { const ast = parse('', parserOptions) const element = ast.children[0] as ElementNode const text = element.children[0] as TextNode expect(text).toStrictEqual({ type: NodeTypes.TEXT, content: '&', loc: { start: { offset: 10, line: 1, column: 11 }, end: { offset: 15, line: 1, column: 16 }, source: '&' } }) }) test('textarea support interpolation', () => { const ast = parse('', parserOptions) const element = ast.children[0] as ElementNode expect(element.children).toMatchObject([ { type: NodeTypes.TEXT, content: `
` }, { type: NodeTypes.INTERPOLATION, content: { type: NodeTypes.SIMPLE_EXPRESSION, content: `foo`, isStatic: false } } ]) }) test('style handles comments/elements as just a text', () => { const ast = parse( '', parserOptions ) const element = ast.children[0] as ElementNode const text = element.children[0] as TextNode expect(text).toStrictEqual({ type: NodeTypes.TEXT, content: 'some
text
and', loc: { start: { offset: 7, line: 1, column: 8 }, end: { offset: 43, line: 1, column: 44 }, source: 'some
text
and' } }) }) test("style doesn't handle character references", () => { const ast = parse('', parserOptions) const element = ast.children[0] as ElementNode const text = element.children[0] as TextNode expect(text).toStrictEqual({ type: NodeTypes.TEXT, content: '&', loc: { start: { offset: 7, line: 1, column: 8 }, end: { offset: 12, line: 1, column: 13 }, source: '&' } }) }) test('CDATA', () => { const ast = parse('some text', parserOptions) const text = (ast.children[0] as ElementNode).children![0] as TextNode expect(text).toStrictEqual({ type: NodeTypes.TEXT, content: 'some text', loc: { start: { offset: 14, line: 1, column: 15 }, end: { offset: 23, line: 1, column: 24 }, source: 'some text' } }) }) test('
 tag should preserve raw whitespace', () => {
      const rawText = `  \na   
foo \n bar
\n c` const ast = parse(`
${rawText}
`, parserOptions) expect((ast.children[0] as ElementNode).children).toMatchObject([ { type: NodeTypes.TEXT, content: ` \na ` }, { type: NodeTypes.ELEMENT, children: [ { type: NodeTypes.TEXT, content: `foo \n bar` } ] }, { type: NodeTypes.TEXT, content: ` \n c` } ]) }) // #908 test('
 tag should remove leading newline', () => {
      const rawText = `\nhello`
      const ast = parse(`
${rawText}
`, parserOptions) expect((ast.children[0] as ElementNode).children[0]).toMatchObject({ type: NodeTypes.TEXT, content: rawText.slice(1) }) }) }) describe('Interpolation', () => { test('HTML entities in interpolation should be translated for backward compatibility.', () => { const ast = parse('
{{ a < b }}
', parserOptions) const element = ast.children[0] as ElementNode const interpolation = element.children[0] as InterpolationNode expect(interpolation).toStrictEqual({ type: NodeTypes.INTERPOLATION, content: { type: NodeTypes.SIMPLE_EXPRESSION, content: `a < b`, isStatic: false, isConstant: false, loc: { start: { offset: 8, line: 1, column: 9 }, end: { offset: 16, line: 1, column: 17 }, source: 'a < b' } }, loc: { start: { offset: 5, line: 1, column: 6 }, end: { offset: 19, line: 1, column: 20 }, source: '{{ a < b }}' } }) }) }) describe('Element', () => { test('void element', () => { const ast = parse('after', parserOptions) const element = ast.children[0] as ElementNode expect(element).toStrictEqual({ type: NodeTypes.ELEMENT, ns: DOMNamespaces.HTML, tag: 'img', tagType: ElementTypes.ELEMENT, props: [], isSelfClosing: false, children: [], loc: { start: { offset: 0, line: 1, column: 1 }, end: { offset: 5, line: 1, column: 6 }, source: '' }, codegenNode: undefined }) }) test('native element', () => { const ast = parse('
', parserOptions) expect(ast.children[0]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'div', tagType: ElementTypes.ELEMENT }) expect(ast.children[1]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'comp', tagType: ElementTypes.COMPONENT }) expect(ast.children[2]).toMatchObject({ type: NodeTypes.ELEMENT, tag: 'Comp', tagType: ElementTypes.COMPONENT }) }) test('Strict end tag detection for textarea.', () => { const ast = parse( '', { ...parserOptions, onError: err => { if (err.code !== ErrorCodes.END_TAG_WITH_ATTRIBUTES) { throw err } } } ) const element = ast.children[0] as ElementNode const text = element.children[0] as TextNode expect(ast.children.length).toBe(1) expect(text).toStrictEqual({ type: NodeTypes.TEXT, content: 'hello', loc: { start: { offset: 10, line: 1, column: 11 }, end: { offset: 37, line: 1, column: 38 }, source: 'hello' } }) }) }) describe('Namespaces', () => { test('HTML namespace', () => { const ast = parse('test', parserOptions) const element = ast.children[0] as ElementNode expect(element.ns).toBe(DOMNamespaces.HTML) }) test('SVG namespace', () => { const ast = parse('test', parserOptions) const element = ast.children[0] as ElementNode expect(element.ns).toBe(DOMNamespaces.SVG) }) test('MATH_ML namespace', () => { const ast = parse('test', parserOptions) const element = ast.children[0] as ElementNode expect(element.ns).toBe(DOMNamespaces.MATH_ML) }) test('SVG in MATH_ML namespace', () => { const ast = parse( '', parserOptions ) const elementMath = ast.children[0] as ElementNode const elementAnnotation = elementMath.children[0] as ElementNode const elementSvg = elementAnnotation.children[0] as ElementNode expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML) expect(elementSvg.ns).toBe(DOMNamespaces.SVG) }) test('html text/html in MATH_ML namespace', () => { const ast = parse( '', parserOptions ) const elementMath = ast.children[0] as ElementNode const elementAnnotation = elementMath.children[0] as ElementNode const element = elementAnnotation.children[0] as ElementNode expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML) expect(element.ns).toBe(DOMNamespaces.HTML) }) test('html application/xhtml+xml in MATH_ML namespace', () => { const ast = parse( '', parserOptions ) const elementMath = ast.children[0] as ElementNode const elementAnnotation = elementMath.children[0] as ElementNode const element = elementAnnotation.children[0] as ElementNode expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML) expect(element.ns).toBe(DOMNamespaces.HTML) }) test('mtext malignmark in MATH_ML namespace', () => { const ast = parse( '', parserOptions ) const elementMath = ast.children[0] as ElementNode const elementText = elementMath.children[0] as ElementNode const element = elementText.children[0] as ElementNode expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML) expect(element.ns).toBe(DOMNamespaces.MATH_ML) }) test('mtext and not malignmark tag in MATH_ML namespace', () => { const ast = parse('', parserOptions) const elementMath = ast.children[0] as ElementNode const elementText = elementMath.children[0] as ElementNode const element = elementText.children[0] as ElementNode expect(elementMath.ns).toBe(DOMNamespaces.MATH_ML) expect(element.ns).toBe(DOMNamespaces.HTML) }) test('foreignObject tag in SVG namespace', () => { const ast = parse( '', parserOptions ) const elementSvg = ast.children[0] as ElementNode const elementForeignObject = elementSvg.children[0] as ElementNode const element = elementForeignObject.children[0] as ElementNode expect(elementSvg.ns).toBe(DOMNamespaces.SVG) expect(element.ns).toBe(DOMNamespaces.HTML) }) test('desc tag in SVG namespace', () => { const ast = parse('', parserOptions) const elementSvg = ast.children[0] as ElementNode const elementDesc = elementSvg.children[0] as ElementNode const element = elementDesc.children[0] as ElementNode expect(elementSvg.ns).toBe(DOMNamespaces.SVG) expect(element.ns).toBe(DOMNamespaces.HTML) }) test('title tag in SVG namespace', () => { const ast = parse('', parserOptions) const elementSvg = ast.children[0] as ElementNode const elementTitle = elementSvg.children[0] as ElementNode const element = elementTitle.children[0] as ElementNode expect(elementSvg.ns).toBe(DOMNamespaces.SVG) expect(element.ns).toBe(DOMNamespaces.HTML) }) test('SVG in HTML namespace', () => { const ast = parse('', parserOptions) const elementHtml = ast.children[0] as ElementNode const element = elementHtml.children[0] as ElementNode expect(elementHtml.ns).toBe(DOMNamespaces.HTML) expect(element.ns).toBe(DOMNamespaces.SVG) }) test('MATH in HTML namespace', () => { const ast = parse('', parserOptions) const elementHtml = ast.children[0] as ElementNode const element = elementHtml.children[0] as ElementNode expect(elementHtml.ns).toBe(DOMNamespaces.HTML) expect(element.ns).toBe(DOMNamespaces.MATH_ML) }) }) })