removeAttributesBySelector.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. exports.type = 'perItem';
  3. exports.active = false;
  4. exports.description = 'removes attributes of elements that match a css selector';
  5. /**
  6. * Removes attributes of elements that match a css selector.
  7. *
  8. * @param {Object} item current iteration item
  9. * @param {Object} params plugin params
  10. * @return {Boolean} if false, item will be filtered out
  11. *
  12. * @example
  13. * <caption>A selector removing a single attribute</caption>
  14. * plugins:
  15. * - removeAttributesBySelector:
  16. * selector: "[fill='#00ff00']"
  17. * attributes: "fill"
  18. *
  19. * <rect x="0" y="0" width="100" height="100" fill="#00ff00" stroke="#00ff00"/>
  20. * ↓
  21. * <rect x="0" y="0" width="100" height="100" stroke="#00ff00"/>
  22. *
  23. * <caption>A selector removing multiple attributes</caption>
  24. * plugins:
  25. * - removeAttributesBySelector:
  26. * selector: "[fill='#00ff00']"
  27. * attributes:
  28. * - fill
  29. * - stroke
  30. *
  31. * <rect x="0" y="0" width="100" height="100" fill="#00ff00" stroke="#00ff00"/>
  32. * ↓
  33. * <rect x="0" y="0" width="100" height="100"/>
  34. *
  35. * <caption>Multiple selectors removing attributes</caption>
  36. * plugins:
  37. * - removeAttributesBySelector:
  38. * selectors:
  39. * - selector: "[fill='#00ff00']"
  40. * attributes: "fill"
  41. *
  42. * - selector: "#remove"
  43. * attributes:
  44. * - stroke
  45. * - id
  46. *
  47. * <rect x="0" y="0" width="100" height="100" fill="#00ff00" stroke="#00ff00"/>
  48. * ↓
  49. * <rect x="0" y="0" width="100" height="100"/>
  50. *
  51. * @see {@link https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors|MDN CSS Selectors}
  52. *
  53. * @author Bradley Mease
  54. */
  55. exports.fn = function(item, params) {
  56. var selectors = Array.isArray(params.selectors) ? params.selectors : [params];
  57. selectors.map(function(i) {
  58. if (item.matches(i.selector)) {
  59. item.removeAttr(i.attributes);
  60. }
  61. });
  62. };