mirror of
https://github.com/svg/svgo.git
synced 2025-08-06 04:22:39 +03:00
Updated addElementsToSVGElement plugin to support attribute values, as well as attribute names.
This commit is contained in:
committed by
Lev Solntsev
parent
239f383f3b
commit
eceb95c310
@@ -7,7 +7,7 @@ exports.active = false;
|
|||||||
exports.description = 'adds attributes to an outer <svg> element';
|
exports.description = 'adds attributes to an outer <svg> element';
|
||||||
|
|
||||||
var ENOCLS = `Error in plugin "addAttributesToSVGElement": absent parameters.
|
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:
|
Config example:
|
||||||
|
|
||||||
plugins:
|
plugins:
|
||||||
@@ -16,7 +16,13 @@ plugins:
|
|||||||
|
|
||||||
plugins:
|
plugins:
|
||||||
- addAttributesToSVGElement:
|
- 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:
|
* Add attributes to an outer <svg> element. Example config:
|
||||||
@@ -29,10 +35,16 @@ plugins:
|
|||||||
* - addAttributesToSVGElement:
|
* - addAttributesToSVGElement:
|
||||||
* attributes: ['data-icon', 'data-disabled']
|
* attributes: ['data-icon', 'data-disabled']
|
||||||
*
|
*
|
||||||
|
* plugins:
|
||||||
|
* - addAttributesToSVGElement:
|
||||||
|
* attributes:
|
||||||
|
* - focusable: false
|
||||||
|
* - data-image: icon
|
||||||
|
*
|
||||||
* @author April Arcus
|
* @author April Arcus
|
||||||
*/
|
*/
|
||||||
exports.fn = function(data, params) {
|
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);
|
console.error(ENOCLS);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
@@ -42,6 +54,7 @@ exports.fn = function(data, params) {
|
|||||||
|
|
||||||
if (svg.isElem('svg')) {
|
if (svg.isElem('svg')) {
|
||||||
attributes.forEach(function (attribute) {
|
attributes.forEach(function (attribute) {
|
||||||
|
if (typeof attribute === 'string') {
|
||||||
if (!svg.hasAttr(attribute)) {
|
if (!svg.hasAttr(attribute)) {
|
||||||
svg.addAttr({
|
svg.addAttr({
|
||||||
name: attribute,
|
name: attribute,
|
||||||
@@ -49,6 +62,18 @@ exports.fn = function(data, params) {
|
|||||||
local: attribute
|
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
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
test/plugins/addAttributesToSVGElement.03.svg
Normal file
13
test/plugins/addAttributesToSVGElement.03.svg
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
test
|
||||||
|
</svg>
|
||||||
|
|
||||||
|
@@@
|
||||||
|
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg" focusable="false" data-image="icon">
|
||||||
|
test
|
||||||
|
</svg>
|
||||||
|
|
||||||
|
@@@
|
||||||
|
|
||||||
|
{"attributes":[{"focusable":"false"},{"data-image":"icon"}]}
|
After Width: | Height: | Size: 223 B |
Reference in New Issue
Block a user