-
Notifications
You must be signed in to change notification settings - Fork 0
/
addClassesToSVGElement.js
executable file
·64 lines (55 loc) · 1.58 KB
/
addClassesToSVGElement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
exports.type = 'full';
exports.active = false;
exports.description = 'adds classnames to an outer <svg> element';
var ENOCLS = 'Error in plugin "addClassesToSVGElement": absent parameters.\n\
It should have a list of classes in "classNames" or one "className".\n\
Config example:\n\n\
\
plugins:\n\
- addClassesToSVGElement:\n\
className: "mySvg"\n\n\
\
plugins:\n\
- addClassesToSVGElement:\n\
classNames: ["mySvg", "size-big"]\n';
/**
* Add classnames to an outer <svg> element. Example config:
*
* plugins:
* - addClassesToSVGElement:
* className: 'mySvg'
*
* plugins:
* - addClassesToSVGElement:
* classNames: ['mySvg', 'size-big']
*
* @author April Arcus
*/
exports.fn = function(data, params) {
if (!params || !(Array.isArray(params.classNames) && params.classNames.some(String) || params.className)) {
console.error(ENOCLS);
return data;
}
var classNames = params.classNames || [ params.className ],
svg = data.content[0];
if (svg.isElem('svg')) {
if (svg.hasAttr('class')) {
var classes = svg.attr('class').value.split(' ');
classNames.forEach(function(className){
if (classes.indexOf(className) < 0) {
classes.push(className);
}
});
svg.attr('class').value = classes.join(' ');
} else {
svg.addAttr({
name: 'class',
value: classNames.join(' '),
prefix: '',
local: 'class'
});
}
}
return data;
};