convertStyleToAttrs.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. 'use strict';
  2. exports.name = 'convertStyleToAttrs';
  3. exports.type = 'perItem';
  4. exports.active = false;
  5. exports.description = 'converts style to attributes';
  6. exports.params = {
  7. keepImportant: false,
  8. };
  9. var stylingProps = require('./_collections').attrsGroups.presentation,
  10. rEscape = '\\\\(?:[0-9a-f]{1,6}\\s?|\\r\\n|.)', // Like \" or \2051. Code points consume one space.
  11. rAttr = '\\s*(' + g('[^:;\\\\]', rEscape) + '*?)\\s*', // attribute name like ‘fill’
  12. rSingleQuotes = "'(?:[^'\\n\\r\\\\]|" + rEscape + ")*?(?:'|$)", // string in single quotes: 'smth'
  13. rQuotes = '"(?:[^"\\n\\r\\\\]|' + rEscape + ')*?(?:"|$)', // string in double quotes: "smth"
  14. rQuotedString = new RegExp('^' + g(rSingleQuotes, rQuotes) + '$'),
  15. // Parentheses, E.g.: url(data:image/png;base64,iVBO...).
  16. // ':' and ';' inside of it should be threated as is. (Just like in strings.)
  17. rParenthesis =
  18. '\\(' + g('[^\'"()\\\\]+', rEscape, rSingleQuotes, rQuotes) + '*?' + '\\)',
  19. // The value. It can have strings and parentheses (see above). Fallbacks to anything in case of unexpected input.
  20. rValue =
  21. '\\s*(' +
  22. g(
  23. '[^!\'"();\\\\]+?',
  24. rEscape,
  25. rSingleQuotes,
  26. rQuotes,
  27. rParenthesis,
  28. '[^;]*?'
  29. ) +
  30. '*?' +
  31. ')',
  32. // End of declaration. Spaces outside of capturing groups help to do natural trimming.
  33. rDeclEnd = '\\s*(?:;\\s*|$)',
  34. // Important rule
  35. rImportant = '(\\s*!important(?![-(\\w]))?',
  36. // Final RegExp to parse CSS declarations.
  37. regDeclarationBlock = new RegExp(
  38. rAttr + ':' + rValue + rImportant + rDeclEnd,
  39. 'ig'
  40. ),
  41. // Comments expression. Honors escape sequences and strings.
  42. regStripComments = new RegExp(
  43. g(rEscape, rSingleQuotes, rQuotes, '/\\*[^]*?\\*/'),
  44. 'ig'
  45. );
  46. /**
  47. * Convert style in attributes. Cleanups comments and illegal declarations (without colon) as a side effect.
  48. *
  49. * @example
  50. * <g style="fill:#000; color: #fff;">
  51. * ⬇
  52. * <g fill="#000" color="#fff">
  53. *
  54. * @example
  55. * <g style="fill:#000; color: #fff; -webkit-blah: blah">
  56. * ⬇
  57. * <g fill="#000" color="#fff" style="-webkit-blah: blah">
  58. *
  59. * @param {Object} item current iteration item
  60. * @return {Boolean} if false, item will be filtered out
  61. *
  62. * @author Kir Belevich
  63. */
  64. exports.fn = function (item, params) {
  65. if (item.type === 'element' && item.attributes.style != null) {
  66. // ['opacity: 1', 'color: #000']
  67. let styles = [];
  68. const newAttributes = {};
  69. // Strip CSS comments preserving escape sequences and strings.
  70. const styleValue = item.attributes.style.replace(
  71. regStripComments,
  72. (match) => {
  73. return match[0] == '/'
  74. ? ''
  75. : match[0] == '\\' && /[-g-z]/i.test(match[1])
  76. ? match[1]
  77. : match;
  78. }
  79. );
  80. regDeclarationBlock.lastIndex = 0;
  81. // eslint-disable-next-line no-cond-assign
  82. for (var rule; (rule = regDeclarationBlock.exec(styleValue)); ) {
  83. if (!params.keepImportant || !rule[3]) {
  84. styles.push([rule[1], rule[2]]);
  85. }
  86. }
  87. if (styles.length) {
  88. styles = styles.filter(function (style) {
  89. if (style[0]) {
  90. var prop = style[0].toLowerCase(),
  91. val = style[1];
  92. if (rQuotedString.test(val)) {
  93. val = val.slice(1, -1);
  94. }
  95. if (stylingProps.includes(prop)) {
  96. newAttributes[prop] = val;
  97. return false;
  98. }
  99. }
  100. return true;
  101. });
  102. Object.assign(item.attributes, newAttributes);
  103. if (styles.length) {
  104. item.attributes.style = styles
  105. .map((declaration) => declaration.join(':'))
  106. .join(';');
  107. } else {
  108. delete item.attributes.style;
  109. }
  110. }
  111. }
  112. };
  113. function g() {
  114. return '(?:' + Array.prototype.join.call(arguments, '|') + ')';
  115. }