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.

cleanupEnableBackground.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. exports.type = 'full';
  3. exports.active = true;
  4. exports.description = 'remove or cleanup enable-background attribute when possible';
  5. /**
  6. * Remove or cleanup enable-background attr which coincides with a width/height box.
  7. *
  8. * @see http://www.w3.org/TR/SVG/filters.html#EnableBackgroundProperty
  9. *
  10. * @example
  11. * <svg width="100" height="50" enable-background="new 0 0 100 50">
  12. * ⬇
  13. * <svg width="100" height="50">
  14. *
  15. * @param {Object} item current iteration item
  16. * @return {Boolean} if false, item will be filtered out
  17. *
  18. * @author Kir Belevich
  19. */
  20. exports.fn = function(data) {
  21. var regEnableBackground = /^new\s0\s0\s([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)\s([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)$/,
  22. hasFilter = false,
  23. elems = ['svg', 'mask', 'pattern'];
  24. function checkEnableBackground(item) {
  25. if (
  26. item.isElem(elems) &&
  27. item.hasAttr('enable-background') &&
  28. item.hasAttr('width') &&
  29. item.hasAttr('height')
  30. ) {
  31. var match = item.attr('enable-background').value.match(regEnableBackground);
  32. if (match) {
  33. if (
  34. item.attr('width').value === match[1] &&
  35. item.attr('height').value === match[3]
  36. ) {
  37. if (item.isElem('svg')) {
  38. item.removeAttr('enable-background');
  39. } else {
  40. item.attr('enable-background').value = 'new';
  41. }
  42. }
  43. }
  44. }
  45. }
  46. function checkForFilter(item) {
  47. if (item.isElem('filter')) {
  48. hasFilter = true;
  49. }
  50. }
  51. function monkeys(items, fn) {
  52. items.content.forEach(function(item) {
  53. fn(item);
  54. if (item.content) {
  55. monkeys(item, fn);
  56. }
  57. });
  58. return items;
  59. }
  60. var firstStep = monkeys(data, function(item) {
  61. checkEnableBackground(item);
  62. if (!hasFilter) {
  63. checkForFilter(item);
  64. }
  65. });
  66. return hasFilter ? firstStep : monkeys(firstStep, function(item) {
  67. //we don't need 'enable-background' if we have no filters
  68. item.removeAttr('enable-background');
  69. });
  70. };