Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1052 about parsing 'srcset' attribute value #1057

Merged
merged 2 commits into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/template-compiler/modules/transform-srcset.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ function transform (node) {
return { require: urlToRequire(url), descriptor: descriptor }
})

let code = ''
imageCandidates.forEach((o, i, a) => {
code += o.require + ' + " ' + o.descriptor + (i < a.length - 1 ? ', " + ' : '"')
})
// "require(url1)"
// "require(url1) 1x"
// "require(url1), require(url2)"
// "require(url1), require(url2) 2x"
// "require(url1) 1x, require(url2)"
// "require(url1) 1x, require(url2) 2x"
const code = imageCandidates.map(
({ require, descriptor }) => `${require} + "${descriptor ? ' ' + descriptor : ''}, " + `
).join('').slice(0, -6).concat('"').replace(/ \+ ""$/, '')

attr.value = code
}
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/transform.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
<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>
<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">
<img src="./logo.png" srcset="./logo.png 2x, ./logo.png">
<img src="./logo.png" srcset="./logo.png 2x, ./logo.png 3x">
<img src="./logo.png" srcset="./logo.png, ./logo.png 2x, ./logo.png 3x">
<img
src="./logo.png"
srcset="
Expand Down
11 changes: 7 additions & 4 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,14 @@ describe('vue-loader', () => {
const dataURL = vnode.children[0].data.attrs.src

// image tag with srcset
expect(vnode.children[4].data.attrs.srcset).to.equal(dataURL + ' 2x')
// image tag with srcset with two candidates
expect(vnode.children[6].data.attrs.srcset).to.equal(dataURL + ' 2x, ' + dataURL + ' 3x')
expect(vnode.children[4].data.attrs.srcset).to.equal(dataURL)
expect(vnode.children[6].data.attrs.srcset).to.equal(dataURL + ' 2x')
// image tag with multiline srcset
expect(vnode.children[8].data.attrs.srcset).to.equal(dataURL + ' 2x, ' + dataURL + ' 3x')
expect(vnode.children[8].data.attrs.srcset).to.equal(dataURL + ', ' + dataURL + ' 2x')
expect(vnode.children[10].data.attrs.srcset).to.equal(dataURL + ' 2x, ' + dataURL)
expect(vnode.children[12].data.attrs.srcset).to.equal(dataURL + ' 2x, ' + dataURL + ' 3x')
expect(vnode.children[14].data.attrs.srcset).to.equal(dataURL + ', ' + dataURL + ' 2x, ' + dataURL + ' 3x')
expect(vnode.children[16].data.attrs.srcset).to.equal(dataURL + ' 2x, ' + dataURL + ' 3x')

// style
expect(includeDataURL(style)).to.equal(true)
Expand Down