Ver código fonte

default slot should use fallback content when it contains all whitespace nodes (fix #5097)

Evan You 9 anos atrás
pai
commit
303824ea9c

+ 6 - 5
src/core/instance/render-helpers/resolve-slots.js

@@ -29,16 +29,17 @@ export function resolveSlots (
       defaultSlot.push(child)
     }
   }
-  // ignore single whitespace
-  if (defaultSlot.length && !(
-    defaultSlot.length === 1 &&
-    (defaultSlot[0].text === ' ' || defaultSlot[0].isComment)
-  )) {
+  // ignore whitespace
+  if (!defaultSlot.every(isWhitespace)) {
     slots.default = defaultSlot
   }
   return slots
 }
 
+function isWhitespace (node: VNode): boolean {
+  return node.isComment || node.text === ' '
+}
+
 export function resolveScopedSlots (
   fns: Array<[string, Function]>
 ): { [key: string]: Function } {

+ 2 - 2
test/unit/features/component/component-slot.spec.js

@@ -274,10 +274,10 @@ describe('Component slot', () => {
           <slot name="second"><p>second named slot</p></slot>
         </div>
       `,
-      parentContent: `<div slot="first">1</div> <div slot="second">2</div>`
+      parentContent: `<div slot="first">1</div> <div slot="second">2</div> <div slot="second">2+</div>`
     })
     expect(child.$el.innerHTML).toBe(
-      '<div>1</div> <p>this is the default slot</p> <div>2</div>'
+      '<div>1</div> <p>this is the default slot</p> <div>2</div><div>2+</div>'
     )
   })