Skip to content

Commit

Permalink
Merge pull request #42 from Frank3K/master
Browse files Browse the repository at this point in the history
Remove empty g elements
  • Loading branch information
FWeinb committed Jul 24, 2014
2 parents 904ff1e + 8d66865 commit 4e6c83b
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 6 deletions.
38 changes: 37 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,48 @@ 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']
}
},

removeemptyg: {
files: {
'tmp/no_empty_g.svg': ['test/fixtures/scissors.svg']
}
}

},

// Unit tests.
Expand Down
25 changes: 22 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 @@ -73,6 +82,14 @@ module.exports = function (grunt) {
xmlMode: true
});

// Remove empty g elements
$('g').each(function(){
var $elem = $(this);
if (!$elem.children().length) {
$elem.remove();
}
});

// Map to store references from id to uniqueId + id;
// N.B.: only IDs that are referenced are mapped.
var mappedIds = {};
Expand All @@ -98,8 +115,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.
2 changes: 1 addition & 1 deletion test/expected/includedemo-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
c0,3.164,2.394,5.812,5.536,6.136C30.838,48.92,51.08,69.161,53.672,94.462c0.322,3.146,2.973,5.539,6.135,5.539h3.098
c1.74,0,3.398-0.732,4.566-2.023c1.168-1.286,1.738-3.009,1.571-4.74C65.841,60.349,39.65,34.158,6.764,30.957z"/> <path d="M99.986,93.431C96.732,43.428,56.568,3.268,6.565,0.012C6.432,0.005,6.298,0,6.166,0C4.604,0,3.095,0.593,1.95,1.667
C0.705,2.832,0,4.461,0,6.167v3.088c0,3.247,2.515,5.938,5.752,6.155c42.113,2.832,76.006,36.726,78.839,78.84
c0.22,3.235,2.907,5.75,6.151,5.75h3.091c1.706,0,3.335-0.706,4.5-1.95C99.498,96.805,100.098,95.135,99.986,93.431z"/> </g> </symbol><symbol viewBox="0 0 100 100" id="scissors"><title>scissors</title> <g> </g> <g> <path d="M88.781,68.58c-4.819-6.068-13.189-8.268-20.355-6.131l-7.984-10.054L86.98,13.793c2.142-3.177,1.713-7.589-0.952-9.786
c0.22,3.235,2.907,5.75,6.151,5.75h3.091c1.706,0,3.335-0.706,4.5-1.95C99.498,96.805,100.098,95.135,99.986,93.431z"/> </g> </symbol><symbol viewBox="0 0 100 100" id="scissors"><title>scissors</title> <g> <path d="M88.781,68.58c-4.819-6.068-13.189-8.268-20.355-6.131l-7.984-10.054L86.98,13.793c2.142-3.177,1.713-7.589-0.952-9.786
C83.359,1.808,81.184,0,81.184,0L50.001,39.257L18.818,0c0,0-2.176,1.808-4.842,4.007c-2.665,2.197-3.095,6.609-0.954,9.786
l26.541,38.603l-7.987,10.054c-7.174-2.137-15.536,0.062-20.354,6.131c-3.182,3.998-4.641,9.024-4.117,14.147
c0.531,5.122,2.991,9.732,6.912,12.973c3.359,2.772,7.586,4.3,11.898,4.3c5.738,0,11.095-2.602,14.71-7.148
Expand Down
13 changes: 13 additions & 0 deletions test/expected/no_empty_g.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 43 additions & 1 deletion test/svgstore_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,46 @@ 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();
},

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

var actual = grunt.file.read('tmp/no_empty_g.svg');
var expected = grunt.file.read('test/expected/no_empty_g.svg');
test.equal(actual, expected, 'All empty g elements should be removed');

test.done();
}

};

0 comments on commit 4e6c83b

Please sign in to comment.