File tree 2 files changed +24
-4
lines changed
2 files changed +24
-4
lines changed Original file line number Diff line number Diff line change @@ -73,6 +73,13 @@ h1 { color: red }
73
73
expect ( parse ( `<custom/>` ) . descriptor . customBlocks . length ) . toBe ( 0 )
74
74
} )
75
75
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
+
76
83
test ( 'nested templates' , ( ) => {
77
84
const content = `
78
85
<template v-if="ok">ok</template>
Original file line number Diff line number Diff line change @@ -108,7 +108,7 @@ export function parse(
108
108
if ( node . type !== NodeTypes . ELEMENT ) {
109
109
return
110
110
}
111
- if ( ! node . children . length ) {
111
+ if ( ! node . children . length && ! hasSrc ( node ) ) {
112
112
return
113
113
}
114
114
switch ( node . tag ) {
@@ -188,9 +188,13 @@ function createBlock(
188
188
pad : SFCParseOptions [ 'pad' ]
189
189
) : SFCBlock {
190
190
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
+ }
194
198
const loc = {
195
199
source : content ,
196
200
start,
@@ -275,3 +279,12 @@ function padContent(
275
279
return Array ( offset ) . join ( padChar )
276
280
}
277
281
}
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
+ }
You can’t perform that action at this time.
0 commit comments