-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCSSProcessor.ts
More file actions
148 lines (141 loc) · 4.42 KB
/
Copy pathCSSProcessor.ts
File metadata and controls
148 lines (141 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { CSSProcessorConfig } from './config';
import { CSSInlineParseRun } from './CSSInlineParseRun';
import { CSSNativeParseRun } from './CSSNativeParseRun';
import { CSSProcessedProps } from './CSSProcessedProps';
import { CSSPropertiesValidationRegistry } from './CSSPropertiesValidationRegistry';
import { defaultCSSProcessorConfig } from './default';
import { createLRUCache, LRUCache } from './lruCache';
import {
ExtraNativeShortStyle,
ExtraNativeTextStyle,
ExtraNativeUntranslatedLongStyles,
ExtraNativeViewStyle
} from './native-types';
import {
WebBlockRetainProperties,
WebTextFlowProperties
} from './processor-types';
// https://www.w3.org/TR/CSS22/
// https://www.w3.org/TR/css3-cascade/
// https://www.w3.org/TR/css-cascade-4/
// https://www.w3.org/TR/css-text-3/
// https://www.w3.org/TR/css3-values/
// https://www.w3.org/TR/css-values-4/
/**
* All those styles that result from processing inline styles.
*/
export type CSSFlattenProcessedTypes =
CSSProcessedProps['native']['text']['flow'] &
CSSProcessedProps['native']['block']['flow'] &
CSSProcessedProps['native']['text']['retain'] &
CSSProcessedProps['native']['block']['retain'];
/**
* These properties can be set to any of the supoprted CSS sizes, including em,
* rem units and special values such as large, larger for `fontSize`, thin,
* medium for `borderWidth`, before passed to {@link CSSProcessor.compileStyleDeclaration}.
*/
export type MixedSizeCSSPropertiesKeys =
| 'fontSize'
| 'borderWidth'
| 'letterSpacing'
| 'bottom'
| 'left'
| 'top'
| 'right'
| 'width'
| 'height'
| 'flexBasis'
| 'borderRadius'
| 'borderBottomLeftRadius'
| 'borderBottomRightRadius'
| 'borderTopLeftRadius'
| 'borderTopRightRadius'
| 'borderWidth'
| 'borderBottomWidth'
| 'borderLeftWidth'
| 'borderRightWidth'
| 'marginBottom'
| 'marginLeft'
| 'marginRight'
| 'marginTop'
| 'margin'
| 'marginHorizontal'
| 'marginVertical'
| 'maxWidth'
| 'maxHeight'
| 'minWidth'
| 'minHeight'
| 'padding'
| 'paddingBottom'
| 'paddingLeft'
| 'paddingRight'
| 'paddingTop'
| 'paddingHorizontal'
| 'paddingVertical';
/**
* A Style object that can contain mixins of a subset of ViewStyle, TextStyle,
* and special style entries such as "whiteSpace", "listStyleType".
*
* @remarks Also note that special lengths,
* such as "em", "rem" units, and special values, such as "%" for fontSize, and
* keyword values ('larger', 'smaller' for fontSize, 'thick', 'thin', 'medium'
* for border*Width) will be handled as per CSS specifications on units.
* Another special use case is fontFamily, which can be a list of font names as
* per the CSS standard. The translated font will be selected with
* {@link CSSProcessorConfig.isFontSupported}.
*/
export type MixedStyleDeclaration = Omit<
CSSFlattenProcessedTypes,
MixedSizeCSSPropertiesKeys
> &
WebTextFlowProperties &
WebBlockRetainProperties &
ExtraNativeTextStyle &
ExtraNativeViewStyle &
ExtraNativeShortStyle &
ExtraNativeUntranslatedLongStyles & {
[k in MixedSizeCSSPropertiesKeys]?: number | string;
};
export class CSSProcessor {
public readonly registry: CSSPropertiesValidationRegistry;
private readonly inlineCssCache: LRUCache<string, CSSProcessedProps> | null;
constructor(userConfig?: Partial<CSSProcessorConfig>) {
const config = {
...defaultCSSProcessorConfig,
...userConfig
};
this.registry = new CSSPropertiesValidationRegistry(config);
this.inlineCssCache = config.enableExperimentalCssLRUCache
? createLRUCache<string, CSSProcessedProps>(
config.maxCssLruCacheSize ?? 256
)
: null;
}
/**
*
* Incoming style declaration:
* - For native styles: any RN compatible style declaration + special units
* (font-size: medium) + relative units (smaller, larger, em, rem and perhaps vw)
*
* @param declaration
*/
compileStyleDeclaration(
declaration: MixedStyleDeclaration
): CSSProcessedProps {
const parseRun = new CSSNativeParseRun(declaration, this.registry);
return parseRun.exec();
}
compileInlineCSS(inlineCSS: string): CSSProcessedProps {
const cache = this.inlineCssCache;
if (cache) {
const cached = cache.get(inlineCSS);
if (cached !== undefined) {
return cached;
}
}
const parseRun = new CSSInlineParseRun(inlineCSS, this.registry);
const result = parseRun.exec();
cache?.set(inlineCSS, result);
return result;
}
}