source: trip-planner-front/node_modules/svgo/plugins/mergePaths.js@ e29cc2e

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

initial commit

  • Property mode set to 100644
File size: 2.9 KB
Line 
1'use strict';
2
3const { detachNodeFromParent } = require('../lib/xast.js');
4const { collectStylesheet, computeStyle } = require('../lib/style.js');
5const { path2js, js2path, intersects } = require('./_path.js');
6
7exports.type = 'visitor';
8exports.name = 'mergePaths';
9exports.active = true;
10exports.description = 'merges multiple paths in one if possible';
11
12/**
13 * Merge multiple Paths into one.
14 *
15 * @author Kir Belevich, Lev Solntsev
16 *
17 * @type {import('../lib/types').Plugin<{
18 * force?: boolean,
19 * floatPrecision?: number,
20 * noSpaceAfterFlags?: boolean
21 * }>}
22 */
23exports.fn = (root, params) => {
24 const {
25 force = false,
26 floatPrecision,
27 noSpaceAfterFlags = false, // a20 60 45 0 1 30 20 → a20 60 45 0130 20
28 } = params;
29 const stylesheet = collectStylesheet(root);
30
31 return {
32 element: {
33 enter: (node) => {
34 let prevChild = null;
35
36 for (const child of node.children) {
37 // skip if previous element is not path or contains animation elements
38 if (
39 prevChild == null ||
40 prevChild.type !== 'element' ||
41 prevChild.name !== 'path' ||
42 prevChild.children.length !== 0 ||
43 prevChild.attributes.d == null
44 ) {
45 prevChild = child;
46 continue;
47 }
48
49 // skip if element is not path or contains animation elements
50 if (
51 child.type !== 'element' ||
52 child.name !== 'path' ||
53 child.children.length !== 0 ||
54 child.attributes.d == null
55 ) {
56 prevChild = child;
57 continue;
58 }
59
60 // preserve paths with markers
61 const computedStyle = computeStyle(stylesheet, child);
62 if (
63 computedStyle['marker-start'] ||
64 computedStyle['marker-mid'] ||
65 computedStyle['marker-end']
66 ) {
67 prevChild = child;
68 continue;
69 }
70
71 const prevChildAttrs = Object.keys(prevChild.attributes);
72 const childAttrs = Object.keys(child.attributes);
73 let attributesAreEqual = prevChildAttrs.length === childAttrs.length;
74 for (const name of childAttrs) {
75 if (name !== 'd') {
76 if (
77 prevChild.attributes[name] == null ||
78 prevChild.attributes[name] !== child.attributes[name]
79 ) {
80 attributesAreEqual = false;
81 }
82 }
83 }
84 const prevPathJS = path2js(prevChild);
85 const curPathJS = path2js(child);
86
87 if (
88 attributesAreEqual &&
89 (force || !intersects(prevPathJS, curPathJS))
90 ) {
91 js2path(prevChild, prevPathJS.concat(curPathJS), {
92 floatPrecision,
93 noSpaceAfterFlags,
94 });
95 detachNodeFromParent(child, node);
96 continue;
97 }
98
99 prevChild = child;
100 }
101 },
102 },
103 };
104};
Note: See TracBrowser for help on using the repository browser.