@react-pdf/stylesheet 4.2.5 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +213 -221
- package/package.json +4 -12
- package/lib/index.cjs +0 -768
package/lib/index.js
CHANGED
|
@@ -3,44 +3,49 @@ import parse$1 from 'postcss-value-parser/lib/parse.js';
|
|
|
3
3
|
import parseUnit from 'postcss-value-parser/lib/unit.js';
|
|
4
4
|
import hlsToHex from 'hsl-to-hex';
|
|
5
5
|
import colorString from 'color-string';
|
|
6
|
-
import _extends from '@babel/runtime/helpers/extends';
|
|
7
6
|
import matchMedia from 'media-engine';
|
|
8
7
|
|
|
9
8
|
// https://developer.mozilla.org/en-US/docs/Web/CSS/flex#values
|
|
10
9
|
|
|
11
10
|
// TODO: change flex defaults to [0, 1, 'auto'] as in spec in next major release
|
|
12
|
-
|
|
11
|
+
const flexDefaults = [1, 1, 0];
|
|
13
12
|
/**
|
|
14
13
|
* @type {(number | 'auto')[]}
|
|
15
14
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const flexAuto = [1, 1, 'auto'];
|
|
16
|
+
const expandFlex = (key, value) => {
|
|
18
17
|
/**
|
|
19
18
|
* @type {(number | 'auto')[]}
|
|
20
19
|
*/
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
let defaults = flexDefaults;
|
|
21
|
+
let matches = [];
|
|
23
22
|
if (value === 'auto') {
|
|
24
23
|
defaults = flexAuto;
|
|
25
24
|
} else {
|
|
26
|
-
matches =
|
|
25
|
+
matches = `${value}`.split(' ');
|
|
27
26
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
const flexGrow = matches[0] || defaults[0];
|
|
28
|
+
const flexShrink = matches[1] || defaults[1];
|
|
29
|
+
const flexBasis = matches[2] || defaults[2];
|
|
31
30
|
return {
|
|
32
|
-
flexGrow
|
|
33
|
-
flexShrink
|
|
34
|
-
flexBasis
|
|
31
|
+
flexGrow,
|
|
32
|
+
flexShrink,
|
|
33
|
+
flexBasis
|
|
35
34
|
};
|
|
36
35
|
};
|
|
37
36
|
|
|
38
37
|
/* eslint-disable no-plusplus */
|
|
39
38
|
// This file is ran directly with Node - needs to have .js extension
|
|
40
39
|
// eslint-disable-next-line import/extensions
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
console.error(
|
|
40
|
+
const BOX_MODEL_UNITS = 'px,in,mm,cm,pt,%,vw,vh';
|
|
41
|
+
const logError = (style, value) => {
|
|
42
|
+
console.error(`
|
|
43
|
+
@react-pdf/stylesheet parsing error:
|
|
44
|
+
|
|
45
|
+
${style}: ${value},
|
|
46
|
+
${' '.repeat(style.length + 2)}^
|
|
47
|
+
Unsupported ${style} value format
|
|
48
|
+
`);
|
|
44
49
|
};
|
|
45
50
|
|
|
46
51
|
/**
|
|
@@ -49,19 +54,17 @@ var logError = function logError(style, value) {
|
|
|
49
54
|
* @param {number} [options.maxValues]
|
|
50
55
|
* @param {boolean} [options.autoSupported]
|
|
51
56
|
*/
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
expandsTo
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
for (var i = 0; i < nodes.length; i++) {
|
|
64
|
-
var node = nodes[i];
|
|
57
|
+
const expandBoxModel = function (_temp) {
|
|
58
|
+
let {
|
|
59
|
+
expandsTo,
|
|
60
|
+
maxValues = 1,
|
|
61
|
+
autoSupported = false
|
|
62
|
+
} = _temp === void 0 ? {} : _temp;
|
|
63
|
+
return (model, value) => {
|
|
64
|
+
const nodes = parse$1(`${value}`);
|
|
65
|
+
const parts = [];
|
|
66
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
67
|
+
const node = nodes[i];
|
|
65
68
|
|
|
66
69
|
// value contains `calc`, `url` or other css function
|
|
67
70
|
// `,`, `/` or strings that unsupported by margin and padding
|
|
@@ -73,7 +76,7 @@ var expandBoxModel = function expandBoxModel(_temp) {
|
|
|
73
76
|
if (node.value === 'auto' && autoSupported) {
|
|
74
77
|
parts.push(node.value);
|
|
75
78
|
} else {
|
|
76
|
-
|
|
79
|
+
const result = parseUnit(node.value);
|
|
77
80
|
|
|
78
81
|
// when unit isn't specified this condition is true
|
|
79
82
|
if (result && BOX_MODEL_UNITS.includes(result.unit)) {
|
|
@@ -91,28 +94,32 @@ var expandBoxModel = function expandBoxModel(_temp) {
|
|
|
91
94
|
logError(model, value);
|
|
92
95
|
return {};
|
|
93
96
|
}
|
|
94
|
-
|
|
97
|
+
const first = parts[0];
|
|
95
98
|
if (expandsTo) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
+
const second = parts[1] || parts[0];
|
|
100
|
+
const third = parts[2] || parts[0];
|
|
101
|
+
const fourth = parts[3] || parts[1] || parts[0];
|
|
99
102
|
return expandsTo({
|
|
100
|
-
first
|
|
101
|
-
second
|
|
102
|
-
third
|
|
103
|
-
fourth
|
|
103
|
+
first,
|
|
104
|
+
second,
|
|
105
|
+
third,
|
|
106
|
+
fourth
|
|
104
107
|
});
|
|
105
108
|
}
|
|
106
|
-
return
|
|
109
|
+
return {
|
|
110
|
+
[model]: first
|
|
111
|
+
};
|
|
107
112
|
};
|
|
108
113
|
};
|
|
109
114
|
|
|
110
|
-
|
|
111
|
-
expandsTo:
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
115
|
+
const processMargin = expandBoxModel({
|
|
116
|
+
expandsTo: _ref => {
|
|
117
|
+
let {
|
|
118
|
+
first,
|
|
119
|
+
second,
|
|
120
|
+
third,
|
|
121
|
+
fourth
|
|
122
|
+
} = _ref;
|
|
116
123
|
return {
|
|
117
124
|
marginTop: first,
|
|
118
125
|
marginRight: second,
|
|
@@ -123,10 +130,12 @@ var processMargin = expandBoxModel({
|
|
|
123
130
|
maxValues: 4,
|
|
124
131
|
autoSupported: true
|
|
125
132
|
});
|
|
126
|
-
|
|
127
|
-
expandsTo:
|
|
128
|
-
|
|
129
|
-
|
|
133
|
+
const processMarginVertical = expandBoxModel({
|
|
134
|
+
expandsTo: _ref2 => {
|
|
135
|
+
let {
|
|
136
|
+
first,
|
|
137
|
+
second
|
|
138
|
+
} = _ref2;
|
|
130
139
|
return {
|
|
131
140
|
marginTop: first,
|
|
132
141
|
marginBottom: second
|
|
@@ -135,10 +144,12 @@ var processMarginVertical = expandBoxModel({
|
|
|
135
144
|
maxValues: 2,
|
|
136
145
|
autoSupported: true
|
|
137
146
|
});
|
|
138
|
-
|
|
139
|
-
expandsTo:
|
|
140
|
-
|
|
141
|
-
|
|
147
|
+
const processMarginHorizontal = expandBoxModel({
|
|
148
|
+
expandsTo: _ref3 => {
|
|
149
|
+
let {
|
|
150
|
+
first,
|
|
151
|
+
second
|
|
152
|
+
} = _ref3;
|
|
142
153
|
return {
|
|
143
154
|
marginRight: first,
|
|
144
155
|
marginLeft: second
|
|
@@ -147,23 +158,24 @@ var processMarginHorizontal = expandBoxModel({
|
|
|
147
158
|
maxValues: 2,
|
|
148
159
|
autoSupported: true
|
|
149
160
|
});
|
|
150
|
-
|
|
161
|
+
const processMarginSingle = expandBoxModel({
|
|
151
162
|
autoSupported: true
|
|
152
163
|
});
|
|
153
164
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
};
|
|
158
|
-
var expandBorders = function expandBorders(key, value) {
|
|
159
|
-
var match = matchBorderShorthand("" + value);
|
|
165
|
+
const BORDER_SHORTHAND_REGEX = /(-?\d+(\.\d+)?(px|in|mm|cm|pt|vw|vh|px)?)\s(\S+)\s(.+)/;
|
|
166
|
+
const matchBorderShorthand = value => value.match(BORDER_SHORTHAND_REGEX) || [];
|
|
167
|
+
const expandBorders = (key, value) => {
|
|
168
|
+
const match = matchBorderShorthand(`${value}`);
|
|
160
169
|
if (match) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
170
|
+
const color = match[5] || value;
|
|
171
|
+
const style = match[4] || value;
|
|
172
|
+
const width = match[1] || value;
|
|
164
173
|
if (key.match(/(Top|Right|Bottom|Left)$/)) {
|
|
165
|
-
|
|
166
|
-
|
|
174
|
+
return {
|
|
175
|
+
[`${key}Color`]: color,
|
|
176
|
+
[`${key}Style`]: style,
|
|
177
|
+
[`${key}Width`]: width
|
|
178
|
+
};
|
|
167
179
|
}
|
|
168
180
|
if (key.match(/Color$/)) {
|
|
169
181
|
return {
|
|
@@ -215,12 +227,14 @@ var expandBorders = function expandBorders(key, value) {
|
|
|
215
227
|
return value;
|
|
216
228
|
};
|
|
217
229
|
|
|
218
|
-
|
|
219
|
-
expandsTo:
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
230
|
+
const processPadding = expandBoxModel({
|
|
231
|
+
expandsTo: _ref => {
|
|
232
|
+
let {
|
|
233
|
+
first,
|
|
234
|
+
second,
|
|
235
|
+
third,
|
|
236
|
+
fourth
|
|
237
|
+
} = _ref;
|
|
224
238
|
return {
|
|
225
239
|
paddingTop: first,
|
|
226
240
|
paddingRight: second,
|
|
@@ -230,10 +244,12 @@ var processPadding = expandBoxModel({
|
|
|
230
244
|
},
|
|
231
245
|
maxValues: 4
|
|
232
246
|
});
|
|
233
|
-
|
|
234
|
-
expandsTo:
|
|
235
|
-
|
|
236
|
-
|
|
247
|
+
const processPaddingVertical = expandBoxModel({
|
|
248
|
+
expandsTo: _ref2 => {
|
|
249
|
+
let {
|
|
250
|
+
first,
|
|
251
|
+
second
|
|
252
|
+
} = _ref2;
|
|
237
253
|
return {
|
|
238
254
|
paddingTop: first,
|
|
239
255
|
paddingBottom: second
|
|
@@ -241,10 +257,12 @@ var processPaddingVertical = expandBoxModel({
|
|
|
241
257
|
},
|
|
242
258
|
maxValues: 2
|
|
243
259
|
});
|
|
244
|
-
|
|
245
|
-
expandsTo:
|
|
246
|
-
|
|
247
|
-
|
|
260
|
+
const processPaddingHorizontal = expandBoxModel({
|
|
261
|
+
expandsTo: _ref3 => {
|
|
262
|
+
let {
|
|
263
|
+
first,
|
|
264
|
+
second
|
|
265
|
+
} = _ref3;
|
|
248
266
|
return {
|
|
249
267
|
paddingRight: first,
|
|
250
268
|
paddingLeft: second
|
|
@@ -252,50 +270,50 @@ var processPaddingHorizontal = expandBoxModel({
|
|
|
252
270
|
},
|
|
253
271
|
maxValues: 2
|
|
254
272
|
});
|
|
255
|
-
|
|
273
|
+
const processPaddingSingle = expandBoxModel();
|
|
256
274
|
|
|
257
|
-
|
|
258
|
-
|
|
275
|
+
const expandObjectPosition = (key, value) => {
|
|
276
|
+
const match = `${value}`.split(' ');
|
|
259
277
|
return {
|
|
260
278
|
objectPositionX: (match === null || match === void 0 ? void 0 : match[0]) || value,
|
|
261
279
|
objectPositionY: (match === null || match === void 0 ? void 0 : match[1]) || value
|
|
262
280
|
};
|
|
263
281
|
};
|
|
264
282
|
|
|
265
|
-
|
|
283
|
+
const Y_AXIS_SHORTHANDS = {
|
|
266
284
|
top: true,
|
|
267
285
|
bottom: true
|
|
268
286
|
};
|
|
269
|
-
|
|
287
|
+
const sortTransformOriginPair = (a, b) => {
|
|
270
288
|
if (Y_AXIS_SHORTHANDS[a]) return 1;
|
|
271
289
|
if (Y_AXIS_SHORTHANDS[b]) return -1;
|
|
272
290
|
return 0;
|
|
273
291
|
};
|
|
274
|
-
|
|
292
|
+
const getTransformOriginPair = values => {
|
|
275
293
|
if (!values || values.length === 0) return ['center', 'center'];
|
|
276
|
-
|
|
294
|
+
const pair = values.length === 1 ? [values[0], 'center'] : values;
|
|
277
295
|
return pair.sort(sortTransformOriginPair);
|
|
278
296
|
};
|
|
279
297
|
|
|
280
298
|
// Transforms shorthand transformOrigin values
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
299
|
+
const expandTransformOrigin = (key, value) => {
|
|
300
|
+
const match = `${value}`.split(' ');
|
|
301
|
+
const pair = getTransformOriginPair(match);
|
|
284
302
|
return {
|
|
285
303
|
transformOriginX: pair[0],
|
|
286
304
|
transformOriginY: pair[1]
|
|
287
305
|
};
|
|
288
306
|
};
|
|
289
307
|
|
|
290
|
-
|
|
291
|
-
|
|
308
|
+
const expandGap = (key, value) => {
|
|
309
|
+
const match = `${value}`.split(' ');
|
|
292
310
|
return {
|
|
293
311
|
rowGap: (match === null || match === void 0 ? void 0 : match[0]) || value,
|
|
294
312
|
columnGap: (match === null || match === void 0 ? void 0 : match[1]) || value
|
|
295
313
|
};
|
|
296
314
|
};
|
|
297
315
|
|
|
298
|
-
|
|
316
|
+
const shorthands = {
|
|
299
317
|
flex: expandFlex,
|
|
300
318
|
gap: expandGap,
|
|
301
319
|
margin: processMargin,
|
|
@@ -332,9 +350,10 @@ var shorthands = {
|
|
|
332
350
|
* @param {string} value style value
|
|
333
351
|
* @returns {string | Number} transformed style values
|
|
334
352
|
*/
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
353
|
+
const expandStyle = (key, value) => {
|
|
354
|
+
return shorthands[key] ? shorthands[key](key, value) : {
|
|
355
|
+
[key]: value
|
|
356
|
+
};
|
|
338
357
|
};
|
|
339
358
|
|
|
340
359
|
/**
|
|
@@ -343,18 +362,18 @@ var expandStyle = function expandStyle(key, value) {
|
|
|
343
362
|
* @param {Object} style object
|
|
344
363
|
* @returns {Object} expanded style object
|
|
345
364
|
*/
|
|
346
|
-
|
|
365
|
+
const expand = style => {
|
|
347
366
|
if (!style) return style;
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
for (
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
for (
|
|
356
|
-
|
|
357
|
-
|
|
367
|
+
const propsArray = Object.keys(style);
|
|
368
|
+
const resolvedStyle = {};
|
|
369
|
+
for (let i = 0; i < propsArray.length; i += 1) {
|
|
370
|
+
const key = propsArray[i];
|
|
371
|
+
const value = style[key];
|
|
372
|
+
const extended = expandStyle(key, value);
|
|
373
|
+
const keys = Object.keys(extended);
|
|
374
|
+
for (let j = 0; j < keys.length; j += 1) {
|
|
375
|
+
const propName = keys[j];
|
|
376
|
+
const propValue = extended[propName];
|
|
358
377
|
resolvedStyle[propName] = propValue;
|
|
359
378
|
}
|
|
360
379
|
}
|
|
@@ -368,9 +387,7 @@ var expand = function expand(style) {
|
|
|
368
387
|
* @param {(T | null | undefined)[]} array
|
|
369
388
|
* @returns {T[]} array without nils
|
|
370
389
|
*/
|
|
371
|
-
|
|
372
|
-
return array.filter(Boolean);
|
|
373
|
-
};
|
|
390
|
+
const compact = array => array.filter(Boolean);
|
|
374
391
|
|
|
375
392
|
/**
|
|
376
393
|
* Merges style objects array
|
|
@@ -378,17 +395,15 @@ var compact = function compact(array) {
|
|
|
378
395
|
* @param {Object[]} styles style objects array
|
|
379
396
|
* @returns {Object} merged style object
|
|
380
397
|
*/
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
}, {});
|
|
391
|
-
};
|
|
398
|
+
const mergeStyles = styles => styles.reduce((acc, style) => {
|
|
399
|
+
const s = Array.isArray(style) ? flatten(style) : style;
|
|
400
|
+
Object.keys(s).forEach(key => {
|
|
401
|
+
if (s[key] !== null && s[key] !== undefined) {
|
|
402
|
+
acc[key] = s[key];
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
return acc;
|
|
406
|
+
}, {});
|
|
392
407
|
|
|
393
408
|
/**
|
|
394
409
|
* Flattens an array of style objects, into one aggregated style object.
|
|
@@ -396,7 +411,7 @@ var mergeStyles = function mergeStyles(styles) {
|
|
|
396
411
|
* @param {Object[]} styles style objects array
|
|
397
412
|
* @returns {Object} flattened style object
|
|
398
413
|
*/
|
|
399
|
-
|
|
414
|
+
const flatten = compose(mergeStyles, compact, castArray);
|
|
400
415
|
|
|
401
416
|
/**
|
|
402
417
|
* Parses scalar value in value and unit pairs
|
|
@@ -404,13 +419,13 @@ var flatten = compose(mergeStyles, compact, castArray);
|
|
|
404
419
|
* @param {string} value scalar value
|
|
405
420
|
* @returns {Object} parsed value
|
|
406
421
|
*/
|
|
407
|
-
|
|
408
|
-
|
|
422
|
+
const parseValue = value => {
|
|
423
|
+
const match = /^(-?\d*\.?\d+)(in|mm|cm|pt|vh|vw|px)?$/g.exec(value);
|
|
409
424
|
return match ? {
|
|
410
425
|
value: parseFloat(match[1]),
|
|
411
426
|
unit: match[2] || 'pt'
|
|
412
427
|
} : {
|
|
413
|
-
value
|
|
428
|
+
value,
|
|
414
429
|
unit: undefined
|
|
415
430
|
};
|
|
416
431
|
};
|
|
@@ -422,11 +437,11 @@ var parseValue = function parseValue(value) {
|
|
|
422
437
|
* @param {string} value styles value
|
|
423
438
|
* @returns {Object} transformed value
|
|
424
439
|
*/
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
440
|
+
const transformUnit = (container, value) => {
|
|
441
|
+
const scalar = parseValue(value);
|
|
442
|
+
const dpi = 72; // Removed: container.dpi || 72
|
|
443
|
+
const mmFactor = 1 / 25.4 * dpi;
|
|
444
|
+
const cmFactor = 1 / 2.54 * dpi;
|
|
430
445
|
switch (scalar.unit) {
|
|
431
446
|
case 'in':
|
|
432
447
|
return scalar.value * dpi;
|
|
@@ -443,12 +458,8 @@ var transformUnit = function transformUnit(container, value) {
|
|
|
443
458
|
}
|
|
444
459
|
};
|
|
445
460
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
};
|
|
449
|
-
var isHsl = function isHsl(value) {
|
|
450
|
-
return /hsla?/g.test(value);
|
|
451
|
-
};
|
|
461
|
+
const isRgb = value => /rgba?/g.test(value);
|
|
462
|
+
const isHsl = value => /hsla?/g.test(value);
|
|
452
463
|
|
|
453
464
|
/**
|
|
454
465
|
* Transform rgb color to hexa
|
|
@@ -456,8 +467,8 @@ var isHsl = function isHsl(value) {
|
|
|
456
467
|
* @param {string} value styles value
|
|
457
468
|
* @returns {Object} transformed value
|
|
458
469
|
*/
|
|
459
|
-
|
|
460
|
-
|
|
470
|
+
const parseRgb = value => {
|
|
471
|
+
const rgb = colorString.get.rgb(value);
|
|
461
472
|
return colorString.to.hex(rgb);
|
|
462
473
|
};
|
|
463
474
|
|
|
@@ -467,9 +478,9 @@ var parseRgb = function parseRgb(value) {
|
|
|
467
478
|
* @param {string} value styles value
|
|
468
479
|
* @returns {Object} transformed value
|
|
469
480
|
*/
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
481
|
+
const parseHsl = value => {
|
|
482
|
+
const hsl = colorString.get.hsl(value).map(Math.round);
|
|
483
|
+
const hex = hlsToHex(...hsl);
|
|
473
484
|
return hex.toUpperCase();
|
|
474
485
|
};
|
|
475
486
|
|
|
@@ -479,58 +490,49 @@ var parseHsl = function parseHsl(value) {
|
|
|
479
490
|
* @param {string} value styles value
|
|
480
491
|
* @returns {Object} transformed value
|
|
481
492
|
*/
|
|
482
|
-
|
|
493
|
+
const transformColor = value => {
|
|
483
494
|
if (isRgb(value)) return parseRgb(value);
|
|
484
495
|
if (isHsl(value)) return parseHsl(value);
|
|
485
496
|
return value;
|
|
486
497
|
};
|
|
487
498
|
|
|
488
|
-
|
|
489
|
-
|
|
499
|
+
const parse = transformString => {
|
|
500
|
+
const transforms = transformString.trim().split(/\) |\)/);
|
|
490
501
|
|
|
491
502
|
// Handle "initial", "inherit", "unset".
|
|
492
503
|
if (transforms.length === 1) {
|
|
493
504
|
return [[transforms[0], true]];
|
|
494
505
|
}
|
|
495
|
-
|
|
496
|
-
for (
|
|
497
|
-
|
|
506
|
+
const parsed = [];
|
|
507
|
+
for (let i = 0; i < transforms.length; i += 1) {
|
|
508
|
+
const transform = transforms[i];
|
|
498
509
|
if (transform) {
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
var splitChar = rawValue.indexOf(',') >= 0 ? ',' : ' ';
|
|
503
|
-
var value = rawValue.split(splitChar).map(function (val) {
|
|
504
|
-
return val.trim();
|
|
505
|
-
});
|
|
510
|
+
const [name, rawValue] = transform.split('(');
|
|
511
|
+
const splitChar = rawValue.indexOf(',') >= 0 ? ',' : ' ';
|
|
512
|
+
const value = rawValue.split(splitChar).map(val => val.trim());
|
|
506
513
|
parsed.push({
|
|
507
514
|
operation: name,
|
|
508
|
-
value
|
|
515
|
+
value
|
|
509
516
|
});
|
|
510
517
|
}
|
|
511
518
|
}
|
|
512
519
|
return parsed;
|
|
513
520
|
};
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
unit = _unitsRegexp$exec[2];
|
|
519
|
-
var number = Number.parseFloat(angle);
|
|
521
|
+
const parseAngle = value => {
|
|
522
|
+
const unitsRegexp = /(-?\d*\.?\d*)(\w*)?/i;
|
|
523
|
+
const [, angle, unit] = unitsRegexp.exec(value);
|
|
524
|
+
const number = Number.parseFloat(angle);
|
|
520
525
|
return unit === 'rad' ? number * 180 / Math.PI : number;
|
|
521
526
|
};
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
527
|
+
const normalizeTransformOperation = _ref => {
|
|
528
|
+
let {
|
|
529
|
+
operation,
|
|
530
|
+
value
|
|
531
|
+
} = _ref;
|
|
525
532
|
switch (operation) {
|
|
526
533
|
case 'scale':
|
|
527
534
|
{
|
|
528
|
-
|
|
529
|
-
return Number.parseFloat(num);
|
|
530
|
-
}),
|
|
531
|
-
scaleX = _value$map[0],
|
|
532
|
-
_value$map$ = _value$map[1],
|
|
533
|
-
scaleY = _value$map$ === void 0 ? scaleX : _value$map$;
|
|
535
|
+
const [scaleX, scaleY = scaleX] = value.map(num => Number.parseFloat(num));
|
|
534
536
|
return {
|
|
535
537
|
operation: 'scale',
|
|
536
538
|
value: [scaleX, scaleY]
|
|
@@ -561,9 +563,7 @@ var normalizeTransformOperation = function normalizeTransformOperation(_ref) {
|
|
|
561
563
|
{
|
|
562
564
|
return {
|
|
563
565
|
operation: 'translate',
|
|
564
|
-
value: value.map(
|
|
565
|
-
return Number.parseFloat(num);
|
|
566
|
-
})
|
|
566
|
+
value: value.map(num => Number.parseFloat(num))
|
|
567
567
|
};
|
|
568
568
|
}
|
|
569
569
|
case 'translateX':
|
|
@@ -604,25 +604,21 @@ var normalizeTransformOperation = function normalizeTransformOperation(_ref) {
|
|
|
604
604
|
default:
|
|
605
605
|
{
|
|
606
606
|
return {
|
|
607
|
-
operation
|
|
608
|
-
value: value.map(
|
|
609
|
-
return Number.parseFloat(num);
|
|
610
|
-
})
|
|
607
|
+
operation,
|
|
608
|
+
value: value.map(num => Number.parseFloat(num))
|
|
611
609
|
};
|
|
612
610
|
}
|
|
613
611
|
}
|
|
614
612
|
};
|
|
615
|
-
|
|
616
|
-
return operations.map(
|
|
617
|
-
return normalizeTransformOperation(operation);
|
|
618
|
-
});
|
|
613
|
+
const normalize = operations => {
|
|
614
|
+
return operations.map(operation => normalizeTransformOperation(operation));
|
|
619
615
|
};
|
|
620
|
-
|
|
616
|
+
const processTransform = value => {
|
|
621
617
|
if (typeof value !== 'string') return value;
|
|
622
618
|
return normalize(parse(value));
|
|
623
619
|
};
|
|
624
620
|
|
|
625
|
-
|
|
621
|
+
const FONT_WEIGHTS = {
|
|
626
622
|
thin: 100,
|
|
627
623
|
hairline: 100,
|
|
628
624
|
ultralight: 200,
|
|
@@ -638,24 +634,22 @@ var FONT_WEIGHTS = {
|
|
|
638
634
|
heavy: 900,
|
|
639
635
|
black: 900
|
|
640
636
|
};
|
|
641
|
-
|
|
637
|
+
const processFontWeight = value => {
|
|
642
638
|
if (!value) return FONT_WEIGHTS.normal;
|
|
643
639
|
if (typeof value === 'number') return value;
|
|
644
|
-
|
|
640
|
+
const lv = value.toLowerCase();
|
|
645
641
|
if (FONT_WEIGHTS[lv]) return FONT_WEIGHTS[lv];
|
|
646
642
|
return value;
|
|
647
643
|
};
|
|
648
644
|
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
};
|
|
652
|
-
var castFloat = function castFloat(value) {
|
|
645
|
+
const matchNumber = value => typeof value === 'string' && /^-?\d*\.?\d*$/.test(value);
|
|
646
|
+
const castFloat = value => {
|
|
653
647
|
if (typeof value !== 'string') return value;
|
|
654
648
|
if (matchNumber(value)) return parseFloat(value);
|
|
655
649
|
return value;
|
|
656
650
|
};
|
|
657
651
|
|
|
658
|
-
|
|
652
|
+
const offsetKeyword = value => {
|
|
659
653
|
switch (value) {
|
|
660
654
|
case 'top':
|
|
661
655
|
case 'left':
|
|
@@ -670,15 +664,11 @@ var offsetKeyword = function offsetKeyword(value) {
|
|
|
670
664
|
}
|
|
671
665
|
};
|
|
672
666
|
|
|
673
|
-
|
|
674
|
-
return offsetKeyword(value) || castFloat(value);
|
|
675
|
-
};
|
|
667
|
+
const transformObjectPosition = value => offsetKeyword(value) || castFloat(value);
|
|
676
668
|
|
|
677
|
-
|
|
678
|
-
return offsetKeyword(value) || castFloat(value);
|
|
679
|
-
};
|
|
669
|
+
const transformTransformOrigin = value => offsetKeyword(value) || castFloat(value);
|
|
680
670
|
|
|
681
|
-
|
|
671
|
+
const handlers = {
|
|
682
672
|
transform: processTransform,
|
|
683
673
|
fontWeight: processFontWeight,
|
|
684
674
|
objectPositionX: transformObjectPosition,
|
|
@@ -686,8 +676,8 @@ var handlers = {
|
|
|
686
676
|
transformOriginX: transformTransformOrigin,
|
|
687
677
|
transformOriginY: transformTransformOrigin
|
|
688
678
|
};
|
|
689
|
-
|
|
690
|
-
|
|
679
|
+
const transformStyle = (key, value, container) => {
|
|
680
|
+
const result = handlers[key] ? handlers[key](value) : value;
|
|
691
681
|
return transformColor(transformUnit(container, castFloat(result)));
|
|
692
682
|
};
|
|
693
683
|
|
|
@@ -703,19 +693,17 @@ var transformStyle = function transformStyle(key, value, container) {
|
|
|
703
693
|
* @param {Object} container
|
|
704
694
|
* @returns {Transform} transform function
|
|
705
695
|
*/
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
return resolvedStyle;
|
|
718
|
-
};
|
|
696
|
+
const transform = container => style => {
|
|
697
|
+
if (!style) return style;
|
|
698
|
+
const propsArray = Object.keys(style);
|
|
699
|
+
const resolvedStyle = {};
|
|
700
|
+
for (let i = 0; i < propsArray.length; i += 1) {
|
|
701
|
+
const key = propsArray[i];
|
|
702
|
+
const value = style[key];
|
|
703
|
+
const transformed = transformStyle(key, value, container);
|
|
704
|
+
resolvedStyle[key] = transformed;
|
|
705
|
+
}
|
|
706
|
+
return resolvedStyle;
|
|
719
707
|
};
|
|
720
708
|
|
|
721
709
|
/**
|
|
@@ -724,14 +712,20 @@ var transform = function transform(container) {
|
|
|
724
712
|
* @param {Object} container
|
|
725
713
|
* @param {Object} styles object
|
|
726
714
|
*/
|
|
727
|
-
|
|
728
|
-
return Object.keys(styles).reduce(
|
|
729
|
-
var _extends2;
|
|
715
|
+
const resolveMediaQueries = (container, styles) => {
|
|
716
|
+
return Object.keys(styles).reduce((acc, key) => {
|
|
730
717
|
if (/@media/.test(key)) {
|
|
731
|
-
|
|
732
|
-
|
|
718
|
+
return {
|
|
719
|
+
...acc,
|
|
720
|
+
...matchMedia({
|
|
721
|
+
[key]: styles[key]
|
|
722
|
+
}, container)
|
|
723
|
+
};
|
|
733
724
|
}
|
|
734
|
-
return
|
|
725
|
+
return {
|
|
726
|
+
...acc,
|
|
727
|
+
[key]: styles[key]
|
|
728
|
+
};
|
|
735
729
|
}, {});
|
|
736
730
|
};
|
|
737
731
|
|
|
@@ -742,10 +736,8 @@ var resolveMediaQueries = function resolveMediaQueries(container, styles) {
|
|
|
742
736
|
* @param {Object} style object
|
|
743
737
|
* @returns {Object} resolved style object
|
|
744
738
|
*/
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
return resolveMediaQueries(container, value);
|
|
748
|
-
};
|
|
739
|
+
const resolveStyles = (container, style) => {
|
|
740
|
+
const computeMediaQueries = value => resolveMediaQueries(container, value);
|
|
749
741
|
return compose(transform(container), expand, computeMediaQueries, flatten)(style);
|
|
750
742
|
};
|
|
751
743
|
|