removeViewBox.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. exports.type = 'perItem';
  3. exports.active = true;
  4. exports.description = 'removes viewBox attribute when possible';
  5. var viewBoxElems = ['svg', 'pattern', 'symbol'];
  6. /**
  7. * Remove viewBox attr which coincides with a width/height box.
  8. *
  9. * @see http://www.w3.org/TR/SVG/coords.html#ViewBoxAttribute
  10. *
  11. * @example
  12. * <svg width="100" height="50" viewBox="0 0 100 50">
  13. * ⬇
  14. * <svg width="100" height="50">
  15. *
  16. * @param {Object} item current iteration item
  17. * @return {Boolean} if false, item will be filtered out
  18. *
  19. * @author Kir Belevich
  20. */
  21. exports.fn = function(item) {
  22. if (
  23. item.isElem(viewBoxElems) &&
  24. item.hasAttr('viewBox') &&
  25. item.hasAttr('width') &&
  26. item.hasAttr('height')
  27. ) {
  28. var nums = item.attr('viewBox').value.split(/[ ,]+/g);
  29. if (
  30. nums[0] === '0' &&
  31. nums[1] === '0' &&
  32. item.attr('width').value.replace(/px$/, '') === nums[2] && // could use parseFloat too
  33. item.attr('height').value.replace(/px$/, '') === nums[3]
  34. ) {
  35. item.removeAttr('viewBox');
  36. }
  37. }
  38. };