@pairdraw/dom-css 0.0.0-stage → 0.16.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/LICENSE +21 -0
- package/README.md +219 -2
- package/dist/browser.d.ts +29 -0
- package/dist/browser.js +2 -0
- package/dist/chunks/behaviours/args.js +59 -0
- package/dist/chunks/behaviours/args.js.map +1 -0
- package/dist/chunks/behaviours/controls.js +109 -0
- package/dist/chunks/behaviours/controls.js.map +1 -0
- package/dist/chunks/behaviours/reset.js +40 -0
- package/dist/chunks/behaviours/reset.js.map +1 -0
- package/dist/chunks/behaviours/roles.js +51 -0
- package/dist/chunks/behaviours/roles.js.map +1 -0
- package/dist/chunks/behaviours/states/conditions.js +103 -0
- package/dist/chunks/behaviours/states/conditions.js.map +1 -0
- package/dist/chunks/behaviours/states/css.js +172 -0
- package/dist/chunks/behaviours/states/css.js.map +1 -0
- package/dist/chunks/behaviours/states/layers.js +1736 -0
- package/dist/chunks/behaviours/states/layers.js.map +1 -0
- package/dist/chunks/behaviours/states/model.js +144 -0
- package/dist/chunks/behaviours/states/model.js.map +1 -0
- package/dist/chunks/behaviours/states/tailwind.js +198 -0
- package/dist/chunks/behaviours/states/tailwind.js.map +1 -0
- package/dist/chunks/browser.js +1352 -0
- package/dist/chunks/browser.js.map +1 -0
- package/dist/chunks/export/bundle.js +337 -0
- package/dist/chunks/export/bundle.js.map +1 -0
- package/dist/chunks/export/components/drawn.js +60 -0
- package/dist/chunks/export/components/drawn.js.map +1 -0
- package/dist/chunks/export/components/fields.js +178 -0
- package/dist/chunks/export/components/fields.js.map +1 -0
- package/dist/chunks/export/components/groups.js +297 -0
- package/dist/chunks/export/components/groups.js.map +1 -0
- package/dist/chunks/export/components/icons.js +260 -0
- package/dist/chunks/export/components/icons.js.map +1 -0
- package/dist/chunks/export/components/layers.js +1594 -0
- package/dist/chunks/export/components/layers.js.map +1 -0
- package/dist/chunks/export/index.js +477 -0
- package/dist/chunks/export/index.js.map +1 -0
- package/dist/chunks/export/storybook/component.js +448 -0
- package/dist/chunks/export/storybook/component.js.map +1 -0
- package/dist/chunks/export/storybook/export.js +410 -0
- package/dist/chunks/export/storybook/export.js.map +1 -0
- package/dist/chunks/import/html.js +406 -0
- package/dist/chunks/import/html.js.map +1 -0
- package/dist/chunks/import/jsx/dev-runtime.js +37 -0
- package/dist/chunks/import/jsx/dev-runtime.js.map +1 -0
- package/dist/chunks/tokens/cssom-validator.js +10 -0
- package/dist/chunks/tokens/cssom-validator.js.map +1 -0
- package/dist/export.d.ts +594 -0
- package/dist/export.js +13 -0
- package/dist/index.d.ts +115 -0
- package/dist/index.js +7 -0
- package/dist/jsx-dev-runtime.d.ts +8 -0
- package/dist/jsx-dev-runtime.js +3 -0
- package/dist/jsx-runtime.d.ts +20 -0
- package/dist/jsx-runtime.js +3 -0
- package/dist/rolldown-runtime.js +13 -0
- package/dist/runtime.d.ts +10 -0
- package/dist/scene-graph.d.ts +2 -0
- package/dist/scene-graph.js +2 -0
- package/dist/scene-graph2.d.ts +10 -0
- package/dist/styling.d.ts +93 -0
- package/dist/tailwind.d.ts +39 -0
- package/dist/types.d.ts +89 -0
- package/package.json +73 -4
- package/src/behaviours/args.ts +85 -0
- package/src/behaviours/controls.ts +195 -0
- package/src/behaviours/index.ts +11 -0
- package/src/behaviours/reset.ts +37 -0
- package/src/behaviours/roles.ts +44 -0
- package/src/behaviours/states/conditions.ts +105 -0
- package/src/behaviours/states/css.ts +123 -0
- package/src/behaviours/states/index.ts +4 -0
- package/src/behaviours/states/layers.ts +205 -0
- package/src/behaviours/states/model.ts +175 -0
- package/src/behaviours/states/names.ts +39 -0
- package/src/behaviours/states/tailwind.ts +148 -0
- package/src/behaviours/states/types.ts +56 -0
- package/src/browser.ts +146 -0
- package/src/cssom.d.ts +38 -0
- package/src/export/bundle.ts +461 -0
- package/src/export/components/drawn.ts +73 -0
- package/src/export/components/fields.ts +178 -0
- package/src/export/components/groups.ts +78 -0
- package/src/export/components/icons.ts +230 -0
- package/src/export/components/layers.ts +121 -0
- package/src/export/components/model.ts +544 -0
- package/src/export/components/properties.ts +167 -0
- package/src/export/components/react/fields.ts +129 -0
- package/src/export/components/react/index.ts +606 -0
- package/src/export/components/react/shared.ts +76 -0
- package/src/export/components/references.ts +212 -0
- package/src/export/components/repeats.ts +128 -0
- package/src/export/components/shaders.ts +125 -0
- package/src/export/components/styling.ts +31 -0
- package/src/export/components/tree.ts +72 -0
- package/src/export/components/vue.ts +517 -0
- package/src/export/css.ts +57 -0
- package/src/export/grid.ts +42 -0
- package/src/export/html.ts +163 -0
- package/src/export/index.ts +44 -0
- package/src/export/projection.ts +581 -0
- package/src/export/storybook/component.ts +334 -0
- package/src/export/storybook/export.ts +430 -0
- package/src/export/storybook/groups.ts +161 -0
- package/src/export/storybook/module.ts +319 -0
- package/src/export/storybook/names.ts +51 -0
- package/src/export/tailwind-jsx.ts +156 -0
- package/src/export/text.ts +415 -0
- package/src/import/css-text.ts +4 -0
- package/src/import/css-values.ts +31 -0
- package/src/import/flow.ts +248 -0
- package/src/import/html.ts +88 -0
- package/src/import/jsx/core.ts +130 -0
- package/src/import/jsx/dev-runtime.ts +8 -0
- package/src/import/jsx/runtime.ts +76 -0
- package/src/import/scene-graph.ts +582 -0
- package/src/import/style-attribute.ts +25 -0
- package/src/import/tailwind.ts +54 -0
- package/src/import/text.ts +518 -0
- package/src/index.ts +51 -0
- package/src/runtime/browser/runtime.ts +301 -0
- package/src/runtime/headless/runtime.ts +99 -0
- package/src/runtime/headless/styles.ts +330 -0
- package/src/runtime/index.ts +11 -0
- package/src/tokens/conditions.ts +66 -0
- package/src/tokens/cssom-validator.ts +8 -0
- package/src/tokens/custom-properties.ts +331 -0
- package/src/tokens/expression.ts +128 -0
- package/src/tokens/formula.ts +140 -0
- package/src/tokens/index.ts +43 -0
- package/src/tokens/names.ts +161 -0
- package/src/tokens/references.ts +201 -0
- package/src/tokens/stylesheet.ts +304 -0
- package/src/tokens/validate.ts +86 -0
- package/src/tokens/values.ts +44 -0
- package/src/types.ts +59 -0
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DesignDocument,
|
|
3
|
+
DesignElement,
|
|
4
|
+
DesignNode,
|
|
5
|
+
DesignStyleDeclaration
|
|
6
|
+
} from '#dom-css/types'
|
|
7
|
+
import {
|
|
8
|
+
parse,
|
|
9
|
+
type CSSGroupingRuleLike,
|
|
10
|
+
type CSSStyleDeclarationLike,
|
|
11
|
+
type CSSStyleRuleLike
|
|
12
|
+
} from '@acemir/cssom'
|
|
13
|
+
|
|
14
|
+
import { tryParseColor } from '@pairdraw/scene-graph/color'
|
|
15
|
+
import { parseCSSNumber, splitCSSValue } from '@pairdraw/scene-graph/css'
|
|
16
|
+
|
|
17
|
+
interface HeadlessCSSRule {
|
|
18
|
+
selector: string
|
|
19
|
+
specificity: number
|
|
20
|
+
order: number
|
|
21
|
+
style: DesignStyleDeclaration
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface ParsedHeadlessCSS {
|
|
25
|
+
rules: HeadlessCSSRule[]
|
|
26
|
+
customProperties: DesignStyleDeclaration
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface AncestorContext {
|
|
30
|
+
element: DesignElement
|
|
31
|
+
parent: AncestorContext | null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const INHERITED_PROPERTIES = new Set([
|
|
35
|
+
'color',
|
|
36
|
+
'font-family',
|
|
37
|
+
'font-size',
|
|
38
|
+
'font-weight',
|
|
39
|
+
'line-height'
|
|
40
|
+
])
|
|
41
|
+
|
|
42
|
+
function styleToRecord(
|
|
43
|
+
style: CSSStyleDeclarationLike,
|
|
44
|
+
customProperties: DesignStyleDeclaration = {}
|
|
45
|
+
): DesignStyleDeclaration {
|
|
46
|
+
const result: DesignStyleDeclaration = {}
|
|
47
|
+
for (const property of Array.from({ length: style.length }, (_, index) => style[index])) {
|
|
48
|
+
const value = resolveCSSValue(style.getPropertyValue(property), customProperties)
|
|
49
|
+
if (property && value) result[property] = value
|
|
50
|
+
}
|
|
51
|
+
return expandStyleShorthands(result)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function isStyleRule(rule: unknown): rule is CSSStyleRuleLike {
|
|
55
|
+
return (
|
|
56
|
+
typeof rule === 'object' &&
|
|
57
|
+
rule !== null &&
|
|
58
|
+
'selectorText' in rule &&
|
|
59
|
+
'style' in rule &&
|
|
60
|
+
typeof rule.selectorText === 'string'
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function isGroupingRule(rule: unknown): rule is CSSGroupingRuleLike {
|
|
65
|
+
return (
|
|
66
|
+
typeof rule === 'object' && rule !== null && 'cssRules' in rule && Array.isArray(rule.cssRules)
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function collectStyleRules(rules: unknown[]): CSSStyleRuleLike[] {
|
|
71
|
+
return rules.flatMap((rule) => {
|
|
72
|
+
if (isStyleRule(rule)) return [rule]
|
|
73
|
+
if (isGroupingRule(rule)) return collectStyleRules(rule.cssRules)
|
|
74
|
+
return []
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function parseRules(cssText: string): ParsedHeadlessCSS {
|
|
79
|
+
let order = 0
|
|
80
|
+
const sheet = parse(cssText)
|
|
81
|
+
const styleRules = collectStyleRules(sheet.cssRules)
|
|
82
|
+
const customProperties = collectCustomProperties(styleRules)
|
|
83
|
+
const rules = styleRules.flatMap((rule) => {
|
|
84
|
+
const style = styleToRecord(rule.style, customProperties)
|
|
85
|
+
return rule.selectorText
|
|
86
|
+
.split(',')
|
|
87
|
+
.map((selector) => selector.trim())
|
|
88
|
+
.filter((selector) => selector.length > 0)
|
|
89
|
+
.map((selector) => ({
|
|
90
|
+
selector,
|
|
91
|
+
specificity: selectorSpecificity(selector),
|
|
92
|
+
order: order++,
|
|
93
|
+
style
|
|
94
|
+
}))
|
|
95
|
+
})
|
|
96
|
+
return { rules, customProperties }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function collectCustomProperties(rules: CSSStyleRuleLike[]): DesignStyleDeclaration {
|
|
100
|
+
const customProperties: DesignStyleDeclaration = {}
|
|
101
|
+
for (const rule of rules) {
|
|
102
|
+
if (!rule.selectorText.split(',').some((selector) => selector.trim() === ':root')) continue
|
|
103
|
+
Object.assign(customProperties, styleToRecord(rule.style))
|
|
104
|
+
}
|
|
105
|
+
return customProperties
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function resolveCSSValue(value: string, customProperties: DesignStyleDeclaration): string {
|
|
109
|
+
const withVariables = value.replaceAll(/var\((--[\w-]+)(?:,[^)]+)?\)/g, (_, name: string) => {
|
|
110
|
+
return customProperties[name] ?? ''
|
|
111
|
+
})
|
|
112
|
+
return resolveSimpleCalc(withVariables)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function resolveSimpleCalc(value: string): string {
|
|
116
|
+
const calc = value.match(/^calc\(([-\d.]+)(rem|px)?\s*\*\s*([-\d.]+)\)$/)
|
|
117
|
+
if (!calc?.[1] || !calc[3]) return value
|
|
118
|
+
|
|
119
|
+
const base = Number.parseFloat(calc[1])
|
|
120
|
+
const multiplier = Number.parseFloat(calc[3])
|
|
121
|
+
const unit = calc.at(2) ?? 'px'
|
|
122
|
+
if (!Number.isFinite(base) || !Number.isFinite(multiplier)) return value
|
|
123
|
+
return `${unit === 'rem' ? base * multiplier * 16 : base * multiplier}px`
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const BORDER_STYLES = new Set([
|
|
127
|
+
'none',
|
|
128
|
+
'hidden',
|
|
129
|
+
'dotted',
|
|
130
|
+
'dashed',
|
|
131
|
+
'solid',
|
|
132
|
+
'double',
|
|
133
|
+
'groove',
|
|
134
|
+
'ridge',
|
|
135
|
+
'inset',
|
|
136
|
+
'outset'
|
|
137
|
+
])
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Shorthands as the longhands they set, in declaration order, so a later declaration wins over
|
|
141
|
+
* an earlier one whether it is a shorthand, a logical side, or a single side, as in CSS.
|
|
142
|
+
*/
|
|
143
|
+
function expandStyleShorthands(style: DesignStyleDeclaration): DesignStyleDeclaration {
|
|
144
|
+
const result: DesignStyleDeclaration = {}
|
|
145
|
+
for (const [property, value] of Object.entries(style)) {
|
|
146
|
+
result[property] = value
|
|
147
|
+
Object.assign(result, longhandsOf(property, value))
|
|
148
|
+
}
|
|
149
|
+
return result
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function longhandsOf(property: string, value: string): DesignStyleDeclaration {
|
|
153
|
+
if (property === 'margin' || property === 'padding') return boxSides(property, value)
|
|
154
|
+
const logical = /^(margin|padding)-(inline|block)$/u.exec(property)
|
|
155
|
+
if (logical) return logicalSides(property, value, logical[2] === 'inline')
|
|
156
|
+
if (property === 'border') return borderLonghands(value)
|
|
157
|
+
if (property === 'background') {
|
|
158
|
+
const color = splitCSSValue(value).find(isColor)
|
|
159
|
+
return color ? { 'background-color': color } : {}
|
|
160
|
+
}
|
|
161
|
+
return {}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function boxSides(property: string, value: string): DesignStyleDeclaration {
|
|
165
|
+
const [top, right = top, bottom = top, left = right] = splitCSSValue(value)
|
|
166
|
+
if (!top || !right || !bottom || !left) return {}
|
|
167
|
+
return {
|
|
168
|
+
[`${property}-top`]: top,
|
|
169
|
+
[`${property}-right`]: right,
|
|
170
|
+
[`${property}-bottom`]: bottom,
|
|
171
|
+
[`${property}-left`]: left
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** `padding-inline` and `padding-block` as the sides they set in left-to-right, horizontal text. */
|
|
176
|
+
function logicalSides(property: string, value: string, inline: boolean): DesignStyleDeclaration {
|
|
177
|
+
const box = property.slice(0, property.lastIndexOf('-'))
|
|
178
|
+
const [first, second = first] = splitCSSValue(value)
|
|
179
|
+
if (!first || !second) return {}
|
|
180
|
+
const [start, end] = inline ? ['left', 'right'] : ['top', 'bottom']
|
|
181
|
+
return { [`${box}-${start}`]: first, [`${box}-${end}`]: second }
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function borderLonghands(value: string): DesignStyleDeclaration {
|
|
185
|
+
const parts = splitCSSValue(value)
|
|
186
|
+
const longhands: DesignStyleDeclaration = {}
|
|
187
|
+
const color = parts.find(isColor)
|
|
188
|
+
const width = parts.find((part) => parseCSSNumber(part) !== null)
|
|
189
|
+
const style = parts.find((part) => BORDER_STYLES.has(part.toLowerCase()))
|
|
190
|
+
if (color) longhands['border-color'] = color
|
|
191
|
+
if (width) longhands['border-width'] = width
|
|
192
|
+
if (style) longhands['border-style'] = style.toLowerCase()
|
|
193
|
+
return longhands
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Any CSS color, including `transparent`, which a shorthand may set on purpose. */
|
|
197
|
+
function isColor(value: string): boolean {
|
|
198
|
+
return tryParseColor(value) !== null
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function classList(element: DesignElement): Set<string> {
|
|
202
|
+
const className = 'class' in element.attrs ? element.attrs.class : ''
|
|
203
|
+
return new Set(className.split(/\s+/).filter((name) => name.length > 0))
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function elementId(element: DesignElement): string | undefined {
|
|
207
|
+
return 'id' in element.attrs ? element.attrs.id : undefined
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function selectorSpecificity(selector: string): number {
|
|
211
|
+
const idCount = selector.match(/#[\w-]+/g)?.length ?? 0
|
|
212
|
+
const classCount = selector.match(/\.[\w-]+/g)?.length ?? 0
|
|
213
|
+
// A tag counts once per compound; the universal selector, alone or as in `*.card`, counts zero.
|
|
214
|
+
const tagCount = selector.split(/[\s>]+/).filter((part) => /^[a-z]/iu.test(part)).length
|
|
215
|
+
return idCount * 100 + classCount * 10 + tagCount
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function matchesSimpleSelector(element: DesignElement, selector: string): boolean {
|
|
219
|
+
if (selector.includes(':') || selector.includes('[')) return false
|
|
220
|
+
|
|
221
|
+
const idMatch = selector.match(/#([\w-]+)/)
|
|
222
|
+
if (idMatch?.[1] && elementId(element) !== idMatch[1]) return false
|
|
223
|
+
|
|
224
|
+
const classes = selector.match(/\.[\w-]+/g) ?? []
|
|
225
|
+
const elementClasses = classList(element)
|
|
226
|
+
if (!classes.every((name) => elementClasses.has(name.slice(1)))) return false
|
|
227
|
+
|
|
228
|
+
const tag = selector.replace(/#[\w-]+/g, '').replace(/\.[\w-]+/g, '')
|
|
229
|
+
// The universal selector, as Tailwind's preflight uses for `box-sizing`, matches any element.
|
|
230
|
+
return tag.length === 0 || tag === '*' || element.tagName.toLowerCase() === tag.toLowerCase()
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function matchesSelector(
|
|
234
|
+
element: DesignElement,
|
|
235
|
+
selector: string,
|
|
236
|
+
parent: AncestorContext | null
|
|
237
|
+
): boolean {
|
|
238
|
+
const childParts = selector.split('>').map((part) => part.trim())
|
|
239
|
+
if (childParts.length > 1) return matchesChildSelector(element, childParts, parent)
|
|
240
|
+
|
|
241
|
+
const descendantParts = selector.split(/\s+/).filter((part) => part.length > 0)
|
|
242
|
+
if (descendantParts.length > 1) return matchesDescendantSelector(element, descendantParts, parent)
|
|
243
|
+
|
|
244
|
+
return matchesSimpleSelector(element, selector)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function matchesSelectorParts(
|
|
248
|
+
element: DesignElement,
|
|
249
|
+
parts: string[],
|
|
250
|
+
parent: AncestorContext | null,
|
|
251
|
+
directParentOnly: boolean
|
|
252
|
+
): boolean {
|
|
253
|
+
const current = parts.at(-1)
|
|
254
|
+
if (!current || !matchesSimpleSelector(element, current)) return false
|
|
255
|
+
|
|
256
|
+
let ancestor = parent
|
|
257
|
+
for (const selector of parts.slice(0, -1).reverse()) {
|
|
258
|
+
if (!directParentOnly) {
|
|
259
|
+
while (ancestor && !matchesSimpleSelector(ancestor.element, selector)) {
|
|
260
|
+
ancestor = ancestor.parent
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (!ancestor || !matchesSimpleSelector(ancestor.element, selector)) return false
|
|
264
|
+
ancestor = ancestor.parent
|
|
265
|
+
}
|
|
266
|
+
return true
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function matchesChildSelector(
|
|
270
|
+
element: DesignElement,
|
|
271
|
+
parts: string[],
|
|
272
|
+
parent: AncestorContext | null
|
|
273
|
+
): boolean {
|
|
274
|
+
return matchesSelectorParts(element, parts, parent, true)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function matchesDescendantSelector(
|
|
278
|
+
element: DesignElement,
|
|
279
|
+
parts: string[],
|
|
280
|
+
parent: AncestorContext | null
|
|
281
|
+
): boolean {
|
|
282
|
+
return matchesSelectorParts(element, parts, parent, false)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function applyComputedStyles(
|
|
286
|
+
node: DesignNode,
|
|
287
|
+
rules: HeadlessCSSRule[],
|
|
288
|
+
parent: AncestorContext | null,
|
|
289
|
+
inheritedStyle: DesignStyleDeclaration
|
|
290
|
+
): DesignNode {
|
|
291
|
+
if (node.type === 'text') return node
|
|
292
|
+
|
|
293
|
+
const computedStyle: DesignStyleDeclaration = pickInheritedStyle(inheritedStyle)
|
|
294
|
+
const matchingRules = rules
|
|
295
|
+
.filter((rule) => matchesSelector(node, rule.selector, parent))
|
|
296
|
+
.sort((left, right) => left.specificity - right.specificity || left.order - right.order)
|
|
297
|
+
|
|
298
|
+
for (const rule of matchingRules) Object.assign(computedStyle, rule.style)
|
|
299
|
+
Object.assign(computedStyle, expandStyleShorthands(node.inlineStyle ?? {}))
|
|
300
|
+
|
|
301
|
+
const context = { element: node, parent }
|
|
302
|
+
return {
|
|
303
|
+
...node,
|
|
304
|
+
computedStyle: Object.keys(computedStyle).length > 0 ? computedStyle : undefined,
|
|
305
|
+
children: node.children.map((child) =>
|
|
306
|
+
applyComputedStyles(child, rules, context, computedStyle)
|
|
307
|
+
)
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function pickInheritedStyle(style: DesignStyleDeclaration): DesignStyleDeclaration {
|
|
312
|
+
const inherited: DesignStyleDeclaration = {}
|
|
313
|
+
for (const property of INHERITED_PROPERTIES) {
|
|
314
|
+
const value = style[property]
|
|
315
|
+
if (value) inherited[property] = value
|
|
316
|
+
}
|
|
317
|
+
return inherited
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export function computeHeadlessStyles(document: DesignDocument, cssText = ''): DesignDocument {
|
|
321
|
+
const stylesheetText = [document.stylesheets?.map((sheet) => sheet.cssText).join('\n'), cssText]
|
|
322
|
+
.filter((text): text is string => !!text)
|
|
323
|
+
.join('\n')
|
|
324
|
+
const { rules } = parseRules(stylesheetText)
|
|
325
|
+
|
|
326
|
+
return {
|
|
327
|
+
...document,
|
|
328
|
+
children: document.children.map((child) => applyComputedStyles(child, rules, null, {}))
|
|
329
|
+
}
|
|
330
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { CSSRuntime } from '../types'
|
|
2
|
+
import { createBrowserCSSRuntime } from './browser/runtime'
|
|
3
|
+
import { createHeadlessCSSRuntime } from './headless/runtime'
|
|
4
|
+
|
|
5
|
+
export { createBrowserCSSRuntime } from './browser/runtime'
|
|
6
|
+
export type { BrowserCSSRuntimeOptions } from './browser/runtime'
|
|
7
|
+
export { createHeadlessCSSRuntime } from './headless/runtime'
|
|
8
|
+
|
|
9
|
+
export function createCSSRuntime(): CSSRuntime {
|
|
10
|
+
return typeof document !== 'undefined' ? createBrowserCSSRuntime() : createHeadlessCSSRuntime()
|
|
11
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// eslint-disable-next-line pairdraw/no-mixed-case-acronym-identifiers -- Upstream export spelling.
|
|
2
|
+
import { parse, type CssNodePlain as CSSNodePlain, type FeaturePlain } from 'css-tree'
|
|
3
|
+
|
|
4
|
+
/** What a feature is tested against: a keyword such as `dark`, or a length such as `640px`. */
|
|
5
|
+
type FeatureValue =
|
|
6
|
+
| { type: 'keyword'; name: string }
|
|
7
|
+
| { type: 'dimension'; value: number; unit: string }
|
|
8
|
+
|
|
9
|
+
/** An at-rule that tests one feature: `@media (prefers-color-scheme: dark)`. */
|
|
10
|
+
export interface FeatureCondition {
|
|
11
|
+
rule: 'media' | 'container'
|
|
12
|
+
/** `prefers-color-scheme`, lowercase. */
|
|
13
|
+
feature: string
|
|
14
|
+
value: FeatureValue
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The one feature a prelude tests, when it tests exactly one and nothing else. */
|
|
18
|
+
function onlyFeature(nodes: readonly CSSNodePlain[]): FeaturePlain | undefined {
|
|
19
|
+
const [node] = nodes
|
|
20
|
+
if (nodes.length !== 1) return undefined
|
|
21
|
+
if (node.type === 'Feature') return node
|
|
22
|
+
if (node.type === 'Condition') return onlyFeature(node.children)
|
|
23
|
+
if (node.type === 'MediaQueryList') return onlyFeature(node.children)
|
|
24
|
+
if (node.type === 'MediaQuery' && !node.modifier && !node.mediaType && node.condition)
|
|
25
|
+
return onlyFeature([node.condition])
|
|
26
|
+
return undefined
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function featureValue(node: FeaturePlain['value']): FeatureValue | undefined {
|
|
30
|
+
if (node?.type === 'Identifier') return { type: 'keyword', name: node.name.toLowerCase() }
|
|
31
|
+
if (node?.type !== 'Dimension') return undefined
|
|
32
|
+
const value = Number(node.value)
|
|
33
|
+
return Number.isFinite(value)
|
|
34
|
+
? { type: 'dimension', value, unit: node.unit.toLowerCase() }
|
|
35
|
+
: undefined
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Reads a condition with css-tree, so spacing, case and comments do not change what it tests, and
|
|
40
|
+
* only what CSS calls whitespace separates its parts. Undefined for anything but one `@media` or
|
|
41
|
+
* unnamed `@container` rule testing one feature: a selector, `@media screen`, or
|
|
42
|
+
* `@media (a: b) and (c: d)`.
|
|
43
|
+
*/
|
|
44
|
+
export function parseFeatureCondition(css: string): FeatureCondition | undefined {
|
|
45
|
+
const errors: unknown[] = []
|
|
46
|
+
const sheet = parse(`${css}{}`, {
|
|
47
|
+
list: false,
|
|
48
|
+
positions: false,
|
|
49
|
+
onParseError: (error) => errors.push(error)
|
|
50
|
+
})
|
|
51
|
+
if (errors.length > 0 || sheet.type !== 'StyleSheet' || sheet.children.length !== 1)
|
|
52
|
+
return undefined
|
|
53
|
+
const [rule] = sheet.children
|
|
54
|
+
if (rule.type !== 'Atrule' || rule.prelude?.type !== 'AtrulePrelude') return undefined
|
|
55
|
+
const name = rule.name.toLowerCase()
|
|
56
|
+
if (name !== 'media' && name !== 'container') return undefined
|
|
57
|
+
const feature = onlyFeature(rule.prelude.children)
|
|
58
|
+
const value = feature && featureValue(feature.value)
|
|
59
|
+
return feature && value ? { rule: name, feature: feature.name.toLowerCase(), value } : undefined
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The condition as the stylesheet writes it: `@media (max-width: 640px)`. */
|
|
63
|
+
export function featureConditionCSS({ rule, feature, value }: FeatureCondition): string {
|
|
64
|
+
const written = value.type === 'keyword' ? value.name : `${value.value}${value.unit}`
|
|
65
|
+
return `@${rule} (${feature}: ${written})`
|
|
66
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Headless only: `./stylesheet` loads this module lazily where the browser's parser is missing.
|
|
2
|
+
// A namespace import, read at call time: browser builds resolve the package to a script that
|
|
3
|
+
// exports nothing, and a named import would fail those builds even though this never runs there.
|
|
4
|
+
import * as cssom from '@acemir/cssom'
|
|
5
|
+
|
|
6
|
+
import { createTokenValidator } from './validate'
|
|
7
|
+
|
|
8
|
+
export const tokenValidator = createTokenValidator((cssText) => cssom.parse(cssText))
|