Skip to content

Commit bdaa822

Browse files
committed
Merge branch 'src-set'
# Conflicts: # README.md
2 parents d1edff9 + dc1447b commit bdaa822

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ Also every `<img srcset="..."`> is converted to `require` statements. For exampl
1010
```
1111
is converted to
1212
``` javascript
13-
"<img src=\"" + require("./image.jpg") + "\" srcset=\"" + require("./image.jpg") + " 1x, " + require("./image@2x.jpg") + " 2x \">"
13+
"<img src=\"" + require("./image.jpg") + "\" srcset=\"" + require("./image.jpg") + " 1x," + require("./image@2x.jpg") " 2x \">"
1414
```
15-
16-
1715
You can specify which tag-attribute combination should be processed by this loader via the query parameter `attrs`. Pass an array or a space-separated list of `<tag>:<attribute>` combinations. (Default: `attrs=[img:src, img:srcset]`)
1816

1917
To completely disable tag-attribute processing (for instance, if you're handling image loading on the client side) you can pass in `attrs=false`.

index.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,28 @@ module.exports = function(content) {
3838
throw new Error("Invalid value to config parameter attrs");
3939
}
4040
var root = config.root;
41-
var links = attrParse(content, function(tag, attr) {
41+
var rawLinks = attrParse(content, function(tag, attr) {
4242
return attributes.indexOf(tag + ":" + attr) >= 0;
4343
});
44+
var links = [];
45+
rawLinks.forEach(function (link) {
46+
var length = link.length;
47+
var start = link.start;
48+
var valueList = link.value.split(",");
49+
valueList.forEach(function (newLink) {
50+
var trimmed = newLink.trim();
51+
var cLength = newLink.length;
52+
var spacePos = trimmed.indexOf(" ");
53+
var spaceStart = newLink.indexOf(trimmed);
54+
var len = cLength+ spaceStart;
55+
if (-1 != spacePos) {
56+
len = spacePos + spaceStart;
57+
trimmed = trimmed.substring(0,spacePos);
58+
}
59+
links.push({start: start, length: len , value: trimmed});
60+
start += cLength+1;
61+
});
62+
});
4463
links.reverse();
4564
var data = {};
4665
content = [content];

test/loaderTest.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ describe("loader", function() {
2929
'module.exports = "Text <script src=\\"" + require("./script.js") + "\\"><img src=\\"" + require("./image.png") + "\\">";'
3030
);
3131
});
32+
it("should handle srcset-attrubute by default", function ()
33+
{
34+
loader.call({
35+
},'Text <img srcset="image.png 1x">').should.be.eql(
36+
'module.exports = "Text <img srcset=\\"" + require("./image.png") + " 1x\\">";'
37+
)
38+
});
39+
it("should handle srcset-attrubute with comma seperated list", function ()
40+
{
41+
loader.call({
42+
},'Text <img srcset="image.png 1x,image@2x.png 2x">').should.be.eql(
43+
'module.exports = "Text <img srcset=\\"" + require("./image.png") + " 1x,\" + require("./image@2x.png") + " 2x\\">";'
44+
)
45+
});
46+
it("should handle srcset-attrubute with comma seperated list, independend of spaces in list", function ()
47+
{
48+
loader.call({
49+
},'Text <img srcset="image.png 1x, image@2x.png 2x">').should.be.eql(
50+
'module.exports = "Text <img srcset=\\"" + require("./image.png") + " 1x,\" + require("./image@2x.png") + " 2x\\">";'
51+
)
52+
});
3253
it("should not make bad things with templates", function() {
3354
loader.call({}, '<h3>#{number} {customer}</h3>\n<p> {title} </p>').should.be.eql(
3455
'module.exports = "<h3>#{number} {customer}</h3>\\n<p> {title} </p>";'
@@ -38,31 +59,31 @@ describe("loader", function() {
3859
loader.call({
3960
minimize: true
4061
}, '<!-- comment --><h3 customAttr="">#{number} {customer}</h3>\n<p> {title} </p>\n\t <!-- comment --> <img src="image.png" />').should.be.eql(
41-
'module.exports = "<h3 customattr=\\"\\">#{number} {customer}</h3> <p> {title} </p> <img src=\\"\" + require("./image.png") + "\\\"/>";'
62+
'module.exports = "<h3 customattr=\\"\\">#{number} {customer}</h3> <p> {title} </p> <img src=\" + require("./image.png") + \" />";'
4263
);
4364
});
4465
// https://github.com/webpack/webpack/issues/752
4566
it("should not remove attributes by default", function() {
4667
loader.call({
4768
minimize: true
4869
}, '<input type="text" />').should.be.eql(
49-
'module.exports = "<input type=\\"text\\"/>";'
70+
'module.exports = "<input type=text />";'
5071
);
5172
});
5273
it("should preserve comments", function() {
5374
loader.call({
5475
minimize: true,
5576
query: "?-removeComments"
5677
}, '<!-- comment --><h3 customAttr="">#{number} {customer}</h3><p>{title}</p><!-- comment --><img src="image.png" />').should.be.eql(
57-
'module.exports = "<!-- comment --><h3 customattr=\\"\\">#{number} {customer}</h3><p>{title}</p><!-- comment --><img src=\\"\" + require("./image.png") + "\\\"/>";'
78+
'module.exports = "<!-- comment --><h3 customattr=\\"\\">#{number} {customer}</h3><p>{title}</p><!-- comment --><img src=\" + require("./image.png") + \" />";'
5879
);
5980
});
6081
it("should treat attributes as case sensitive", function() {
6182
loader.call({
6283
minimize: true,
6384
query: "?caseSensitive"
6485
}, '<!-- comment --><h3 customAttr="">#{number} {customer}</h3><p>{title}</p><!-- comment --><img src="image.png" />').should.be.eql(
65-
'module.exports = "<h3 customAttr=\\"\\">#{number} {customer}</h3><p>{title}</p><img src=\\"\" + require("./image.png") + "\\\"/>";'
86+
'module.exports = "<h3 customAttr=\\"\\">#{number} {customer}</h3><p>{title}</p><img src=\" + require("./image.png") + \" />";'
6687
);
6788
});
6889
it("should accept complex options via a webpack config property", function() {

0 commit comments

Comments
 (0)