source: trip-planner-front/node_modules/svgo/plugins/convertStyleToAttrs.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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