fix(compiler-sfc): externalRE support automatic http/https prefix url pattern (#4922)

fix #4920
This commit is contained in:
zisasign 2021-11-15 10:37:50 +08:00 committed by GitHub
parent fd7c3407c7
commit 574070f43f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 3 deletions

View File

@ -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 */))
@ -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 */))
}"
`;

View File

@ -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"/>
`)
@ -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
}

View File

@ -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', () => {

View File

@ -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)
}