1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-29 20:21:14 +03:00

Updated addElementsToSVGElement plugin to support attribute values, as well as attribute names.

This commit is contained in:
Keegan Street
2017-10-25 11:20:29 +11:00
committed by Lev Solntsev
parent 239f383f3b
commit eceb95c310
2 changed files with 46 additions and 8 deletions

View File

@ -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:
@ -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:
@ -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;
}
@ -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
});
}
});
}
});