@react-pdf/stylesheet 4.3.0 → 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.
Files changed (3) hide show
  1. package/lib/index.js +16 -10
  2. package/package.json +4 -12
  3. package/lib/index.cjs +0 -753
package/lib/index.js CHANGED
@@ -22,7 +22,7 @@ const expandFlex = (key, value) => {
22
22
  if (value === 'auto') {
23
23
  defaults = flexAuto;
24
24
  } else {
25
- matches = ("" + value).split(' ');
25
+ matches = `${value}`.split(' ');
26
26
  }
27
27
  const flexGrow = matches[0] || defaults[0];
28
28
  const flexShrink = matches[1] || defaults[1];
@@ -39,7 +39,13 @@ const expandFlex = (key, value) => {
39
39
  // eslint-disable-next-line import/extensions
40
40
  const BOX_MODEL_UNITS = 'px,in,mm,cm,pt,%,vw,vh';
41
41
  const logError = (style, value) => {
42
- console.error("\n @react-pdf/stylesheet parsing error:\n\n " + style + ": " + value + ",\n " + ' '.repeat(style.length + 2) + "^\n Unsupported " + style + " value format\n ");
42
+ console.error(`
43
+ @react-pdf/stylesheet parsing error:
44
+
45
+ ${style}: ${value},
46
+ ${' '.repeat(style.length + 2)}^
47
+ Unsupported ${style} value format
48
+ `);
43
49
  };
44
50
 
45
51
  /**
@@ -55,7 +61,7 @@ const expandBoxModel = function (_temp) {
55
61
  autoSupported = false
56
62
  } = _temp === void 0 ? {} : _temp;
57
63
  return (model, value) => {
58
- const nodes = parse$1("" + value);
64
+ const nodes = parse$1(`${value}`);
59
65
  const parts = [];
60
66
  for (let i = 0; i < nodes.length; i++) {
61
67
  const node = nodes[i];
@@ -159,16 +165,16 @@ const processMarginSingle = expandBoxModel({
159
165
  const BORDER_SHORTHAND_REGEX = /(-?\d+(\.\d+)?(px|in|mm|cm|pt|vw|vh|px)?)\s(\S+)\s(.+)/;
160
166
  const matchBorderShorthand = value => value.match(BORDER_SHORTHAND_REGEX) || [];
161
167
  const expandBorders = (key, value) => {
162
- const match = matchBorderShorthand("" + value);
168
+ const match = matchBorderShorthand(`${value}`);
163
169
  if (match) {
164
170
  const color = match[5] || value;
165
171
  const style = match[4] || value;
166
172
  const width = match[1] || value;
167
173
  if (key.match(/(Top|Right|Bottom|Left)$/)) {
168
174
  return {
169
- [key + "Color"]: color,
170
- [key + "Style"]: style,
171
- [key + "Width"]: width
175
+ [`${key}Color`]: color,
176
+ [`${key}Style`]: style,
177
+ [`${key}Width`]: width
172
178
  };
173
179
  }
174
180
  if (key.match(/Color$/)) {
@@ -267,7 +273,7 @@ const processPaddingHorizontal = expandBoxModel({
267
273
  const processPaddingSingle = expandBoxModel();
268
274
 
269
275
  const expandObjectPosition = (key, value) => {
270
- const match = ("" + value).split(' ');
276
+ const match = `${value}`.split(' ');
271
277
  return {
272
278
  objectPositionX: (match === null || match === void 0 ? void 0 : match[0]) || value,
273
279
  objectPositionY: (match === null || match === void 0 ? void 0 : match[1]) || value
@@ -291,7 +297,7 @@ const getTransformOriginPair = values => {
291
297
 
292
298
  // Transforms shorthand transformOrigin values
293
299
  const expandTransformOrigin = (key, value) => {
294
- const match = ("" + value).split(' ');
300
+ const match = `${value}`.split(' ');
295
301
  const pair = getTransformOriginPair(match);
296
302
  return {
297
303
  transformOriginX: pair[0],
@@ -300,7 +306,7 @@ const expandTransformOrigin = (key, value) => {
300
306
  };
301
307
 
302
308
  const expandGap = (key, value) => {
303
- const match = ("" + value).split(' ');
309
+ const match = `${value}`.split(' ');
304
310
  return {
305
311
  rowGap: (match === null || match === void 0 ? void 0 : match[0]) || value,
306
312
  columnGap: (match === null || match === void 0 ? void 0 : match[1]) || value
package/package.json CHANGED
@@ -1,20 +1,12 @@
1
1
  {
2
2
  "name": "@react-pdf/stylesheet",
3
- "version": "4.3.0",
3
+ "version": "5.0.0",
4
4
  "license": "MIT",
5
5
  "description": "A styles engine for Node and the browser",
6
6
  "author": "Diego Muracciole <diegomuracciole@gmail.com>",
7
7
  "homepage": "https://github.com/diegomura/react-pdf#readme",
8
8
  "type": "module",
9
- "main": "./lib/index.cjs",
10
- "module": "./lib/index.js",
11
- "exports": {
12
- ".": {
13
- "import": "./lib/index.js",
14
- "require": "./lib/index.cjs",
15
- "default": "./lib/index.js"
16
- }
17
- },
9
+ "main": "./lib/index.js",
18
10
  "repository": {
19
11
  "type": "git",
20
12
  "url": "https://github.com/diegomura/react-pdf.git",
@@ -27,8 +19,8 @@
27
19
  },
28
20
  "dependencies": {
29
21
  "@babel/runtime": "^7.20.13",
30
- "@react-pdf/fns": "2.2.1",
31
- "@react-pdf/types": "^2.6.0",
22
+ "@react-pdf/fns": "3.0.0",
23
+ "@react-pdf/types": "^2.7.0",
32
24
  "color-string": "^1.9.1",
33
25
  "hsl-to-hex": "^1.0.0",
34
26
  "media-engine": "^1.0.3",
package/lib/index.cjs DELETED
@@ -1,753 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var fns = require('@react-pdf/fns');
6
- var parse$1 = require('postcss-value-parser/lib/parse.js');
7
- var parseUnit = require('postcss-value-parser/lib/unit.js');
8
- var hlsToHex = require('hsl-to-hex');
9
- var colorString = require('color-string');
10
- var matchMedia = require('media-engine');
11
-
12
- function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
13
-
14
- var parse__default = /*#__PURE__*/_interopDefaultCompat(parse$1);
15
- var parseUnit__default = /*#__PURE__*/_interopDefaultCompat(parseUnit);
16
- var hlsToHex__default = /*#__PURE__*/_interopDefaultCompat(hlsToHex);
17
- var colorString__default = /*#__PURE__*/_interopDefaultCompat(colorString);
18
- var matchMedia__default = /*#__PURE__*/_interopDefaultCompat(matchMedia);
19
-
20
- // https://developer.mozilla.org/en-US/docs/Web/CSS/flex#values
21
-
22
- // TODO: change flex defaults to [0, 1, 'auto'] as in spec in next major release
23
- const flexDefaults = [1, 1, 0];
24
- /**
25
- * @type {(number | 'auto')[]}
26
- */
27
- const flexAuto = [1, 1, 'auto'];
28
- const expandFlex = (key, value) => {
29
- /**
30
- * @type {(number | 'auto')[]}
31
- */
32
- let defaults = flexDefaults;
33
- let matches = [];
34
- if (value === 'auto') {
35
- defaults = flexAuto;
36
- } else {
37
- matches = ("" + value).split(' ');
38
- }
39
- const flexGrow = matches[0] || defaults[0];
40
- const flexShrink = matches[1] || defaults[1];
41
- const flexBasis = matches[2] || defaults[2];
42
- return {
43
- flexGrow,
44
- flexShrink,
45
- flexBasis
46
- };
47
- };
48
-
49
- /* eslint-disable no-plusplus */
50
- // This file is ran directly with Node - needs to have .js extension
51
- // eslint-disable-next-line import/extensions
52
- const BOX_MODEL_UNITS = 'px,in,mm,cm,pt,%,vw,vh';
53
- const logError = (style, value) => {
54
- console.error("\n @react-pdf/stylesheet parsing error:\n\n " + style + ": " + value + ",\n " + ' '.repeat(style.length + 2) + "^\n Unsupported " + style + " value format\n ");
55
- };
56
-
57
- /**
58
- * @param {Object} options
59
- * @param {Function} [options.expandsTo]
60
- * @param {number} [options.maxValues]
61
- * @param {boolean} [options.autoSupported]
62
- */
63
- const expandBoxModel = function (_temp) {
64
- let {
65
- expandsTo,
66
- maxValues = 1,
67
- autoSupported = false
68
- } = _temp === void 0 ? {} : _temp;
69
- return (model, value) => {
70
- const nodes = parse__default.default("" + value);
71
- const parts = [];
72
- for (let i = 0; i < nodes.length; i++) {
73
- const node = nodes[i];
74
-
75
- // value contains `calc`, `url` or other css function
76
- // `,`, `/` or strings that unsupported by margin and padding
77
- if (node.type === 'function' || node.type === 'string' || node.type === 'div') {
78
- logError(model, value);
79
- return {};
80
- }
81
- if (node.type === 'word') {
82
- if (node.value === 'auto' && autoSupported) {
83
- parts.push(node.value);
84
- } else {
85
- const result = parseUnit__default.default(node.value);
86
-
87
- // when unit isn't specified this condition is true
88
- if (result && BOX_MODEL_UNITS.includes(result.unit)) {
89
- parts.push(node.value);
90
- } else {
91
- logError(model, value);
92
- return {};
93
- }
94
- }
95
- }
96
- }
97
-
98
- // checks that we have enough parsed values
99
- if (parts.length > maxValues) {
100
- logError(model, value);
101
- return {};
102
- }
103
- const first = parts[0];
104
- if (expandsTo) {
105
- const second = parts[1] || parts[0];
106
- const third = parts[2] || parts[0];
107
- const fourth = parts[3] || parts[1] || parts[0];
108
- return expandsTo({
109
- first,
110
- second,
111
- third,
112
- fourth
113
- });
114
- }
115
- return {
116
- [model]: first
117
- };
118
- };
119
- };
120
-
121
- const processMargin = expandBoxModel({
122
- expandsTo: _ref => {
123
- let {
124
- first,
125
- second,
126
- third,
127
- fourth
128
- } = _ref;
129
- return {
130
- marginTop: first,
131
- marginRight: second,
132
- marginBottom: third,
133
- marginLeft: fourth
134
- };
135
- },
136
- maxValues: 4,
137
- autoSupported: true
138
- });
139
- const processMarginVertical = expandBoxModel({
140
- expandsTo: _ref2 => {
141
- let {
142
- first,
143
- second
144
- } = _ref2;
145
- return {
146
- marginTop: first,
147
- marginBottom: second
148
- };
149
- },
150
- maxValues: 2,
151
- autoSupported: true
152
- });
153
- const processMarginHorizontal = expandBoxModel({
154
- expandsTo: _ref3 => {
155
- let {
156
- first,
157
- second
158
- } = _ref3;
159
- return {
160
- marginRight: first,
161
- marginLeft: second
162
- };
163
- },
164
- maxValues: 2,
165
- autoSupported: true
166
- });
167
- const processMarginSingle = expandBoxModel({
168
- autoSupported: true
169
- });
170
-
171
- const BORDER_SHORTHAND_REGEX = /(-?\d+(\.\d+)?(px|in|mm|cm|pt|vw|vh|px)?)\s(\S+)\s(.+)/;
172
- const matchBorderShorthand = value => value.match(BORDER_SHORTHAND_REGEX) || [];
173
- const expandBorders = (key, value) => {
174
- const match = matchBorderShorthand("" + value);
175
- if (match) {
176
- const color = match[5] || value;
177
- const style = match[4] || value;
178
- const width = match[1] || value;
179
- if (key.match(/(Top|Right|Bottom|Left)$/)) {
180
- return {
181
- [key + "Color"]: color,
182
- [key + "Style"]: style,
183
- [key + "Width"]: width
184
- };
185
- }
186
- if (key.match(/Color$/)) {
187
- return {
188
- borderTopColor: color,
189
- borderRightColor: color,
190
- borderBottomColor: color,
191
- borderLeftColor: color
192
- };
193
- }
194
- if (key.match(/Style$/)) {
195
- return {
196
- borderTopStyle: style,
197
- borderRightStyle: style,
198
- borderBottomStyle: style,
199
- borderLeftStyle: style
200
- };
201
- }
202
- if (key.match(/Width$/)) {
203
- return {
204
- borderTopWidth: width,
205
- borderRightWidth: width,
206
- borderBottomWidth: width,
207
- borderLeftWidth: width
208
- };
209
- }
210
- if (key.match(/Radius$/)) {
211
- return {
212
- borderTopLeftRadius: value,
213
- borderTopRightRadius: value,
214
- borderBottomRightRadius: value,
215
- borderBottomLeftRadius: value
216
- };
217
- }
218
- return {
219
- borderTopColor: color,
220
- borderTopStyle: style,
221
- borderTopWidth: width,
222
- borderRightColor: color,
223
- borderRightStyle: style,
224
- borderRightWidth: width,
225
- borderBottomColor: color,
226
- borderBottomStyle: style,
227
- borderBottomWidth: width,
228
- borderLeftColor: color,
229
- borderLeftStyle: style,
230
- borderLeftWidth: width
231
- };
232
- }
233
- return value;
234
- };
235
-
236
- const processPadding = expandBoxModel({
237
- expandsTo: _ref => {
238
- let {
239
- first,
240
- second,
241
- third,
242
- fourth
243
- } = _ref;
244
- return {
245
- paddingTop: first,
246
- paddingRight: second,
247
- paddingBottom: third,
248
- paddingLeft: fourth
249
- };
250
- },
251
- maxValues: 4
252
- });
253
- const processPaddingVertical = expandBoxModel({
254
- expandsTo: _ref2 => {
255
- let {
256
- first,
257
- second
258
- } = _ref2;
259
- return {
260
- paddingTop: first,
261
- paddingBottom: second
262
- };
263
- },
264
- maxValues: 2
265
- });
266
- const processPaddingHorizontal = expandBoxModel({
267
- expandsTo: _ref3 => {
268
- let {
269
- first,
270
- second
271
- } = _ref3;
272
- return {
273
- paddingRight: first,
274
- paddingLeft: second
275
- };
276
- },
277
- maxValues: 2
278
- });
279
- const processPaddingSingle = expandBoxModel();
280
-
281
- const expandObjectPosition = (key, value) => {
282
- const match = ("" + value).split(' ');
283
- return {
284
- objectPositionX: (match === null || match === void 0 ? void 0 : match[0]) || value,
285
- objectPositionY: (match === null || match === void 0 ? void 0 : match[1]) || value
286
- };
287
- };
288
-
289
- const Y_AXIS_SHORTHANDS = {
290
- top: true,
291
- bottom: true
292
- };
293
- const sortTransformOriginPair = (a, b) => {
294
- if (Y_AXIS_SHORTHANDS[a]) return 1;
295
- if (Y_AXIS_SHORTHANDS[b]) return -1;
296
- return 0;
297
- };
298
- const getTransformOriginPair = values => {
299
- if (!values || values.length === 0) return ['center', 'center'];
300
- const pair = values.length === 1 ? [values[0], 'center'] : values;
301
- return pair.sort(sortTransformOriginPair);
302
- };
303
-
304
- // Transforms shorthand transformOrigin values
305
- const expandTransformOrigin = (key, value) => {
306
- const match = ("" + value).split(' ');
307
- const pair = getTransformOriginPair(match);
308
- return {
309
- transformOriginX: pair[0],
310
- transformOriginY: pair[1]
311
- };
312
- };
313
-
314
- const expandGap = (key, value) => {
315
- const match = ("" + value).split(' ');
316
- return {
317
- rowGap: (match === null || match === void 0 ? void 0 : match[0]) || value,
318
- columnGap: (match === null || match === void 0 ? void 0 : match[1]) || value
319
- };
320
- };
321
-
322
- const shorthands = {
323
- flex: expandFlex,
324
- gap: expandGap,
325
- margin: processMargin,
326
- marginHorizontal: processMarginHorizontal,
327
- marginVertical: processMarginVertical,
328
- marginTop: processMarginSingle,
329
- marginRight: processMarginSingle,
330
- marginBottom: processMarginSingle,
331
- marginLeft: processMarginSingle,
332
- padding: processPadding,
333
- paddingHorizontal: processPaddingHorizontal,
334
- paddingVertical: processPaddingVertical,
335
- paddingTop: processPaddingSingle,
336
- paddingRight: processPaddingSingle,
337
- paddingBottom: processPaddingSingle,
338
- paddingLeft: processPaddingSingle,
339
- border: expandBorders,
340
- borderTop: expandBorders,
341
- borderRight: expandBorders,
342
- borderBottom: expandBorders,
343
- borderLeft: expandBorders,
344
- borderColor: expandBorders,
345
- borderRadius: expandBorders,
346
- borderStyle: expandBorders,
347
- borderWidth: expandBorders,
348
- objectPosition: expandObjectPosition,
349
- transformOrigin: expandTransformOrigin
350
- };
351
-
352
- /**
353
- * Transforms style key-value
354
- *
355
- * @param {string} key style key
356
- * @param {string} value style value
357
- * @returns {string | Number} transformed style values
358
- */
359
- const expandStyle = (key, value) => {
360
- return shorthands[key] ? shorthands[key](key, value) : {
361
- [key]: value
362
- };
363
- };
364
-
365
- /**
366
- * Expand the shorthand properties.
367
- *
368
- * @param {Object} style object
369
- * @returns {Object} expanded style object
370
- */
371
- const expand = style => {
372
- if (!style) return style;
373
- const propsArray = Object.keys(style);
374
- const resolvedStyle = {};
375
- for (let i = 0; i < propsArray.length; i += 1) {
376
- const key = propsArray[i];
377
- const value = style[key];
378
- const extended = expandStyle(key, value);
379
- const keys = Object.keys(extended);
380
- for (let j = 0; j < keys.length; j += 1) {
381
- const propName = keys[j];
382
- const propValue = extended[propName];
383
- resolvedStyle[propName] = propValue;
384
- }
385
- }
386
- return resolvedStyle;
387
- };
388
-
389
- /**
390
- * Remove nil values from array
391
- *
392
- * @template T
393
- * @param {(T | null | undefined)[]} array
394
- * @returns {T[]} array without nils
395
- */
396
- const compact = array => array.filter(Boolean);
397
-
398
- /**
399
- * Merges style objects array
400
- *
401
- * @param {Object[]} styles style objects array
402
- * @returns {Object} merged style object
403
- */
404
- const mergeStyles = styles => styles.reduce((acc, style) => {
405
- const s = Array.isArray(style) ? flatten(style) : style;
406
- Object.keys(s).forEach(key => {
407
- if (s[key] !== null && s[key] !== undefined) {
408
- acc[key] = s[key];
409
- }
410
- });
411
- return acc;
412
- }, {});
413
-
414
- /**
415
- * Flattens an array of style objects, into one aggregated style object.
416
- *
417
- * @param {Object[]} styles style objects array
418
- * @returns {Object} flattened style object
419
- */
420
- const flatten = fns.compose(mergeStyles, compact, fns.castArray);
421
-
422
- /**
423
- * Parses scalar value in value and unit pairs
424
- *
425
- * @param {string} value scalar value
426
- * @returns {Object} parsed value
427
- */
428
- const parseValue = value => {
429
- const match = /^(-?\d*\.?\d+)(in|mm|cm|pt|vh|vw|px)?$/g.exec(value);
430
- return match ? {
431
- value: parseFloat(match[1]),
432
- unit: match[2] || 'pt'
433
- } : {
434
- value,
435
- unit: undefined
436
- };
437
- };
438
-
439
- /**
440
- * Transform given scalar value
441
- *
442
- * @param {Object} container
443
- * @param {string} value styles value
444
- * @returns {Object} transformed value
445
- */
446
- const transformUnit = (container, value) => {
447
- const scalar = parseValue(value);
448
- const dpi = 72; // Removed: container.dpi || 72
449
- const mmFactor = 1 / 25.4 * dpi;
450
- const cmFactor = 1 / 2.54 * dpi;
451
- switch (scalar.unit) {
452
- case 'in':
453
- return scalar.value * dpi;
454
- case 'mm':
455
- return scalar.value * mmFactor;
456
- case 'cm':
457
- return scalar.value * cmFactor;
458
- case 'vh':
459
- return scalar.value * (container.height / 100);
460
- case 'vw':
461
- return scalar.value * (container.width / 100);
462
- default:
463
- return scalar.value;
464
- }
465
- };
466
-
467
- const isRgb = value => /rgba?/g.test(value);
468
- const isHsl = value => /hsla?/g.test(value);
469
-
470
- /**
471
- * Transform rgb color to hexa
472
- *
473
- * @param {string} value styles value
474
- * @returns {Object} transformed value
475
- */
476
- const parseRgb = value => {
477
- const rgb = colorString__default.default.get.rgb(value);
478
- return colorString__default.default.to.hex(rgb);
479
- };
480
-
481
- /**
482
- * Transform Hsl color to hexa
483
- *
484
- * @param {string} value styles value
485
- * @returns {Object} transformed value
486
- */
487
- const parseHsl = value => {
488
- const hsl = colorString__default.default.get.hsl(value).map(Math.round);
489
- const hex = hlsToHex__default.default(...hsl);
490
- return hex.toUpperCase();
491
- };
492
-
493
- /**
494
- * Transform given color to hexa
495
- *
496
- * @param {string} value styles value
497
- * @returns {Object} transformed value
498
- */
499
- const transformColor = value => {
500
- if (isRgb(value)) return parseRgb(value);
501
- if (isHsl(value)) return parseHsl(value);
502
- return value;
503
- };
504
-
505
- const parse = transformString => {
506
- const transforms = transformString.trim().split(/\) |\)/);
507
-
508
- // Handle "initial", "inherit", "unset".
509
- if (transforms.length === 1) {
510
- return [[transforms[0], true]];
511
- }
512
- const parsed = [];
513
- for (let i = 0; i < transforms.length; i += 1) {
514
- const transform = transforms[i];
515
- if (transform) {
516
- const [name, rawValue] = transform.split('(');
517
- const splitChar = rawValue.indexOf(',') >= 0 ? ',' : ' ';
518
- const value = rawValue.split(splitChar).map(val => val.trim());
519
- parsed.push({
520
- operation: name,
521
- value
522
- });
523
- }
524
- }
525
- return parsed;
526
- };
527
- const parseAngle = value => {
528
- const unitsRegexp = /(-?\d*\.?\d*)(\w*)?/i;
529
- const [, angle, unit] = unitsRegexp.exec(value);
530
- const number = Number.parseFloat(angle);
531
- return unit === 'rad' ? number * 180 / Math.PI : number;
532
- };
533
- const normalizeTransformOperation = _ref => {
534
- let {
535
- operation,
536
- value
537
- } = _ref;
538
- switch (operation) {
539
- case 'scale':
540
- {
541
- const [scaleX, scaleY = scaleX] = value.map(num => Number.parseFloat(num));
542
- return {
543
- operation: 'scale',
544
- value: [scaleX, scaleY]
545
- };
546
- }
547
- case 'scaleX':
548
- {
549
- return {
550
- operation: 'scale',
551
- value: [Number.parseFloat(value), 1]
552
- };
553
- }
554
- case 'scaleY':
555
- {
556
- return {
557
- operation: 'scale',
558
- value: [1, Number.parseFloat(value)]
559
- };
560
- }
561
- case 'rotate':
562
- {
563
- return {
564
- operation: 'rotate',
565
- value: [parseAngle(value)]
566
- };
567
- }
568
- case 'translate':
569
- {
570
- return {
571
- operation: 'translate',
572
- value: value.map(num => Number.parseFloat(num))
573
- };
574
- }
575
- case 'translateX':
576
- {
577
- return {
578
- operation: 'translate',
579
- value: [Number.parseFloat(value), 0]
580
- };
581
- }
582
- case 'translateY':
583
- {
584
- return {
585
- operation: 'translate',
586
- value: [0, Number.parseFloat(value)]
587
- };
588
- }
589
- case 'skew':
590
- {
591
- return {
592
- operation: 'skew',
593
- value: value.map(parseAngle)
594
- };
595
- }
596
- case 'skewX':
597
- {
598
- return {
599
- operation: 'skew',
600
- value: [parseAngle(value), 0]
601
- };
602
- }
603
- case 'skewY':
604
- {
605
- return {
606
- operation: 'skew',
607
- value: [0, parseAngle(value)]
608
- };
609
- }
610
- default:
611
- {
612
- return {
613
- operation,
614
- value: value.map(num => Number.parseFloat(num))
615
- };
616
- }
617
- }
618
- };
619
- const normalize = operations => {
620
- return operations.map(operation => normalizeTransformOperation(operation));
621
- };
622
- const processTransform = value => {
623
- if (typeof value !== 'string') return value;
624
- return normalize(parse(value));
625
- };
626
-
627
- const FONT_WEIGHTS = {
628
- thin: 100,
629
- hairline: 100,
630
- ultralight: 200,
631
- extralight: 200,
632
- light: 300,
633
- normal: 400,
634
- medium: 500,
635
- semibold: 600,
636
- demibold: 600,
637
- bold: 700,
638
- ultrabold: 800,
639
- extrabold: 800,
640
- heavy: 900,
641
- black: 900
642
- };
643
- const processFontWeight = value => {
644
- if (!value) return FONT_WEIGHTS.normal;
645
- if (typeof value === 'number') return value;
646
- const lv = value.toLowerCase();
647
- if (FONT_WEIGHTS[lv]) return FONT_WEIGHTS[lv];
648
- return value;
649
- };
650
-
651
- const matchNumber = value => typeof value === 'string' && /^-?\d*\.?\d*$/.test(value);
652
- const castFloat = value => {
653
- if (typeof value !== 'string') return value;
654
- if (matchNumber(value)) return parseFloat(value);
655
- return value;
656
- };
657
-
658
- const offsetKeyword = value => {
659
- switch (value) {
660
- case 'top':
661
- case 'left':
662
- return '0%';
663
- case 'right':
664
- case 'bottom':
665
- return '100%';
666
- case 'center':
667
- return '50%';
668
- default:
669
- return null;
670
- }
671
- };
672
-
673
- const transformObjectPosition = value => offsetKeyword(value) || castFloat(value);
674
-
675
- const transformTransformOrigin = value => offsetKeyword(value) || castFloat(value);
676
-
677
- const handlers = {
678
- transform: processTransform,
679
- fontWeight: processFontWeight,
680
- objectPositionX: transformObjectPosition,
681
- objectPositionY: transformObjectPosition,
682
- transformOriginX: transformTransformOrigin,
683
- transformOriginY: transformTransformOrigin
684
- };
685
- const transformStyle = (key, value, container) => {
686
- const result = handlers[key] ? handlers[key](value) : value;
687
- return transformColor(transformUnit(container, castFloat(result)));
688
- };
689
-
690
- /**
691
- * @typedef {Function} Transform
692
- * @param {Object} style styles object
693
- * @returns {Object} transformed styles
694
- */
695
-
696
- /**
697
- * Transform styles values
698
- *
699
- * @param {Object} container
700
- * @returns {Transform} transform function
701
- */
702
- const transform = container => style => {
703
- if (!style) return style;
704
- const propsArray = Object.keys(style);
705
- const resolvedStyle = {};
706
- for (let i = 0; i < propsArray.length; i += 1) {
707
- const key = propsArray[i];
708
- const value = style[key];
709
- const transformed = transformStyle(key, value, container);
710
- resolvedStyle[key] = transformed;
711
- }
712
- return resolvedStyle;
713
- };
714
-
715
- /**
716
- * Resolves media queries in styles object
717
- *
718
- * @param {Object} container
719
- * @param {Object} styles object
720
- */
721
- const resolveMediaQueries = (container, styles) => {
722
- return Object.keys(styles).reduce((acc, key) => {
723
- if (/@media/.test(key)) {
724
- return {
725
- ...acc,
726
- ...matchMedia__default.default({
727
- [key]: styles[key]
728
- }, container)
729
- };
730
- }
731
- return {
732
- ...acc,
733
- [key]: styles[key]
734
- };
735
- }, {});
736
- };
737
-
738
- /**
739
- * Resolves styles
740
- *
741
- * @param {Object} container
742
- * @param {Object} style object
743
- * @returns {Object} resolved style object
744
- */
745
- const resolveStyles = (container, style) => {
746
- const computeMediaQueries = value => resolveMediaQueries(container, value);
747
- return fns.compose(transform(container), expand, computeMediaQueries, flatten)(style);
748
- };
749
-
750
- exports.default = resolveStyles;
751
- exports.flatten = flatten;
752
- exports.processTransform = processTransform;
753
- exports.transformColor = transformColor;