removeViewBox.js 1.2 KB

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