source: trip-planner-front/node_modules/svgo/plugins/removeEditorsNSData.js@ ceaed42

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

initial commit

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[6a3a178]1'use strict';
2
3const { detachNodeFromParent } = require('../lib/xast.js');
4const { editorNamespaces } = require('./_collections.js');
5
6exports.type = 'visitor';
7exports.name = 'removeEditorsNSData';
8exports.active = true;
9exports.description = 'removes editors namespaces, elements and attributes';
10
11/**
12 * Remove editors namespaces, elements and attributes.
13 *
14 * @example
15 * <svg xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd">
16 * <sodipodi:namedview/>
17 * <path sodipodi:nodetypes="cccc"/>
18 *
19 * @author Kir Belevich
20 *
21 * @type {import('../lib/types').Plugin<{
22 * additionalNamespaces?: Array<string>
23 * }>}
24 */
25exports.fn = (_root, params) => {
26 let namespaces = editorNamespaces;
27 if (Array.isArray(params.additionalNamespaces)) {
28 namespaces = [...editorNamespaces, ...params.additionalNamespaces];
29 }
30 /**
31 * @type {Array<string>}
32 */
33 const prefixes = [];
34 return {
35 element: {
36 enter: (node, parentNode) => {
37 // collect namespace aliases from svg element
38 if (node.name === 'svg') {
39 for (const [name, value] of Object.entries(node.attributes)) {
40 if (name.startsWith('xmlns:') && namespaces.includes(value)) {
41 prefixes.push(name.slice('xmlns:'.length));
42 // <svg xmlns:sodipodi="">
43 delete node.attributes[name];
44 }
45 }
46 }
47 // remove editor attributes, for example
48 // <* sodipodi:*="">
49 for (const name of Object.keys(node.attributes)) {
50 if (name.includes(':')) {
51 const [prefix] = name.split(':');
52 if (prefixes.includes(prefix)) {
53 delete node.attributes[name];
54 }
55 }
56 }
57 // remove editor elements, for example
58 // <sodipodi:*>
59 if (node.name.includes(':')) {
60 const [prefix] = node.name.split(':');
61 if (prefixes.includes(prefix)) {
62 detachNodeFromParent(node, parentNode);
63 }
64 }
65 },
66 },
67 };
68};
Note: See TracBrowser for help on using the repository browser.