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