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
{{msg}}
hello world
', baseOptions) expect(ast.if).toBe('show') expect(ast.ifConditions[0].exp).toBe('show') }) it('v-else-if directive syntax', () => { const ast = parse('hello
elseifworld
hello
world
world
world
world
', baseOptions) expect(ast.once).toBe(true) }) it('slot tag single syntax', () => { const ast = parse('hello world
', baseOptions) expect(ast.slotTarget).toBe('"one"') }) it('component properties', () => { const ast = parse('hello world
', baseOptions) expect(ast1.staticClass).toBe('"class1"') // dynamic const ast2 = parse('hello world
', baseOptions) expect(ast2.classBinding).toBe('class1') // interpolation warning parse('hello world
', baseOptions) expect('Interpolation inside attributes has been removed').toHaveBeenWarned() }) it('style binding', () => { const ast = parse('hello world
', baseOptions) expect(ast.styleBinding).toBe('error') }) it('attribute with v-bind', () => { const ast = parse('', baseOptions) expect(ast.attrsList[0].name).toBe('type') expect(ast.attrsList[0].value).toBe('text') expect(ast.attrsList[1].name).toBe('name') expect(ast.attrsList[1].value).toBe('field1') expect(ast.attrsMap['type']).toBe('text') expect(ast.attrsMap['name']).toBe('field1') expect(ast.attrs[0].name).toBe('type') expect(ast.attrs[0].value).toBe('"text"') expect(ast.attrs[1].name).toBe('name') expect(ast.attrs[1].value).toBe('"field1"') expect(ast.props[0].name).toBe('value') expect(ast.props[0].value).toBe('msg') }) it('attribute with v-on', () => { const ast = parse('', baseOptions) expect(ast.events.input.value).toBe('onInput') }) it('attribute with directive', () => { const ast = parse('', baseOptions) expect(ast.directives[0].name).toBe('validate') expect(ast.directives[0].value).toBe('required') expect(ast.directives[0].arg).toBe('field1') }) it('attribute with modifiered directive', () => { const ast = parse('', baseOptions) expect(ast.directives[0].modifiers.on).toBe(true) expect(ast.directives[0].modifiers.off).toBe(true) }) it('literal attribute', () => { // basic const ast1 = parse('', baseOptions) expect(ast1.attrsList[0].name).toBe('type') expect(ast1.attrsList[0].value).toBe('text') expect(ast1.attrsList[1].name).toBe('name') expect(ast1.attrsList[1].value).toBe('field1') expect(ast1.attrsList[2].name).toBe('value') expect(ast1.attrsList[2].value).toBe('hello world') expect(ast1.attrsMap['type']).toBe('text') expect(ast1.attrsMap['name']).toBe('field1') expect(ast1.attrsMap['value']).toBe('hello world') expect(ast1.attrs[0].name).toBe('type') expect(ast1.attrs[0].value).toBe('"text"') expect(ast1.attrs[1].name).toBe('name') expect(ast1.attrs[1].value).toBe('"field1"') expect(ast1.attrs[2].name).toBe('value') expect(ast1.attrs[2].value).toBe('"hello world"') // interpolation warning parse('', baseOptions) expect('Interpolation inside attributes has been removed').toHaveBeenWarned() }) if (!isIE) { it('duplicate attribute', () => { parse('hello world
', baseOptions) expect('duplicate attribute').toHaveBeenWarned() }) } it('custom delimiter', () => { const ast = parse('{msg}
', extend({ delimiters: ['{', '}'] }, baseOptions)) expect(ast.children[0].expression).toBe('_s(msg)') }) it('not specified getTagNamespace option', () => { const options = extend({}, baseOptions) delete options.getTagNamespace const ast = parse('', options) expect(ast.tag).toBe('svg') expect(ast.ns).toBeUndefined() }) it('not specified mustUseProp', () => { const options = extend({}, baseOptions) delete options.mustUseProp const ast = parse('', options) expect(ast.props).toBeUndefined() }) it('pre/post transforms', () => { const options = extend({}, baseOptions) const spy1 = jasmine.createSpy('preTransform') const spy2 = jasmine.createSpy('postTransform') options.modules = options.modules.concat([{ preTransformNode (el) { spy1(el.tag) }, postTransformNode (el) { expect(el.attrs.length).toBe(1) spy2(el.tag) } }]) parse('', options) expect(spy1).toHaveBeenCalledWith('img') expect(spy2).toHaveBeenCalledWith('img') }) it('preserve whitespace intag', function () { const options = extend({}, baseOptions) const ast = parse('', options) const code = ast.children[0] expect(code.children[0].type).toBe(3) expect(code.children[0].text).toBe(' \n') expect(code.children[2].type).toBe(3) expect(code.children[2].text).toBe('\n ') const span = ast.children[1] expect(span.children[0].type).toBe(3) expect(span.children[0].text).toBe(' ') }) it('forgivingly handle < in plain text', () => { const options = extend({}, baseOptions) const ast = parse('\nhi\n
1 < 2 < 3
', options) expect(ast.tag).toBe('p') expect(ast.children.length).toBe(1) expect(ast.children[0].type).toBe(3) expect(ast.children[0].text).toBe('1 < 2 < 3') }) it('IE conditional comments', () => { const options = extend({}, baseOptions) const ast = parse(``, options) expect(ast.tag).toBe('div') expect(ast.children.length).toBe(0) }) it('parse content in textarea as text', () => { const options = extend({}, baseOptions) const whitespace = parse(` `, options) expect(whitespace.tag).toBe('textarea') expect(whitespace.children.length).toBe(1) expect(whitespace.children[0].type).toBe(3) // textarea is whitespace sensitive expect(whitespace.children[0].text).toBe(`Test 1
test2 `) const comment = parse('', options) expect(comment.tag).toBe('textarea') expect(comment.children.length).toBe(1) expect(comment.children[0].type).toBe(3) expect(comment.children[0].text).toBe('') }) })