Skip to content

Commit

Permalink
fix(compiler-sfc): externalRE support automatic http/https prefix url…
Browse files Browse the repository at this point in the history
… pattern (#4922)

fix #4920
  • Loading branch information
qiyuan-wang committed Nov 15, 2021
1 parent fd7c340 commit 574070f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
Expand Up @@ -67,6 +67,7 @@ export function render(_ctx, _cache) {
_createElementVNode(\\"img\\", { src: _imports_1 }),
_createElementVNode(\\"img\\", { src: _imports_1 }),
_createElementVNode(\\"img\\", { src: \\"http://example.com/fixtures/logo.png\\" }),
_createElementVNode(\\"img\\", { src: \\"//example.com/fixtures/logo.png\\" }),
_createElementVNode(\\"img\\", { src: \\"/fixtures/logo.png\\" }),
_createElementVNode(\\"img\\", { src: \\"data:image/png;base64,i\\" })
], 64 /* STABLE_FRAGMENT */))
Expand Down Expand Up @@ -99,7 +100,8 @@ export function render(_ctx, _cache) {
return (_openBlock(), _createElementBlock(_Fragment, null, [
_createElementVNode(\\"img\\", { src: _imports_0 }),
_createElementVNode(\\"img\\", { src: _imports_1 }),
_createElementVNode(\\"img\\", { src: \\"https://foo.bar/baz.png\\" })
_createElementVNode(\\"img\\", { src: \\"https://foo.bar/baz.png\\" }),
_createElementVNode(\\"img\\", { src: \\"//foo.bar/baz.png\\" })
], 64 /* STABLE_FRAGMENT */))
}"
`;
Expand Up @@ -29,6 +29,7 @@ describe('compiler sfc: transform asset url', () => {
<img src="~fixtures/logo.png"/>
<img src="~/fixtures/logo.png"/>
<img src="http://example.com/fixtures/logo.png"/>
<img src="//example.com/fixtures/logo.png"/>
<img src="/fixtures/logo.png"/>
<img src="data:image/png;base64,i"/>
`)
Expand Down Expand Up @@ -76,7 +77,8 @@ describe('compiler sfc: transform asset url', () => {
const { code } = compileWithAssetUrls(
`<img src="./bar.png"/>` +
`<img src="/bar.png"/>` +
`<img src="https://foo.bar/baz.png"/>`,
`<img src="https://foo.bar/baz.png"/>` +
`<img src="//foo.bar/baz.png"/>`,
{
includeAbsolute: true
}
Expand Down
6 changes: 6 additions & 0 deletions packages/compiler-sfc/__tests__/templateUtils.spec.ts
Expand Up @@ -36,6 +36,12 @@ describe('compiler sfc:templateUtils isExternalUrl', () => {
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', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/templateUtils.ts
Expand Up @@ -6,7 +6,7 @@ export function isRelativeUrl(url: string): boolean {
return firstChar === '.' || firstChar === '~' || firstChar === '@'
}

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

0 comments on commit 574070f

Please sign in to comment.