Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/compiler-sfc/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
21 changes: 17 additions & 4 deletions packages/compiler-sfc/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'
})
}