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.

125 lines
4.1 KiB

4 years ago
  1. /* jshint quotmark: false */
  2. 'use strict';
  3. exports.type = 'perItem';
  4. exports.active = true;
  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 = '\\(' + g('[^\'"()\\\\]+', rEscape, rSingleQuotes, rQuotes) + '*?' + '\\)',
  18. // The value. It can have strings and parentheses (see above). Fallbacks to anything in case of unexpected input.
  19. rValue = '\\s*(' + g('[^!\'"();\\\\]+?', rEscape, rSingleQuotes, rQuotes, rParenthesis, '[^;]*?') + '*?' + ')',
  20. // End of declaration. Spaces outside of capturing groups help to do natural trimming.
  21. rDeclEnd = '\\s*(?:;\\s*|$)',
  22. // Important rule
  23. rImportant = '(\\s*!important(?![-(\w]))?',
  24. // Final RegExp to parse CSS declarations.
  25. regDeclarationBlock = new RegExp(rAttr + ':' + rValue + rImportant + rDeclEnd, 'ig'),
  26. // Comments expression. Honors escape sequences and strings.
  27. regStripComments = new RegExp(g(rEscape, rSingleQuotes, rQuotes, '/\\*[^]*?\\*/'), 'ig');
  28. /**
  29. * Convert style in attributes. Cleanups comments and illegal declarations (without colon) as a side effect.
  30. *
  31. * @example
  32. * <g style="fill:#000; color: #fff;">
  33. *
  34. * <g fill="#000" color="#fff">
  35. *
  36. * @example
  37. * <g style="fill:#000; color: #fff; -webkit-blah: blah">
  38. *
  39. * <g fill="#000" color="#fff" style="-webkit-blah: blah">
  40. *
  41. * @param {Object} item current iteration item
  42. * @return {Boolean} if false, item will be filtered out
  43. *
  44. * @author Kir Belevich
  45. */
  46. exports.fn = function(item, params) {
  47. /* jshint boss: true */
  48. if (item.elem && item.hasAttr('style')) {
  49. // ['opacity: 1', 'color: #000']
  50. var styleValue = item.attr('style').value,
  51. styles = [],
  52. attrs = {};
  53. // Strip CSS comments preserving escape sequences and strings.
  54. styleValue = styleValue.replace(regStripComments, function(match) {
  55. return match[0] == '/' ? '' :
  56. match[0] == '\\' && /[-g-z]/i.test(match[1]) ? match[1] : match;
  57. });
  58. regDeclarationBlock.lastIndex = 0;
  59. for (var rule; rule = regDeclarationBlock.exec(styleValue);) {
  60. if (!params.keepImportant || !rule[3]) {
  61. styles.push([rule[1], rule[2]]);
  62. }
  63. }
  64. if (styles.length) {
  65. styles = styles.filter(function(style) {
  66. if (style[0]) {
  67. var prop = style[0].toLowerCase(),
  68. val = style[1];
  69. if (rQuotedString.test(val)) {
  70. val = val.slice(1, -1);
  71. }
  72. if (stylingProps.indexOf(prop) > -1) {
  73. attrs[prop] = {
  74. name: prop,
  75. value: val,
  76. local: prop,
  77. prefix: ''
  78. };
  79. return false;
  80. }
  81. }
  82. return true;
  83. });
  84. Object.assign(item.attrs, attrs);
  85. if (styles.length) {
  86. item.attr('style').value = styles
  87. .map(function(declaration) { return declaration.join(':') })
  88. .join(';');
  89. } else {
  90. item.removeAttr('style');
  91. }
  92. }
  93. }
  94. };
  95. function g() {
  96. return '(?:' + Array.prototype.join.call(arguments, '|') + ')';
  97. }