Skip to content

Commit fa10b6b

Browse files
committed
Further fixes for whitespace handling
Also added a test for srcset.
1 parent 8659eaf commit fa10b6b

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

lib/template-compiler/modules/transform-srcset.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = function () {
1010

1111
function transform (node) {
1212
const tags = ['img', 'source']
13-
13+
1414
if (tags.indexOf(node.tag) !== -1 && node.attrs) {
1515
node.attrs.forEach(attr => {
1616
if (attr.name === 'srcset') {
@@ -22,12 +22,11 @@ function transform (node) {
2222
}
2323

2424
// http://w3c.github.io/html/semantics-embedded-content.html#ref-for-image-candidate-string-5
25-
26-
const spaceCharacters = /[ \t\n\f\r]+/
27-
const escapedSpaceCharacters = /^([ \t\n\f\r]|\\t|\\n|\\f|\\r)*/
25+
const escapedSpaceCharacters = /( |\\t|\\n|\\f|\\r)+/g
2826

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

@@ -42,15 +41,14 @@ function transform (node) {
4241
}
4342
}
4443

45-
function urlToRequire (url) {
46-
// same logic as in transform-require.js
47-
var firstChar = url.charAt(0)
48-
if (firstChar === '.' || firstChar === '~') {
49-
if (firstChar === '~') {
50-
var secondChar = url.charAt(1)
51-
url = '"' + url.slice(secondChar === '/' ? 2 : 1)
52-
}
53-
return `require("${url}")`
54-
44+
function urlToRequire (url) {
45+
// same logic as in transform-require.js
46+
var firstChar = url.charAt(0)
47+
if (firstChar === '.' || firstChar === '~') {
48+
if (firstChar === '~') {
49+
var secondChar = url.charAt(1)
50+
url = '"' + url.slice(secondChar === '/' ? 2 : 1)
5551
}
52+
return `require("${url}")`
5653
}
54+
}

test/fixtures/transform.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink">
55
<image xlink:href="./logo.png" />
66
</svg>
7+
<img src="./logo.png" srcset="./logo.png 2x">
8+
<img src="./logo.png" srcset="./logo.png 2x, ./logo.png 3x">
9+
<img
10+
src="./logo.png"
11+
srcset="
12+
./logo.png 2x,
13+
./logo.png 3x
14+
">
715
</div>
816
</template>
917

test/test.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,15 +450,25 @@ describe('vue-loader', function () {
450450
]
451451
}
452452
}, (window, module) => {
453-
function includeDataURL (s) {
454-
return !!s.match(/\s*data:([a-z]+\/[a-z]+(;[a-z\-]+\=[a-z\-]+)?)?(;base64)?,[a-z0-9\!\$\&\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*/i)
455-
}
453+
function includeDataURL (s) {
454+
return !!s.match(/\s*data:([a-z]+\/[a-z]+(;[a-z\-]+\=[a-z\-]+)?)?(;base64)?,[a-z0-9\!\$\&\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*/i)
455+
}
456456
var vnode = mockRender(module)
457457
// img tag
458458
expect(includeDataURL(vnode.children[0].data.attrs.src)).to.equal(true)
459459
// image tag (SVG)
460460
expect(includeDataURL(vnode.children[2].children[0].data.attrs['xlink:href'])).to.equal(true)
461461
var style = window.document.querySelector('style').textContent
462+
463+
let dataURL = vnode.children[0].data.attrs.src
464+
465+
// image tag with srcset
466+
expect(vnode.children[4].data.attrs.srcset).to.equal(dataURL + " 2x")
467+
// image tag with srcset with two candidates
468+
expect(vnode.children[6].data.attrs.srcset).to.equal(dataURL + " 2x, " + dataURL + " 3x")
469+
// image tag with multiline srcset
470+
expect(vnode.children[8].data.attrs.srcset).to.equal(dataURL + " 2x, " + dataURL + " 3x")
471+
462472
// style
463473
expect(includeDataURL(style)).to.equal(true)
464474
done()

0 commit comments

Comments
 (0)