removeEmptyAttrs.js 777 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. const { attrsGroups } = require('./_collections.js');
  3. exports.type = 'visitor';
  4. exports.name = 'removeEmptyAttrs';
  5. exports.active = true;
  6. exports.description = 'removes empty attributes';
  7. /**
  8. * Remove attributes with empty values.
  9. *
  10. * @author Kir Belevich
  11. *
  12. * @type {import('../lib/types').Plugin<void>}
  13. */
  14. exports.fn = () => {
  15. return {
  16. element: {
  17. enter: (node) => {
  18. for (const [name, value] of Object.entries(node.attributes)) {
  19. if (
  20. value === '' &&
  21. // empty conditional processing attributes prevents elements from rendering
  22. attrsGroups.conditionalProcessing.includes(name) === false
  23. ) {
  24. delete node.attributes[name];
  25. }
  26. }
  27. },
  28. },
  29. };
  30. };