move-from-symbol-to-root.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. const traverse = require('traverse');
  2. const clone = require('clone');
  3. // Fixes Firefox bug https://bugzilla.mozilla.org/show_bug.cgi?id=353575
  4. const defaultConfig = {
  5. tags: ['linearGradient', 'radialGradient', 'pattern', 'clipPath', 'mask']
  6. };
  7. function moveFromSymbolToRoot(config = null) {
  8. const cfg = Object.assign({}, defaultConfig, config);
  9. return (tree) => {
  10. traverse(tree).forEach(function (node) {
  11. if (!this.isLeaf && node.tag && node.tag === 'symbol') {
  12. const symbol = this.parent.node;
  13. const nodesToRemove = [];
  14. traverse(node.content).forEach(function (n) {
  15. if (!this.isLeaf && n.tag && cfg.tags.indexOf(n.tag) !== -1) {
  16. const parent = this.parent.node;
  17. const cloned = clone(this.node);
  18. symbol.push(cloned);
  19. nodesToRemove.push({ parent, node: n });
  20. }
  21. });
  22. nodesToRemove.forEach((item) => {
  23. const nodeIndex = item.parent.indexOf(item.node);
  24. item.parent.splice(nodeIndex, 1);
  25. });
  26. }
  27. });
  28. return tree;
  29. };
  30. }
  31. module.exports = moveFromSymbolToRoot;