|
|
@@ -28,8 +28,12 @@ import { createObjectMatcher } from '../testUtils'
|
|
|
import { PatchFlags } from '@vue/shared'
|
|
|
import { transformFor } from '../../src/transforms/vFor'
|
|
|
import { transformIf } from '../../src/transforms/vIf'
|
|
|
+import { transformText } from '../../src/transforms/transformText'
|
|
|
|
|
|
-function parseWithSlots(template: string, options: CompilerOptions = {}) {
|
|
|
+function parseWithSlots(
|
|
|
+ template: string,
|
|
|
+ options: CompilerOptions & { transformText?: boolean } = {},
|
|
|
+) {
|
|
|
const ast = parse(template, {
|
|
|
whitespace: options.whitespace,
|
|
|
})
|
|
|
@@ -43,6 +47,7 @@ function parseWithSlots(template: string, options: CompilerOptions = {}) {
|
|
|
transformSlotOutlet,
|
|
|
transformElement,
|
|
|
trackSlotScopes,
|
|
|
+ ...(options.transformText ? [transformText] : []),
|
|
|
],
|
|
|
directiveTransforms: {
|
|
|
on: transformOn,
|
|
|
@@ -307,6 +312,40 @@ describe('compiler: transform component slots', () => {
|
|
|
expect(generate(root).code).toMatchSnapshot()
|
|
|
})
|
|
|
|
|
|
+ test('named slots w/ implicit default slot containing non-breaking space', () => {
|
|
|
+ const { root, slots } = parseWithSlots(
|
|
|
+ `<Comp>
|
|
|
+ \u00a0
|
|
|
+ <template #one>foo</template>
|
|
|
+ </Comp>`,
|
|
|
+ )
|
|
|
+ expect(slots).toMatchObject(
|
|
|
+ createSlotMatcher({
|
|
|
+ one: {
|
|
|
+ type: NodeTypes.JS_FUNCTION_EXPRESSION,
|
|
|
+ params: undefined,
|
|
|
+ returns: [
|
|
|
+ {
|
|
|
+ type: NodeTypes.TEXT,
|
|
|
+ content: `foo`,
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ default: {
|
|
|
+ type: NodeTypes.JS_FUNCTION_EXPRESSION,
|
|
|
+ params: undefined,
|
|
|
+ returns: [
|
|
|
+ {
|
|
|
+ type: NodeTypes.TEXT,
|
|
|
+ content: ` \u00a0 `,
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ },
|
|
|
+ }),
|
|
|
+ )
|
|
|
+ expect(generate(root).code).toMatchSnapshot()
|
|
|
+ })
|
|
|
+
|
|
|
test('dynamically named slots', () => {
|
|
|
const { root, slots } = parseWithSlots(
|
|
|
`<Comp>
|
|
|
@@ -1011,6 +1050,27 @@ describe('compiler: transform component slots', () => {
|
|
|
expect(generate(root, { prefixIdentifiers: true }).code).toMatchSnapshot()
|
|
|
})
|
|
|
|
|
|
+ test('implicit default slot with non-breaking space', () => {
|
|
|
+ const source = `
|
|
|
+ <Comp>
|
|
|
+
|
|
|
+ <template #header> Header </template>
|
|
|
+ </Comp>
|
|
|
+ `
|
|
|
+ const { root } = parseWithSlots(source, {
|
|
|
+ whitespace: 'preserve',
|
|
|
+ })
|
|
|
+
|
|
|
+ const slots = (root as any).children[0].codegenNode.children
|
|
|
+ .properties as ObjectExpression['properties']
|
|
|
+
|
|
|
+ expect(
|
|
|
+ slots.some(p => (p.key as SimpleExpressionNode).content === 'default'),
|
|
|
+ ).toBe(true)
|
|
|
+
|
|
|
+ expect(generate(root, { prefixIdentifiers: true }).code).toMatchSnapshot()
|
|
|
+ })
|
|
|
+
|
|
|
test('named slot with v-if + v-else', () => {
|
|
|
const source = `
|
|
|
<Comp>
|
|
|
@@ -1024,5 +1084,23 @@ describe('compiler: transform component slots', () => {
|
|
|
|
|
|
expect(generate(root, { prefixIdentifiers: true }).code).toMatchSnapshot()
|
|
|
})
|
|
|
+
|
|
|
+ test('named slot with v-if + v-else and comments', () => {
|
|
|
+ const source = `
|
|
|
+ <Comp>
|
|
|
+ <template #one v-if="ok">foo</template>
|
|
|
+ <!-- start -->
|
|
|
+
|
|
|
+ <!-- end -->
|
|
|
+ <template #two v-else>baz</template>
|
|
|
+ </Comp>
|
|
|
+ `
|
|
|
+ const { root } = parseWithSlots(source, {
|
|
|
+ transformText: true,
|
|
|
+ whitespace: 'preserve',
|
|
|
+ })
|
|
|
+
|
|
|
+ expect(generate(root, { prefixIdentifiers: true }).code).toMatchSnapshot()
|
|
|
+ })
|
|
|
})
|
|
|
})
|