Skip to content

Commit 574070f

Browse files
authored
fix(compiler-sfc): externalRE support automatic http/https prefix url pattern (#4922)
fix #4920
1 parent fd7c340 commit 574070f

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

packages/compiler-sfc/__tests__/__snapshots__/templateTransformAssetUrl.spec.ts.snap

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export function render(_ctx, _cache) {
6767
_createElementVNode(\\"img\\", { src: _imports_1 }),
6868
_createElementVNode(\\"img\\", { src: _imports_1 }),
6969
_createElementVNode(\\"img\\", { src: \\"http://example.com/fixtures/logo.png\\" }),
70+
_createElementVNode(\\"img\\", { src: \\"//example.com/fixtures/logo.png\\" }),
7071
_createElementVNode(\\"img\\", { src: \\"/fixtures/logo.png\\" }),
7172
_createElementVNode(\\"img\\", { src: \\"data:image/png;base64,i\\" })
7273
], 64 /* STABLE_FRAGMENT */))
@@ -99,7 +100,8 @@ export function render(_ctx, _cache) {
99100
return (_openBlock(), _createElementBlock(_Fragment, null, [
100101
_createElementVNode(\\"img\\", { src: _imports_0 }),
101102
_createElementVNode(\\"img\\", { src: _imports_1 }),
102-
_createElementVNode(\\"img\\", { src: \\"https://foo.bar/baz.png\\" })
103+
_createElementVNode(\\"img\\", { src: \\"https://foo.bar/baz.png\\" }),
104+
_createElementVNode(\\"img\\", { src: \\"//foo.bar/baz.png\\" })
103105
], 64 /* STABLE_FRAGMENT */))
104106
}"
105107
`;

packages/compiler-sfc/__tests__/templateTransformAssetUrl.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('compiler sfc: transform asset url', () => {
2929
<img src="~fixtures/logo.png"/>
3030
<img src="~/fixtures/logo.png"/>
3131
<img src="http://example.com/fixtures/logo.png"/>
32+
<img src="//example.com/fixtures/logo.png"/>
3233
<img src="/fixtures/logo.png"/>
3334
<img src="data:image/png;base64,i"/>
3435
`)
@@ -76,7 +77,8 @@ describe('compiler sfc: transform asset url', () => {
7677
const { code } = compileWithAssetUrls(
7778
`<img src="./bar.png"/>` +
7879
`<img src="/bar.png"/>` +
79-
`<img src="https://foo.bar/baz.png"/>`,
80+
`<img src="https://foo.bar/baz.png"/>` +
81+
`<img src="//foo.bar/baz.png"/>`,
8082
{
8183
includeAbsolute: true
8284
}

packages/compiler-sfc/__tests__/templateUtils.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ describe('compiler sfc:templateUtils isExternalUrl', () => {
3636
const result = isExternalUrl(url)
3737
expect(result).toBe(true)
3838
})
39+
40+
test('should return true when String starts with //', () => {
41+
const url = '//vuejs.org/'
42+
const result = isExternalUrl(url)
43+
expect(result).toBe(true)
44+
})
3945
})
4046

4147
describe('compiler sfc:templateUtils isDataUrl', () => {

packages/compiler-sfc/src/templateUtils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function isRelativeUrl(url: string): boolean {
66
return firstChar === '.' || firstChar === '~' || firstChar === '@'
77
}
88

9-
const externalRE = /^https?:\/\//
9+
const externalRE = /^(https?:)?\/\//
1010
export function isExternalUrl(url: string): boolean {
1111
return externalRE.test(url)
1212
}

0 commit comments

Comments
 (0)