defs.mjs 867 B

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