Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support "srcset" attribute for image tag #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Puts HTML partials in the Angular's $templateCache so directives can use templat

## Webpack and loaders

Webpack is the [webpack](http://webpack.github.io/) and it's module bundler. [Loaders](http://webpack.github.io/docs/using-loaders.html) wrap content in the javascript code that executes in the browser.
Webpack is the [webpack](http://webpack.github.io/) and it's module bundler. [Loaders](http://webpack.github.io/docs/using-loaders.html) wrap content in the javascript code that executes in the browser.

# Install

Expand All @@ -21,7 +21,7 @@ require('ng-cache!./demo/template/myPartial.html');
Partial will be available as `ng-include="'myPartial.html'"`
or `templateUrl: 'myPartial.html'`.

Note that the inline require syntax is used in examples for simplicity.
Note that the inline require syntax is used in examples for simplicity.
It's recommended to use webpack [config](#webpack-config).

## Named templates
Expand Down Expand Up @@ -81,21 +81,21 @@ or retrieve the real directory name (use `*` or `[dir]`):

``` javascript
require('ng-cache?prefix=public/*/templates!./path/to/myPartial.html')
// => ng-include="'public/path/templates/myPartial.html'"
// => ng-include="'public/path/templates/myPartial.html'"
```

Prefix can strip the real directory name (use `//`):

``` javascript
require('ng-cache?prefix=public/*//*/templates!./far/far/away/path/to/myPartial.html')
// => ng-include="'public/far/path/templates/myPartial.html'"
// => ng-include="'public/far/path/templates/myPartial.html'"
```

Prefix can be extended through a directory tree (use `**` or `[dirs]`). See the next section.

## Root

You can specify root directory for templates separated by a colon `prefix=root:**`.
You can specify root directory for templates separated by a colon `prefix=root:**`.
It is enough to specify a single directory name. Prefix counts real template path from right to left and takes first (rightmost) occurance of the root directory.

```
Expand All @@ -121,19 +121,19 @@ It is also possible to combine wildcards in prefix, i.e. `prefix=packman:**/tmpl
Use `name` query parameter to strip file extension or add suffix:

``` javascript
//
//
require('ng-cache?name=[name].tpl!./field.html')
// => ng-include="'field.tpl'"

require('ng-cache?name=[name]-foo.[ext]!./field.html')
// => ng-include="'field-foo.html'"
```

Note. File extension are defined as any char sequence after the last `.`.

## Module

By default, templates will be added to the default AngularJS 'ng' module run() method.
By default, templates will be added to the default AngularJS 'ng' module run() method.
Use this parameter to use a different module name:

``` javascript
Expand All @@ -151,13 +151,13 @@ require('ng-cache?module=moduleName.[root]&prefix=packs:**!./packs/path/to/myPar
In such query `[root]` means that first part of `templateId` *(here it is `path/to/myPartial.html`)* will be stripped out and placed as a part of `moduleId`.

In current example resulting values:
- `moduleId`: `"moduleName.path"`
- `templateId`: `"to/myPartial.html"`
- `moduleId`: `"moduleName.path"`
- `templateId`: `"to/myPartial.html"`

Useful in case you want save few bytes.

## Template id

To obtain template id use `exportIdOnly` query parameter. Loader exports `id` of a template.

```javascript
Expand Down Expand Up @@ -191,7 +191,7 @@ module: {
}
```

Note that the inline require syntax is used in examples for simplicity. It's recommended to use webpack config.
Note that the inline require syntax is used in examples for simplicity. It's recommended to use webpack config.
Please see this [comment](https://github.com/webpack/webpack/issues/1626#issuecomment-156758230)
and the [manual](https://webpack.github.io/docs/using-loaders.html#loaders-in-require).

Expand Down Expand Up @@ -272,6 +272,8 @@ To switch off url resolving use `-url` query param:
require('ng-cache?-url!./myPartial.html')
```

Supported attributes: `src`, `srcset`, `md-svg-src`.

# License

MIT (http://www.opensource.org/licenses/mit-license.php)
6 changes: 3 additions & 3 deletions lib/urlParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ Parser = new Parser({
'<[^\\s/>]+': 'tag',
},
tag: {
'[\\s\\n](md-svg-)?src[\\s\\n]*=[\\s\\n]*[\'"]': 'srcAttr',
'[\\s\\n](md-svg-)?src(set)?[\\s\\n]*=[\\s\\n]*[\'"]': 'srcAttr',
'>': 'root',
},
srcAttr: {
'[^\'"]+': function(match, idx) {
'[^\\s\\n\'",]+': function(match, idx) {
if (relativeImageUrl.test(match)) {
this.urls.push({idx: idx, path: match});
}
return 'tag';
return 'srcAttr';
},
'[\'"]': 'tag',
},
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"chai": "^3.2.0",
"eslint": "^2.13.1",
"eslint-config-google": "^0.6.0",
"file-loader": "^2.0.0",
"mocha": "^2.3.0",
"url-loader": "^1.1.2",
"webpack": "^2.4.1"
},
"dependencies": {
Expand Down
Binary file added test/public/AngularJS-large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/public/AngularJS-small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions test/src/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,15 @@ <h2>Second View</h2>

<script type="text/ng-template">
<div class="view"></div>
</script>

<script type="text/ng-template" id="myTemplateWithImage">
<img src="../public/AngularJS-small.png"></div>
<img
src="../public/AngularJS-small.png"
srcset="
../public/AngularJS-small.png 1x,
../public/AngularJS-large.png 2x
"
></div>
</script>
4 changes: 4 additions & 0 deletions test/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ module.exports = {
loader: '../index.js?prefix=grot/[dir]//[dir]//tmpl&module=appModule&name=[name].tpl&-exportId' +
'&minimizeOptions=' + minimizeOptions + '&conservativeCollapse',
},
{
test: /\.(png|bmp|raw|jpg|jpeg|tiff|gif|svg)$/,
loader: 'url-loader?limit=10240',
},
],
},
};
8 changes: 8 additions & 0 deletions test/webpack2.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ module.exports = {
},
},
}],
},{
test: /\.(png|bmp|raw|jpg|jpeg|tiff|gif|svg)$/,
use: [{
loader: 'url-loader',
options: {
limit: 10240,
}
}]
}],
},
};