import { parse } from 'compiler/parser/index' import { extend } from 'shared/util' import { baseOptions } from 'web/compiler/index' import { isIE } from 'core/util/env' describe('parser', () => { it('simple element', () => { const ast = parse('

hello world

', baseOptions) expect(ast.tag).toBe('h1') expect(ast.plain).toBe(true) expect(ast.children[0].text).toBe('hello world') }) it('interpolation in element', () => { const ast = parse('

{{msg}}

', baseOptions) expect(ast.tag).toBe('h1') expect(ast.plain).toBe(true) expect(ast.children[0].expression).toBe('_s(msg)') }) it('child elements', () => { const ast = parse('', baseOptions) expect(ast.tag).toBe('ul') expect(ast.plain).toBe(true) expect(ast.children[0].tag).toBe('li') expect(ast.children[0].plain).toBe(true) expect(ast.children[0].children[0].text).toBe('hello world') expect(ast.children[0].parent).toBe(ast) }) it('unary element', () => { const ast = parse('
', baseOptions) expect(ast.tag).toBe('hr') expect(ast.plain).toBe(true) expect(ast.children.length).toBe(0) }) it('svg element', () => { const ast = parse('hello world', baseOptions) expect(ast.tag).toBe('svg') expect(ast.ns).toBe('svg') expect(ast.plain).toBe(true) expect(ast.children[0].tag).toBe('text') expect(ast.children[0].children[0].text).toBe('hello world') expect(ast.children[0].parent).toBe(ast) }) it('camelCase element', () => { const ast = parse('

hello world

', baseOptions) expect(ast.tag).toBe('MyComponent') expect(ast.plain).toBe(true) expect(ast.children[0].tag).toBe('p') expect(ast.children[0].plain).toBe(true) expect(ast.children[0].children[0].text).toBe('hello world') expect(ast.children[0].parent).toBe(ast) }) it('forbidden element', () => { // style const styleAst = parse('', baseOptions) expect(styleAst.tag).toBe('style') expect(styleAst.plain).toBe(true) expect(styleAst.forbidden).toBe(true) expect(styleAst.children[0].text).toBe('error { color: red; }') expect('Templates should only be responsible for mapping the state').toHaveBeenWarned() // script const scriptAst = parse('', baseOptions) expect(scriptAst.tag).toBe('script') expect(scriptAst.plain).toBe(false) expect(scriptAst.forbidden).toBe(true) expect(scriptAst.children[0].text).toBe('alert("hello world!")') expect('Templates should only be responsible for mapping the state').toHaveBeenWarned() }) it('not contain root element', () => { parse('hello world', baseOptions) expect('Component template requires a root element, rather than just text').toHaveBeenWarned() }) it('warn multiple root elements', () => { parse('
', baseOptions) expect('Component template should contain exactly one root element:\n\n
').toHaveBeenWarned() }) it('remove duplicate whitespace text nodes caused by comments', () => { const ast = parse(`
`, baseOptions) expect(ast.children.length).toBe(3) expect(ast.children[0].tag).toBe('a') expect(ast.children[1].text).toBe(' ') expect(ast.children[2].tag).toBe('a') }) it('remove text nodes between v-if conditions', () => { const ast = parse(`
`, baseOptions) expect(ast.children.length).toBe(3) expect(ast.children[0].tag).toBe('div') expect(ast.children[0].ifConditions.length).toBe(3) expect(ast.children[1].text).toBe(' ') // text expect(ast.children[2].tag).toBe('span') }) it('warn non whitespace text between v-if conditions', () => { parse(`
foo
`, baseOptions) expect(`text "foo" between v-if and v-else(-if) will be ignored`).toHaveBeenWarned() }) it('not warn 2 root elements with v-if and v-else', () => { parse('
', baseOptions) expect('Component template should contain exactly one root element') .not.toHaveBeenWarned() }) it('not warn 3 root elements with v-if, v-else-if and v-else', () => { parse('
', baseOptions) expect('Component template should contain exactly one root element') .not.toHaveBeenWarned() }) it('not warn 2 root elements with v-if and v-else on separate lines', () => { parse(`
`, baseOptions) expect('Component template should contain exactly one root element') .not.toHaveBeenWarned() }) it('not warn 3 or more root elements with v-if, v-else-if and v-else on separate lines', () => { parse(`
`, baseOptions) expect('Component template should contain exactly one root element') .not.toHaveBeenWarned() parse(`
`, baseOptions) expect('Component template should contain exactly one root element') .not.toHaveBeenWarned() }) it('generate correct ast for 2 root elements with v-if and v-else on separate lines', () => { const ast = parse(`

`, baseOptions) expect(ast.tag).toBe('div') expect(ast.ifConditions[1].block.tag).toBe('p') }) it('generate correct ast for 3 or more root elements with v-if and v-else on separate lines', () => { const ast = parse(`

`, baseOptions) expect(ast.tag).toBe('div') expect(ast.ifConditions[0].block.tag).toBe('div') expect(ast.ifConditions[1].block.tag).toBe('span') expect(ast.ifConditions[2].block.tag).toBe('p') const astMore = parse(`

`, baseOptions) expect(astMore.tag).toBe('div') expect(astMore.ifConditions[0].block.tag).toBe('div') expect(astMore.ifConditions[1].block.tag).toBe('span') expect(astMore.ifConditions[2].block.tag).toBe('div') expect(astMore.ifConditions[3].block.tag).toBe('span') expect(astMore.ifConditions[4].block.tag).toBe('p') }) it('warn 2 root elements with v-if', () => { parse('
', baseOptions) expect('Component template should contain exactly one root element:\n\n
') .toHaveBeenWarned() }) it('warn 3 root elements with v-if and v-else on first 2', () => { parse('
', baseOptions) expect('Component template should contain exactly one root element:\n\n
') .toHaveBeenWarned() }) it('warn 3 root elements with v-if and v-else-if on first 2', () => { parse('
', baseOptions) expect('Component template should contain exactly one root element:\n\n' + '
') .toHaveBeenWarned() }) it('warn 4 root elements with v-if, v-else-if and v-else on first 2', () => { parse('
', baseOptions) expect('Component template should contain exactly one root element:\n\n' + '
') .toHaveBeenWarned() }) it('warn 2 root elements with v-if and v-else with v-for on 2nd', () => { parse('
', baseOptions) expect('Cannot use v-for on stateful component root element because it renders multiple elements:\n' + '
') .toHaveBeenWarned() }) it('warn 2 root elements with v-if and v-else-if with v-for on 2nd', () => { parse('
', baseOptions) expect('Cannot use v-for on stateful component root element because it renders multiple elements:\n' + '
') .toHaveBeenWarned() }) it('warn