Skip to content

Commit 8659eaf

Browse files
therealpecusAndreKR
authored andcommitted
fix srcset import when attribute uses whitespace
covers cases like: ``` <img srcset=" ../image.jpg 1x, ../image.2x.jpg 2x, ../image.1920x1280.jpg 1920w " alt="srcset on multiple lines and with extra whitespace"> ```
1 parent e3c9b4e commit 8659eaf

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ module.exports = function () {
99
}
1010

1111
function transform (node) {
12-
if (node.tag === 'img' && node.attrs) {
12+
const tags = ['img', 'source']
13+
14+
if (tags.indexOf(node.tag) !== -1 && node.attrs) {
1315
node.attrs.forEach(attr => {
1416
if (attr.name === 'srcset') {
1517
// same logic as in transform-require.js
@@ -22,15 +24,16 @@ function transform (node) {
2224
// http://w3c.github.io/html/semantics-embedded-content.html#ref-for-image-candidate-string-5
2325

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

2629
const imageCandidates = value.substr(1, value.length - 2).split(',').map(s => {
27-
const [url, descriptor] = s.trim().split(spaceCharacters, 2)
30+
const [url, descriptor] = s.replace(escapedSpaceCharacters, '').split(spaceCharacters, 2)
2831
return { require: urlToRequire(url), descriptor: descriptor }
2932
})
3033

3134
let code = ''
3235
imageCandidates.forEach((o, i, a) => {
33-
code += o.require + ' + " ' + o.descriptor + (i < a.length - 1 ? ',' : '') + '"'
36+
code += o.require + ' + " ' + o.descriptor + (i < a.length - 1 ? ', " + ' : '"')
3437
})
3538

3639
attr.value = code
@@ -39,14 +42,15 @@ function transform (node) {
3942
}
4043
}
4144

42-
function urlToRequire (url) {
43-
// same logic as in transform-require.js
44-
var firstChar = url.charAt(0)
45-
if (firstChar === '.' || firstChar === '~') {
46-
if (firstChar === '~') {
47-
var secondChar = url.charAt(1)
48-
url = '"' + url.slice(secondChar === '/' ? 2 : 1)
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+
4955
}
50-
return 'require("' + url + '")'
5156
}
52-
}

0 commit comments

Comments
 (0)