diff --git a/Gruntfile.js b/Gruntfile.js index 59915f6..0360e16 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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. diff --git a/tasks/svgstore.js b/tasks/svgstore.js index 8373acd..d4ab4ae 100644 --- a/tasks/svgstore.js +++ b/tasks/svgstore.js @@ -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('', { lowerCaseAttributeNames : false }), $resultSvg = $resultDocument('svg'), @@ -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); + } } }); }); diff --git a/test/expected/cleanup_fill.svg b/test/expected/cleanup_fill.svg new file mode 100644 index 0000000..a128bdb --- /dev/null +++ b/test/expected/cleanup_fill.svg @@ -0,0 +1,12 @@ +dribbble \ No newline at end of file diff --git a/test/expected/defs_clean.svg b/test/expected/defs_clean.svg new file mode 100644 index 0000000..51d020e --- /dev/null +++ b/test/expected/defs_clean.svg @@ -0,0 +1 @@ + usingdef \ No newline at end of file diff --git a/test/expected/defs_noclean.svg b/test/expected/defs_noclean.svg new file mode 100644 index 0000000..e96ff22 --- /dev/null +++ b/test/expected/defs_noclean.svg @@ -0,0 +1 @@ + usingdef \ No newline at end of file diff --git a/test/expected/no_unref_ids.svg b/test/expected/no_unref_ids.svg new file mode 100644 index 0000000..51d020e --- /dev/null +++ b/test/expected/no_unref_ids.svg @@ -0,0 +1 @@ + usingdef \ No newline at end of file diff --git a/test/svgstore_test.js b/test/svgstore_test.js index 36bc36b..c547c6b 100644 --- a/test/svgstore_test.js +++ b/test/svgstore_test.js @@ -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 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 should be removed'); + + test.done(); + } + +};