fix(compiler-sfc): handle empty nodes with src attribute (#695)

This commit is contained in:
Jason
2020-02-04 23:03:32 +08:00
committed by GitHub
parent 5742a0b826
commit 2d56dfdc4f
2 changed files with 24 additions and 4 deletions

View 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'
})
}