Skip to content

Commit

Permalink
Updated addElementsToSVGElement plugin to support attribute values, a…
Browse files Browse the repository at this point in the history
…s well as attribute names.
  • Loading branch information
Keegan Street authored and GreLI committed Oct 25, 2017
1 parent 239f383 commit eceb95c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
41 changes: 33 additions & 8 deletions plugins/addAttributesToSVGElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exports.active = false;
exports.description = 'adds attributes to an outer <svg> element';

var ENOCLS = `Error in plugin "addAttributesToSVGElement": absent parameters.
It should have a list of classes in "attributes" or one "attribute".
It should have a list of "attributes" or one "attribute".
Config example:
plugins:
Expand All @@ -16,7 +16,13 @@ plugins:
plugins:
- addAttributesToSVGElement:
attributes: ["mySvg", "size-big"]`;
attributes: ["mySvg", "size-big"]
plugins:
- addAttributesToSVGElement:
attributes:
- focusable: false
- data-image: icon`;

/**
* Add attributes to an outer <svg> element. Example config:
Expand All @@ -29,10 +35,16 @@ plugins:
* - addAttributesToSVGElement:
* attributes: ['data-icon', 'data-disabled']
*
* plugins:
* - addAttributesToSVGElement:
* attributes:
* - focusable: false
* - data-image: icon
*
* @author April Arcus
*/
exports.fn = function(data, params) {
if (!params || !(Array.isArray(params.attributes) && params.attributes.some(String) || params.attribute)) {
if (!params || !(Array.isArray(params.attributes) || params.attribute)) {
console.error(ENOCLS);
return data;
}
Expand All @@ -42,11 +54,24 @@ exports.fn = function(data, params) {

if (svg.isElem('svg')) {
attributes.forEach(function (attribute) {
if (!svg.hasAttr(attribute)) {
svg.addAttr({
name: attribute,
prefix: '',
local: attribute
if (typeof attribute === 'string') {
if (!svg.hasAttr(attribute)) {
svg.addAttr({
name: attribute,
prefix: '',
local: attribute
});
}
} else if (typeof attribute === 'object') {
Object.keys(attribute).forEach(function (key) {
if (!svg.hasAttr(key)) {
svg.addAttr({
name: key,
value: attribute[key],
prefix: '',
local: key
});
}
});
}
});
Expand Down
13 changes: 13 additions & 0 deletions test/plugins/addAttributesToSVGElement.03.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 eceb95c

Please sign in to comment.