source: trip-planner-front/node_modules/svgo/plugins/removeAttrs.js@ 1ad8e64

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

initial commit

  • Property mode set to 100644
File size: 4.1 KB
Line 
1'use strict';
2
3exports.name = 'removeAttrs';
4exports.type = 'visitor';
5exports.active = false;
6exports.description = 'removes specified attributes';
7
8const DEFAULT_SEPARATOR = ':';
9const ENOATTRS = `Warning: The plugin "removeAttrs" requires the "attrs" parameter.
10It should have a pattern to remove, otherwise the plugin is a noop.
11Config example:
12
13plugins: [
14 {
15 name: "removeAttrs",
16 params: {
17 attrs: "(fill|stroke)"
18 }
19 }
20]
21`;
22
23/**
24 * Remove attributes
25 *
26 * @example elemSeparator
27 * format: string
28 *
29 * @example preserveCurrentColor
30 * format: boolean
31 *
32 * @example attrs:
33 *
34 * format: [ element* : attribute* : value* ]
35 *
36 * element : regexp (wrapped into ^...$), single * or omitted > all elements (must be present when value is used)
37 * attribute : regexp (wrapped into ^...$)
38 * value : regexp (wrapped into ^...$), single * or omitted > all values
39 *
40 * examples:
41 *
42 * > basic: remove fill attribute
43 * ---
44 * removeAttrs:
45 * attrs: 'fill'
46 *
47 * > remove fill attribute on path element
48 * ---
49 * attrs: 'path:fill'
50 *
51 * > remove fill attribute on path element where value is none
52 * ---
53 * attrs: 'path:fill:none'
54 *
55 *
56 * > remove all fill and stroke attribute
57 * ---
58 * attrs:
59 * - 'fill'
60 * - 'stroke'
61 *
62 * [is same as]
63 *
64 * attrs: '(fill|stroke)'
65 *
66 * [is same as]
67 *
68 * attrs: '*:(fill|stroke)'
69 *
70 * [is same as]
71 *
72 * attrs: '.*:(fill|stroke)'
73 *
74 * [is same as]
75 *
76 * attrs: '.*:(fill|stroke):.*'
77 *
78 *
79 * > remove all stroke related attributes
80 * ----
81 * attrs: 'stroke.*'
82 *
83 *
84 * @author Benny Schudel
85 *
86 * @type {import('../lib/types').Plugin<{
87 * elemSeparator?: string,
88 * preserveCurrentColor?: boolean,
89 * attrs: string | Array<string>
90 * }>}
91 */
92exports.fn = (root, params) => {
93 if (typeof params.attrs == 'undefined') {
94 console.warn(ENOATTRS);
95 return null;
96 }
97
98 const elemSeparator =
99 typeof params.elemSeparator == 'string'
100 ? params.elemSeparator
101 : DEFAULT_SEPARATOR;
102 const preserveCurrentColor =
103 typeof params.preserveCurrentColor == 'boolean'
104 ? params.preserveCurrentColor
105 : false;
106 const attrs = Array.isArray(params.attrs) ? params.attrs : [params.attrs];
107
108 return {
109 element: {
110 enter: (node) => {
111 for (let pattern of attrs) {
112 // if no element separators (:), assume it's attribute name, and apply to all elements *regardless of value*
113 if (pattern.includes(elemSeparator) === false) {
114 pattern = ['.*', elemSeparator, pattern, elemSeparator, '.*'].join(
115 ''
116 );
117 // if only 1 separator, assume it's element and attribute name, and apply regardless of attribute value
118 } else if (pattern.split(elemSeparator).length < 3) {
119 pattern = [pattern, elemSeparator, '.*'].join('');
120 }
121
122 // create regexps for element, attribute name, and attribute value
123 const list = pattern.split(elemSeparator).map((value) => {
124 // adjust single * to match anything
125 if (value === '*') {
126 value = '.*';
127 }
128 return new RegExp(['^', value, '$'].join(''), 'i');
129 });
130
131 // matches element
132 if (list[0].test(node.name)) {
133 // loop attributes
134 for (const [name, value] of Object.entries(node.attributes)) {
135 const isFillCurrentColor =
136 preserveCurrentColor &&
137 name == 'fill' &&
138 value == 'currentColor';
139 const isStrokeCurrentColor =
140 preserveCurrentColor &&
141 name == 'stroke' &&
142 value == 'currentColor';
143 if (
144 !isFillCurrentColor &&
145 !isStrokeCurrentColor &&
146 // matches attribute name
147 list[1].test(name) &&
148 // matches attribute value
149 list[2].test(value)
150 ) {
151 delete node.attributes[name];
152 }
153 }
154 }
155 }
156 },
157 },
158 };
159};
Note: See TracBrowser for help on using the repository browser.