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.

56 lines
1.3 KiB

4 years ago
  1. 'use strict';
  2. exports.type = 'perItem';
  3. exports.active = true;
  4. exports.description = 'cleanups attributes from newlines, trailing and repeating spaces';
  5. exports.params = {
  6. newlines: true,
  7. trim: true,
  8. spaces: true
  9. };
  10. var regNewlinesNeedSpace = /(\S)\r?\n(\S)/g,
  11. regNewlines = /\r?\n/g,
  12. regSpaces = /\s{2,}/g;
  13. /**
  14. * Cleanup attributes values from newlines, trailing and repeating spaces.
  15. *
  16. * @param {Object} item current iteration item
  17. * @param {Object} params plugin params
  18. * @return {Boolean} if false, item will be filtered out
  19. *
  20. * @author Kir Belevich
  21. */
  22. exports.fn = function(item, params) {
  23. if (item.isElem()) {
  24. item.eachAttr(function(attr) {
  25. if (params.newlines) {
  26. // new line which requires a space instead of themselve
  27. attr.value = attr.value.replace(regNewlinesNeedSpace, function(match, p1, p2) {
  28. return p1 + ' ' + p2;
  29. });
  30. // simple new line
  31. attr.value = attr.value.replace(regNewlines, '');
  32. }
  33. if (params.trim) {
  34. attr.value = attr.value.trim();
  35. }
  36. if (params.spaces) {
  37. attr.value = attr.value.replace(regSpaces, ' ');
  38. }
  39. });
  40. }
  41. };