ats-form-react-pdf-stylesheet 6.1.2

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/README.md ADDED
@@ -0,0 +1,68 @@
1
+ <p align="center">
2
+ <img src="https://user-images.githubusercontent.com/5600341/27505816-c8bc37aa-587f-11e7-9a86-08a2d081a8b9.png" height="280px">
3
+ </p>
4
+
5
+ # @react-pdf/stylesheet
6
+
7
+ > React-pdf styles engine
8
+
9
+ ## How to install
10
+
11
+ ```sh
12
+ yarn add @react-pdf/stylesheet
13
+ ```
14
+
15
+ ## How it works
16
+
17
+ ```js
18
+ import stylesheet from '@react-pdf/stylesheet';
19
+
20
+ const container = {
21
+ width: 400,
22
+ height: 600,
23
+ orientation: 'portrait',
24
+ };
25
+
26
+ const style = {
27
+ margin: 20,
28
+ width: '50vw',
29
+ height: '20vh',
30
+ borderRadius: 5,
31
+ fontWeight: 'semibold',
32
+ borderBottom: '2 solid yellow',
33
+ '@media max-width: 500': {
34
+ backgroundColor: 'rgb(255, 0, 0)',
35
+ },
36
+ };
37
+
38
+ const computed = stylesheet(container, style);
39
+
40
+ // Computed
41
+ // {
42
+ // width: 200,
43
+ // height: 120,
44
+ // marginTop: 20,
45
+ // marginLeft: 20,
46
+ // marginRight: 20,
47
+ // marginBottom: 20,
48
+ // marginBottom: 20,
49
+ // borderTopLeftRadius: 5,
50
+ // borderTopRightRadius: 5,
51
+ // borderBottomLeftRadius: 5,
52
+ // borderBottomRightRadius: 5,
53
+ // fontWeight: 600,
54
+ // borderBottomWidth: 2,
55
+ // borderBottomStyle: 'solid',
56
+ // borderBottomColor: 'yellow',
57
+ // backgroundColor: '#FF0000'
58
+ // }
59
+ ```
60
+
61
+ This library exports a `stylesheet` function that takes two arguments:
62
+
63
+ - _container_: Container where the styles are being computed into. It specifies the `width` and `height` in points (needed for media queries and unit conversions), and optionally the container `orientation` (needed for certain media queries).
64
+ - _style_: Style to be computed. JS object with raw styles that you would like to get in a normalized format.
65
+
66
+ ## License
67
+
68
+ MIT © [Diego Muracciole](http://github.com/diegomura)
package/lib/index.d.ts ADDED
@@ -0,0 +1,314 @@
1
+ type Container = {
2
+ width: number;
3
+ height: number;
4
+ dpi?: number;
5
+ remBase?: number;
6
+ orientation?: 'landscape' | 'portrait';
7
+ };
8
+ type Percentage = `${string}%`;
9
+ type BorderStyleValue = 'dashed' | 'dotted' | 'solid';
10
+ type BorderShorthandStyle = {
11
+ border?: number | string;
12
+ borderTop?: number | string;
13
+ borderRight?: number | string;
14
+ borderBottom?: number | string;
15
+ borderLeft?: number | string;
16
+ borderColor?: string;
17
+ borderRadius?: number | string;
18
+ borderStyle?: BorderStyleValue;
19
+ borderWidth?: number | string;
20
+ };
21
+ type BorderExpandedStyle = {
22
+ borderTopColor?: string;
23
+ borderTopStyle?: BorderStyleValue;
24
+ borderTopWidth?: number | string;
25
+ borderRightColor?: string;
26
+ borderRightStyle?: BorderStyleValue;
27
+ borderRightWidth?: number | string;
28
+ borderBottomColor?: string;
29
+ borderBottomStyle?: BorderStyleValue;
30
+ borderBottomWidth?: number | string;
31
+ borderLeftColor?: string;
32
+ borderLeftStyle?: BorderStyleValue;
33
+ borderLeftWidth?: number | string;
34
+ borderTopLeftRadius?: number | string;
35
+ borderTopRightRadius?: number | string;
36
+ borderBottomRightRadius?: number | string;
37
+ borderBottomLeftRadius?: number | string;
38
+ };
39
+ type BorderSafeStyle = BorderExpandedStyle & {
40
+ borderTopWidth?: number;
41
+ borderRightWidth?: number;
42
+ borderBottomWidth?: number;
43
+ borderLeftWidth?: number;
44
+ borderTopLeftRadius?: number | Percentage;
45
+ borderTopRightRadius?: number | Percentage;
46
+ borderBottomRightRadius?: number | Percentage;
47
+ borderBottomLeftRadius?: number | Percentage;
48
+ };
49
+ type BorderStyle = BorderShorthandStyle & BorderExpandedStyle;
50
+ type FlexboxShorthandStyle = {
51
+ flex?: number | string;
52
+ };
53
+ type AlignContent = 'flex-start' | 'flex-end' | 'center' | 'stretch' | 'space-between' | 'space-around' | 'space-evenly';
54
+ type AlignItems = 'flex-start' | 'flex-end' | 'center' | 'stretch' | 'baseline';
55
+ type AlignSelf = 'auto' | 'flex-start' | 'flex-end' | 'center' | 'baseline' | 'stretch';
56
+ type FlexDirection = 'row' | 'row-reverse' | 'column' | 'column-reverse';
57
+ type FlexWrap = 'nowrap' | 'wrap' | 'wrap-reverse';
58
+ type JustifyContent = 'flex-start' | 'flex-end' | 'center' | 'space-around' | 'space-between' | 'space-evenly';
59
+ type JustifySelf = string;
60
+ type FlexboxExpandedStyle = {
61
+ alignContent?: AlignContent;
62
+ alignItems?: AlignItems;
63
+ alignSelf?: AlignSelf;
64
+ flexDirection?: FlexDirection;
65
+ flexWrap?: FlexWrap;
66
+ flexFlow?: number | string;
67
+ flexGrow?: number | string;
68
+ flexShrink?: number | string;
69
+ flexBasis?: number | string;
70
+ justifySelf?: JustifySelf;
71
+ justifyContent?: JustifyContent;
72
+ };
73
+ type FlexboxSafeStyle = FlexboxExpandedStyle & {
74
+ flexGrow?: number;
75
+ flexShrink?: number;
76
+ };
77
+ type FlexboxStyle = FlexboxShorthandStyle & FlexboxExpandedStyle;
78
+ type GapShorthandStyle = {
79
+ gap?: number | string;
80
+ };
81
+ type GapExpandedStyle = {
82
+ rowGap?: number | string;
83
+ columnGap?: number | string;
84
+ };
85
+ type GapSafeStyle = {
86
+ rowGap?: number | Percentage;
87
+ columnGap?: number | Percentage;
88
+ };
89
+ type GapStyle = GapShorthandStyle & GapExpandedStyle;
90
+ type PositionShorthandStyle = {
91
+ objectPosition?: number | string;
92
+ };
93
+ type PositionExpandedStyle = {
94
+ objectPositionX?: number | string;
95
+ objectPositionY?: number | string;
96
+ objectFit?: string;
97
+ };
98
+ type PositionSafeStyle = PositionExpandedStyle & {
99
+ objectPositionX?: number;
100
+ objectPositionY?: number;
101
+ };
102
+ type PositioningStyle = PositionShorthandStyle & PositionExpandedStyle;
103
+ type ScaleTransform = {
104
+ operation: 'scale';
105
+ value: [number, number];
106
+ };
107
+ type TranslateTransform = {
108
+ operation: 'translate';
109
+ value: [number, number];
110
+ };
111
+ type RotateTransform = {
112
+ operation: 'rotate';
113
+ value: [number];
114
+ };
115
+ type SkewTransform = {
116
+ operation: 'skew';
117
+ value: [number, number];
118
+ };
119
+ type MatrixTransform = {
120
+ operation: 'matrix';
121
+ value: [number, number, number, number, number, number];
122
+ };
123
+ type Transform = ScaleTransform | TranslateTransform | RotateTransform | SkewTransform | MatrixTransform;
124
+ type TransformShorthandStyle = {
125
+ transformOrigin?: number | string;
126
+ };
127
+ type TransformExpandedStyle = {
128
+ transformOriginX?: number | string;
129
+ transformOriginY?: number | string;
130
+ transform?: string | Transform[];
131
+ gradientTransform?: string | Transform[];
132
+ };
133
+ type TransformSafeStyle = Omit<TransformExpandedStyle, 'transform'> & {
134
+ transformOriginX?: number | Percentage;
135
+ transformOriginY?: number | Percentage;
136
+ transform?: Transform[];
137
+ gradientTransform?: Transform[];
138
+ };
139
+ type TransformStyle = TransformShorthandStyle & TransformExpandedStyle;
140
+ type Display = 'flex' | 'none';
141
+ type Position = 'absolute' | 'relative' | 'static';
142
+ type LayoutStyle = {
143
+ aspectRatio?: number | string;
144
+ bottom?: number | string;
145
+ display?: Display;
146
+ left?: number | string;
147
+ position?: Position;
148
+ right?: number | string;
149
+ top?: number | string;
150
+ overflow?: 'hidden';
151
+ zIndex?: number | string;
152
+ };
153
+ type LayoutExpandedStyle = LayoutStyle;
154
+ type LayoutSafeStyle = LayoutExpandedStyle & {
155
+ aspectRatio?: number;
156
+ bottom?: number;
157
+ left?: number;
158
+ right?: number;
159
+ top?: number;
160
+ zIndex?: number;
161
+ };
162
+ type DimensionStyle = {
163
+ height?: number | string;
164
+ maxHeight?: number | string;
165
+ maxWidth?: number | string;
166
+ minHeight?: number | string;
167
+ minWidth?: number | string;
168
+ width?: number | string;
169
+ };
170
+ type DimensionExpandedStyle = DimensionStyle;
171
+ type DimensionSafeStyle = DimensionExpandedStyle & {
172
+ height?: number | Percentage;
173
+ maxHeight?: number | Percentage;
174
+ maxWidth?: number | Percentage;
175
+ minHeight?: number | Percentage;
176
+ minWidth?: number | Percentage;
177
+ width?: number | Percentage;
178
+ };
179
+ type ColorStyle = {
180
+ backgroundColor?: string;
181
+ color?: string;
182
+ opacity?: number | string;
183
+ };
184
+ type ColorExpandedStyle = ColorStyle;
185
+ type ColorSafeStyle = {
186
+ backgroundColor?: string;
187
+ color?: string;
188
+ opacity?: number;
189
+ };
190
+ type FontStyle = 'normal' | 'italic' | 'oblique';
191
+ type FontWeight = string | number | 'thin' | 'hairline' | 'ultralight' | 'extralight' | 'light' | 'normal' | 'medium' | 'semibold' | 'demibold' | 'bold' | 'ultrabold' | 'extrabold' | 'heavy' | 'black';
192
+ type TextAlign = 'left' | 'right' | 'center' | 'justify';
193
+ type TextDecoration = 'line-through' | 'underline' | 'none' | 'line-through underline' | 'underline line-through';
194
+ type TextDecorationStyle = 'dashed' | 'dotted' | 'solid' | string;
195
+ type TextTransform = 'capitalize' | 'lowercase' | 'uppercase' | 'upperfirst' | 'none';
196
+ type VerticalAlign = 'sub' | 'super';
197
+ type TextStyle = {
198
+ direction?: 'ltr' | 'rtl';
199
+ fontSize?: number | string;
200
+ fontFamily?: string | string[];
201
+ fontStyle?: FontStyle;
202
+ fontWeight?: FontWeight;
203
+ letterSpacing?: number | string;
204
+ lineHeight?: number | string;
205
+ maxLines?: number;
206
+ textAlign?: TextAlign;
207
+ textDecoration?: TextDecoration;
208
+ textDecorationColor?: string;
209
+ textDecorationStyle?: TextDecorationStyle;
210
+ textIndent?: any;
211
+ textOverflow?: 'ellipsis';
212
+ textTransform?: TextTransform;
213
+ verticalAlign?: VerticalAlign;
214
+ };
215
+ type TextExpandedStyle = TextStyle;
216
+ type TextSafeStyle = TextExpandedStyle & {
217
+ fontSize?: number;
218
+ fontWeight?: number;
219
+ letterSpacing?: number;
220
+ lineHeight?: number;
221
+ };
222
+ type MarginShorthandStyle = {
223
+ margin?: number | string;
224
+ marginHorizontal?: number | string;
225
+ marginVertical?: number | string;
226
+ };
227
+ type MarginExpandedStyle = {
228
+ marginTop?: number | string;
229
+ marginRight?: number | string;
230
+ marginBottom?: number | string;
231
+ marginLeft?: number | string;
232
+ };
233
+ type MarginSafeStyle = MarginExpandedStyle & {
234
+ marginTop?: number | Percentage;
235
+ marginRight?: number | Percentage;
236
+ marginBottom?: number | Percentage;
237
+ marginLeft?: number | Percentage;
238
+ };
239
+ type MarginStyle = MarginShorthandStyle & MarginExpandedStyle;
240
+ type PaddingShorthandStyle = {
241
+ padding?: number | string;
242
+ paddingHorizontal?: number | string;
243
+ paddingVertical?: number | string;
244
+ };
245
+ type PaddingExpandedStyle = {
246
+ paddingTop?: number | string;
247
+ paddingRight?: number | string;
248
+ paddingBottom?: number | string;
249
+ paddingLeft?: number | string;
250
+ };
251
+ type PaddingSafeStyle = PaddingExpandedStyle & {
252
+ paddingTop?: number | Percentage;
253
+ paddingRight?: number | Percentage;
254
+ paddingBottom?: number | Percentage;
255
+ paddingLeft?: number | Percentage;
256
+ };
257
+ type PaddingStyle = PaddingShorthandStyle & PaddingExpandedStyle;
258
+ interface SvgStyle {
259
+ fill?: string;
260
+ stroke?: string;
261
+ strokeDasharray?: string;
262
+ strokeWidth?: string | number;
263
+ fillOpacity?: string | number;
264
+ fillRule?: 'nonzero' | 'evenodd';
265
+ strokeOpacity?: string | number;
266
+ textAnchor?: 'start' | 'middle' | 'end';
267
+ strokeLinecap?: 'butt' | 'round' | 'square';
268
+ strokeLinejoin?: 'butt' | 'round' | 'square' | 'miter' | 'bevel';
269
+ visibility?: 'visible' | 'hidden' | 'collapse';
270
+ clipPath?: string;
271
+ dominantBaseline?: 'auto' | 'middle' | 'central' | 'hanging' | 'mathematical' | 'text-after-edge' | 'text-before-edge';
272
+ }
273
+ type SvgExpandedStyle = SvgStyle;
274
+ type SvgSafeStyle = SvgStyle & {
275
+ strokeWidth?: number;
276
+ fillOpacity?: number;
277
+ strokeOpacity?: number;
278
+ };
279
+ type BaseStyle = BorderStyle & ColorStyle & DimensionStyle & FlexboxStyle & GapStyle & LayoutStyle & MarginStyle & PaddingStyle & PositioningStyle & TextStyle & TransformStyle & SvgStyle;
280
+ type MediaQueryStyle = {
281
+ [key in `@media${string}`]: BaseStyle;
282
+ };
283
+ type Style = BaseStyle & MediaQueryStyle;
284
+ type StyleKey = keyof BaseStyle;
285
+ type ExpandedStyle = BorderExpandedStyle & ColorExpandedStyle & DimensionExpandedStyle & FlexboxExpandedStyle & GapExpandedStyle & LayoutExpandedStyle & MarginExpandedStyle & PaddingExpandedStyle & PositionExpandedStyle & TextExpandedStyle & TransformExpandedStyle & SvgExpandedStyle;
286
+ type SafeStyle = BorderSafeStyle & ColorSafeStyle & DimensionSafeStyle & FlexboxSafeStyle & GapSafeStyle & LayoutSafeStyle & MarginSafeStyle & PaddingSafeStyle & PositionSafeStyle & TextSafeStyle & TransformSafeStyle & SvgSafeStyle;
287
+
288
+ /**
289
+ * Transform given color to hexa
290
+ *
291
+ * @param value - Styles value
292
+ * @returns Transformed value
293
+ */
294
+ declare const transformColor: (value: string) => string;
295
+
296
+ /**
297
+ * Flattens an array of style objects, into one aggregated style object.
298
+ *
299
+ * @param styles - Style or style array
300
+ * @returns Flattened style object
301
+ */
302
+ declare const flatten: (value: Style | Style[], ...args: any[]) => Style;
303
+
304
+ type StyleParam = Style | null | undefined;
305
+ /**
306
+ * Resolves styles
307
+ *
308
+ * @param container
309
+ * @param style - Style
310
+ * @returns Resolved style
311
+ */
312
+ declare const resolveStyles: (container: Container, style: StyleParam | StyleParam[]) => SafeStyle;
313
+
314
+ export { type AlignContent, type AlignItems, type AlignSelf, type BorderExpandedStyle, type BorderSafeStyle, type BorderShorthandStyle, type BorderStyle, type BorderStyleValue, type ColorExpandedStyle, type ColorSafeStyle, type ColorStyle, type Container, type DimensionExpandedStyle, type DimensionSafeStyle, type DimensionStyle, type Display, type ExpandedStyle, type FlexDirection, type FlexWrap, type FlexboxExpandedStyle, type FlexboxSafeStyle, type FlexboxShorthandStyle, type FlexboxStyle, type FontStyle, type FontWeight, type GapExpandedStyle, type GapSafeStyle, type GapShorthandStyle, type GapStyle, type JustifyContent, type JustifySelf, type LayoutExpandedStyle, type LayoutSafeStyle, type LayoutStyle, type MarginExpandedStyle, type MarginSafeStyle, type MarginShorthandStyle, type MarginStyle, type MatrixTransform, type PaddingExpandedStyle, type PaddingSafeStyle, type PaddingShorthandStyle, type PaddingStyle, type Percentage, type Position, type PositionExpandedStyle, type PositionSafeStyle, type PositionShorthandStyle, type PositioningStyle, type RotateTransform, type SafeStyle, type ScaleTransform, type SkewTransform, type Style, type StyleKey, type SvgExpandedStyle, type SvgSafeStyle, type SvgStyle, type TextAlign, type TextDecoration, type TextDecorationStyle, type TextExpandedStyle, type TextSafeStyle, type TextStyle, type TextTransform, type Transform, type TransformExpandedStyle, type TransformSafeStyle, type TransformShorthandStyle, type TransformStyle, type TranslateTransform, type VerticalAlign, resolveStyles as default, flatten, transformColor };
package/lib/index.js ADDED
@@ -0,0 +1,767 @@
1
+ import { compose, castArray, parseFloat as parseFloat$1, matchPercent } from '@react-pdf/fns';
2
+ import matchMedia from 'media-engine';
3
+ import hlsToHex from 'hsl-to-hex';
4
+ import colorString from 'color-string';
5
+ import parse$1 from 'postcss-value-parser/lib/parse.js';
6
+ import parseUnit from 'postcss-value-parser/lib/unit.js';
7
+
8
+ /**
9
+ * Remove nil values from array
10
+ *
11
+ * @param array - Style array
12
+ * @returns Style array without nils
13
+ */
14
+ const compact = (array) => array.filter(Boolean);
15
+ /**
16
+ * Merges style objects array
17
+ *
18
+ * @param styles - Style array
19
+ * @returns Merged style object
20
+ */
21
+ const mergeStyles = (styles) => styles.reduce((acc, style) => {
22
+ const s = Array.isArray(style) ? flatten(style) : style;
23
+ Object.keys(s).forEach((key) => {
24
+ if (s[key] !== null && s[key] !== undefined) {
25
+ acc[key] = s[key];
26
+ }
27
+ });
28
+ return acc;
29
+ }, {});
30
+ /**
31
+ * Flattens an array of style objects, into one aggregated style object.
32
+ *
33
+ * @param styles - Style or style array
34
+ * @returns Flattened style object
35
+ */
36
+ const flatten = compose(mergeStyles, compact, (castArray));
37
+
38
+ /**
39
+ * Resolves media queries in styles object
40
+ *
41
+ * @param container - Container for which styles are resolved
42
+ * @param style - Style description
43
+ * @returns Resolved style object
44
+ */
45
+ const resolveMediaQueries = (container, style) => {
46
+ return Object.keys(style).reduce((acc, key) => {
47
+ if (/@media/.test(key)) {
48
+ return {
49
+ ...acc,
50
+ ...matchMedia({ [key]: style[key] }, container),
51
+ };
52
+ }
53
+ return { ...acc, [key]: style[key] };
54
+ }, {});
55
+ };
56
+
57
+ const isRgb = (value) => /rgba?/g.test(value);
58
+ const isHsl = (value) => /hsla?/g.test(value);
59
+ /**
60
+ * Transform rgb color to hexa
61
+ *
62
+ * @param value - Styles value
63
+ * @returns Transformed value
64
+ */
65
+ const parseRgb = (value) => {
66
+ const rgb = colorString.get.rgb(value);
67
+ return colorString.to.hex(rgb);
68
+ };
69
+ /**
70
+ * Transform Hsl color to hexa
71
+ *
72
+ * @param value - Styles value
73
+ * @returns Transformed value
74
+ */
75
+ const parseHsl = (value) => {
76
+ const hsl = colorString.get.hsl(value).map(Math.round);
77
+ const hex = hlsToHex(...hsl);
78
+ return hex.toUpperCase();
79
+ };
80
+ /**
81
+ * Transform given color to hexa
82
+ *
83
+ * @param value - Styles value
84
+ * @returns Transformed value
85
+ */
86
+ const transformColor = (value) => {
87
+ if (isRgb(value))
88
+ return parseRgb(value);
89
+ if (isHsl(value))
90
+ return parseHsl(value);
91
+ return value;
92
+ };
93
+
94
+ /**
95
+ * Parses scalar value in value and unit pairs
96
+ *
97
+ * @param value - Scalar value
98
+ * @returns Parsed value
99
+ */
100
+ const parseValue = (value) => {
101
+ if (typeof value === 'number')
102
+ return { value, unit: undefined };
103
+ const match = /^(-?\d*\.?\d+)(in|mm|cm|pt|vh|vw|px|rem)?$/g.exec(value);
104
+ return match
105
+ ? { value: parseFloat(match[1]), unit: match[2] || 'pt' }
106
+ : { value, unit: undefined };
107
+ };
108
+ /**
109
+ * Transform given scalar value
110
+ *
111
+ * @param container
112
+ * @param value - Styles value
113
+ * @returns Transformed value
114
+ */
115
+ const transformUnit = (container, value) => {
116
+ const scalar = parseValue(value);
117
+ const outputDpi = 72;
118
+ const inputDpi = container.dpi || 72;
119
+ const mmFactor = (1 / 25.4) * outputDpi;
120
+ const cmFactor = (1 / 2.54) * outputDpi;
121
+ if (typeof scalar.value !== 'number')
122
+ return scalar.value;
123
+ switch (scalar.unit) {
124
+ case 'rem':
125
+ return scalar.value * (container.remBase || 18);
126
+ case 'in':
127
+ return scalar.value * outputDpi;
128
+ case 'mm':
129
+ return scalar.value * mmFactor;
130
+ case 'cm':
131
+ return scalar.value * cmFactor;
132
+ case 'vh':
133
+ return scalar.value * (container.height / 100);
134
+ case 'vw':
135
+ return scalar.value * (container.width / 100);
136
+ case 'px':
137
+ return Math.round(scalar.value * (outputDpi / inputDpi));
138
+ default:
139
+ return scalar.value;
140
+ }
141
+ };
142
+
143
+ const processNumberValue = (key, value) => ({
144
+ [key]: parseFloat$1(value),
145
+ });
146
+ const processUnitValue = (key, value, container) => ({
147
+ [key]: transformUnit(container, value),
148
+ });
149
+ const processColorValue = (key, value) => {
150
+ const result = { [key]: transformColor(value) };
151
+ return result;
152
+ };
153
+ const processNoopValue = (key, value) => ({
154
+ [key]: value,
155
+ });
156
+
157
+ const BORDER_SHORTHAND_REGEX = /(-?\d+(\.\d+)?(in|mm|cm|pt|vw|vh|px|rem)?)\s(\S+)\s(.+)/;
158
+ const matchBorderShorthand = (value) => value.match(BORDER_SHORTHAND_REGEX) || [];
159
+ const resolveBorderShorthand = (key, value, container) => {
160
+ const match = matchBorderShorthand(`${value}`);
161
+ if (match) {
162
+ const widthMatch = match[1] || value;
163
+ const styleMatch = match[4] || value || 'solid';
164
+ const colorMatch = match[5] || value;
165
+ const style = styleMatch;
166
+ const color = colorMatch ? transformColor(colorMatch) : undefined;
167
+ const width = typeof widthMatch !== 'undefined'
168
+ ? transformUnit(container, widthMatch)
169
+ : undefined;
170
+ if (key.match(/(Top|Right|Bottom|Left)$/)) {
171
+ return {
172
+ [`${key}Color`]: color,
173
+ [`${key}Style`]: style,
174
+ [`${key}Width`]: width,
175
+ };
176
+ }
177
+ if (key.match(/Color$/)) {
178
+ return {
179
+ borderTopColor: color,
180
+ borderRightColor: color,
181
+ borderBottomColor: color,
182
+ borderLeftColor: color,
183
+ };
184
+ }
185
+ if (key.match(/Style$/)) {
186
+ if (typeof style === 'number')
187
+ throw new Error(`Invalid border style: ${style}`);
188
+ return {
189
+ borderTopStyle: style,
190
+ borderRightStyle: style,
191
+ borderBottomStyle: style,
192
+ borderLeftStyle: style,
193
+ };
194
+ }
195
+ if (key.match(/Width$/)) {
196
+ if (typeof width !== 'number')
197
+ throw new Error(`Invalid border width: ${width}`);
198
+ return {
199
+ borderTopWidth: width,
200
+ borderRightWidth: width,
201
+ borderBottomWidth: width,
202
+ borderLeftWidth: width,
203
+ };
204
+ }
205
+ if (key.match(/Radius$/)) {
206
+ const radius = value ? transformUnit(container, value) : undefined;
207
+ if (typeof radius !== 'number')
208
+ throw new Error(`Invalid border radius: ${radius}`);
209
+ return {
210
+ borderTopLeftRadius: radius,
211
+ borderTopRightRadius: radius,
212
+ borderBottomRightRadius: radius,
213
+ borderBottomLeftRadius: radius,
214
+ };
215
+ }
216
+ if (typeof width !== 'number')
217
+ throw new Error(`Invalid border width: ${width}`);
218
+ if (typeof style === 'number')
219
+ throw new Error(`Invalid border style: ${style}`);
220
+ return {
221
+ borderTopColor: color,
222
+ borderTopStyle: style,
223
+ borderTopWidth: width,
224
+ borderRightColor: color,
225
+ borderRightStyle: style,
226
+ borderRightWidth: width,
227
+ borderBottomColor: color,
228
+ borderBottomStyle: style,
229
+ borderBottomWidth: width,
230
+ borderLeftColor: color,
231
+ borderLeftStyle: style,
232
+ borderLeftWidth: width,
233
+ };
234
+ }
235
+ return { [key]: value };
236
+ };
237
+ const handlers$b = {
238
+ border: (resolveBorderShorthand),
239
+ borderBottom: (resolveBorderShorthand),
240
+ borderBottomColor: (processColorValue),
241
+ borderBottomLeftRadius: (processUnitValue),
242
+ borderBottomRightRadius: (processUnitValue),
243
+ borderBottomStyle: (processNoopValue),
244
+ borderBottomWidth: (processUnitValue),
245
+ borderColor: (resolveBorderShorthand),
246
+ borderLeft: (resolveBorderShorthand),
247
+ borderLeftColor: (processColorValue),
248
+ borderLeftStyle: (processNoopValue),
249
+ borderLeftWidth: (processUnitValue),
250
+ borderRadius: (resolveBorderShorthand),
251
+ borderRight: (resolveBorderShorthand),
252
+ borderRightColor: (processColorValue),
253
+ borderRightStyle: (processNoopValue),
254
+ borderRightWidth: (processUnitValue),
255
+ borderStyle: (resolveBorderShorthand),
256
+ borderTop: (resolveBorderShorthand),
257
+ borderTopColor: (processColorValue),
258
+ borderTopLeftRadius: (processUnitValue),
259
+ borderTopRightRadius: (processUnitValue),
260
+ borderTopStyle: (processNoopValue),
261
+ borderTopWidth: (processUnitValue),
262
+ borderWidth: (resolveBorderShorthand),
263
+ };
264
+
265
+ const handlers$a = {
266
+ backgroundColor: (processColorValue),
267
+ color: (processColorValue),
268
+ opacity: (processNumberValue),
269
+ };
270
+
271
+ const handlers$9 = {
272
+ height: (processUnitValue),
273
+ maxHeight: (processUnitValue),
274
+ maxWidth: (processUnitValue),
275
+ minHeight: (processUnitValue),
276
+ minWidth: (processUnitValue),
277
+ width: (processUnitValue),
278
+ };
279
+
280
+ // https://developer.mozilla.org/en-US/docs/Web/CSS/flex#values
281
+ // TODO: change flex defaults to [0, 1, 'auto'] as in spec in next major release
282
+ const flexDefaults = [1, 1, 0];
283
+ const flexAuto = [1, 1, 'auto'];
284
+ const processFlexShorthand = (key, value, container) => {
285
+ let defaults = flexDefaults;
286
+ let matches = [];
287
+ if (value === 'auto') {
288
+ defaults = flexAuto;
289
+ }
290
+ else {
291
+ matches = `${value}`.split(' ');
292
+ }
293
+ const flexGrow = parseFloat$1(matches[0] || defaults[0]);
294
+ const flexShrink = parseFloat$1(matches[1] || defaults[1]);
295
+ const flexBasis = transformUnit(container, matches[2] || defaults[2]);
296
+ return { flexGrow, flexShrink, flexBasis };
297
+ };
298
+ const handlers$8 = {
299
+ alignContent: (processNoopValue),
300
+ alignItems: (processNoopValue),
301
+ alignSelf: (processNoopValue),
302
+ flex: (processFlexShorthand),
303
+ flexBasis: (processUnitValue),
304
+ flexDirection: (processNoopValue),
305
+ flexFlow: (processNoopValue),
306
+ flexGrow: (processNumberValue),
307
+ flexShrink: (processNumberValue),
308
+ flexWrap: (processNoopValue),
309
+ justifyContent: (processNoopValue),
310
+ justifySelf: (processNoopValue),
311
+ };
312
+
313
+ const processGapShorthand = (key, value, container) => {
314
+ const match = `${value}`.split(' ');
315
+ const rowGap = transformUnit(container, match?.[0] || value);
316
+ const columnGap = transformUnit(container, match?.[1] || value);
317
+ return { rowGap, columnGap };
318
+ };
319
+ const handlers$7 = {
320
+ gap: (processGapShorthand),
321
+ columnGap: (processUnitValue),
322
+ rowGap: (processUnitValue),
323
+ };
324
+
325
+ const handlers$6 = {
326
+ aspectRatio: (processNumberValue),
327
+ bottom: (processUnitValue),
328
+ display: (processNoopValue),
329
+ left: (processUnitValue),
330
+ position: (processNoopValue),
331
+ right: (processUnitValue),
332
+ top: (processUnitValue),
333
+ overflow: (processNoopValue),
334
+ zIndex: (processNumberValue),
335
+ };
336
+
337
+ const BOX_MODEL_UNITS = 'px,in,mm,cm,pt,%,vw,vh';
338
+ const logError = (style, value) => {
339
+ const name = style.toString();
340
+ // eslint-disable-next-line no-console
341
+ console.error(`
342
+ @react-pdf/stylesheet parsing error:
343
+ ${name}: ${value},
344
+ ${' '.repeat(name.length + 2)}^
345
+ Unsupported ${name} value format
346
+ `);
347
+ };
348
+ /**
349
+ * @param options
350
+ * @param [options.expandsTo]
351
+ * @param [options.maxValues]
352
+ * @param [options.autoSupported]
353
+ */
354
+ const expandBoxModel = ({ expandsTo, maxValues = 1, autoSupported = false, } = {}) => (model, value, container) => {
355
+ const nodes = parse$1(`${value}`);
356
+ const parts = [];
357
+ for (let i = 0; i < nodes.length; i++) {
358
+ const node = nodes[i];
359
+ // value contains `calc`, `url` or other css function
360
+ // `,`, `/` or strings that unsupported by margin and padding
361
+ if (node.type === 'function' ||
362
+ node.type === 'string' ||
363
+ node.type === 'div') {
364
+ logError(model, value);
365
+ return {};
366
+ }
367
+ if (node.type === 'word') {
368
+ if (node.value === 'auto' && autoSupported) {
369
+ parts.push(node.value);
370
+ }
371
+ else {
372
+ const result = parseUnit(node.value);
373
+ // when unit isn't specified this condition is true
374
+ if (result && BOX_MODEL_UNITS.includes(result.unit)) {
375
+ parts.push(node.value);
376
+ }
377
+ else {
378
+ logError(model, value);
379
+ return {};
380
+ }
381
+ }
382
+ }
383
+ }
384
+ // checks that we have enough parsed values
385
+ if (parts.length > maxValues) {
386
+ logError(model, value);
387
+ return {};
388
+ }
389
+ const first = transformUnit(container, parts[0]);
390
+ if (expandsTo) {
391
+ const second = transformUnit(container, parts[1] || parts[0]);
392
+ const third = transformUnit(container, parts[2] || parts[0]);
393
+ const fourth = transformUnit(container, parts[3] || parts[1] || parts[0]);
394
+ return expandsTo({ first, second, third, fourth });
395
+ }
396
+ return {
397
+ [model]: first,
398
+ };
399
+ };
400
+
401
+ const processMargin = expandBoxModel({
402
+ expandsTo: ({ first, second, third, fourth }) => ({
403
+ marginTop: first,
404
+ marginRight: second,
405
+ marginBottom: third,
406
+ marginLeft: fourth,
407
+ }),
408
+ maxValues: 4,
409
+ autoSupported: true,
410
+ });
411
+ const processMarginVertical = expandBoxModel({
412
+ expandsTo: ({ first, second }) => ({
413
+ marginTop: first,
414
+ marginBottom: second,
415
+ }),
416
+ maxValues: 2,
417
+ autoSupported: true,
418
+ });
419
+ const processMarginHorizontal = expandBoxModel({
420
+ expandsTo: ({ first, second }) => ({
421
+ marginRight: first,
422
+ marginLeft: second,
423
+ }),
424
+ maxValues: 2,
425
+ autoSupported: true,
426
+ });
427
+ const processMarginSingle = expandBoxModel({
428
+ autoSupported: true,
429
+ });
430
+ const handlers$5 = {
431
+ margin: (processMargin),
432
+ marginBottom: (processMarginSingle),
433
+ marginHorizontal: (processMarginHorizontal),
434
+ marginLeft: (processMarginSingle),
435
+ marginRight: (processMarginSingle),
436
+ marginTop: (processMarginSingle),
437
+ marginVertical: (processMarginVertical),
438
+ };
439
+
440
+ const processPadding = expandBoxModel({
441
+ expandsTo: ({ first, second, third, fourth }) => ({
442
+ paddingTop: first,
443
+ paddingRight: second,
444
+ paddingBottom: third,
445
+ paddingLeft: fourth,
446
+ }),
447
+ maxValues: 4,
448
+ });
449
+ const processPaddingVertical = expandBoxModel({
450
+ expandsTo: ({ first, second }) => ({
451
+ paddingTop: first,
452
+ paddingBottom: second,
453
+ }),
454
+ maxValues: 2,
455
+ });
456
+ const processPaddingHorizontal = expandBoxModel({
457
+ expandsTo: ({ first, second }) => ({
458
+ paddingRight: first,
459
+ paddingLeft: second,
460
+ }),
461
+ maxValues: 2,
462
+ });
463
+ const processPaddingSingle = expandBoxModel();
464
+ const handlers$4 = {
465
+ padding: (processPadding),
466
+ paddingBottom: (processPaddingSingle),
467
+ paddingHorizontal: (processPaddingHorizontal),
468
+ paddingLeft: (processPaddingSingle),
469
+ paddingRight: (processPaddingSingle),
470
+ paddingTop: (processPaddingSingle),
471
+ paddingVertical: (processPaddingVertical),
472
+ };
473
+
474
+ const offsetKeyword = (value) => {
475
+ switch (value) {
476
+ case 'top':
477
+ case 'left':
478
+ return '0%';
479
+ case 'right':
480
+ case 'bottom':
481
+ return '100%';
482
+ case 'center':
483
+ return '50%';
484
+ default:
485
+ return value;
486
+ }
487
+ };
488
+
489
+ const processObjectPosition = (key, value, container) => {
490
+ const match = `${value}`.split(' ');
491
+ const objectPositionX = offsetKeyword(transformUnit(container, match?.[0] || value));
492
+ const objectPositionY = offsetKeyword(transformUnit(container, match?.[1] || value));
493
+ return { objectPositionX, objectPositionY };
494
+ };
495
+ const processObjectPositionValue = (key, value, container) => ({
496
+ [key]: offsetKeyword(transformUnit(container, value)),
497
+ });
498
+ const handlers$3 = {
499
+ objectPosition: (processObjectPosition),
500
+ objectPositionX: (processObjectPositionValue),
501
+ objectPositionY: (processObjectPositionValue),
502
+ objectFit: (processNoopValue),
503
+ };
504
+
505
+ const castInt = (value) => {
506
+ if (typeof value === 'number')
507
+ return value;
508
+ return parseInt(value, 10);
509
+ };
510
+
511
+ const FONT_WEIGHTS = {
512
+ thin: 100,
513
+ hairline: 100,
514
+ ultralight: 200,
515
+ extralight: 200,
516
+ light: 300,
517
+ normal: 400,
518
+ medium: 500,
519
+ semibold: 600,
520
+ demibold: 600,
521
+ bold: 700,
522
+ ultrabold: 800,
523
+ extrabold: 800,
524
+ heavy: 900,
525
+ black: 900,
526
+ };
527
+ const transformFontWeight = (value) => {
528
+ if (!value)
529
+ return FONT_WEIGHTS.normal;
530
+ if (typeof value === 'number')
531
+ return value;
532
+ const lv = value.toLowerCase();
533
+ if (FONT_WEIGHTS[lv])
534
+ return FONT_WEIGHTS[lv];
535
+ return castInt(value);
536
+ };
537
+ const processFontWeight = (key, value) => {
538
+ return { [key]: transformFontWeight(value) };
539
+ };
540
+ const transformLineHeight = (value, styles, container) => {
541
+ if (value === '')
542
+ return value;
543
+ const fontSize = transformUnit(container, styles.fontSize || 18);
544
+ const lineHeight = transformUnit(container, value);
545
+ // Percent values: use this number multiplied by the element's font size
546
+ const { percent } = matchPercent(lineHeight) || {};
547
+ if (percent)
548
+ return percent * fontSize;
549
+ // Unitless values: use this number multiplied by the element's font size
550
+ return isNaN(value) ? lineHeight : lineHeight * fontSize;
551
+ };
552
+ const processLineHeight = (key, value, container, styles) => {
553
+ return {
554
+ [key]: transformLineHeight(value, styles, container),
555
+ };
556
+ };
557
+ const handlers$2 = {
558
+ direction: (processNoopValue),
559
+ fontFamily: (processNoopValue),
560
+ fontSize: (processUnitValue),
561
+ fontStyle: (processNoopValue),
562
+ fontWeight: (processFontWeight),
563
+ letterSpacing: (processUnitValue),
564
+ lineHeight: (processLineHeight),
565
+ maxLines: (processNumberValue),
566
+ textAlign: (processNoopValue),
567
+ textDecoration: (processNoopValue),
568
+ textDecorationColor: (processColorValue),
569
+ textDecorationStyle: (processNoopValue),
570
+ textIndent: (processNoopValue),
571
+ textOverflow: (processNoopValue),
572
+ textTransform: (processNoopValue),
573
+ verticalAlign: (processNoopValue),
574
+ };
575
+
576
+ const matchNumber = (value) => typeof value === 'string' && /^-?\d*\.?\d*$/.test(value);
577
+ const castFloat = (value) => {
578
+ if (typeof value !== 'string')
579
+ return value;
580
+ if (matchNumber(value))
581
+ return parseFloat(value);
582
+ return value;
583
+ };
584
+
585
+ const parse = (transformString) => {
586
+ const transforms = transformString.trim().split(/\)[ ,]|\)/);
587
+ // Handle "initial", "inherit", "unset".
588
+ if (transforms.length === 1) {
589
+ return [[transforms[0], true]];
590
+ }
591
+ const parsed = [];
592
+ for (let i = 0; i < transforms.length; i += 1) {
593
+ const transform = transforms[i];
594
+ if (transform) {
595
+ const [name, rawValue] = transform.split('(');
596
+ const splitChar = rawValue.indexOf(',') >= 0 ? ',' : ' ';
597
+ const value = rawValue.split(splitChar).map((val) => val.trim());
598
+ parsed.push({ operation: name.trim(), value });
599
+ }
600
+ }
601
+ return parsed;
602
+ };
603
+ const parseAngle = (value) => {
604
+ const unitsRegexp = /(-?\d*\.?\d*)(\w*)?/i;
605
+ const [, angle, unit] = unitsRegexp.exec(value);
606
+ const number = Number.parseFloat(angle);
607
+ return unit === 'rad' ? (number * 180) / Math.PI : number;
608
+ };
609
+ const normalizeTransformOperation = ({ operation, value }) => {
610
+ switch (operation) {
611
+ case 'scale': {
612
+ const [scaleX, scaleY = scaleX] = value.map((num) => Number.parseFloat(num));
613
+ return { operation: 'scale', value: [scaleX, scaleY] };
614
+ }
615
+ case 'scaleX': {
616
+ return { operation: 'scale', value: [Number.parseFloat(value), 1] };
617
+ }
618
+ case 'scaleY': {
619
+ return { operation: 'scale', value: [1, Number.parseFloat(value)] };
620
+ }
621
+ case 'rotate': {
622
+ return { operation: 'rotate', value: [parseAngle(value)] };
623
+ }
624
+ case 'translate': {
625
+ return {
626
+ operation: 'translate',
627
+ value: value.map((num) => Number.parseFloat(num)),
628
+ };
629
+ }
630
+ case 'translateX': {
631
+ return {
632
+ operation: 'translate',
633
+ value: [Number.parseFloat(value), 0],
634
+ };
635
+ }
636
+ case 'translateY': {
637
+ return { operation: 'translate', value: [0, Number.parseFloat(value)] };
638
+ }
639
+ case 'skew': {
640
+ return { operation: 'skew', value: value.map(parseAngle) };
641
+ }
642
+ case 'skewX': {
643
+ return { operation: 'skew', value: [parseAngle(value), 0] };
644
+ }
645
+ case 'skewY': {
646
+ return { operation: 'skew', value: [0, parseAngle(value)] };
647
+ }
648
+ default: {
649
+ return { operation, value: value.map((num) => Number.parseFloat(num)) };
650
+ }
651
+ }
652
+ };
653
+ const normalize = (operations) => {
654
+ return operations.map((operation) => normalizeTransformOperation(operation));
655
+ };
656
+ const processTransform = (key, value) => {
657
+ if (typeof value !== 'string')
658
+ return { [key]: value };
659
+ return { [key]: normalize(parse(value)) };
660
+ };
661
+ const Y_AXIS_SHORTHANDS = { top: true, bottom: true };
662
+ const sortTransformOriginPair = (a, b) => {
663
+ if (Y_AXIS_SHORTHANDS[a])
664
+ return 1;
665
+ if (Y_AXIS_SHORTHANDS[b])
666
+ return -1;
667
+ return 0;
668
+ };
669
+ const getTransformOriginPair = (values) => {
670
+ if (!values || values.length === 0)
671
+ return ['center', 'center'];
672
+ const pair = values.length === 1 ? [values[0], 'center'] : values;
673
+ return pair.sort(sortTransformOriginPair);
674
+ };
675
+ // Transforms shorthand transformOrigin values
676
+ const processTransformOriginShorthand = (key, value, container) => {
677
+ const match = `${value}`.split(' ');
678
+ const pair = getTransformOriginPair(match);
679
+ const transformOriginX = transformUnit(container, pair[0]);
680
+ const transformOriginY = transformUnit(container, pair[1]);
681
+ return {
682
+ transformOriginX: offsetKeyword(transformOriginX) || castFloat(transformOriginX),
683
+ transformOriginY: offsetKeyword(transformOriginY) || castFloat(transformOriginY),
684
+ };
685
+ };
686
+ const processTransformOriginValue = (key, value, container) => {
687
+ const v = transformUnit(container, value);
688
+ return { [key]: offsetKeyword(v) || castFloat(v) };
689
+ };
690
+ const handlers$1 = {
691
+ transform: processTransform,
692
+ gradientTransform: processTransform,
693
+ transformOrigin: (processTransformOriginShorthand),
694
+ transformOriginX: (processTransformOriginValue),
695
+ transformOriginY: (processTransformOriginValue),
696
+ };
697
+
698
+ const handlers = {
699
+ fill: (processColorValue),
700
+ stroke: (processColorValue),
701
+ strokeDasharray: (processNoopValue),
702
+ strokeWidth: (processUnitValue),
703
+ fillOpacity: (processNumberValue),
704
+ strokeOpacity: (processNumberValue),
705
+ fillRule: (processNoopValue),
706
+ textAnchor: (processNoopValue),
707
+ strokeLinecap: (processNoopValue),
708
+ strokeLinejoin: (processNoopValue),
709
+ visibility: (processNoopValue),
710
+ clipPath: (processNoopValue),
711
+ dominantBaseline: (processNoopValue),
712
+ };
713
+
714
+ const shorthands = {
715
+ ...handlers$b,
716
+ ...handlers$a,
717
+ ...handlers$9,
718
+ ...handlers$8,
719
+ ...handlers$7,
720
+ ...handlers$6,
721
+ ...handlers$5,
722
+ ...handlers$4,
723
+ ...handlers$3,
724
+ ...handlers$2,
725
+ ...handlers$1,
726
+ ...handlers,
727
+ };
728
+ /**
729
+ * Expand the shorthand properties.
730
+ *
731
+ * @param style - Style object
732
+ * @returns Expanded style object
733
+ */
734
+ const resolve = (container) => (style) => {
735
+ const propsArray = Object.keys(style);
736
+ const resolvedStyle = {};
737
+ for (let i = 0; i < propsArray.length; i += 1) {
738
+ const key = propsArray[i];
739
+ const value = style[key];
740
+ if (!shorthands[key]) {
741
+ resolvedStyle[key] = value;
742
+ continue;
743
+ }
744
+ const resolved = shorthands[key](key, value, container, style);
745
+ const keys = Object.keys(resolved);
746
+ for (let j = 0; j < keys.length; j += 1) {
747
+ const propName = keys[j];
748
+ const propValue = resolved[propName];
749
+ resolvedStyle[propName] = propValue;
750
+ }
751
+ }
752
+ return resolvedStyle;
753
+ };
754
+
755
+ /**
756
+ * Resolves styles
757
+ *
758
+ * @param container
759
+ * @param style - Style
760
+ * @returns Resolved style
761
+ */
762
+ const resolveStyles = (container, style) => {
763
+ const computeMediaQueries = (value) => resolveMediaQueries(container, value);
764
+ return compose(resolve(container), computeMediaQueries, flatten)(style);
765
+ };
766
+
767
+ export { resolveStyles as default, flatten, transformColor };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "ats-form-react-pdf-stylesheet",
3
+ "version": "6.1.2",
4
+ "license": "MIT",
5
+ "description": "A styles engine for Node and the browser",
6
+ "author": "Diego Muracciole <diegomuracciole@gmail.com>",
7
+ "homepage": "https://github.com/diegomura/react-pdf#readme",
8
+ "type": "module",
9
+ "main": "./lib/index.js",
10
+ "types": "./lib/index.d.ts",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/diegomura/react-pdf.git",
14
+ "directory": "packages/stylesheet"
15
+ },
16
+ "scripts": {
17
+ "test": "vitest",
18
+ "build": "rimraf ./lib && rollup -c",
19
+ "watch": "rimraf ./lib && rollup -c -w",
20
+ "typecheck": "tsc --noEmit"
21
+ },
22
+ "dependencies": {
23
+ "@react-pdf/fns": "3.1.2",
24
+ "@react-pdf/types": "^2.9.1",
25
+ "color-string": "^1.9.1",
26
+ "hsl-to-hex": "^1.0.0",
27
+ "media-engine": "^1.0.3",
28
+ "postcss-value-parser": "^4.1.0"
29
+ },
30
+ "files": [
31
+ "lib"
32
+ ]
33
+ }