You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.8 KiB

4 years ago
  1. 'use strict';
  2. exports.type = 'perItem';
  3. exports.active = true;
  4. exports.description = 'moves some group attributes to the content elements';
  5. var collections = require('./_collections.js'),
  6. pathElems = collections.pathElems.concat(['g', 'text']),
  7. referencesProps = collections.referencesProps;
  8. /**
  9. * Move group attrs to the content elements.
  10. *
  11. * @example
  12. * <g transform="scale(2)">
  13. * <path transform="rotate(45)" d="M0,0 L10,20"/>
  14. * <path transform="translate(10, 20)" d="M0,10 L20,30"/>
  15. * </g>
  16. *
  17. * <g>
  18. * <path transform="scale(2) rotate(45)" d="M0,0 L10,20"/>
  19. * <path transform="scale(2) translate(10, 20)" d="M0,10 L20,30"/>
  20. * </g>
  21. *
  22. * @param {Object} item current iteration item
  23. * @return {Boolean} if false, item will be filtered out
  24. *
  25. * @author Kir Belevich
  26. */
  27. exports.fn = function(item) {
  28. // move group transform attr to content's pathElems
  29. if (
  30. item.isElem('g') &&
  31. item.hasAttr('transform') &&
  32. !item.isEmpty() &&
  33. !item.someAttr(function(attr) {
  34. return ~referencesProps.indexOf(attr.name) && ~attr.value.indexOf('url(');
  35. }) &&
  36. item.content.every(function(inner) {
  37. return inner.isElem(pathElems) && !inner.hasAttr('id');
  38. })
  39. ) {
  40. item.content.forEach(function(inner) {
  41. var attr = item.attr('transform');
  42. if (inner.hasAttr('transform')) {
  43. inner.attr('transform').value = attr.value + ' ' + inner.attr('transform').value;
  44. } else {
  45. inner.addAttr({
  46. 'name': attr.name,
  47. 'local': attr.local,
  48. 'prefix': attr.prefix,
  49. 'value': attr.value
  50. });
  51. }
  52. });
  53. item.removeAttr('transform');
  54. }
  55. };