| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- import {
- isDataUrl,
- isExternalUrl,
- isRelativeUrl,
- } from '../src/template/templateUtils'
- import { compileSFCScript as compile } from './utils'
- describe('compiler sfc:templateUtils isRelativeUrl', () => {
- test('should return true when The first character of the string path is .', () => {
- const url = './**.vue'
- const result = isRelativeUrl(url)
- expect(result).toBe(true)
- })
- test('should return true when The first character of the string path is ~', () => {
- const url = '~/xx.vue'
- const result = isRelativeUrl(url)
- expect(result).toBe(true)
- })
- test('should return true when The first character of the string path is @', () => {
- const url = '@/xx.vue'
- const result = isRelativeUrl(url)
- expect(result).toBe(true)
- })
- })
- describe('compiler sfc:templateUtils isExternalUrl', () => {
- test('should return true when String starts with http://', () => {
- const url = 'http://vuejs.org/'
- const result = isExternalUrl(url)
- expect(result).toBe(true)
- })
- test('should return true when String starts with https://', () => {
- const url = 'https://vuejs.org/'
- const result = isExternalUrl(url)
- expect(result).toBe(true)
- })
- test('should return true when String starts with //', () => {
- const url = '//vuejs.org/'
- const result = isExternalUrl(url)
- expect(result).toBe(true)
- })
- })
- describe('compiler sfc:templateUtils isDataUrl', () => {
- test('should return true w/ hasn`t media type and encode', () => {
- expect(isDataUrl('data:,i')).toBe(true)
- })
- test('should return true w/ media type + encode', () => {
- expect(isDataUrl('data:image/png;base64,i')).toBe(true)
- })
- test('should return true w/ media type + hasn`t encode', () => {
- expect(isDataUrl('data:image/png,i')).toBe(true)
- })
- })
- describe('multiRoot metadata', () => {
- test('marks a non-inline component as multi-root', () => {
- const { content } = compile(
- `
- <script setup vapor>
- const ok = true
- </script>
- <template>
- <div />
- <div />
- </template>
- `,
- {
- inlineTemplate: false,
- vapor: true,
- },
- )
- expect(content).toContain(`__multiRoot: true`)
- })
- test('treats root control flow as a single owner unit', () => {
- const { content } = compile(
- `
- <script setup vapor>
- const ok = true
- </script>
- <template>
- <template v-if="ok">
- <div />
- <div />
- </template>
- </template>
- `,
- {
- inlineTemplate: false,
- vapor: true,
- },
- )
- expect(content).toContain(`__multiRoot: false`)
- })
- test('treats a root if / else-if / else chain as a single owner unit', () => {
- const { content } = compile(
- `
- <script setup vapor>
- const ok = true
- const maybe = false
- </script>
- <template>
- <template v-if="ok">
- <div />
- </template>
- <template v-else-if="maybe">
- <div />
- </template>
- <template v-else>
- <div />
- </template>
- </template>
- `,
- {
- inlineTemplate: false,
- vapor: true,
- },
- )
- expect(content).toContain(`__multiRoot: false`)
- })
- test('treats preserved root comments as root units', () => {
- const { content } = compile(
- `
- <script setup vapor>
- const ok = true
- </script>
- <template>
- <!-- root comment -->
- <div />
- </template>
- `,
- {
- inlineTemplate: false,
- vapor: true,
- },
- )
- expect(content).toContain(`__multiRoot: true`)
- })
- test('respects template comments option when inferring multiRoot', () => {
- const { content } = compile(
- `
- <script setup vapor>
- const ok = true
- </script>
- <template>
- <!-- root comment -->
- <div />
- </template>
- `,
- {
- inlineTemplate: false,
- vapor: true,
- templateOptions: {
- compilerOptions: {
- comments: false,
- },
- },
- },
- )
- expect(content).toContain(`__multiRoot: false`)
- })
- test('ignores comments between root if branches', () => {
- const { content } = compile(
- `
- <script setup vapor>
- const ok = true
- </script>
- <template>
- <template v-if="ok">
- <div />
- </template>
- <!-- branch separator -->
- <template v-else>
- <div />
- </template>
- </template>
- `,
- {
- inlineTemplate: false,
- vapor: true,
- },
- )
- expect(content).toContain(`__multiRoot: false`)
- })
- test('ignores preserved whitespace between root if branches', () => {
- const { content } = compile(
- `
- <script setup vapor>
- const ok = true
- </script>
- <template>
- <template v-if="ok">
- <div />
- </template>
- <template v-else>
- <div />
- </template>
- </template>
- `,
- {
- inlineTemplate: false,
- vapor: true,
- },
- {
- templateParseOptions: {
- whitespace: 'preserve',
- },
- },
- )
- expect(content).toContain(`__multiRoot: false`)
- })
- test('treats root v-for as a single owner unit', () => {
- const { content } = compile(
- `
- <script setup vapor>
- const list = [1, 2]
- </script>
- <template>
- <template v-for="item in list" :key="item">
- <div>{{ item }}</div>
- </template>
- </template>
- `,
- {
- inlineTemplate: false,
- vapor: true,
- },
- )
- expect(content).toContain(`__multiRoot: false`)
- })
- test('treats a root slot outlet as a single owner unit', () => {
- const { content } = compile(
- `
- <script setup vapor>
- const ok = true
- </script>
- <template>
- <slot />
- </template>
- `,
- {
- inlineTemplate: false,
- vapor: true,
- },
- )
- expect(content).toContain(`__multiRoot: false`)
- })
- test('treats root text with a sibling as multi-root', () => {
- const { content } = compile(
- `
- <script setup vapor>
- const msg = 'hello'
- </script>
- <template>
- hello
- <div />
- </template>
- `,
- {
- inlineTemplate: false,
- vapor: true,
- },
- )
- expect(content).toContain(`__multiRoot: true`)
- })
- test('treats a root component as a single owner unit', () => {
- const { content } = compile(
- `
- <script setup vapor>
- const Foo = {}
- </script>
- <template>
- <Foo />
- </template>
- `,
- {
- inlineTemplate: false,
- vapor: true,
- },
- )
- expect(content).toContain(`__multiRoot: false`)
- })
- test('treats a root component with a sibling as multi-root', () => {
- const { content } = compile(
- `
- <script setup vapor>
- import { KeepAlive } from 'vue'
- </script>
- <template>
- <KeepAlive>
- <div />
- </KeepAlive>
- <span />
- </template>
- `,
- {
- inlineTemplate: true,
- vapor: true,
- },
- )
- expect(content).toContain(`__multiRoot: true`)
- })
- test('treats a plain root template element as a single root', () => {
- const { content } = compile(
- `
- <script setup vapor>
- const ok = true
- </script>
- <template>
- <template>
- <div />
- <div />
- </template>
- </template>
- `,
- {
- inlineTemplate: false,
- vapor: true,
- },
- )
- expect(content).toContain(`__multiRoot: false`)
- })
- test('marks an inline component as multi-root', () => {
- const { content } = compile(
- `
- <script setup vapor>
- const ok = true
- </script>
- <template>
- <div />
- <div />
- </template>
- `,
- {
- inlineTemplate: true,
- vapor: true,
- },
- )
- expect(content).toContain(`__multiRoot: true`)
- })
- })
|