zhangnaisong
2024-03-23 4532b321444257453a86c0f5289a3a5f576db71e
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
const traverse = require('traverse');
const clone = require('clone');
 
// Fixes Firefox bug https://bugzilla.mozilla.org/show_bug.cgi?id=353575
const defaultConfig = {
  tags: ['linearGradient', 'radialGradient', 'pattern', 'clipPath', 'mask']
};
 
function moveFromSymbolToRoot(config = null) {
  const cfg = Object.assign({}, defaultConfig, config);
 
  return (tree) => {
    traverse(tree).forEach(function (node) {
      if (!this.isLeaf && node.tag && node.tag === 'symbol') {
        const symbol = this.parent.node;
        const nodesToRemove = [];
 
        traverse(node.content).forEach(function (n) {
          if (!this.isLeaf && n.tag && cfg.tags.indexOf(n.tag) !== -1) {
            const parent = this.parent.node;
            const cloned = clone(this.node);
            symbol.push(cloned);
            nodesToRemove.push({ parent, node: n });
          }
        });
 
        nodesToRemove.forEach((item) => {
          const nodeIndex = item.parent.indexOf(item.node);
          item.parent.splice(nodeIndex, 1);
        });
      }
    });
 
    return tree;
  };
}
 
module.exports = moveFromSymbolToRoot;