Skip to content

Commit

Permalink
feat: add support for empty tags in tag:attribute matching (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuragic authored and joshwiens committed Jul 26, 2017
1 parent 15869bf commit 70370dc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ By default every local `<img src="image.png">` is required (`require('./image.pn

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

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.

```js
{
test: /\.(html)$/,
use: {
loader: 'html-loader',
options: {
attrs: [':data-src']
}
}
}
```

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

<h2 align="center">Examples</h2>
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ module.exports = function(content) {
}
var root = config.root;
var links = attrParse(content, function(tag, attr) {
return attributes.indexOf(tag + ":" + attr) >= 0;
var item = tag + ":" + attr;
var res = attributes.find(function(a) {
return item.indexOf(a) >= 0;
});
return !!res;
});
links.reverse();
var data = {};
Expand Down
7 changes: 7 additions & 0 deletions test/loaderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ describe("loader", function() {
'module.exports = "Text <script src=\\"" + require("./script.js") + "\\"><img src=\\"" + require("./image.png") + "\\">";'
);
});
it("should accept :attribute (empty tag) from query", function() {
loader.call({
query: "?attrs[]=:custom-src"
}, 'Text <custom-element custom-src="image1.png"><custom-img custom-src="image2.png"/></custom-element>').should.be.eql(
'module.exports = "Text <custom-element custom-src=\\"" + require("./image1.png") + "\\"><custom-img custom-src=\\"" + require("./image2.png") + "\\"/></custom-element>";'
);
});
it("should not make bad things with templates", function() {
loader.call({}, '<h3>#{number} {customer}</h3>\n<p> {title} </p>').should.be.eql(
'module.exports = "<h3>#{number} {customer}</h3>\\n<p> {title} </p>";'
Expand Down

0 comments on commit 70370dc

Please sign in to comment.