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.

removeAttrs.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. var DEFAULT_SEPARATOR = ':';
  3. exports.type = 'perItem';
  4. exports.active = false;
  5. exports.description = 'removes specified attributes';
  6. exports.params = {
  7. elemSeparator: DEFAULT_SEPARATOR,
  8. preserveCurrentColor: false,
  9. attrs: []
  10. };
  11. /**
  12. * Remove attributes
  13. *
  14. * @param elemSeparator
  15. * format: string
  16. *
  17. * @param preserveCurrentColor
  18. * format: boolean
  19. *
  20. * @param attrs:
  21. *
  22. * format: [ element* : attribute* : value* ]
  23. *
  24. * element : regexp (wrapped into ^...$), single * or omitted > all elements (must be present when value is used)
  25. * attribute : regexp (wrapped into ^...$)
  26. * value : regexp (wrapped into ^...$), single * or omitted > all values
  27. *
  28. * examples:
  29. *
  30. * > basic: remove fill attribute
  31. * ---
  32. * removeAttrs:
  33. * attrs: 'fill'
  34. *
  35. * > remove fill attribute on path element
  36. * ---
  37. * attrs: 'path:fill'
  38. *
  39. * > remove fill attribute on path element where value is none
  40. * ---
  41. * attrs: 'path:fill:none'
  42. *
  43. *
  44. * > remove all fill and stroke attribute
  45. * ---
  46. * attrs:
  47. * - 'fill'
  48. * - 'stroke'
  49. *
  50. * [is same as]
  51. *
  52. * attrs: '(fill|stroke)'
  53. *
  54. * [is same as]
  55. *
  56. * attrs: '*:(fill|stroke)'
  57. *
  58. * [is same as]
  59. *
  60. * attrs: '.*:(fill|stroke)'
  61. *
  62. * [is same as]
  63. *
  64. * attrs: '.*:(fill|stroke):.*'
  65. *
  66. *
  67. * > remove all stroke related attributes
  68. * ----
  69. * attrs: 'stroke.*'
  70. *
  71. *
  72. * @param {Object} item current iteration item
  73. * @param {Object} params plugin params
  74. * @return {Boolean} if false, item will be filtered out
  75. *
  76. * @author Benny Schudel
  77. */
  78. exports.fn = function(item, params) {
  79. // wrap into an array if params is not
  80. if (!Array.isArray(params.attrs)) {
  81. params.attrs = [params.attrs];
  82. }
  83. if (item.isElem()) {
  84. var elemSeparator = typeof params.elemSeparator == 'string' ? params.elemSeparator : DEFAULT_SEPARATOR;
  85. var preserveCurrentColor = typeof params.preserveCurrentColor == 'boolean' ? params.preserveCurrentColor : false;
  86. // prepare patterns
  87. var patterns = params.attrs.map(function(pattern) {
  88. // if no element separators (:), assume it's attribute name, and apply to all elements *regardless of value*
  89. if (pattern.indexOf(elemSeparator) === -1) {
  90. pattern = ['.*', elemSeparator, pattern, elemSeparator, '.*'].join('');
  91. // if only 1 separator, assume it's element and attribute name, and apply regardless of attribute value
  92. } else if (pattern.split(elemSeparator).length < 3) {
  93. pattern = [pattern, elemSeparator, '.*'].join('');
  94. }
  95. // create regexps for element, attribute name, and attribute value
  96. return pattern.split(elemSeparator)
  97. .map(function(value) {
  98. // adjust single * to match anything
  99. if (value === '*') { value = '.*'; }
  100. return new RegExp(['^', value, '$'].join(''), 'i');
  101. });
  102. });
  103. // loop patterns
  104. patterns.forEach(function(pattern) {
  105. // matches element
  106. if (pattern[0].test(item.elem)) {
  107. // loop attributes
  108. item.eachAttr(function(attr) {
  109. var name = attr.name;
  110. var value = attr.value;
  111. var isFillCurrentColor = preserveCurrentColor && name == 'fill' && value == 'currentColor';
  112. var isStrokeCurrentColor = preserveCurrentColor && name == 'stroke' && value == 'currentColor';
  113. if (!(isFillCurrentColor || isStrokeCurrentColor)) {
  114. // matches attribute name
  115. if (pattern[1].test(name)) {
  116. // matches attribute value
  117. if (pattern[2].test(attr.value)) {
  118. item.removeAttr(name);
  119. }
  120. }
  121. }
  122. });
  123. }
  124. });
  125. }
  126. };