Skip to content

Commit 640609d

Browse files
nuragicmichael-ciniawsky
authored andcommitted
feat: add support for empty tags in tag:attribute matching (#129)
1 parent 432becb commit 640609d

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ By default every local `<img src="image.png">` is required (`require('./image.pn
2727

2828
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`)
2929

30+
If you use `<custom-elements>`, and lots of them make use of a `custom-src` attribute, you don't have to specify each combination `<tag>:<attribute>`: just specify an empty tag like `attrs=:custom-src` and it will match every element.
31+
32+
```js
33+
{
34+
test: /\.(html)$/,
35+
use: {
36+
loader: 'html-loader',
37+
options: {
38+
attrs: [':data-src']
39+
}
40+
}
41+
}
42+
```
43+
3044
To completely disable tag-attribute processing (for instance, if you're handling image loading on the client side) you can pass in `attrs=false`.
3145

3246
<h2 align="center">Examples</h2>

index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ module.exports = function(content) {
3939
}
4040
var root = config.root;
4141
var links = attrParse(content, function(tag, attr) {
42-
return attributes.indexOf(tag + ":" + attr) >= 0;
42+
var item = tag + ":" + attr;
43+
var res = attributes.find(function(a) {
44+
return item.indexOf(a) >= 0;
45+
});
46+
return !!res;
4347
});
4448
links.reverse();
4549
var data = {};

test/loaderTest.js

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ describe("loader", function() {
2929
'module.exports = "Text <script src=\\"" + require("./script.js") + "\\"><img src=\\"" + require("./image.png") + "\\">";'
3030
);
3131
});
32+
it("should accept :attribute (empty tag) from query", function() {
33+
loader.call({
34+
query: "?attrs[]=:custom-src"
35+
}, 'Text <custom-element custom-src="image1.png"><custom-img custom-src="image2.png"/></custom-element>').should.be.eql(
36+
'module.exports = "Text <custom-element custom-src=\\"" + require("./image1.png") + "\\"><custom-img custom-src=\\"" + require("./image2.png") + "\\"/></custom-element>";'
37+
);
38+
});
3239
it("should not make bad things with templates", function() {
3340
loader.call({}, '<h3>#{number} {customer}</h3>\n<p> {title} </p>').should.be.eql(
3441
'module.exports = "<h3>#{number} {customer}</h3>\\n<p> {title} </p>";'

0 commit comments

Comments
 (0)