Skip to content

Commit 2d56dfd

Browse files
authored
fix(compiler-sfc): handle empty nodes with src attribute (#695)
1 parent 5742a0b commit 2d56dfd

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

packages/compiler-sfc/__tests__/parse.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ h1 { color: red }
7373
expect(parse(`<custom/>`).descriptor.customBlocks.length).toBe(0)
7474
})
7575

76+
test('handle empty nodes with src attribute', () => {
77+
const { descriptor } = parse(`<script src="com"/>`)
78+
expect(descriptor.script).toBeTruthy()
79+
expect(descriptor.script!.content).toBeFalsy()
80+
expect(descriptor.script!.attrs['src']).toBe('com')
81+
})
82+
7683
test('nested templates', () => {
7784
const content = `
7885
<template v-if="ok">ok</template>

packages/compiler-sfc/src/parse.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export function parse(
108108
if (node.type !== NodeTypes.ELEMENT) {
109109
return
110110
}
111-
if (!node.children.length) {
111+
if (!node.children.length && !hasSrc(node)) {
112112
return
113113
}
114114
switch (node.tag) {
@@ -188,9 +188,13 @@ function createBlock(
188188
pad: SFCParseOptions['pad']
189189
): SFCBlock {
190190
const type = node.tag
191-
const start = node.children[0].loc.start
192-
const end = node.children[node.children.length - 1].loc.end
193-
const content = source.slice(start.offset, end.offset)
191+
let { start, end } = node.loc
192+
let content = ''
193+
if (node.children.length) {
194+
start = node.children[0].loc.start
195+
end = node.children[node.children.length - 1].loc.end
196+
content = source.slice(start.offset, end.offset)
197+
}
194198
const loc = {
195199
source: content,
196200
start,
@@ -275,3 +279,12 @@ function padContent(
275279
return Array(offset).join(padChar)
276280
}
277281
}
282+
283+
function hasSrc(node: ElementNode) {
284+
return node.props.some(p => {
285+
if (p.type !== NodeTypes.ATTRIBUTE) {
286+
return false
287+
}
288+
return p.name === 'src'
289+
})
290+
}

0 commit comments

Comments
 (0)