Skip to content

Commit

Permalink
Added option 'cleanupdefs' (default: false); When set to true no clea…
Browse files Browse the repository at this point in the history
…nup is done on elements inside the <defs> element.

Changed option 'cleanup'; it now accepts an array of attributes. For backwards compatibility cleanup === true equals ['style']. The attributes in the array are removed from all elements.
  • Loading branch information
Frank3K committed Jun 28, 2014
1 parent 7fc96d1 commit d0a4c39
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 5 deletions.
32 changes: 31 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,42 @@ module.exports = function(grunt) {

removeunreferencedids: {
options: {
cleanup: true
cleanup: true,
cleanupdefs: true
},
files: {
'tmp/no_unref_ids.svg': ['test/fixtures/usingdef.svg']
}
},

cleanupfill: {
options: {
cleanup: ['fill']
},
files: {
'tmp/cleanup_fill.svg': ['test/fixtures/dribbble.svg']
}
},

nocleandefs: {
options: {
cleanup: ['style']
},
files: {
'tmp/defs_noclean.svg': ['test/fixtures/usingdef.svg']
}
},

cleandefs: {
options: {
cleanup: ['style'],
cleanupdefs: true
},
files: {
'tmp/defs_clean.svg': ['test/fixtures/usingdef.svg']
}
}

},

// Unit tests.
Expand Down
17 changes: 14 additions & 3 deletions tasks/svgstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,18 @@ module.exports = function (grunt) {
},
formatting: false,
includedemo: false,
symbol: {}
symbol: {},
cleanupdefs: false
});

var cleanupAttributes = [];
if (options.cleanup && typeof options.cleanup === 'boolean') {
// For backwards compatibility (introduced in 0.2.6).
cleanupAttributes = ['style'];
} else if (Array.isArray(options.cleanup)){
cleanupAttributes = options.cleanup;
}

this.files.forEach(function (file) {
var $resultDocument = cheerio.load('<svg><defs></defs></svg>', { lowerCaseAttributeNames : false }),
$resultSvg = $resultDocument('svg'),
Expand Down Expand Up @@ -95,8 +104,10 @@ module.exports = function (grunt) {
$elem.attr(key, value.replace(match[0], 'url(#' + mappedIds[refId] + ')'));
}

if (options.cleanup && key === 'style') {
$elem.removeAttr(key);
if (options.cleanupdefs || !$elem.parents('defs').length) {
if (cleanupAttributes.indexOf(key) > -1){
$elem.removeAttr(key);
}
}
});
});
Expand Down
12 changes: 12 additions & 0 deletions test/expected/cleanup_fill.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/expected/defs_clean.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/expected/defs_noclean.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/expected/no_unref_ids.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 33 additions & 1 deletion test/svgstore_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,36 @@ exports.svgstore = {
test.equal(actual, expected, 'Remove unreferenced IDs');

test.done();
}};
},

with_cleanup_fill: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/cleanup_fill.svg');
var expected = grunt.file.read('test/expected/cleanup_fill.svg');
test.equal(actual, expected, 'All fill attributes should be removed');

test.done();
},

with_cleanupdefs: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/defs_clean.svg');
var expected = grunt.file.read('test/expected/defs_clean.svg');
test.equal(actual, expected, 'All style attributes inside <defs> should be removed');

test.done();
},

without_cleanupdefs: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/defs_noclean.svg');
var expected = grunt.file.read('test/expected/defs_noclean.svg');
test.equal(actual, expected, 'All style attributes inside <defs> should be removed');

test.done();
}

};

0 comments on commit d0a4c39

Please sign in to comment.