test(compiler-core): more test cases for baseParse (#2211)

This commit is contained in:
meteorlxy 2020-10-14 04:03:33 +08:00 committed by GitHub
parent 15898307aa
commit e2c9ab1991
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 206 additions and 1 deletions

View File

@ -1471,6 +1471,58 @@ Object {
} }
`; `;
exports[`compiler: parse Errors EOF_IN_TAG <div></div 1`] = `
Object {
"cached": 0,
"children": Array [
Object {
"children": Array [],
"codegenNode": undefined,
"isSelfClosing": false,
"loc": Object {
"end": Object {
"column": 11,
"line": 1,
"offset": 10,
},
"source": "<div></div",
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"ns": 0,
"props": Array [],
"tag": "div",
"tagType": 0,
"type": 1,
},
],
"codegenNode": undefined,
"components": Array [],
"directives": Array [],
"helpers": Array [],
"hoists": Array [],
"imports": Array [],
"loc": Object {
"end": Object {
"column": 11,
"line": 1,
"offset": 10,
},
"source": "<div></div",
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"temps": 0,
"type": 0,
}
`;
exports[`compiler: parse Errors EOF_IN_TAG <template><div 1`] = ` exports[`compiler: parse Errors EOF_IN_TAG <template><div 1`] = `
Object { Object {
"cached": 0, "cached": 0,
@ -4293,6 +4345,76 @@ Object {
} }
`; `;
exports[`compiler: parse Errors NESTED_COMMENT <template><!--a<!--b<!----></template> 1`] = `
Object {
"cached": 0,
"children": Array [
Object {
"children": Array [
Object {
"content": "a<!--b<!--",
"loc": Object {
"end": Object {
"column": 28,
"line": 1,
"offset": 27,
},
"source": "<!--a<!--b<!---->",
"start": Object {
"column": 11,
"line": 1,
"offset": 10,
},
},
"type": 3,
},
],
"codegenNode": undefined,
"isSelfClosing": false,
"loc": Object {
"end": Object {
"column": 39,
"line": 1,
"offset": 38,
},
"source": "<template><!--a<!--b<!----></template>",
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"ns": 0,
"props": Array [],
"tag": "template",
"tagType": 0,
"type": 1,
},
],
"codegenNode": undefined,
"components": Array [],
"directives": Array [],
"helpers": Array [],
"hoists": Array [],
"imports": Array [],
"loc": Object {
"end": Object {
"column": 39,
"line": 1,
"offset": 38,
},
"source": "<template><!--a<!--b<!----></template>",
"start": Object {
"column": 1,
"line": 1,
"offset": 0,
},
},
"temps": 0,
"type": 0,
}
`;
exports[`compiler: parse Errors NESTED_COMMENT <template><!--a<!--b<!--c--></template> 1`] = ` exports[`compiler: parse Errors NESTED_COMMENT <template><!--a<!--b<!--c--></template> 1`] = `
Object { Object {
"cached": 0, "cached": 0,

View File

@ -377,10 +377,12 @@ describe('compiler: parse', () => {
test('comments option', () => { test('comments option', () => {
__DEV__ = false __DEV__ = false
const astNoComment = baseParse('<!--abc-->') const astDefaultComment = baseParse('<!--abc-->')
const astNoComment = baseParse('<!--abc-->', { comments: false })
const astWithComments = baseParse('<!--abc-->', { comments: true }) const astWithComments = baseParse('<!--abc-->', { comments: true })
__DEV__ = true __DEV__ = true
expect(astDefaultComment.children).toHaveLength(0)
expect(astNoComment.children).toHaveLength(0) expect(astNoComment.children).toHaveLength(0)
expect(astWithComments.children).toHaveLength(1) expect(astWithComments.children).toHaveLength(1)
}) })
@ -636,6 +638,40 @@ describe('compiler: parse', () => {
}) })
}) })
test('built-in component', () => {
const ast = baseParse('<div></div><comp></comp>', {
isBuiltInComponent: tag => (tag === 'comp' ? Symbol() : void 0)
})
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
})
})
test('slot element', () => {
const ast = baseParse('<slot></slot><Comp></Comp>')
expect(ast.children[0]).toMatchObject({
type: NodeTypes.ELEMENT,
tag: 'slot',
tagType: ElementTypes.SLOT
})
expect(ast.children[1]).toMatchObject({
type: NodeTypes.ELEMENT,
tag: 'Comp',
tagType: ElementTypes.COMPONENT
})
})
test('attribute with no value', () => { test('attribute with no value', () => {
const ast = baseParse('<div id></div>') const ast = baseParse('<div id></div>')
const element = ast.children[0] as ElementNode const element = ast.children[0] as ElementNode
@ -1679,6 +1715,14 @@ foo
}) })
describe('decodeEntities option', () => { describe('decodeEntities option', () => {
test('use default map', () => {
const ast: any = baseParse('&gt;&lt;&amp;&apos;&quot;&foo;')
expect(ast.children.length).toBe(1)
expect(ast.children[0].type).toBe(NodeTypes.TEXT)
expect(ast.children[0].content).toBe('><&\'"&foo;')
})
test('use the given map', () => { test('use the given map', () => {
const ast: any = baseParse('&amp;&cups;', { const ast: any = baseParse('&amp;&cups;', {
decodeEntities: text => text.replace('&cups;', '\u222A\uFE00'), decodeEntities: text => text.replace('&cups;', '\u222A\uFE00'),
@ -1746,6 +1790,27 @@ foo
const ast = baseParse(` foo \n bar baz `) const ast = baseParse(` foo \n bar baz `)
expect((ast.children[0] as TextNode).content).toBe(` foo bar baz `) expect((ast.children[0] as TextNode).content).toBe(` foo bar baz `)
}) })
it('should remove leading newline character immediately following the pre element start tag', () => {
const ast = baseParse(`<pre>\n foo bar </pre>`, {
isPreTag: tag => tag === 'pre'
})
expect(ast.children).toHaveLength(1)
const preElement = ast.children[0] as ElementNode
expect(preElement.children).toHaveLength(1)
expect((preElement.children[0] as TextNode).content).toBe(` foo bar `)
})
it('should NOT remove leading newline character immediately following child-tag of pre element', () => {
const ast = baseParse(`<pre><span></span>\n foo bar </pre>`, {
isPreTag: tag => tag === 'pre'
})
const preElement = ast.children[0] as ElementNode
expect(preElement.children).toHaveLength(2)
expect((preElement.children[1] as TextNode).content).toBe(
`\n foo bar `
)
})
}) })
describe('Errors', () => { describe('Errors', () => {
@ -2222,6 +2287,15 @@ foo
loc: { offset: 0, line: 1, column: 1 } loc: { offset: 0, line: 1, column: 1 }
} }
] ]
},
{
code: '<div></div',
errors: [
{
type: ErrorCodes.EOF_IN_TAG,
loc: { offset: 10, line: 1, column: 11 }
}
]
} }
], ],
INCORRECTLY_CLOSED_COMMENT: [ INCORRECTLY_CLOSED_COMMENT: [
@ -2391,6 +2465,15 @@ foo
} }
] ]
}, },
{
code: '<template><!--a<!--b<!----></template>',
errors: [
{
type: ErrorCodes.NESTED_COMMENT,
loc: { offset: 15, line: 1, column: 16 }
}
]
},
{ {
code: '<template><!--a<!--></template>', code: '<template><!--a<!--></template>',
errors: [] errors: []