@pairdraw/dom-css 0.21.1 → 0.22.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/dist/chunks/behaviours/states/layers.js +78 -12
- package/dist/chunks/behaviours/states/layers.js.map +1 -1
- package/dist/chunks/browser.js +373 -37
- package/dist/chunks/browser.js.map +1 -1
- package/dist/chunks/import/html.js +66 -11
- package/dist/chunks/import/html.js.map +1 -1
- package/dist/chunks/import/jsx/source/ast.js +207 -0
- package/dist/chunks/import/jsx/source/ast.js.map +1 -0
- package/dist/chunks/import/jsx/source/candidates.js +74 -0
- package/dist/chunks/import/jsx/source/candidates.js.map +1 -0
- package/dist/chunks/import/jsx/source/evaluate.js +213 -0
- package/dist/chunks/import/jsx/source/evaluate.js.map +1 -0
- package/dist/chunks/import/jsx/source/index.js +448 -0
- package/dist/chunks/import/jsx/source/index.js.map +1 -0
- package/dist/export.d.ts +15 -2
- package/dist/export.js +2 -2
- package/dist/index.d.ts +66 -1
- package/dist/index.js +5 -2
- package/package.json +5 -3
- package/src/cssom/browser.ts +63 -0
- package/src/export/css.ts +28 -1
- package/src/export/index.ts +1 -0
- package/src/export/projection.ts +69 -10
- package/src/import/flow.ts +127 -6
- package/src/import/jsx/source/ast.ts +267 -0
- package/src/import/jsx/source/candidates.ts +72 -0
- package/src/import/jsx/source/evaluate.ts +300 -0
- package/src/import/jsx/source/index.ts +36 -0
- package/src/import/jsx/source/render.ts +543 -0
- package/src/import/jsx/source/report.ts +49 -0
- package/src/import/scene-graph.ts +19 -7
- package/src/index.ts +10 -0
- package/src/runtime/browser/runtime.ts +13 -5
- package/src/runtime/browser/sandbox.ts +176 -27
- package/src/runtime/css/scan.ts +45 -0
- package/src/runtime/css/url.ts +92 -0
- package/src/runtime/headless/styles.ts +71 -15
|
@@ -7,7 +7,7 @@ import type {
|
|
|
7
7
|
DesignNode
|
|
8
8
|
} from '#dom-css/types'
|
|
9
9
|
|
|
10
|
-
import {
|
|
10
|
+
import { sandboxContent } from './sandbox'
|
|
11
11
|
|
|
12
12
|
export interface BrowserCSSRuntimeOptions {
|
|
13
13
|
document?: Document
|
|
@@ -52,6 +52,10 @@ const DEFAULT_COMPUTED_PROPERTIES = [
|
|
|
52
52
|
'font-style',
|
|
53
53
|
'font-weight',
|
|
54
54
|
'gap',
|
|
55
|
+
'grid-column',
|
|
56
|
+
'grid-row',
|
|
57
|
+
'grid-template-columns',
|
|
58
|
+
'grid-template-rows',
|
|
55
59
|
'height',
|
|
56
60
|
'justify-content',
|
|
57
61
|
'letter-spacing',
|
|
@@ -216,13 +220,15 @@ async function computeStylesInShadowRoot(
|
|
|
216
220
|
const host = browserDocument.createElement('div')
|
|
217
221
|
applySandboxHostStyle(host)
|
|
218
222
|
|
|
223
|
+
// The sandbox is a live document: only addresses that load nothing may reach it.
|
|
224
|
+
const sandbox = sandboxContent(designDocument, cssText)
|
|
219
225
|
const shadow = host.attachShadow({ mode: 'open' })
|
|
220
226
|
const style = browserDocument.createElement('style')
|
|
221
|
-
style.textContent =
|
|
227
|
+
style.textContent = sandbox.css
|
|
222
228
|
shadow.append(style)
|
|
223
229
|
|
|
224
230
|
const content = browserDocument.createElement('div')
|
|
225
|
-
content.innerHTML =
|
|
231
|
+
content.innerHTML = sandbox.html
|
|
226
232
|
shadow.append(content)
|
|
227
233
|
browserDocument.body.append(host)
|
|
228
234
|
|
|
@@ -253,12 +259,14 @@ async function computeStylesInIframe(
|
|
|
253
259
|
iframeDocument.write(`<!doctype html><html><head></head><body></body></html>`)
|
|
254
260
|
iframeDocument.close()
|
|
255
261
|
|
|
262
|
+
// The iframe is a live document: only addresses that load nothing may reach it.
|
|
263
|
+
const sandbox = sandboxContent(designDocument, cssText)
|
|
256
264
|
const style = iframeDocument.createElement('style')
|
|
257
|
-
style.textContent =
|
|
265
|
+
style.textContent = sandbox.css
|
|
258
266
|
iframeDocument.head.append(style)
|
|
259
267
|
|
|
260
268
|
const content = iframeDocument.createElement('div')
|
|
261
|
-
content.innerHTML =
|
|
269
|
+
content.innerHTML = sandbox.html
|
|
262
270
|
iframeDocument.body.append(content)
|
|
263
271
|
|
|
264
272
|
// The sandbox sits offscreen, where WebKit never runs an animation frame of its own; the
|
|
@@ -1,17 +1,22 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { serializeHTML } from '#dom-css/export/html'
|
|
2
|
+
import { closingParen } from '#dom-css/runtime/css/scan'
|
|
3
|
+
import {
|
|
4
|
+
decodedCSS,
|
|
5
|
+
isLocalResource,
|
|
6
|
+
mentionsResource,
|
|
7
|
+
resourcesAreLocal,
|
|
8
|
+
withoutResourceDeclarations,
|
|
9
|
+
withoutResourceRules
|
|
10
|
+
} from '#dom-css/runtime/css/url'
|
|
11
|
+
import type {
|
|
12
|
+
DesignDocument,
|
|
13
|
+
DesignElement,
|
|
14
|
+
DesignNode,
|
|
15
|
+
DesignStyleDeclaration,
|
|
16
|
+
DesignText
|
|
17
|
+
} from '#dom-css/types'
|
|
2
18
|
|
|
3
|
-
/**
|
|
4
|
-
* What a styling sandbox inserts, with nothing the browser would execute: imported markup reaches
|
|
5
|
-
* the frame as a design document, and an attribute such as `onerror` on an image runs there just as
|
|
6
|
-
* it would on a page. A sandboxed iframe has no `allow-scripts`, so a script that got in is blocked
|
|
7
|
-
* and reported; the shadow root is the app's own document, where it would really run.
|
|
8
|
-
*
|
|
9
|
-
* This is the executable-markup half of the sandbox copy. The network half, which rewrites fetchable
|
|
10
|
-
* addresses so a sandbox never loads a remote resource, walks the same tree: when both are present
|
|
11
|
-
* they are one pass over the nodes, and no pass may keep a `srcdoc`, which this one always drops.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/** Elements that hold a document or code of their own, which the sandbox shows as a plain box. */
|
|
19
|
+
/** Elements that hold a document or code of their own; a script is emptied, a style is cleaned. */
|
|
15
20
|
const CODE_TAGS: ReadonlySet<string> = new Set(['script', 'style'])
|
|
16
21
|
|
|
17
22
|
/** Elements that load another document; their fallback content stays, the element does not. */
|
|
@@ -44,27 +49,171 @@ function executableAttribute(name: string, value: string): boolean {
|
|
|
44
49
|
return EXECUTABLE_URL.test(withoutSchemeNoise(value))
|
|
45
50
|
}
|
|
46
51
|
|
|
47
|
-
|
|
52
|
+
/**
|
|
53
|
+
* The styling sandbox inserts pasted code into a live document, where every external address it
|
|
54
|
+
* names is fetched: image sources, frames, stylesheets, inline `url()`s, and a pasted stylesheet's
|
|
55
|
+
* rules. This module builds the markup and CSS the sandbox may see, and only that: the design
|
|
56
|
+
* document keeps the original addresses, so the importer still records an image's source as its
|
|
57
|
+
* asset reference.
|
|
58
|
+
*/
|
|
59
|
+
export interface SandboxContent {
|
|
60
|
+
html: string
|
|
61
|
+
css: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** `url(...)` in a style attribute, an inline style value, or a stylesheet rule. */
|
|
65
|
+
const URL_FUNCTION = /url\(\s*(?:"([^"]*)"|'([^']*)'|([^)]*))\s*\)/gi
|
|
66
|
+
|
|
67
|
+
/** A stylesheet loaded from elsewhere; the sanitizer drops it too, but not from JSX or CSS text. */
|
|
68
|
+
const CSS_IMPORT = /@import[^;]*;?/gi
|
|
69
|
+
|
|
70
|
+
/** Attributes naming a resource the sandbox would load as soon as the element is live. */
|
|
71
|
+
const URL_ATTRIBUTES: Record<string, boolean> = {
|
|
72
|
+
src: true,
|
|
73
|
+
srcset: true,
|
|
74
|
+
imagesrcset: true,
|
|
75
|
+
poster: true,
|
|
76
|
+
background: true,
|
|
77
|
+
srcdoc: true,
|
|
78
|
+
data: true,
|
|
79
|
+
action: true,
|
|
80
|
+
formaction: true,
|
|
81
|
+
cite: true,
|
|
82
|
+
ping: true,
|
|
83
|
+
manifest: true,
|
|
84
|
+
longdesc: true,
|
|
85
|
+
profile: true,
|
|
86
|
+
usemap: true
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Elements whose `href` addresses another document; everywhere else `href` may stay a link. */
|
|
90
|
+
const LINK_ELEMENTS: Record<string, boolean> = { a: true, area: true }
|
|
91
|
+
|
|
92
|
+
/** The replacement for an address the sandbox must not fetch: a valid, local, empty resource. */
|
|
93
|
+
const INERT_URL = 'url("data:,")'
|
|
94
|
+
|
|
95
|
+
/** Every candidate address in a comma-separated `srcset` is local, so none of them is fetched. */
|
|
96
|
+
function isLocalSrcset(value: string): boolean {
|
|
97
|
+
return value.split(',').every((candidate) => {
|
|
98
|
+
const address = candidate.trim().split(/\s+/)[0]
|
|
99
|
+
return !address || isLocalResource(address)
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** A quoted image candidate, as `image-set("…" 1x)` takes instead of a `url()`. */
|
|
104
|
+
const QUOTED_CANDIDATE = /"([^"]*)"|'([^']*)'/g
|
|
105
|
+
|
|
106
|
+
/** `image-set(...)` and its WebKit spelling, whose arguments may hold quoted image sources. */
|
|
107
|
+
const IMAGE_SET_OPEN = /(?:-webkit-)?image-set\s*\(/gi
|
|
108
|
+
|
|
109
|
+
/** A quoted value that is local, or the inert one; an image-set candidate without `url()`. */
|
|
110
|
+
function localCandidate(token: string, double?: string, single?: string): string {
|
|
111
|
+
return isLocalResource(double || single || '') ? token : '"data:,"'
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Every quoted source inside each `image-set(...)`, rewritten when it is not local. */
|
|
115
|
+
function withLocalImageSets(css: string): string {
|
|
116
|
+
let result = ''
|
|
117
|
+
let last = 0
|
|
118
|
+
for (const match of css.matchAll(IMAGE_SET_OPEN)) {
|
|
119
|
+
const start = match.index + match[0].length
|
|
120
|
+
const end = closingParen(css, start)
|
|
121
|
+
if (end === -1) break
|
|
122
|
+
result +=
|
|
123
|
+
css.slice(last, start) + css.slice(start, end).replace(QUOTED_CANDIDATE, localCandidate)
|
|
124
|
+
last = end
|
|
125
|
+
}
|
|
126
|
+
return result + css.slice(last)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Style text and stylesheets with every fetchable resource replaced, so a pasted rule loads none. */
|
|
130
|
+
function withLocalResources(value: string): string {
|
|
131
|
+
const urls = value.replace(
|
|
132
|
+
URL_FUNCTION,
|
|
133
|
+
(match, double: string, single: string, bare: string) => {
|
|
134
|
+
const target = double || single || bare
|
|
135
|
+
return isLocalResource(target) ? match : INERT_URL
|
|
136
|
+
}
|
|
137
|
+
)
|
|
138
|
+
return withLocalImageSets(urls)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A pasted stylesheet, without its imports and with only local resources it may load. */
|
|
142
|
+
function sandboxCSS(cssText: string): string {
|
|
143
|
+
return withLocalResources(decodedCSS(cssText).replace(CSS_IMPORT, ''))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function sandboxStyle(
|
|
147
|
+
style: DesignStyleDeclaration | undefined
|
|
148
|
+
): DesignStyleDeclaration | undefined {
|
|
149
|
+
return (
|
|
150
|
+
style &&
|
|
151
|
+
Object.fromEntries(
|
|
152
|
+
Object.entries(style).filter(
|
|
153
|
+
([, value]) => !mentionsResource(value) || resourcesAreLocal(value)
|
|
154
|
+
)
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function sandboxAttribute(element: DesignElement, name: string, value: string): string {
|
|
160
|
+
const lower = name.toLowerCase()
|
|
161
|
+
if (lower === 'style') return withoutResourceDeclarations(value)
|
|
162
|
+
if (lower === 'srcset' || lower === 'imagesrcset') return isLocalSrcset(value) ? value : ''
|
|
163
|
+
if (lower === 'href' || lower === 'xlink:href') {
|
|
164
|
+
return LINK_ELEMENTS[element.tagName.toLowerCase()] || isLocalResource(value) ? value : ''
|
|
165
|
+
}
|
|
166
|
+
if (URL_ATTRIBUTES[lower]) return isLocalResource(value) ? value : ''
|
|
167
|
+
// SVG paints images, filters, masks, and clip paths through `url()` in their own attributes.
|
|
168
|
+
if (mentionsResource(value)) return resourcesAreLocal(value) ? value : ''
|
|
169
|
+
return value
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function sandboxChildren(element: DesignElement): DesignNode[] {
|
|
173
|
+
const tagName = element.tagName.toLowerCase()
|
|
174
|
+
// A `<style>` element is applied when inserted; HTML parsing drops it and reads its text as CSS,
|
|
175
|
+
// but a JSX paste can bring one, so its text gets the same rule.
|
|
176
|
+
if (tagName === 'style') {
|
|
177
|
+
// The live `<style>` reads only its own text; an element child would serialize its text as
|
|
178
|
+
// raw CSS, so only text children stay and each is cleaned as a stylesheet.
|
|
179
|
+
return element.children
|
|
180
|
+
.filter((child): child is DesignText => child.type === 'text')
|
|
181
|
+
.map((child) => ({ ...child, text: withoutResourceRules(child.text) }))
|
|
182
|
+
}
|
|
183
|
+
if (CODE_TAGS.has(tagName)) return []
|
|
184
|
+
return element.children.map(sandboxNode)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function sandboxNode(node: DesignNode): DesignNode {
|
|
48
188
|
if (node.type === 'text') return node
|
|
49
189
|
const tagName = node.tagName.toLowerCase()
|
|
50
190
|
const attrs = Object.fromEntries(
|
|
51
|
-
Object.entries(node.attrs)
|
|
191
|
+
Object.entries(node.attrs)
|
|
192
|
+
.filter(([name, value]) => !executableAttribute(name, value))
|
|
193
|
+
.map(([name, value]) => [name, sandboxAttribute(node, name, value)])
|
|
52
194
|
)
|
|
53
|
-
|
|
195
|
+
return {
|
|
54
196
|
...node,
|
|
55
|
-
|
|
197
|
+
// A script or an embedded document becomes a plain box; the element's own styles still apply.
|
|
198
|
+
tagName:
|
|
199
|
+
(CODE_TAGS.has(tagName) && tagName !== 'style') || EMBEDDED_TAGS.has(tagName)
|
|
200
|
+
? 'div'
|
|
201
|
+
: node.tagName,
|
|
56
202
|
attrs,
|
|
57
|
-
|
|
203
|
+
inlineStyle: sandboxStyle(node.inlineStyle),
|
|
204
|
+
children: sandboxChildren(node),
|
|
205
|
+
...(node.shader?.frame && !isLocalResource(node.shader.frame)
|
|
206
|
+
? { shader: { ...node.shader, frame: undefined } }
|
|
207
|
+
: {})
|
|
58
208
|
}
|
|
59
|
-
return element
|
|
60
209
|
}
|
|
61
210
|
|
|
62
|
-
/**
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
*/
|
|
68
|
-
export function
|
|
69
|
-
return {
|
|
211
|
+
/** The design document as the styling sandbox may see it: same tree, no fetchable address. */
|
|
212
|
+
function sandboxDocument(document: DesignDocument): DesignDocument {
|
|
213
|
+
return { ...document, children: document.children.map(sandboxNode) }
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Exactly what a styling sandbox inserts: the markup it assigns, and the CSS it applies. */
|
|
217
|
+
export function sandboxContent(document: DesignDocument, cssText: string): SandboxContent {
|
|
218
|
+
return { html: serializeHTML(sandboxDocument(document)), css: sandboxCSS(cssText) }
|
|
70
219
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** Where a CSS scan stopped, and the quote or parenthesis it left open. */
|
|
2
|
+
export interface CSSScan {
|
|
3
|
+
/** The index whose visit asked to stop, or -1 when the text ended first. */
|
|
4
|
+
stop: number
|
|
5
|
+
quote: boolean
|
|
6
|
+
depth: number
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Visits each character outside quotes with its parenthesis depth after the character, so a
|
|
11
|
+
* caller can find a matching parenthesis or split at a top-level separator. An escaped character
|
|
12
|
+
* inside a quote is skipped, as CSS reads it.
|
|
13
|
+
*/
|
|
14
|
+
export function scanCSS(
|
|
15
|
+
text: string,
|
|
16
|
+
from: number,
|
|
17
|
+
visit: (character: string, index: number, depth: number) => boolean | undefined
|
|
18
|
+
): CSSScan {
|
|
19
|
+
let quote = ''
|
|
20
|
+
let depth = 0
|
|
21
|
+
for (let index = from; index < text.length; index++) {
|
|
22
|
+
const character = text[index]
|
|
23
|
+
if (quote) {
|
|
24
|
+
if (character === '\\') index += 1
|
|
25
|
+
else if (character === quote) quote = ''
|
|
26
|
+
continue
|
|
27
|
+
}
|
|
28
|
+
if (character === '"' || character === "'") {
|
|
29
|
+
quote = character
|
|
30
|
+
continue
|
|
31
|
+
}
|
|
32
|
+
if (character === '(') {
|
|
33
|
+
depth += 1
|
|
34
|
+
continue
|
|
35
|
+
}
|
|
36
|
+
if (character === ')') depth -= 1
|
|
37
|
+
if (visit(character, index, depth)) return { stop: index, quote: quote !== '', depth }
|
|
38
|
+
}
|
|
39
|
+
return { stop: -1, quote: quote !== '', depth }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The index of the `)` closing a function whose arguments start at `from`, or -1 when unclosed. */
|
|
43
|
+
export function closingParen(css: string, from: number): number {
|
|
44
|
+
return scanCSS(css, from, (_character, _index, depth) => depth < 0).stop
|
|
45
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { scanCSS } from './scan'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* CSS spellings that a plain text match misses: `\72` is `r`, and a comment splits `url` in two.
|
|
5
|
+
* The styling sandbox decides on the decoded text, so an escaped or commented spelling cannot
|
|
6
|
+
* make it load a resource.
|
|
7
|
+
*/
|
|
8
|
+
const CSS_ESCAPE = /\\([0-9a-f]{1,6})\s?/gi
|
|
9
|
+
const CSS_COMMENT = /\/\*[\s\S]*?\*\//g
|
|
10
|
+
|
|
11
|
+
/** A stylesheet value with its comments removed and its escapes decoded, as CSS parses it. */
|
|
12
|
+
export function decodedCSS(text: string): string {
|
|
13
|
+
return text.replace(CSS_COMMENT, '').replace(CSS_ESCAPE, (_, hex: string) => {
|
|
14
|
+
const code = Number.parseInt(hex, 16)
|
|
15
|
+
return code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : ''
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Whether a declaration or rule loads a picture, in any spelling CSS allows. */
|
|
20
|
+
export function mentionsURL(text: string): boolean {
|
|
21
|
+
return /url\s*\(/i.test(decodedCSS(text))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Whether text can load anything: `url()`, `image-set()`, or a stylesheet import. */
|
|
25
|
+
export function mentionsResource(text: string): boolean {
|
|
26
|
+
return /url\s*\(|image-set\s*\(|@import/i.test(decodedCSS(text))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A resource that loads nothing over the network: an inline value or a document fragment. */
|
|
30
|
+
const LOCAL_RESOURCE = /^(?:data:|#|about:blank$)/i
|
|
31
|
+
|
|
32
|
+
export function isLocalResource(value: string): boolean {
|
|
33
|
+
return LOCAL_RESOURCE.test(value.trim())
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Every resource the text names is local, so keeping it cannot fetch anything. */
|
|
37
|
+
export function resourcesAreLocal(text: string): boolean {
|
|
38
|
+
const decoded = decodedCSS(text)
|
|
39
|
+
if (/@import/i.test(decoded)) return false
|
|
40
|
+
for (const match of decoded.matchAll(/url\(\s*(?:"([^"]*)"|'([^']*)'|([^)]*))\s*\)/gi)) {
|
|
41
|
+
if (!isLocalResource(match[1] || match[2] || match[3] || '')) return false
|
|
42
|
+
}
|
|
43
|
+
for (const match of decoded.matchAll(/image-set\s*\(([^)]*)\)/gi)) {
|
|
44
|
+
for (const candidate of match[1].matchAll(/"([^"]*)"|'([^']*)'/g)) {
|
|
45
|
+
if (!isLocalResource(candidate[1] || candidate[2] || '')) return false
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return true
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A declaration list split on semicolons outside quotes and parentheses; null when a quote or
|
|
53
|
+
* parenthesis is unclosed, so a malformed style fails closed instead of being cut into fragments.
|
|
54
|
+
*/
|
|
55
|
+
export function splitDeclarations(text: string): string[] | null {
|
|
56
|
+
const parts: string[] = []
|
|
57
|
+
let start = 0
|
|
58
|
+
const scan = scanCSS(text, 0, (character, index, depth) => {
|
|
59
|
+
if (character !== ';' || depth !== 0) return
|
|
60
|
+
parts.push(text.slice(start, index))
|
|
61
|
+
start = index + 1
|
|
62
|
+
})
|
|
63
|
+
if (scan.quote || scan.depth !== 0) return null
|
|
64
|
+
parts.push(text.slice(start))
|
|
65
|
+
return parts
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A declaration list with every part that can load a resource dropped. */
|
|
69
|
+
export function withoutResourceDeclarations(text: string): string {
|
|
70
|
+
const parts = splitDeclarations(decodedCSS(text).replace(/@import[^;{}]*;?/gi, ''))
|
|
71
|
+
if (!parts) return ''
|
|
72
|
+
return parts
|
|
73
|
+
.map((part) => part.trim())
|
|
74
|
+
.filter((part) => part.length > 0 && (!mentionsResource(part) || resourcesAreLocal(part)))
|
|
75
|
+
.join('; ')
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A pasted stylesheet with its resource-loading declarations and imports removed, keeping the
|
|
80
|
+
* rules that only style. A block the flat scan cannot take apart is dropped whole.
|
|
81
|
+
*/
|
|
82
|
+
export function withoutResourceRules(cssText: string): string {
|
|
83
|
+
const cleaned = decodedCSS(cssText)
|
|
84
|
+
.replace(/@import[^;{}]*;?/gi, '')
|
|
85
|
+
.replace(/([^{}]*)\{([^{}]*)\}/g, (_, selector: string, body: string) => {
|
|
86
|
+
const declarations = withoutResourceDeclarations(body)
|
|
87
|
+
return declarations.length > 0 ? `${selector.trim()} { ${declarations} }` : ''
|
|
88
|
+
})
|
|
89
|
+
// Nested at-rules or malformed blocks may survive the flat scan; fail closed rather than leave
|
|
90
|
+
// a spelling the scan did not understand in the styling document.
|
|
91
|
+
return resourcesAreLocal(cleaned) ? cleaned : ''
|
|
92
|
+
}
|
|
@@ -264,27 +264,83 @@ function elementId(element: DesignElement): string | undefined {
|
|
|
264
264
|
return 'id' in element.attrs ? element.attrs.id : undefined
|
|
265
265
|
}
|
|
266
266
|
|
|
267
|
+
/** A compound selector read with its escapes, as Tailwind escapes arbitrary values. */
|
|
268
|
+
interface CompoundSelector {
|
|
269
|
+
tag: string
|
|
270
|
+
id?: string
|
|
271
|
+
classes: string[]
|
|
272
|
+
/** A selector this runtime cannot match, such as an attribute or pseudo-class one. */
|
|
273
|
+
unsupported: boolean
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** Reads a CSS identifier from `start`, where a backslash makes the next character a literal. */
|
|
277
|
+
function readIdentifier(selector: string, start: number): { value: string; end: number } {
|
|
278
|
+
let value = ''
|
|
279
|
+
let index = start
|
|
280
|
+
while (index < selector.length) {
|
|
281
|
+
const character = selector[index]
|
|
282
|
+
if (character === '\\') {
|
|
283
|
+
value += selector[index + 1] ?? ''
|
|
284
|
+
index += 2
|
|
285
|
+
continue
|
|
286
|
+
}
|
|
287
|
+
if (/[\w-]/.test(character)) {
|
|
288
|
+
value += character
|
|
289
|
+
index += 1
|
|
290
|
+
continue
|
|
291
|
+
}
|
|
292
|
+
break
|
|
293
|
+
}
|
|
294
|
+
return { value, end: index }
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** One compound selector, such as `div.card#main` or `w-\[1024px\]`, as its parts. */
|
|
298
|
+
function readCompound(selector: string): CompoundSelector {
|
|
299
|
+
const compound: CompoundSelector = { tag: '', classes: [], unsupported: false }
|
|
300
|
+
let index = 0
|
|
301
|
+
while (index < selector.length) {
|
|
302
|
+
const character = selector[index]
|
|
303
|
+
if (character === '.' || character === '#') {
|
|
304
|
+
const identifier = readIdentifier(selector, index + 1)
|
|
305
|
+
if (!identifier.value) {
|
|
306
|
+
compound.unsupported = true
|
|
307
|
+
return compound
|
|
308
|
+
}
|
|
309
|
+
if (character === '.') compound.classes.push(identifier.value)
|
|
310
|
+
else compound.id = identifier.value
|
|
311
|
+
index = identifier.end
|
|
312
|
+
continue
|
|
313
|
+
}
|
|
314
|
+
if (character === '*') {
|
|
315
|
+
index += 1
|
|
316
|
+
continue
|
|
317
|
+
}
|
|
318
|
+
if (/[A-Za-z]/.test(character)) {
|
|
319
|
+
const identifier = readIdentifier(selector, index)
|
|
320
|
+
compound.tag = identifier.value
|
|
321
|
+
index = identifier.end
|
|
322
|
+
continue
|
|
323
|
+
}
|
|
324
|
+
// A pseudo-class, attribute selector, or namespace this runtime does not match.
|
|
325
|
+
compound.unsupported = true
|
|
326
|
+
return compound
|
|
327
|
+
}
|
|
328
|
+
return compound
|
|
329
|
+
}
|
|
330
|
+
|
|
267
331
|
function selectorSpecificity(selector: string): number {
|
|
268
|
-
const
|
|
269
|
-
const classCount = selector.match(/\.[\w-]+/g)?.length ?? 0
|
|
332
|
+
const compound = readCompound(selector)
|
|
270
333
|
// A tag counts once per compound; the universal selector, alone or as in `*.card`, counts zero.
|
|
271
|
-
|
|
272
|
-
return idCount * 100 + classCount * 10 + tagCount
|
|
334
|
+
return (compound.id ? 100 : 0) + compound.classes.length * 10 + (compound.tag ? 1 : 0)
|
|
273
335
|
}
|
|
274
336
|
|
|
275
337
|
function matchesSimpleSelector(element: DesignElement, selector: string): boolean {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
if (idMatch?.[1] && elementId(element) !== idMatch[1]) return false
|
|
280
|
-
|
|
281
|
-
const classes = selector.match(/\.[\w-]+/g) ?? []
|
|
338
|
+
const compound = readCompound(selector)
|
|
339
|
+
if (compound.unsupported) return false
|
|
340
|
+
if (compound.id && elementId(element) !== compound.id) return false
|
|
282
341
|
const elementClasses = classList(element)
|
|
283
|
-
if (!classes.every((name) => elementClasses.has(name
|
|
284
|
-
|
|
285
|
-
const tag = selector.replace(/#[\w-]+/g, '').replace(/\.[\w-]+/g, '')
|
|
286
|
-
// The universal selector, as Tailwind's preflight uses for `box-sizing`, matches any element.
|
|
287
|
-
return tag.length === 0 || tag === '*' || element.tagName.toLowerCase() === tag.toLowerCase()
|
|
342
|
+
if (!compound.classes.every((name) => elementClasses.has(name))) return false
|
|
343
|
+
return compound.tag === '' || element.tagName.toLowerCase() === compound.tag.toLowerCase()
|
|
288
344
|
}
|
|
289
345
|
|
|
290
346
|
interface SelectorStep {
|