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.

removeDimensions.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. exports.type = 'perItem';
  3. exports.active = false;
  4. exports.description = 'removes width and height in presence of viewBox (opposite to removeViewBox, disable it first)';
  5. /**
  6. * Remove width/height attributes and add the viewBox attribute if it's missing
  7. *
  8. * @example
  9. * <svg width="100" height="50" />
  10. * ↓
  11. * <svg viewBox="0 0 100 50" />
  12. *
  13. * @param {Object} item current iteration item
  14. * @return {Boolean} if true, with and height will be filtered out
  15. *
  16. * @author Benny Schudel
  17. */
  18. exports.fn = function(item) {
  19. if (item.isElem('svg')) {
  20. if (item.hasAttr('viewBox')) {
  21. item.removeAttr('width');
  22. item.removeAttr('height');
  23. } else if (
  24. item.hasAttr('width') &&
  25. item.hasAttr('height') &&
  26. !isNaN(Number(item.attr('width').value)) &&
  27. !isNaN(Number(item.attr('height').value))
  28. ) {
  29. item.addAttr({
  30. name: 'viewBox',
  31. value:
  32. '0 0 ' +
  33. Number(item.attr('width').value) +
  34. ' ' +
  35. Number(item.attr('height').value),
  36. prefix: '',
  37. local: 'viewBox'
  38. });
  39. item.removeAttr('width');
  40. item.removeAttr('height');
  41. }
  42. }
  43. };