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

Preserve attribute feature #71

Merged
merged 3 commits into from
Nov 8, 2014
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 16 additions & 8 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -41,7 +41,7 @@ module.exports = function(grunt) {
},

prefix: {
options:{
options: {
prefix: 'icon-'
},
files: {
Expand All @@ -50,7 +50,7 @@ module.exports = function(grunt) {
},

svgattr: {
options:{
options: {
svg: {
viewBox : '0 0 100 100'
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -251,8 +259,8 @@ module.exports = function(grunt) {

// Unit tests.
nodeunit: {
tests: ['test/*_test.js'],
},
tests: ['test/*_test.js']
}

});

Expand Down
26 changes: 20 additions & 6 deletions tasks/svgstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ module.exports = function (grunt) {

Object.keys(attrs).forEach(function (key) {
var value = attrs[key];
var id, match;
var id, match, preservedKey = '';

while ( (match = urlPattern.exec(value)) !== null){
id = match[1];
Expand All @@ -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--
preservedKey = key.substring(10);
}

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);
}
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);
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion test/expected/includedemo-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
c8.147-1.632,14.055-7.437,14.764-13.808H91V0L34.654,21.67z"/> </symbol><symbol viewBox="0 0 44 39" id="nestedsymbol"><title>menu-pill</title> <symbol viewBox="0 0 44 6" id="nestedsymbol-menu-pill"> <rect fill="currentColor" x="0" y="0" width="44px" height="5px" rx="3px" ry="3px"/></symbol> <text transform="matrix(1 0 0 1 0.1514 38)" fill="currentColor" font-family="&apos;Neue Helvetica eText W01&apos;, sans-serif" font-size="15.5">menu</text> <use xlink:href="#nestedsymbol-menu-pill" width="44" height="6" x="-22" y="-3" transform="matrix(1 0 0 -1 22 21)" overflow="visible"/> <use xlink:href="#nestedsymbol-menu-pill" width="44" height="6" x="-22" y="-3" transform="matrix(1 0 0 -1 22 12)" overflow="visible"/> <use xlink:href="#nestedsymbol-menu-pill" width="44" height="6" x="-22" y="-3" transform="matrix(1 0 0 -1 22 3)" overflow="visible"/> </symbol><symbol viewBox="0 0 100 100" id="pinboard"><title>pinboard</title> <path d="M51.447,59.185c0,0-12.542,12.915-18.583,17.828c-3.561-7.274,2.275-18.725,2.275-18.725l-23.604-30.95
c0,0-7.694,3.517-11.535,2.013C9.471,19.252,19.254,9.47,29.352,0c1.434,3.759-2.012,11.283-2.012,11.283l30.701,23.854
c0,0,11.578-5.927,18.977-2.275c-4.955,5.917-17.83,18.33-17.83,18.33s27.875,31.664,40.66,48.649c0.353,0.301,0.021,0.098-0.252,0
C83.035,86.802,51.447,59.185,51.447,59.185z"/> </symbol><symbol viewBox="0 0 100 100" id="rss"><title>rss</title> <g> <path d="M7.336,62.163c-0.388-0.075-0.783-0.112-1.171-0.112c-1.42,0-2.81,0.491-3.923,1.409C0.822,64.632,0,66.375,0,68.216
C83.035,86.802,51.447,59.185,51.447,59.185z"/> </symbol><symbol id="preserve_attribute"><title>preserve_attribute</title> <rect x="400" y="100" width="400" height="200" preserve--fill="blue" preserve--stroke="red" preserve--imafake="idunno" stroke-width="10"/> </symbol><symbol viewBox="0 0 100 100" id="rss"><title>rss</title> <g> <path d="M7.336,62.163c-0.388-0.075-0.783-0.112-1.171-0.112c-1.42,0-2.81,0.491-3.923,1.409C0.822,64.632,0,66.375,0,68.216
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The preserve-- prefix should be removed here.

v25.617C0,97.238,2.76,100,6.166,100h25.616c1.84,0,3.586-0.822,4.757-2.243c1.17-1.421,1.646-3.287,1.297-5.098
C34.876,77.378,22.62,65.12,7.336,62.163z"/> <path d="M6.764,30.957c-0.199-0.02-0.399-0.029-0.597-0.029c-1.525,0-3.005,0.563-4.142,1.6C0.733,33.699,0,35.357,0,37.096v3.098
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
Expand Down Expand Up @@ -159,6 +159,9 @@
<svg>
<use xlink:href="#pinboard" />
</svg>
<svg>
<use xlink:href="#preserve_attribute" />
</svg>
<svg>
<use xlink:href="#rss" />
</svg>
Expand Down
1 change: 1 addition & 0 deletions test/expected/preserve_attribute.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/fixtures/preserve_attribute.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions test/svgstore_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down