defs.cjs 949 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. function splitSVGDefs(content, tag = "defs") {
  3. let defs = "";
  4. const index = content.indexOf("<" + tag);
  5. while (index >= 0) {
  6. const start = content.indexOf(">", index);
  7. const end = content.indexOf("</" + tag);
  8. if (start === -1 || end === -1) {
  9. break;
  10. }
  11. const endEnd = content.indexOf(">", end);
  12. if (endEnd === -1) {
  13. break;
  14. }
  15. defs += content.slice(start + 1, end).trim();
  16. content = content.slice(0, index).trim() + content.slice(endEnd + 1);
  17. }
  18. return {
  19. defs,
  20. content
  21. };
  22. }
  23. function mergeDefsAndContent(defs, content) {
  24. return defs ? "<defs>" + defs + "</defs>" + content : content;
  25. }
  26. function wrapSVGContent(body, start, end) {
  27. const split = splitSVGDefs(body);
  28. return mergeDefsAndContent(split.defs, start + split.content + end);
  29. }
  30. exports.mergeDefsAndContent = mergeDefsAndContent;
  31. exports.splitSVGDefs = splitSVGDefs;
  32. exports.wrapSVGContent = wrapSVGContent;