@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.
Files changed (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +219 -2
  3. package/dist/browser.d.ts +29 -0
  4. package/dist/browser.js +2 -0
  5. package/dist/chunks/behaviours/args.js +59 -0
  6. package/dist/chunks/behaviours/args.js.map +1 -0
  7. package/dist/chunks/behaviours/controls.js +109 -0
  8. package/dist/chunks/behaviours/controls.js.map +1 -0
  9. package/dist/chunks/behaviours/reset.js +40 -0
  10. package/dist/chunks/behaviours/reset.js.map +1 -0
  11. package/dist/chunks/behaviours/roles.js +51 -0
  12. package/dist/chunks/behaviours/roles.js.map +1 -0
  13. package/dist/chunks/behaviours/states/conditions.js +103 -0
  14. package/dist/chunks/behaviours/states/conditions.js.map +1 -0
  15. package/dist/chunks/behaviours/states/css.js +172 -0
  16. package/dist/chunks/behaviours/states/css.js.map +1 -0
  17. package/dist/chunks/behaviours/states/layers.js +1736 -0
  18. package/dist/chunks/behaviours/states/layers.js.map +1 -0
  19. package/dist/chunks/behaviours/states/model.js +144 -0
  20. package/dist/chunks/behaviours/states/model.js.map +1 -0
  21. package/dist/chunks/behaviours/states/tailwind.js +198 -0
  22. package/dist/chunks/behaviours/states/tailwind.js.map +1 -0
  23. package/dist/chunks/browser.js +1352 -0
  24. package/dist/chunks/browser.js.map +1 -0
  25. package/dist/chunks/export/bundle.js +337 -0
  26. package/dist/chunks/export/bundle.js.map +1 -0
  27. package/dist/chunks/export/components/drawn.js +60 -0
  28. package/dist/chunks/export/components/drawn.js.map +1 -0
  29. package/dist/chunks/export/components/fields.js +178 -0
  30. package/dist/chunks/export/components/fields.js.map +1 -0
  31. package/dist/chunks/export/components/groups.js +297 -0
  32. package/dist/chunks/export/components/groups.js.map +1 -0
  33. package/dist/chunks/export/components/icons.js +260 -0
  34. package/dist/chunks/export/components/icons.js.map +1 -0
  35. package/dist/chunks/export/components/layers.js +1594 -0
  36. package/dist/chunks/export/components/layers.js.map +1 -0
  37. package/dist/chunks/export/index.js +477 -0
  38. package/dist/chunks/export/index.js.map +1 -0
  39. package/dist/chunks/export/storybook/component.js +448 -0
  40. package/dist/chunks/export/storybook/component.js.map +1 -0
  41. package/dist/chunks/export/storybook/export.js +410 -0
  42. package/dist/chunks/export/storybook/export.js.map +1 -0
  43. package/dist/chunks/import/html.js +406 -0
  44. package/dist/chunks/import/html.js.map +1 -0
  45. package/dist/chunks/import/jsx/dev-runtime.js +37 -0
  46. package/dist/chunks/import/jsx/dev-runtime.js.map +1 -0
  47. package/dist/chunks/tokens/cssom-validator.js +10 -0
  48. package/dist/chunks/tokens/cssom-validator.js.map +1 -0
  49. package/dist/export.d.ts +594 -0
  50. package/dist/export.js +13 -0
  51. package/dist/index.d.ts +115 -0
  52. package/dist/index.js +7 -0
  53. package/dist/jsx-dev-runtime.d.ts +8 -0
  54. package/dist/jsx-dev-runtime.js +3 -0
  55. package/dist/jsx-runtime.d.ts +20 -0
  56. package/dist/jsx-runtime.js +3 -0
  57. package/dist/rolldown-runtime.js +13 -0
  58. package/dist/runtime.d.ts +10 -0
  59. package/dist/scene-graph.d.ts +2 -0
  60. package/dist/scene-graph.js +2 -0
  61. package/dist/scene-graph2.d.ts +10 -0
  62. package/dist/styling.d.ts +93 -0
  63. package/dist/tailwind.d.ts +39 -0
  64. package/dist/types.d.ts +89 -0
  65. package/package.json +73 -4
  66. package/src/behaviours/args.ts +85 -0
  67. package/src/behaviours/controls.ts +195 -0
  68. package/src/behaviours/index.ts +11 -0
  69. package/src/behaviours/reset.ts +37 -0
  70. package/src/behaviours/roles.ts +44 -0
  71. package/src/behaviours/states/conditions.ts +105 -0
  72. package/src/behaviours/states/css.ts +123 -0
  73. package/src/behaviours/states/index.ts +4 -0
  74. package/src/behaviours/states/layers.ts +205 -0
  75. package/src/behaviours/states/model.ts +175 -0
  76. package/src/behaviours/states/names.ts +39 -0
  77. package/src/behaviours/states/tailwind.ts +148 -0
  78. package/src/behaviours/states/types.ts +56 -0
  79. package/src/browser.ts +146 -0
  80. package/src/cssom.d.ts +38 -0
  81. package/src/export/bundle.ts +461 -0
  82. package/src/export/components/drawn.ts +73 -0
  83. package/src/export/components/fields.ts +178 -0
  84. package/src/export/components/groups.ts +78 -0
  85. package/src/export/components/icons.ts +230 -0
  86. package/src/export/components/layers.ts +121 -0
  87. package/src/export/components/model.ts +544 -0
  88. package/src/export/components/properties.ts +167 -0
  89. package/src/export/components/react/fields.ts +129 -0
  90. package/src/export/components/react/index.ts +606 -0
  91. package/src/export/components/react/shared.ts +76 -0
  92. package/src/export/components/references.ts +212 -0
  93. package/src/export/components/repeats.ts +128 -0
  94. package/src/export/components/shaders.ts +125 -0
  95. package/src/export/components/styling.ts +31 -0
  96. package/src/export/components/tree.ts +72 -0
  97. package/src/export/components/vue.ts +517 -0
  98. package/src/export/css.ts +57 -0
  99. package/src/export/grid.ts +42 -0
  100. package/src/export/html.ts +163 -0
  101. package/src/export/index.ts +44 -0
  102. package/src/export/projection.ts +581 -0
  103. package/src/export/storybook/component.ts +334 -0
  104. package/src/export/storybook/export.ts +430 -0
  105. package/src/export/storybook/groups.ts +161 -0
  106. package/src/export/storybook/module.ts +319 -0
  107. package/src/export/storybook/names.ts +51 -0
  108. package/src/export/tailwind-jsx.ts +156 -0
  109. package/src/export/text.ts +415 -0
  110. package/src/import/css-text.ts +4 -0
  111. package/src/import/css-values.ts +31 -0
  112. package/src/import/flow.ts +248 -0
  113. package/src/import/html.ts +88 -0
  114. package/src/import/jsx/core.ts +130 -0
  115. package/src/import/jsx/dev-runtime.ts +8 -0
  116. package/src/import/jsx/runtime.ts +76 -0
  117. package/src/import/scene-graph.ts +582 -0
  118. package/src/import/style-attribute.ts +25 -0
  119. package/src/import/tailwind.ts +54 -0
  120. package/src/import/text.ts +518 -0
  121. package/src/index.ts +51 -0
  122. package/src/runtime/browser/runtime.ts +301 -0
  123. package/src/runtime/headless/runtime.ts +99 -0
  124. package/src/runtime/headless/styles.ts +330 -0
  125. package/src/runtime/index.ts +11 -0
  126. package/src/tokens/conditions.ts +66 -0
  127. package/src/tokens/cssom-validator.ts +8 -0
  128. package/src/tokens/custom-properties.ts +331 -0
  129. package/src/tokens/expression.ts +128 -0
  130. package/src/tokens/formula.ts +140 -0
  131. package/src/tokens/index.ts +43 -0
  132. package/src/tokens/names.ts +161 -0
  133. package/src/tokens/references.ts +201 -0
  134. package/src/tokens/stylesheet.ts +304 -0
  135. package/src/tokens/validate.ts +86 -0
  136. package/src/tokens/values.ts +44 -0
  137. package/src/types.ts +59 -0
@@ -0,0 +1,301 @@
1
+ import { serializeHTML } from '#dom-css/export/html'
2
+ import type {
3
+ CSSComputeOptions,
4
+ CSSRuntime,
5
+ DesignDocument,
6
+ DesignElement,
7
+ DesignNode
8
+ } from '#dom-css/types'
9
+
10
+ export interface BrowserCSSRuntimeOptions {
11
+ document?: Document
12
+ sandbox?: 'shadow-root' | 'iframe'
13
+ }
14
+
15
+ const DEFAULT_COMPUTED_PROPERTIES = [
16
+ 'align-items',
17
+ 'aspect-ratio',
18
+ 'background-color',
19
+ 'background-image',
20
+ 'border-bottom-color',
21
+ 'border-bottom-style',
22
+ 'border-bottom-left-radius',
23
+ 'border-bottom-right-radius',
24
+ 'border-bottom-width',
25
+ 'border-left-color',
26
+ 'border-left-style',
27
+ 'border-left-width',
28
+ 'border-radius',
29
+ 'border-right-color',
30
+ 'border-right-style',
31
+ 'border-right-width',
32
+ 'border-top-color',
33
+ 'border-top-style',
34
+ 'border-top-left-radius',
35
+ 'border-top-right-radius',
36
+ 'border-top-width',
37
+ 'box-shadow',
38
+ 'box-sizing',
39
+ 'color',
40
+ 'column-gap',
41
+ 'display',
42
+ 'align-self',
43
+ 'bottom',
44
+ 'flex-direction',
45
+ 'flex-wrap',
46
+ 'font-family',
47
+ 'font-size',
48
+ 'font-style',
49
+ 'font-weight',
50
+ 'gap',
51
+ 'height',
52
+ 'justify-content',
53
+ 'letter-spacing',
54
+ 'left',
55
+ 'line-height',
56
+ 'max-height',
57
+ 'max-width',
58
+ 'min-height',
59
+ 'min-width',
60
+ 'object-fit',
61
+ 'opacity',
62
+ 'overflow',
63
+ 'padding-bottom',
64
+ 'padding-left',
65
+ 'padding-right',
66
+ 'padding-top',
67
+ 'position',
68
+ 'right',
69
+ 'row-gap',
70
+ 'text-align',
71
+ 'text-decoration-line',
72
+ 'text-shadow',
73
+ 'text-transform',
74
+ 'top',
75
+ 'white-space',
76
+ 'width'
77
+ ] as const
78
+
79
+ function resolveBrowserDocument(documentOverride: Document | undefined): Document {
80
+ if (documentOverride) return documentOverride
81
+ if (typeof document === 'undefined') {
82
+ throw new TypeError('Browser CSS runtime requires a DOM document')
83
+ }
84
+ return document
85
+ }
86
+
87
+ function attributesToRecord(element: Element): Record<string, string> {
88
+ const attrs: Record<string, string> = {}
89
+ for (const attr of Array.from(element.attributes)) {
90
+ // Event handlers draw nothing, and the styled copy of the markup must not run them.
91
+ if (attr.name.toLowerCase().startsWith('on')) continue
92
+ attrs[attr.name] = attr.value
93
+ }
94
+ return attrs
95
+ }
96
+
97
+ function styleToRecord(style: CSSStyleDeclaration): Record<string, string> | undefined {
98
+ const entries: Record<string, string> = {}
99
+ for (const property of Array.from(style)) {
100
+ const value = style.getPropertyValue(property)
101
+ if (value) entries[property] = value
102
+ }
103
+ return Object.keys(entries).length > 0 ? entries : undefined
104
+ }
105
+
106
+ const NON_RENDERED_TAGS = new Set(['head', 'link', 'meta', 'script', 'style', 'template', 'title'])
107
+
108
+ function domNodeToDesignNode(node: Node): DesignNode | null {
109
+ if (node.nodeType === 3) {
110
+ const text = node.textContent ?? ''
111
+ return text.length > 0 ? { type: 'text', text } : null
112
+ }
113
+
114
+ if (node.nodeType !== 1) return null
115
+
116
+ const element = node as Element
117
+ if (NON_RENDERED_TAGS.has(element.tagName.toLowerCase())) return null
118
+ const children = Array.from(element.childNodes)
119
+ .map(domNodeToDesignNode)
120
+ .filter((child): child is DesignNode => child !== null)
121
+ const style = 'style' in element ? (element.style as CSSStyleDeclaration) : undefined
122
+
123
+ return {
124
+ type: 'element',
125
+ tagName: element.tagName.toLowerCase(),
126
+ attrs: attributesToRecord(element),
127
+ children,
128
+ inlineStyle: style ? styleToRecord(style) : undefined
129
+ }
130
+ }
131
+
132
+ function parseHTMLWithDocument(browserDocument: Document, html: string): DesignDocument {
133
+ const Parser = browserDocument.defaultView?.DOMParser
134
+ if (!Parser) throw new TypeError('Browser CSS runtime requires DOMParser')
135
+ const parser = new Parser()
136
+ const parsed = parser.parseFromString(html, 'text/html')
137
+ return {
138
+ type: 'document',
139
+ children: Array.from(parsed.body.childNodes)
140
+ .map(domNodeToDesignNode)
141
+ .filter((node): node is DesignNode => node !== null)
142
+ }
143
+ }
144
+
145
+ function collectElementPairs(
146
+ designNode: DesignNode,
147
+ domNode: Node,
148
+ pairs: [DesignElement, Element][]
149
+ ): void {
150
+ if (designNode.type === 'text') return
151
+ const view = domNode.ownerDocument?.defaultView
152
+ if (!view || !(domNode instanceof view.Element)) return
153
+
154
+ pairs.push([designNode, domNode])
155
+
156
+ const elementChildren = designNode.children.filter(
157
+ (child): child is DesignElement => child.type === 'element'
158
+ )
159
+ const domChildren = Array.from(domNode.children)
160
+ for (const [index, child] of elementChildren.entries()) {
161
+ const domChild = domChildren.at(index)
162
+ if (domChild) collectElementPairs(child, domChild, pairs)
163
+ }
164
+ }
165
+
166
+ function computedStyleToRecord(
167
+ style: CSSStyleDeclaration,
168
+ options: CSSComputeOptions
169
+ ): Record<string, string> {
170
+ const entries: Record<string, string> = {}
171
+ const properties = options.includeBrowserDefaults
172
+ ? Array.from(style)
173
+ : DEFAULT_COMPUTED_PROPERTIES
174
+
175
+ for (const property of properties) {
176
+ const value = style.getPropertyValue(property)
177
+ if (value) entries[property] = value
178
+ }
179
+
180
+ return entries
181
+ }
182
+
183
+ function requestFrame(browserDocument: Document): Promise<void> {
184
+ const requestAnimationFrame = browserDocument.defaultView?.requestAnimationFrame
185
+ if (!requestAnimationFrame) return Promise.resolve()
186
+ return new Promise((resolve) => {
187
+ requestAnimationFrame(() => resolve())
188
+ })
189
+ }
190
+
191
+ function applySandboxHostStyle(element: HTMLElement): void {
192
+ element.style.cssText = [
193
+ 'position: fixed',
194
+ 'left: -100000px',
195
+ 'top: 0',
196
+ 'width: 1000px',
197
+ 'height: auto',
198
+ 'visibility: hidden',
199
+ 'pointer-events: none',
200
+ 'contain: layout style paint'
201
+ ].join(';')
202
+ }
203
+
204
+ async function computeStylesInShadowRoot(
205
+ browserDocument: Document,
206
+ designDocument: DesignDocument,
207
+ cssText: string,
208
+ options: CSSComputeOptions
209
+ ): Promise<DesignDocument> {
210
+ const host = browserDocument.createElement('div')
211
+ applySandboxHostStyle(host)
212
+
213
+ const shadow = host.attachShadow({ mode: 'open' })
214
+ const style = browserDocument.createElement('style')
215
+ style.textContent = cssText
216
+ shadow.append(style)
217
+
218
+ const content = browserDocument.createElement('div')
219
+ content.innerHTML = serializeHTML(designDocument)
220
+ shadow.append(content)
221
+ browserDocument.body.append(host)
222
+
223
+ try {
224
+ await requestFrame(browserDocument)
225
+ return copyComputedStyles(designDocument, content, options)
226
+ } finally {
227
+ host.remove()
228
+ }
229
+ }
230
+
231
+ async function computeStylesInIframe(
232
+ browserDocument: Document,
233
+ designDocument: DesignDocument,
234
+ cssText: string,
235
+ options: CSSComputeOptions
236
+ ): Promise<DesignDocument> {
237
+ const iframe = browserDocument.createElement('iframe')
238
+ applySandboxHostStyle(iframe)
239
+ // Same origin so its styles can be read, without scripts so imported markup never runs.
240
+ iframe.setAttribute('sandbox', 'allow-same-origin')
241
+ browserDocument.body.append(iframe)
242
+
243
+ try {
244
+ const iframeDocument = iframe.contentDocument
245
+ if (!iframeDocument) throw new TypeError('Browser CSS runtime could not create iframe document')
246
+ iframeDocument.open()
247
+ iframeDocument.write(`<!doctype html><html><head></head><body></body></html>`)
248
+ iframeDocument.close()
249
+
250
+ const style = iframeDocument.createElement('style')
251
+ style.textContent = cssText
252
+ iframeDocument.head.append(style)
253
+
254
+ const content = iframeDocument.createElement('div')
255
+ content.innerHTML = serializeHTML(designDocument)
256
+ iframeDocument.body.append(content)
257
+
258
+ await requestFrame(iframeDocument)
259
+ return copyComputedStyles(designDocument, content, options)
260
+ } finally {
261
+ iframe.remove()
262
+ }
263
+ }
264
+
265
+ function copyComputedStyles(
266
+ designDocument: DesignDocument,
267
+ content: Element,
268
+ options: CSSComputeOptions
269
+ ): DesignDocument {
270
+ const view = content.ownerDocument.defaultView
271
+ if (!view) throw new TypeError('Browser CSS runtime requires getComputedStyle')
272
+
273
+ const nextDocument = structuredClone(designDocument)
274
+ const pairs: [DesignElement, Element][] = []
275
+ const domChildren = Array.from(content.childNodes)
276
+ for (const [index, child] of nextDocument.children.entries()) {
277
+ const domChild = domChildren.at(index)
278
+ if (domChild) collectElementPairs(child, domChild, pairs)
279
+ }
280
+
281
+ for (const [designElement, domElement] of pairs) {
282
+ designElement.computedStyle = computedStyleToRecord(view.getComputedStyle(domElement), options)
283
+ }
284
+
285
+ return nextDocument
286
+ }
287
+
288
+ export function createBrowserCSSRuntime(options: BrowserCSSRuntimeOptions = {}): CSSRuntime {
289
+ const browserDocument = resolveBrowserDocument(options.document)
290
+ const sandbox = options.sandbox ?? 'shadow-root'
291
+
292
+ return {
293
+ kind: 'browser',
294
+ parseHTML: (html) => parseHTMLWithDocument(browserDocument, html),
295
+ serializeHTML,
296
+ computeStyles: (designDocument, cssText = '', computeOptions = {}) =>
297
+ sandbox === 'iframe'
298
+ ? computeStylesInIframe(browserDocument, designDocument, cssText, computeOptions)
299
+ : computeStylesInShadowRoot(browserDocument, designDocument, cssText, computeOptions)
300
+ }
301
+ }
@@ -0,0 +1,99 @@
1
+ import { serializeHTML } from '#dom-css/export/html'
2
+ import { INLINE_TAGS } from '#dom-css/import/flow'
3
+ import { parseStyleAttribute } from '#dom-css/import/style-attribute'
4
+ import type { CSSRuntime, DesignDocument, DesignElement, DesignNode } from '#dom-css/types'
5
+ import { parseFragment, type DefaultTreeAdapterTypes } from 'parse5'
6
+
7
+ import { computeHeadlessStyles } from './styles'
8
+
9
+ function attrsToRecord(attrs: DefaultTreeAdapterTypes.Element['attrs']): Record<string, string> {
10
+ const result: Record<string, string> = {}
11
+ for (const attr of attrs) result[attr.name] = attr.value
12
+ return result
13
+ }
14
+
15
+ function isTextNode(
16
+ node: DefaultTreeAdapterTypes.ChildNode
17
+ ): node is DefaultTreeAdapterTypes.TextNode {
18
+ return node.nodeName === '#text'
19
+ }
20
+
21
+ const NON_RENDERED_TAGS = new Set(['head', 'link', 'meta', 'script', 'style', 'template', 'title'])
22
+
23
+ function isInlineElement(node: DefaultTreeAdapterTypes.ChildNode | undefined): boolean {
24
+ return node !== undefined && 'tagName' in node && INLINE_TAGS.has(node.tagName.toLowerCase())
25
+ }
26
+
27
+ /**
28
+ * Text, and the spaces in or beside inline elements, which CSS draws among their words; spaces
29
+ * between blocks lay nothing out, so they are left out.
30
+ */
31
+ function keepsText(
32
+ node: DefaultTreeAdapterTypes.TextNode,
33
+ siblings: readonly DefaultTreeAdapterTypes.ChildNode[],
34
+ index: number,
35
+ inInline: boolean
36
+ ): boolean {
37
+ if (node.value.trim().length > 0) return true
38
+ return (
39
+ node.value.length > 0 &&
40
+ (inInline || isInlineElement(siblings[index - 1]) || isInlineElement(siblings[index + 1]))
41
+ )
42
+ }
43
+
44
+ function childrenToDesignNodes(
45
+ nodes: readonly DefaultTreeAdapterTypes.ChildNode[],
46
+ inInline: boolean
47
+ ): DesignNode[] {
48
+ return nodes
49
+ .map((node, index) => childToDesignNode(node, index, nodes, inInline))
50
+ .filter((child): child is DesignNode => child !== null)
51
+ }
52
+
53
+ function childToDesignNode(
54
+ node: DefaultTreeAdapterTypes.ChildNode,
55
+ index: number,
56
+ siblings: readonly DefaultTreeAdapterTypes.ChildNode[],
57
+ inInline: boolean
58
+ ): DesignNode | null {
59
+ if (isTextNode(node)) {
60
+ return keepsText(node, siblings, index, inInline) ? { type: 'text', text: node.value } : null
61
+ }
62
+
63
+ if (!('tagName' in node)) return null
64
+ if (NON_RENDERED_TAGS.has(node.tagName.toLowerCase())) return null
65
+
66
+ const attrs = attrsToRecord(node.attrs)
67
+ const element: DesignElement = {
68
+ type: 'element',
69
+ tagName: node.tagName.toLowerCase(),
70
+ attrs,
71
+ children: childrenToDesignNodes(node.childNodes, isInlineElement(node))
72
+ }
73
+
74
+ const inlineStyle = parseStyleAttribute(attrs.style)
75
+ if (inlineStyle) element.inlineStyle = inlineStyle
76
+
77
+ return element
78
+ }
79
+
80
+ function parseHTML(html: string): DesignDocument {
81
+ const fragment = parseFragment(html)
82
+ return {
83
+ type: 'document',
84
+ children: childrenToDesignNodes(fragment.childNodes, false)
85
+ }
86
+ }
87
+
88
+ export function createHeadlessCSSRuntime(): CSSRuntime {
89
+ return {
90
+ kind: 'headless',
91
+ parseHTML,
92
+ serializeHTML(document: DesignDocument) {
93
+ return serializeHTML(document)
94
+ },
95
+ async computeStyles(document: DesignDocument, cssText = '') {
96
+ return computeHeadlessStyles(document, cssText)
97
+ }
98
+ }
99
+ }