fix(compiler-sfc): handle empty nodes with src attribute (#695)
This commit is contained in:
		
							parent
							
								
									5742a0b826
								
							
						
					
					
						commit
						2d56dfdc4f
					
				@ -73,6 +73,13 @@ h1 { color: red }
 | 
				
			|||||||
    expect(parse(`<custom/>`).descriptor.customBlocks.length).toBe(0)
 | 
					    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', () => {
 | 
					  test('nested templates', () => {
 | 
				
			||||||
    const content = `
 | 
					    const content = `
 | 
				
			||||||
    <template v-if="ok">ok</template>
 | 
					    <template v-if="ok">ok</template>
 | 
				
			||||||
 | 
				
			|||||||
@ -108,7 +108,7 @@ export function parse(
 | 
				
			|||||||
    if (node.type !== NodeTypes.ELEMENT) {
 | 
					    if (node.type !== NodeTypes.ELEMENT) {
 | 
				
			||||||
      return
 | 
					      return
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (!node.children.length) {
 | 
					    if (!node.children.length && !hasSrc(node)) {
 | 
				
			||||||
      return
 | 
					      return
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    switch (node.tag) {
 | 
					    switch (node.tag) {
 | 
				
			||||||
@ -188,9 +188,13 @@ function createBlock(
 | 
				
			|||||||
  pad: SFCParseOptions['pad']
 | 
					  pad: SFCParseOptions['pad']
 | 
				
			||||||
): SFCBlock {
 | 
					): SFCBlock {
 | 
				
			||||||
  const type = node.tag
 | 
					  const type = node.tag
 | 
				
			||||||
  const start = node.children[0].loc.start
 | 
					  let { start, end } = node.loc
 | 
				
			||||||
  const end = node.children[node.children.length - 1].loc.end
 | 
					  let content = ''
 | 
				
			||||||
  const content = source.slice(start.offset, end.offset)
 | 
					  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 = {
 | 
					  const loc = {
 | 
				
			||||||
    source: content,
 | 
					    source: content,
 | 
				
			||||||
    start,
 | 
					    start,
 | 
				
			||||||
@ -275,3 +279,12 @@ function padContent(
 | 
				
			|||||||
    return Array(offset).join(padChar)
 | 
					    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'
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user