From f2500bc58aa4cb42857a333a23961cc50bf7eaae Mon Sep 17 00:00:00 2001 From: Rob Levin Date: Tue, 4 Nov 2014 22:42:49 -0800 Subject: [PATCH 1/3] Preserve attribute feature --- Gruntfile.js | 24 ++++++++++++++++-------- tasks/svgstore.js | 26 ++++++++++++++++++++------ test/expected/includedemo-demo.html | 5 ++++- test/expected/preserve_attribute.svg | 1 + test/fixtures/preserve_attribute.svg | 3 +++ test/svgstore_test.js | 10 ++++++++++ 6 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 test/expected/preserve_attribute.svg create mode 100644 test/fixtures/preserve_attribute.svg diff --git a/Gruntfile.js b/Gruntfile.js index ad758d4..9129002 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -18,16 +18,16 @@ module.exports = function(grunt) { all: [ 'Gruntfile.js', 'tasks/*.js', - '<%= nodeunit.tests %>', + '<%= nodeunit.tests %>' ], options: { - jshintrc: '.jshintrc', + jshintrc: '.jshintrc' }, }, // Before generating any new files, remove any previously-created files. clean: { - tests: ['tmp'], + tests: ['tmp'] }, // Configuration to be run (and then tested). @@ -41,7 +41,7 @@ module.exports = function(grunt) { }, prefix: { - options:{ + options: { prefix: 'icon-' }, files: { @@ -50,7 +50,7 @@ module.exports = function(grunt) { }, svgattr: { - options:{ + options: { svg: { viewBox : '0 0 100 100' } @@ -129,7 +129,15 @@ module.exports = function(grunt) { 'tmp/cleanup_with_currentColor.svg': ['test/fixtures/cleanup_with_currentColor.svg'] } }, - + + preserveattribute: { + options: { + cleanup: ['fill', 'stroke', 'imafake'] + }, + files: { + 'tmp/preserve_attribute.svg': ['test/fixtures/preserve_attribute.svg'] + } + }, cleanupwithinheritviewbox: { options: { cleanup: true, @@ -251,8 +259,8 @@ module.exports = function(grunt) { // Unit tests. nodeunit: { - tests: ['test/*_test.js'], - }, + tests: ['test/*_test.js'] + } }); diff --git a/tasks/svgstore.js b/tasks/svgstore.js index 2a3fa43..eff998c 100644 --- a/tasks/svgstore.js +++ b/tasks/svgstore.js @@ -143,7 +143,7 @@ module.exports = function (grunt) { Object.keys(attrs).forEach(function (key) { var value = attrs[key]; - var id, match; + var id, match, newKey = ''; while ( (match = urlPattern.exec(value)) !== null){ id = match[1]; @@ -164,14 +164,28 @@ module.exports = function (grunt) { // IDs are handled separately if (key !== 'id') { + if (options.cleanupdefs || !$elem.parents('defs').length) { - if (cleanupAttributes.indexOf(key) > -1){ - - // Letting fill inherit the `currentColor` allows shared inline defs to - // be styled differently based on an xlink element's `color` so we leave these - if (!(key === 'fill' && $elem.attr('fill') === 'currentColor')) { + + if (key.match(/preserve--/)) { + //Strip off the preserve-- + newKey = key.substring(10); + } + + if (cleanupAttributes.indexOf(key) > -1 || cleanupAttributes.indexOf(newKey) > -1){ + + if (newKey && newKey.length) { + //Add the new key preserving value + $elem.attr(newKey, $elem.attr(key)); + + //Remove the old preserve--foo key $elem.removeAttr(key); } + else if (!(key === 'fill' && $elem.attr('fill') === 'currentColor')) { + // Letting fill inherit the `currentColor` allows shared inline defs to + // be styled differently based on an xlink element's `color` so we leave these + $elem.removeAttr(key); + } } } } diff --git a/test/expected/includedemo-demo.html b/test/expected/includedemo-demo.html index 758cb50..6da850b 100644 --- a/test/expected/includedemo-demo.html +++ b/test/expected/includedemo-demo.html @@ -91,7 +91,7 @@ c8.147-1.632,14.055-7.437,14.764-13.808H91V0L34.654,21.67z"/> menu-pill menu pinboard rss preserve_attribute rss + + + diff --git a/test/expected/preserve_attribute.svg b/test/expected/preserve_attribute.svg new file mode 100644 index 0000000..38e59e7 --- /dev/null +++ b/test/expected/preserve_attribute.svg @@ -0,0 +1 @@ +preserve_attribute \ No newline at end of file diff --git a/test/fixtures/preserve_attribute.svg b/test/fixtures/preserve_attribute.svg new file mode 100644 index 0000000..b8705c3 --- /dev/null +++ b/test/fixtures/preserve_attribute.svg @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/svgstore_test.js b/test/svgstore_test.js index 8cf06a0..7bc282c 100644 --- a/test/svgstore_test.js +++ b/test/svgstore_test.js @@ -171,6 +171,16 @@ exports.svgstore = { test.done(); }, + cleanup_preserve_leaves_attributes: function(test) { + test.expect(1); + + var actual = grunt.file.read('tmp/preserve_attribute.svg'); + var expected = grunt.file.read('test/expected/preserve_attribute.svg'); + test.equal(actual, expected, 'attribute with value of preserve-- should strip preserve'); + + test.done(); + }, + cleanup_with_inheritviewbox: function(test) { test.expect(1); From 16e4f23e59b6401c70782d79f2bf8963fc53ba53 Mon Sep 17 00:00:00 2001 From: Rob Levin Date: Tue, 4 Nov 2014 22:47:25 -0800 Subject: [PATCH 2/3] Better variable name --- tasks/svgstore.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tasks/svgstore.js b/tasks/svgstore.js index eff998c..9b5dd25 100644 --- a/tasks/svgstore.js +++ b/tasks/svgstore.js @@ -143,7 +143,7 @@ module.exports = function (grunt) { Object.keys(attrs).forEach(function (key) { var value = attrs[key]; - var id, match, newKey = ''; + var id, match, preservedKey = ''; while ( (match = urlPattern.exec(value)) !== null){ id = match[1]; @@ -169,14 +169,14 @@ module.exports = function (grunt) { if (key.match(/preserve--/)) { //Strip off the preserve-- - newKey = key.substring(10); + preservedKey = key.substring(10); } - if (cleanupAttributes.indexOf(key) > -1 || cleanupAttributes.indexOf(newKey) > -1){ + if (cleanupAttributes.indexOf(key) > -1 || cleanupAttributes.indexOf(preservedKey) > -1){ - if (newKey && newKey.length) { + if (preservedKey && preservedKey.length) { //Add the new key preserving value - $elem.attr(newKey, $elem.attr(key)); + $elem.attr(preservedKey, $elem.attr(key)); //Remove the old preserve--foo key $elem.removeAttr(key); From 0faed283b7009dcb1141fd6670155b6835592ac1 Mon Sep 17 00:00:00 2001 From: Rob Levin Date: Wed, 5 Nov 2014 15:51:44 -0800 Subject: [PATCH 3/3] Handles cases where NOT configured for cleanup, but we still remove the preserve-- prefix. Also prunes trailing spaces --- tasks/svgstore.js | 22 +++++++++++++++------- test/expected/includedemo-demo.html | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/tasks/svgstore.js b/tasks/svgstore.js index 9b5dd25..ba6c993 100644 --- a/tasks/svgstore.js +++ b/tasks/svgstore.js @@ -164,20 +164,20 @@ module.exports = function (grunt) { // IDs are handled separately if (key !== 'id') { - + if (options.cleanupdefs || !$elem.parents('defs').length) { - + if (key.match(/preserve--/)) { //Strip off the preserve-- preservedKey = key.substring(10); } - - if (cleanupAttributes.indexOf(key) > -1 || cleanupAttributes.indexOf(preservedKey) > -1){ - + + if (cleanupAttributes.indexOf(key) > -1 || cleanupAttributes.indexOf(preservedKey) > -1){ + if (preservedKey && preservedKey.length) { //Add the new key preserving value $elem.attr(preservedKey, $elem.attr(key)); - + //Remove the old preserve--foo key $elem.removeAttr(key); } @@ -185,7 +185,15 @@ module.exports = function (grunt) { // Letting fill inherit the `currentColor` allows shared inline defs to // be styled differently based on an xlink element's `color` so we leave these $elem.removeAttr(key); - } + } + } else { + if (preservedKey && preservedKey.length) { + //Add the new key preserving value + $elem.attr(preservedKey, $elem.attr(key)); + + //Remove the old preserve--foo key + $elem.removeAttr(key); + } } } } diff --git a/test/expected/includedemo-demo.html b/test/expected/includedemo-demo.html index 6da850b..78ae86a 100644 --- a/test/expected/includedemo-demo.html +++ b/test/expected/includedemo-demo.html @@ -91,7 +91,7 @@ c8.147-1.632,14.055-7.437,14.764-13.808H91V0L34.654,21.67z"/> menu-pill menu pinboard preserve_attribute rss preserve_attribute rss