Skip to content

Commit

Permalink
feat: add 'use' tag of SVG to 'transformAssetUrls' option as default (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mliew21396 authored and haoqunjiang committed Jan 8, 2019
1 parent ad4affc commit f4e3336
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ interface TemplateCompileOptions {
// source: 'src',
// img: 'src',
// image: 'xlink:href'
// use: 'xlink:href'
// }
transformAssetUrls?: AssetURLOptions | boolean

Expand Down
3 changes: 2 additions & 1 deletion lib/templateCompilerModules/assetUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const defaultOptions: AssetURLOptions = {
video: ['src', 'poster'],
source: 'src',
img: 'src',
image: ['xlink:href', 'href']
image: ['xlink:href', 'href'],
use: ['xlink:href', 'href']
}

export default (userOptions?: AssetURLOptions) => {
Expand Down
22 changes: 15 additions & 7 deletions test/compileTemplate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ test('transform srcset', () => {
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink">
<image xlink:href="./logo.png" />
</svg>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink">
<use xlink:href="./logo.png"/>
</svg>
</svg>
<img src="./logo.png" srcset="./logo.png">
<img src="./logo.png" srcset="./logo.png 2x">
<img src="./logo.png" srcset="./logo.png, ./logo.png 2x">
Expand Down Expand Up @@ -183,16 +187,20 @@ test('transform srcset', () => {
expect(vnode.children[2].children[0].data.attrs['xlink:href']).toBe(
'test-url'
)
// use tag (SVG)
expect(vnode.children[4].children[0].data.attrs['xlink:href']).toBe(
'test-url'
)

// image tag with srcset
expect(vnode.children[4].data.attrs.srcset).toBe('test-url')
expect(vnode.children[6].data.attrs.srcset).toBe('test-url 2x')
expect(vnode.children[6].data.attrs.srcset).toBe('test-url')
expect(vnode.children[8].data.attrs.srcset).toBe('test-url 2x')
// image tag with multiline srcset
expect(vnode.children[8].data.attrs.srcset).toBe('test-url, test-url 2x')
expect(vnode.children[10].data.attrs.srcset).toBe('test-url 2x, test-url')
expect(vnode.children[12].data.attrs.srcset).toBe('test-url 2x, test-url 3x')
expect(vnode.children[14].data.attrs.srcset).toBe(
expect(vnode.children[10].data.attrs.srcset).toBe('test-url, test-url 2x')
expect(vnode.children[12].data.attrs.srcset).toBe('test-url 2x, test-url')
expect(vnode.children[14].data.attrs.srcset).toBe('test-url 2x, test-url 3x')
expect(vnode.children[16].data.attrs.srcset).toBe(
'test-url, test-url 2x, test-url 3x'
)
expect(vnode.children[16].data.attrs.srcset).toBe('test-url 2x, test-url 3x')
expect(vnode.children[18].data.attrs.srcset).toBe('test-url 2x, test-url 3x')
})

0 comments on commit f4e3336

Please sign in to comment.