|
1 | 1 | import { EmotionCache } from '@emotion/utils' |
2 | | -import { |
3 | | - alloc, |
4 | | - dealloc, |
5 | | - delimit, |
6 | | - Element, |
7 | | - from, |
8 | | - Middleware, |
9 | | - next, |
10 | | - peek, |
11 | | - position, |
12 | | - slice, |
13 | | - token |
14 | | -} from 'stylis' |
15 | | - |
16 | | -// based on https://github.com/thysultan/stylis.js/blob/e6843c373ebcbbfade25ebcc23f540ed8508da0a/src/Tokenizer.js#L239-L244 |
17 | | -const identifierWithPointTracking = ( |
18 | | - begin: number, |
19 | | - points: number[], |
20 | | - index: number |
21 | | -) => { |
22 | | - let previous = 0 |
23 | | - let character = 0 |
24 | | - |
25 | | - while (true) { |
26 | | - previous = character |
27 | | - character = peek() |
28 | | - |
29 | | - // &\f |
30 | | - if (previous === 38 && character === 12) { |
31 | | - points[index] = 1 |
32 | | - } |
33 | | - |
34 | | - if (token(character)) { |
35 | | - break |
36 | | - } |
37 | | - |
38 | | - next() |
39 | | - } |
40 | | - |
41 | | - return slice(begin, position) |
42 | | -} |
43 | | - |
44 | | -const toRules = (parsed: string[], points: number[]) => { |
45 | | - // pretend we've started with a comma |
46 | | - let index = -1 |
47 | | - let character = 44 |
48 | | - |
49 | | - do { |
50 | | - switch (token(character)) { |
51 | | - case 0: |
52 | | - // &\f |
53 | | - if (character === 38 && peek() === 12) { |
54 | | - // this is not 100% correct, we don't account for literal sequences here - like for example quoted strings |
55 | | - // stylis inserts \f after & to know when & where it should replace this sequence with the context selector |
56 | | - // and when it should just concatenate the outer and inner selectors |
57 | | - // it's very unlikely for this sequence to actually appear in a different context, so we just leverage this fact here |
58 | | - points[index] = 1 |
59 | | - } |
60 | | - parsed[index] += identifierWithPointTracking( |
61 | | - position - 1, |
62 | | - points, |
63 | | - index |
64 | | - ) |
65 | | - break |
66 | | - case 2: |
67 | | - parsed[index] += delimit(character) |
68 | | - break |
69 | | - case 4: |
70 | | - // comma |
71 | | - if (character === 44) { |
72 | | - // colon |
73 | | - parsed[++index] = peek() === 58 ? '&\f' : '' |
74 | | - points[index] = parsed[index].length |
75 | | - break |
76 | | - } |
77 | | - // fallthrough |
78 | | - default: |
79 | | - parsed[index] += from(character) |
80 | | - } |
81 | | - } while ((character = next())) |
82 | | - |
83 | | - return parsed |
84 | | -} |
85 | | - |
86 | | -const getRules = (value: string, points: number[]) => |
87 | | - dealloc(toRules(alloc(value) as string[], points)) |
88 | | - |
89 | | -// WeakSet would be more appropriate, but only WeakMap is supported in IE11 |
90 | | -const fixedElements = /* #__PURE__ */ new WeakMap() |
91 | | - |
92 | | -export let compat: Middleware = element => { |
93 | | - if ( |
94 | | - element.type !== 'rule' || |
95 | | - !element.parent || |
96 | | - // positive .length indicates that this rule contains pseudo |
97 | | - // negative .length indicates that this rule has been already prefixed |
98 | | - element.length < 1 |
99 | | - ) { |
100 | | - return |
101 | | - } |
102 | | - |
103 | | - let value = element.value |
104 | | - let parent: Element | null = element.parent |
105 | | - let isImplicitRule = |
106 | | - element.column === parent.column && element.line === parent.line |
107 | | - |
108 | | - while (parent.type !== 'rule') { |
109 | | - parent = parent.parent |
110 | | - if (!parent) return |
111 | | - } |
112 | | - |
113 | | - // short-circuit for the simplest case |
114 | | - if ( |
115 | | - element.props.length === 1 && |
116 | | - value.charCodeAt(0) !== 58 /* colon */ && |
117 | | - !fixedElements.get(parent) |
118 | | - ) { |
119 | | - return |
120 | | - } |
121 | | - |
122 | | - // if this is an implicitly inserted rule (the one eagerly inserted at the each new nested level) |
123 | | - // then the props has already been manipulated beforehand as they that array is shared between it and its "rule parent" |
124 | | - if (isImplicitRule) { |
125 | | - return |
126 | | - } |
127 | | - |
128 | | - fixedElements.set(element, true) |
129 | | - |
130 | | - const points: number[] = [] |
131 | | - const rules = getRules(value, points) |
132 | | - const parentRules = parent.props |
133 | | - |
134 | | - for (let i = 0, k = 0; i < rules.length; i++) { |
135 | | - for (let j = 0; j < parentRules.length; j++, k++) { |
136 | | - ;(element.props as string[])[k] = points[i] |
137 | | - ? rules[i].replace(/&\f/g, parentRules[j]) |
138 | | - : `${parentRules[j]} ${rules[i]}` |
139 | | - } |
140 | | - } |
141 | | -} |
| 2 | +import { Element, Middleware } from 'stylis' |
142 | 3 |
|
143 | 4 | export let removeLabel: Middleware = element => { |
144 | 5 | if (element.type === 'decl') { |
|
0 commit comments