Skip to content

Support for srcset #953

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

Merged
merged 4 commits into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Further fixes for whitespace handling
Also added a test for srcset.
  • Loading branch information
AndreKR committed Sep 19, 2017
commit d31d0976ab78a534eb2816c83590779822e87c68
28 changes: 13 additions & 15 deletions lib/template-compiler/modules/transform-srcset.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function () {

function transform (node) {
const tags = ['img', 'source']

if (tags.indexOf(node.tag) !== -1 && node.attrs) {
node.attrs.forEach(attr => {
if (attr.name === 'srcset') {
Expand All @@ -22,12 +22,11 @@ function transform (node) {
}

// http://w3c.github.io/html/semantics-embedded-content.html#ref-for-image-candidate-string-5

const spaceCharacters = /[ \t\n\f\r]+/
const escapedSpaceCharacters = /^([ \t\n\f\r]|\\t|\\n|\\f|\\r)*/
const escapedSpaceCharacters = /( |\\t|\\n|\\f|\\r)+/g

const imageCandidates = value.substr(1, value.length - 2).split(',').map(s => {
const [url, descriptor] = s.replace(escapedSpaceCharacters, '').split(spaceCharacters, 2)
// The attribute value arrives here with all whitespace, except normal spaces, represented by escape sequences
const [url, descriptor] = s.replace(escapedSpaceCharacters, ' ').trim().split(' ', 2)
return { require: urlToRequire(url), descriptor: descriptor }
})

Expand All @@ -42,15 +41,14 @@ function transform (node) {
}
}

function urlToRequire (url) {
// same logic as in transform-require.js
var firstChar = url.charAt(0)
if (firstChar === '.' || firstChar === '~') {
if (firstChar === '~') {
var secondChar = url.charAt(1)
url = '"' + url.slice(secondChar === '/' ? 2 : 1)
}
return `require("${url}")`

function urlToRequire (url) {
// same logic as in transform-require.js
var firstChar = url.charAt(0)
if (firstChar === '.' || firstChar === '~') {
if (firstChar === '~') {
var secondChar = url.charAt(1)
url = '"' + url.slice(secondChar === '/' ? 2 : 1)
}
return `require("${url}")`
}
}
8 changes: 8 additions & 0 deletions test/fixtures/transform.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
<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 2x">
<img src="./logo.png" srcset="./logo.png 2x, ./logo.png 3x">
<img
src="./logo.png"
srcset="
./logo.png 2x,
./logo.png 3x
">
</div>
</template>

Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,16 @@ describe('vue-loader', function () {
// image tag (SVG)
expect(includeDataURL(vnode.children[2].children[0].data.attrs['xlink:href'])).to.equal(true)
var style = window.document.querySelector('style').textContent

let 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")
// image tag with multiline srcset
expect(vnode.children[8].data.attrs.srcset).to.equal(dataURL + " 2x, " + dataURL + " 3x")

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