Skip to content

Commit

Permalink
Merge pull request svg#546 from gjjones/master
Browse files Browse the repository at this point in the history
adds addAttributes plugin
  • Loading branch information
GreLI authored Aug 25, 2016
2 parents f5a1ecf + 34d9500 commit 1d6c055
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 6 deletions.
17 changes: 12 additions & 5 deletions lib/svgo/js2svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,18 @@ JS2SVG.prototype.createAttrs = function(elem) {

elem.eachAttr(function(attr) {

attrs += ' ' +
attr.name +
this.config.attrStart +
String(attr.value).replace(this.config.regValEntities, this.config.encodeEntity) +
this.config.attrEnd;
if (attr.value !== undefined) {
attrs += ' ' +
attr.name +
this.config.attrStart +
String(attr.value).replace(this.config.regValEntities, this.config.encodeEntity) +
this.config.attrEnd;
}
else {
attrs += ' ' +
attr.name;
}


}, this);

Expand Down
1 change: 0 additions & 1 deletion lib/svgo/jsAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ JSAPI.prototype.renameElem = function(name) {
attr = attr || {};

if (attr.name === undefined ||
attr.value === undefined ||
attr.prefix === undefined ||
attr.local === undefined
) return false;
Expand Down
57 changes: 57 additions & 0 deletions plugins/addAttributesToSVGElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

exports.type = 'full';

exports.active = false;

exports.description = 'adds attributes to an outer <svg> element';

var ENOCLS = 'Error in plugin "addAttributesToSVGElement": absent parameters.\n\
It should have a list of classes in "attributes" or one "attribute".\n\
Config example:\n\n\
\
plugins:\n\
- addAttributesToSVGElement:\n\
attribute: "mySvg"\n\n\
\
plugins:\n\
- addAttributesToSVGElement:\n\
attributes: ["mySvg", "size-big"]\n';

/**
* Add attributes to an outer <svg> element. Example config:
*
* plugins:
* - addAttributesToSVGElement:
* attribute: 'data-icon'
*
* plugins:
* - addAttributesToSVGElement:
* attributes: ['data-icon', 'data-disabled']
*
* @author April Arcus
*/
exports.fn = function(data, params) {
if (!params || !(Array.isArray(params.attributes) && params.attributes.some(String) || params.attribute)) {
console.error(ENOCLS);
return data;
}

var attributes = params.attributes || [ params.attribute ],
svg = data.content[0];

if (svg.isElem('svg')) {
attributes.forEach(function (attribute) {
if (!svg.hasAttr(attribute)) {
svg.addAttr({
name: attribute,
prefix: '',
local: attribute
});
}
});
}

return data;

};
13 changes: 13 additions & 0 deletions test/plugins/addAttributesToSVGElement.01.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/plugins/addAttributesToSVGElement.02.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1d6c055

Please sign in to comment.