]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): handle empty nodes with src attribute (#695)
authorJason <jason.daurus@gmail.com>
Tue, 4 Feb 2020 15:03:32 +0000 (23:03 +0800)
committerGitHub <noreply@github.com>
Tue, 4 Feb 2020 15:03:32 +0000 (10:03 -0500)
packages/compiler-sfc/__tests__/parse.spec.ts
packages/compiler-sfc/src/parse.ts

index a55e2e6b2885b04edb749ac8f0b072c3a14f3217..fcb59061aebcd4705a44b33a9820cc0e7fa4bb9e 100644 (file)
@@ -73,6 +73,13 @@ h1 { color: red }
     expect(parse(`<custom/>`).descriptor.customBlocks.length).toBe(0)
   })
 
+  test('handle empty nodes with src attribute', () => {
+    const { descriptor } = parse(`<script src="com"/>`)
+    expect(descriptor.script).toBeTruthy()
+    expect(descriptor.script!.content).toBeFalsy()
+    expect(descriptor.script!.attrs['src']).toBe('com')
+  })
+
   test('nested templates', () => {
     const content = `
     <template v-if="ok">ok</template>
index 1953027c903c7cbd4cb4c680935c0efd13753b6f..da3753643f3b8bb57ff8cb2591da56979824b877 100644 (file)
@@ -108,7 +108,7 @@ export function parse(
     if (node.type !== NodeTypes.ELEMENT) {
       return
     }
-    if (!node.children.length) {
+    if (!node.children.length && !hasSrc(node)) {
       return
     }
     switch (node.tag) {
@@ -188,9 +188,13 @@ function createBlock(
   pad: SFCParseOptions['pad']
 ): SFCBlock {
   const type = node.tag
-  const start = node.children[0].loc.start
-  const end = node.children[node.children.length - 1].loc.end
-  const content = source.slice(start.offset, end.offset)
+  let { start, end } = node.loc
+  let content = ''
+  if (node.children.length) {
+    start = node.children[0].loc.start
+    end = node.children[node.children.length - 1].loc.end
+    content = source.slice(start.offset, end.offset)
+  }
   const loc = {
     source: content,
     start,
@@ -275,3 +279,12 @@ function padContent(
     return Array(offset).join(padChar)
   }
 }
+
+function hasSrc(node: ElementNode) {
+  return node.props.some(p => {
+    if (p.type !== NodeTypes.ATTRIBUTE) {
+      return false
+    }
+    return p.name === 'src'
+  })
+}