source: trip-planner-front/node_modules/svgo/plugins/addClassesToSVGElement.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[6a3a178]1'use strict';
2
3exports.name = 'addClassesToSVGElement';
4exports.type = 'visitor';
5exports.active = false;
6exports.description = 'adds classnames to an outer <svg> element';
7
8var ENOCLS = `Error in plugin "addClassesToSVGElement": absent parameters.
9It should have a list of classes in "classNames" or one "className".
10Config example:
11
12plugins: [
13 {
14 name: "addClassesToSVGElement",
15 params: {
16 className: "mySvg"
17 }
18 }
19]
20
21plugins: [
22 {
23 name: "addClassesToSVGElement",
24 params: {
25 classNames: ["mySvg", "size-big"]
26 }
27 }
28]
29`;
30
31/**
32 * Add classnames to an outer <svg> element. Example config:
33 *
34 * plugins: [
35 * {
36 * name: "addClassesToSVGElement",
37 * params: {
38 * className: "mySvg"
39 * }
40 * }
41 * ]
42 *
43 * plugins: [
44 * {
45 * name: "addClassesToSVGElement",
46 * params: {
47 * classNames: ["mySvg", "size-big"]
48 * }
49 * }
50 * ]
51 *
52 * @author April Arcus
53 *
54 * @type {import('../lib/types').Plugin<{
55 * className?: string,
56 * classNames?: Array<string>
57 * }>}
58 */
59exports.fn = (root, params) => {
60 if (
61 !(Array.isArray(params.classNames) && params.classNames.some(String)) &&
62 !params.className
63 ) {
64 console.error(ENOCLS);
65 return null;
66 }
67 const classNames = params.classNames || [params.className];
68 return {
69 element: {
70 enter: (node, parentNode) => {
71 if (node.name === 'svg' && parentNode.type === 'root') {
72 const classList = new Set(
73 node.attributes.class == null
74 ? null
75 : node.attributes.class.split(' ')
76 );
77 for (const className of classNames) {
78 if (className != null) {
79 classList.add(className);
80 }
81 }
82 node.attributes.class = Array.from(classList).join(' ');
83 }
84 },
85 },
86 };
87};
Note: See TracBrowser for help on using the repository browser.