@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,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
|
+
}
|