Skip to content

Commit

Permalink
fix(compiler-sfc): do not transform external and data urls in templates
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 17, 2022
1 parent b2332fa commit 328ebff
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
15 changes: 14 additions & 1 deletion packages/compiler-sfc/src/templateCompilerModules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ export function urlToRequire(
url = url.slice(secondChar === '/' ? 2 : 1)
}

const uriParts = parseUriParts(url)
if (isExternalUrl(url) || isDataUrl(url) || firstChar === '#') {
return returnValue
}

const uriParts = parseUriParts(url)
if (transformAssetUrlsOption.base) {
// explicit base - directly rewrite the url into absolute url
// does not apply to absolute urls or urls that start with `@`
Expand Down Expand Up @@ -67,3 +70,13 @@ function parseUriParts(urlString: string): UrlWithStringQuery {
}
return returnValue
}

const externalRE = /^(https?:)?\/\//
function isExternalUrl(url: string): boolean {
return externalRE.test(url)
}

const dataUrlRE = /^\s*data:/i
function isDataUrl(url: string): boolean {
return dataUrlRE.test(url)
}
8 changes: 4 additions & 4 deletions packages/compiler-sfc/test/compileStyle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('preprocess less', () => {
'.color { color: @red; }\n' +
'</style>\n',
filename: 'example.vue',
needMap: true
sourceMap: true
}).styles[0]

const result = compileStyle({
Expand All @@ -34,7 +34,7 @@ test('preprocess scss', () => {
'.color { color: $red; }\n' +
'</style>\n',
filename: 'example.vue',
needMap: true
sourceMap: true
}).styles[0]
const result = compileStyle({
id: 'v-scope-xxx',
Expand All @@ -59,7 +59,7 @@ test('preprocess sass', () => {
' color: $red\n' +
'</style>\n',
filename: 'example.vue',
needMap: true
sourceMap: true
}).styles[0]
const result = compileStyle({
id: 'v-scope-xxx',
Expand All @@ -84,7 +84,7 @@ test('preprocess stylus', () => {
' color: red-color\n' +
'</style>\n',
filename: 'example.vue',
needMap: true
sourceMap: true
}).styles[0]
const result = compileStyle({
id: 'v-scope-xxx',
Expand Down
31 changes: 28 additions & 3 deletions packages/compiler-sfc/test/compileTemplate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test('preprocess pug', () => {
' p Cool Pug example!\n' +
'</template>\n',
filename: 'example.vue',
needMap: true
sourceMap: true
}).template as SFCBlock

const result = compileTemplate({
Expand Down Expand Up @@ -103,7 +103,7 @@ test('warn missing preprocessor', () => {
source: '<template lang="unknownLang">\n' + '</template>\n',

filename: 'example.vue',
needMap: true
sourceMap: true
}).template as SFCBlock

const result = compileTemplate({
Expand Down Expand Up @@ -141,7 +141,6 @@ test('transform assetUrls', () => {
})

test('transform srcset', () => {
// TODO:
const source = `
<div>
<img src="./logo.png">
Expand Down Expand Up @@ -227,3 +226,29 @@ test('transform assetUrls and srcset with base option', () => {
'/base/logo.png 2x, /base/logo.png 3x'
)
})

test('transform with includeAbsolute', () => {
const source = `
<div>
<img src="./logo.png">
<img src="/logo.png">
<img src="https://foo.com/logo.png">
</div>
`
const result = compileTemplate({
filename: 'example.vue',
source,
transformAssetUrls: true,
transformAssetUrlsOptions: { includeAbsolute: true }
})

expect(result.errors.length).toBe(0)

const vnode = mockRender(result.code, {
'./logo.png': 'relative',
'/logo.png': 'absolute'
})
expect(vnode.children[0].data.attrs.src).toBe('relative')
expect(vnode.children[2].data.attrs.src).toBe('absolute')
expect(vnode.children[4].data.attrs.src).toBe('https://foo.com/logo.png')
})

0 comments on commit 328ebff

Please sign in to comment.