convertEllipseToCircle.js 938 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. exports.name = 'convertEllipseToCircle';
  3. exports.type = 'visitor';
  4. exports.active = true;
  5. exports.description = 'converts non-eccentric <ellipse>s to <circle>s';
  6. /**
  7. * Converts non-eccentric <ellipse>s to <circle>s.
  8. *
  9. * @see https://www.w3.org/TR/SVG11/shapes.html
  10. *
  11. * @author Taylor Hunt
  12. *
  13. * @type {import('../lib/types').Plugin<void>}
  14. */
  15. exports.fn = () => {
  16. return {
  17. element: {
  18. enter: (node) => {
  19. if (node.name === 'ellipse') {
  20. const rx = node.attributes.rx || '0';
  21. const ry = node.attributes.ry || '0';
  22. if (
  23. rx === ry ||
  24. rx === 'auto' ||
  25. ry === 'auto' // SVG2
  26. ) {
  27. node.name = 'circle';
  28. const radius = rx === 'auto' ? ry : rx;
  29. delete node.attributes.rx;
  30. delete node.attributes.ry;
  31. node.attributes.r = radius;
  32. }
  33. }
  34. },
  35. },
  36. };
  37. };