File tree 2 files changed +19
-1
lines changed
2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -135,8 +135,13 @@ h1 { color: red }
135
135
136
136
test ( 'should ignore other nodes with no content' , ( ) => {
137
137
expect ( parse ( `<script/>` ) . descriptor . script ) . toBe ( null )
138
+ expect ( parse ( `<script> \n\t </script>` ) . descriptor . script ) . toBe ( null )
138
139
expect ( parse ( `<style/>` ) . descriptor . styles . length ) . toBe ( 0 )
140
+ expect ( parse ( `<style> \n\t </style>` ) . descriptor . styles . length ) . toBe ( 0 )
139
141
expect ( parse ( `<custom/>` ) . descriptor . customBlocks . length ) . toBe ( 0 )
142
+ expect (
143
+ parse ( `<custom> \n\t </custom>` ) . descriptor . customBlocks . length
144
+ ) . toBe ( 0 )
140
145
} )
141
146
142
147
test ( 'handle empty nodes with src attribute' , ( ) => {
Original file line number Diff line number Diff line change @@ -162,7 +162,8 @@ export function parse(
162
162
if ( node . type !== NodeTypes . ELEMENT ) {
163
163
return
164
164
}
165
- if ( ! node . children . length && ! hasSrc ( node ) && node . tag !== 'template' ) {
165
+ // we only want to keep the nodes that are not empty (when the tag is not a template)
166
+ if ( node . tag !== 'template' && isEmpty ( node ) && ! hasSrc ( node ) ) {
166
167
return
167
168
}
168
169
switch ( node . tag ) {
@@ -415,3 +416,15 @@ function hasSrc(node: ElementNode) {
415
416
return p . name === 'src'
416
417
} )
417
418
}
419
+
420
+ /**
421
+ * Returns true if the node has no children
422
+ * once the empty text nodes (trimmed content) have been filtered out.
423
+ */
424
+ function isEmpty ( node : ElementNode ) {
425
+ return (
426
+ node . children . filter (
427
+ child => child . type !== NodeTypes . TEXT || child . content . trim ( ) !== ''
428
+ ) . length === 0
429
+ )
430
+ }
You can’t perform that action at this time.
0 commit comments