addAttributesToSVGElement.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. exports.type = 'full';
  3. exports.active = false;
  4. exports.description = 'adds attributes to an outer <svg> element';
  5. var ENOCLS = 'Error in plugin "addAttributesToSVGElement": absent parameters.\n\
  6. It should have a list of classes in "attributes" or one "attribute".\n\
  7. Config example:\n\n\
  8. \
  9. plugins:\n\
  10. - addAttributesToSVGElement:\n\
  11. attribute: "mySvg"\n\n\
  12. \
  13. plugins:\n\
  14. - addAttributesToSVGElement:\n\
  15. attributes: ["mySvg", "size-big"]\n';
  16. /**
  17. * Add attributes to an outer <svg> element. Example config:
  18. *
  19. * plugins:
  20. * - addAttributesToSVGElement:
  21. * attribute: 'data-icon'
  22. *
  23. * plugins:
  24. * - addAttributesToSVGElement:
  25. * attributes: ['data-icon', 'data-disabled']
  26. *
  27. * @author April Arcus
  28. */
  29. exports.fn = function(data, params) {
  30. if (!params || !(Array.isArray(params.attributes) && params.attributes.some(String) || params.attribute)) {
  31. console.error(ENOCLS);
  32. return data;
  33. }
  34. var attributes = params.attributes || [ params.attribute ],
  35. svg = data.content[0];
  36. if (svg.isElem('svg')) {
  37. attributes.forEach(function (attribute) {
  38. if (!svg.hasAttr(attribute)) {
  39. svg.addAttr({
  40. name: attribute,
  41. prefix: '',
  42. local: attribute
  43. });
  44. }
  45. });
  46. }
  47. return data;
  48. };