]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test(compiler-core): more test cases for baseParse (#2211)
authormeteorlxy <meteor.lxy@foxmail.com>
Tue, 13 Oct 2020 20:03:33 +0000 (04:03 +0800)
committerGitHub <noreply@github.com>
Tue, 13 Oct 2020 20:03:33 +0000 (16:03 -0400)
packages/compiler-core/__tests__/__snapshots__/parse.spec.ts.snap
packages/compiler-core/__tests__/parse.spec.ts

index 998b00ab7b924ea66adf3aa91af7335ceacd01b8..a645e631d60e3a3110170f3c77a07c4181654514 100644 (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`] = `
 Object {
   "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`] = `
 Object {
   "cached": 0,
index b73609ee194752cd16f8abb15f9f0491f2048dd5..3fdb3d950ad539693750d7efc168b3717d245e57 100644 (file)
@@ -377,10 +377,12 @@ describe('compiler: parse', () => {
 
     test('comments option', () => {
       __DEV__ = false
-      const astNoComment = baseParse('<!--abc-->')
+      const astDefaultComment = baseParse('<!--abc-->')
+      const astNoComment = baseParse('<!--abc-->', { comments: false })
       const astWithComments = baseParse('<!--abc-->', { comments: true })
       __DEV__ = true
 
+      expect(astDefaultComment.children).toHaveLength(0)
       expect(astNoComment.children).toHaveLength(0)
       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', () => {
       const ast = baseParse('<div id></div>')
       const element = ast.children[0] as ElementNode
@@ -1679,6 +1715,14 @@ foo
   })
 
   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', () => {
       const ast: any = baseParse('&amp;&cups;', {
         decodeEntities: text => text.replace('&cups;', '\u222A\uFE00'),
@@ -1746,6 +1790,27 @@ foo
       const ast = baseParse(`   foo  \n    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', () => {
@@ -2222,6 +2287,15 @@ foo
               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: [
@@ -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>',
           errors: []