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

Add read filter option #3

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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ grunt.initConfig({
your_target: {
options: {
//You typically would only specify one option per target but they may be combined
read: {selector:'link',attribute:'href',writeto:'myCssRefs',isPath:true},
read: {selector:'link',attribute:'href',writeto:'myCssRefs',isPath:true, filter:function(val){return val;}},
remove: '#removeMe',
update: {selector:'html',attribute:'appmode',value:'production'},
append: {selector:'body',html:'<div id="appended">Im being appended</div>'},
Expand All @@ -55,7 +55,7 @@ grunt.initConfig({
Note: each option (except callback) requires a `selector`. This can be any valid JQuery selector.

#### options.read
Extract the value of a given attribute from the set of matched elements then set the values into `dom_munger.data.{writeto}`. A typical use-case is to grab the script references from your html file and pass that to `concat`,`uglify`, or `cssmin`.
Extract the value of a given attribute from the set of matched elements then set the values into `dom_munger.data.{writeto}`. If you pass a `filter` function it will be called against each attribute retrieved. A typical use-case is to grab the script references from your html file and pass that to `concat`,`uglify`, or `cssmin`.

```js
grunt.initConfig({
Expand Down
5 changes: 4 additions & 1 deletion tasks/dom_munger.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ module.exports = function(grunt) {
if (!options.read.selector || !options.read.attribute || !options.read.writeto){
grunt.log.error('Read config missing selector, attribute, and/or writeto options');
} else {
if(!options.read.filter) {
options.read.filter = function(val) {return val;};
}
var vals = $.map($(options.read.selector),function(elem){
return $(elem).attr(options.read.attribute);
return options.read.filter($(elem).attr(options.read.attribute));
});

if (options.read.isPath){
Expand Down