@open-pencil/scene-graph 0.17.0-beta.2 → 0.17.0-beta.3
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/asset-keys.d.ts +11 -1
- package/dist/asset-keys.d.ts.map +1 -1
- package/dist/asset-keys.js +16 -1
- package/dist/asset-keys.js.map +1 -1
- package/dist/checkpoint.d.ts +3 -3
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +53 -23
- package/dist/checkpoint.js.map +1 -1
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/properties.d.ts +6 -1
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +9 -1
- package/dist/components/properties.js.map +1 -1
- package/dist/constants.d.ts +6 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +17 -1
- package/dist/constants.js.map +1 -1
- package/dist/constraints.d.ts +25 -0
- package/dist/constraints.d.ts.map +1 -0
- package/dist/constraints.js +51 -0
- package/dist/constraints.js.map +1 -0
- package/dist/coordinate.d.ts +64 -1
- package/dist/coordinate.d.ts.map +1 -1
- package/dist/coordinate.js +217 -1
- package/dist/coordinate.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +8 -1
- package/dist/copy.js.map +1 -1
- package/dist/css/index.d.ts +3 -2
- package/dist/css/index.js +3 -2
- package/dist/css/text.d.ts +28 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +84 -17
- package/dist/css/text.js.map +1 -1
- package/dist/css/transform.d.ts +34 -0
- package/dist/css/transform.d.ts.map +1 -0
- package/dist/css/transform.js +320 -0
- package/dist/css/transform.js.map +1 -0
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +11 -1
- package/dist/css/values.js.map +1 -1
- package/dist/figjam/palette.d.ts +1 -1
- package/dist/figjam/palette.js +1 -1
- package/dist/figjam/palette.js.map +1 -1
- package/dist/geometry.d.ts +8 -1
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +19 -8
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +48 -12
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +110 -24
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts +14 -4
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +76 -11
- package/dist/group-bounds.js.map +1 -1
- package/dist/hit-test.d.ts +5 -0
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +18 -12
- package/dist/hit-test.js.map +1 -1
- package/dist/images.d.ts +27 -3
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +57 -3
- package/dist/images.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +3 -0
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/placement.js +55 -12
- package/dist/instances/placement.js.map +1 -1
- package/dist/instances/swap.js +3 -0
- package/dist/instances/swap.js.map +1 -1
- package/dist/instances/sync.js +34 -13
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -0
- package/dist/node-defaults.js.map +1 -1
- package/dist/order-keys.d.ts +3 -1
- package/dist/order-keys.d.ts.map +1 -1
- package/dist/order-keys.js +6 -1
- package/dist/order-keys.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +2 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +2 -0
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/resize.d.ts +22 -11
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +227 -93
- package/dist/resize.js.map +1 -1
- package/dist/scaling/index.d.ts +3 -2
- package/dist/scaling/index.js +3 -2
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +5 -1
- package/dist/scaling/node.js.map +1 -1
- package/dist/scaling/snapshot.d.ts +25 -0
- package/dist/scaling/snapshot.d.ts.map +1 -0
- package/dist/scaling/snapshot.js +39 -0
- package/dist/scaling/snapshot.js.map +1 -0
- package/dist/scaling/tree.d.ts +24 -2
- package/dist/scaling/tree.d.ts.map +1 -1
- package/dist/scaling/tree.js +37 -14
- package/dist/scaling/tree.js.map +1 -1
- package/dist/shaders/model.d.ts.map +1 -1
- package/dist/shaders/model.js +2 -1
- package/dist/shaders/model.js.map +1 -1
- package/dist/shared-styles.d.ts +3 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +7 -1
- package/dist/shared-styles.js.map +1 -1
- package/dist/slots/authoring.d.ts +3 -2
- package/dist/slots/authoring.d.ts.map +1 -1
- package/dist/slots/authoring.js +5 -3
- package/dist/slots/authoring.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +4 -1
- package/dist/slots/content.js.map +1 -1
- package/dist/text/direction.d.ts +3 -1
- package/dist/text/direction.d.ts.map +1 -1
- package/dist/text/direction.js +5 -1
- package/dist/text/direction.js.map +1 -1
- package/dist/transfer/apply.d.ts.map +1 -1
- package/dist/transfer/apply.js +1 -6
- package/dist/transfer/apply.js.map +1 -1
- package/dist/transfer/copied.d.ts +35 -3
- package/dist/transfer/copied.d.ts.map +1 -1
- package/dist/transfer/copied.js +44 -13
- package/dist/transfer/copied.js.map +1 -1
- package/dist/transfer/dependencies.d.ts +38 -0
- package/dist/transfer/dependencies.d.ts.map +1 -0
- package/dist/transfer/dependencies.js +100 -0
- package/dist/transfer/dependencies.js.map +1 -0
- package/dist/transfer/history.js +0 -1
- package/dist/transfer/history.js.map +1 -1
- package/dist/transfer/index.d.ts +5 -3
- package/dist/transfer/index.js +5 -3
- package/dist/transfer/nodes.d.ts.map +1 -1
- package/dist/transfer/nodes.js +7 -14
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/transfer/plan.d.ts +0 -1
- package/dist/transfer/plan.js +3 -5
- package/dist/transfer/plan.js.map +1 -1
- package/dist/transfer/remove.js +5 -2
- package/dist/transfer/remove.js.map +1 -1
- package/dist/transfer/resources.d.ts +21 -0
- package/dist/transfer/resources.d.ts.map +1 -0
- package/dist/transfer/resources.js +38 -0
- package/dist/transfer/resources.js.map +1 -0
- package/dist/transfer/variables.d.ts +8 -1
- package/dist/transfer/variables.d.ts.map +1 -1
- package/dist/transfer/variables.js +24 -1
- package/dist/transfer/variables.js.map +1 -1
- package/dist/types.d.ts +47 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +28 -3
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +24 -7
- package/dist/undo.js.map +1 -1
- package/dist/variables/bindings.d.ts +17 -14
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +30 -72
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/fields.d.ts +1 -1
- package/dist/variables/fields.js +1 -1
- package/dist/variables/fields.js.map +1 -1
- package/dist/variables/formula/check.d.ts +1 -1
- package/dist/variables/formula/index.d.ts +2 -2
- package/dist/variables/formula/index.js +1 -1
- package/dist/variables/formula/index.js.map +1 -1
- package/dist/variables/formula/print.d.ts +1 -1
- package/dist/variables/formula/signatures.js +1 -1
- package/dist/variables/index.d.ts +6 -6
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +40 -24
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/paints.d.ts +57 -0
- package/dist/variables/paints.d.ts.map +1 -0
- package/dist/variables/paints.js +231 -0
- package/dist/variables/paints.js.map +1 -0
- package/dist/variables/resolution.d.ts +14 -11
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +36 -17
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/subtree.js +17 -0
- package/dist/variables/subtree.js.map +1 -0
- package/dist/vector-network.d.ts.map +1 -1
- package/dist/vector-network.js +2 -8
- package/dist/vector-network.js.map +1 -1
- package/package.json +8 -1
- package/src/asset-keys.ts +20 -0
- package/src/checkpoint.ts +87 -32
- package/src/components/properties.ts +9 -0
- package/src/constants.ts +17 -0
- package/src/constraints.ts +60 -0
- package/src/coordinate.ts +220 -0
- package/src/copy.ts +9 -4
- package/src/css/index.ts +17 -1
- package/src/css/text.ts +85 -17
- package/src/css/transform.ts +266 -0
- package/src/css/values.ts +8 -0
- package/src/figjam/palette.ts +1 -1
- package/src/geometry.ts +20 -9
- package/src/graph.ts +128 -42
- package/src/group-bounds.ts +84 -17
- package/src/hit-test.ts +30 -14
- package/src/images.ts +71 -3
- package/src/instances/index.ts +4 -0
- package/src/instances/placement.ts +63 -10
- package/src/instances/swap.ts +5 -0
- package/src/instances/sync.ts +48 -21
- package/src/node-defaults.ts +1 -0
- package/src/order-keys.ts +6 -0
- package/src/plugin-data/fields.ts +6 -0
- package/src/resize.ts +267 -119
- package/src/scaling/index.ts +9 -1
- package/src/scaling/node.ts +9 -1
- package/src/scaling/snapshot.ts +51 -0
- package/src/scaling/tree.ts +63 -19
- package/src/shaders/model.ts +2 -3
- package/src/shared-styles.ts +8 -0
- package/src/slots/authoring.ts +5 -3
- package/src/slots/content.ts +4 -1
- package/src/text/direction.ts +5 -0
- package/src/transfer/apply.ts +1 -13
- package/src/transfer/copied.ts +83 -15
- package/src/transfer/dependencies.ts +126 -0
- package/src/transfer/history.ts +0 -6
- package/src/transfer/index.ts +20 -2
- package/src/transfer/nodes.ts +10 -26
- package/src/transfer/plan.ts +3 -11
- package/src/transfer/remove.ts +5 -2
- package/src/transfer/resources.ts +54 -0
- package/src/transfer/variables.ts +34 -0
- package/src/types.ts +55 -1
- package/src/undo.ts +48 -10
- package/src/variables/bindings.ts +44 -91
- package/src/variables/fields.ts +1 -1
- package/src/variables/formula/index.ts +3 -1
- package/src/variables/index.ts +56 -38
- package/src/variables/paints.ts +296 -0
- package/src/variables/resolution.ts +39 -28
- package/src/variables/subtree.ts +15 -0
- package/src/vector-network.ts +4 -2
package/dist/css/text.d.ts
CHANGED
|
@@ -1,11 +1,38 @@
|
|
|
1
1
|
import { TextLength } from "../types.js";
|
|
2
2
|
//#region src/css/text.d.ts
|
|
3
|
+
/** A family of a CSS `font-family` list: its name, and whether it was a quoted string. */
|
|
4
|
+
interface CSSFontFamily {
|
|
5
|
+
name: string;
|
|
6
|
+
quoted: boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Every family of a CSS `font-family` list in order, without quotes and escapes, as
|
|
10
|
+
* `parseCSSFontFamily` reads the first.
|
|
11
|
+
*/
|
|
12
|
+
declare function parseCSSFontFamilies(value: string | undefined): CSSFontFamily[];
|
|
13
|
+
/**
|
|
14
|
+
* The real family a family of a `font-family` list names: a generic one, such as `sans-serif`,
|
|
15
|
+
* mapped to a real one, and any other name as it is. A quoted name is never a generic.
|
|
16
|
+
*/
|
|
17
|
+
declare function cssFontFamilyName(family: CSSFontFamily): string;
|
|
3
18
|
/**
|
|
4
19
|
* The first family of a CSS `font-family` list, without its quotes and escapes. A family is a quoted
|
|
5
20
|
* string or unquoted words, which join with single spaces; a quote opens a string only at the start
|
|
6
21
|
* of the family, so a name list such as draw.io's keeps `O'Reilly`. Null for an empty list.
|
|
7
22
|
*/
|
|
8
23
|
declare function parseCSSFontFamily(value: string | undefined): string | null;
|
|
24
|
+
/**
|
|
25
|
+
* Real families for CSS generic families, which name no font but whatever the browser picks for
|
|
26
|
+
* them, and for `-apple-system` and `BlinkMacSystemFont`, which browsers read as `system-ui`.
|
|
27
|
+
* Keyed by lowercase name; the rest of the generics, such as `cursive`, draw in the sans-serif one.
|
|
28
|
+
*/
|
|
29
|
+
declare const CSS_GENERIC_FONT_FAMILIES: Readonly<Record<string, string>>;
|
|
30
|
+
/**
|
|
31
|
+
* The family a CSS `font-family` list sets text in: its first family, with a generic family
|
|
32
|
+
* such as `sans-serif` or `system-ui` mapped to a real one. A quoted name is a family's own name,
|
|
33
|
+
* never a generic. Null for an empty list.
|
|
34
|
+
*/
|
|
35
|
+
declare function resolveCSSFontFamily(value: string | undefined): string | null;
|
|
9
36
|
/**
|
|
10
37
|
* A CSS `line-height` as a text length: a unitless multiple, `em`, or a percent follows the font size,
|
|
11
38
|
* as a percent does in Figma; pixels stay pixels; `normal` is automatic, a null. Undefined for a
|
|
@@ -18,5 +45,5 @@ declare function parseCSSLineHeight(value: string | undefined): TextLength | nul
|
|
|
18
45
|
*/
|
|
19
46
|
declare function parseCSSLetterSpacing(value: string | undefined): TextLength | undefined;
|
|
20
47
|
//#endregion
|
|
21
|
-
export { parseCSSFontFamily, parseCSSLetterSpacing, parseCSSLineHeight };
|
|
48
|
+
export { CSSFontFamily, CSS_GENERIC_FONT_FAMILIES, cssFontFamilyName, parseCSSFontFamilies, parseCSSFontFamily, parseCSSLetterSpacing, parseCSSLineHeight, resolveCSSFontFamily };
|
|
22
49
|
//# sourceMappingURL=text.d.ts.map
|
package/dist/css/text.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.d.ts","names":[],"sources":["../../src/css/text.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"text.d.ts","names":[],"sources":["../../src/css/text.ts"],"mappings":";;;UA+BiB;EACf;EACA;;;;;;iBAuBc,qBAAqB,4BAA4B;;;;;iBAgBjD,kBAAkB,QAAQ;;;;;;iBAU1B,mBAAmB;;;;;;cActB,2BAA2B,SAAS;;;;;;iBAsBjC,qBAAqB;;;;;;iBAUrB,mBAAmB,4BAA4B;;;;;iBAe/C,sBAAsB,4BAA4B"}
|
package/dist/css/text.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { DEFAULT_FONT_FAMILY } from "../constants.js";
|
|
1
2
|
import { percentLength, pixelLength } from "../text/lengths.js";
|
|
2
|
-
import { parseCSSNumber } from "./values.js";
|
|
3
|
-
import valueParser from "postcss-value-parser";
|
|
3
|
+
import { dimension, parseCSSNumber } from "./values.js";
|
|
4
4
|
//#region src/css/text.ts
|
|
5
5
|
/** A CSS escape: hex digits and one optional whitespace, an escaped line break, or any one character. */
|
|
6
6
|
const CSS_ESCAPE = /\\(?:([0-9a-fA-F]{1,6})(?:\r\n|[ \t\n\r\f])?|(?:\r\n|[\n\r\f])|([\s\S]))?/gu;
|
|
@@ -20,27 +20,94 @@ function unescapedIndexOf(text, stop, from) {
|
|
|
20
20
|
while (index < text.length && text[index] !== stop) index += text[index] === "\\" ? 2 : 1;
|
|
21
21
|
return index;
|
|
22
22
|
}
|
|
23
|
+
/** The family at the start of `text` and where the next one starts, or null when none is left. */
|
|
24
|
+
function nextFontFamily(text) {
|
|
25
|
+
const trimmed = text.trimStart();
|
|
26
|
+
if (!trimmed) return null;
|
|
27
|
+
const quote = trimmed[0];
|
|
28
|
+
if (quote === "\"" || quote === "'") {
|
|
29
|
+
const end = unescapedIndexOf(trimmed, quote, 1);
|
|
30
|
+
const name = decodeCSSEscapes(trimmed.slice(1, end));
|
|
31
|
+
const comma = unescapedIndexOf(trimmed, ",", end);
|
|
32
|
+
return {
|
|
33
|
+
family: name ? {
|
|
34
|
+
name,
|
|
35
|
+
quoted: true
|
|
36
|
+
} : null,
|
|
37
|
+
rest: trimmed.slice(comma + 1)
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
const comma = unescapedIndexOf(trimmed, ",", 0);
|
|
41
|
+
const name = decodeCSSEscapes(trimmed.slice(0, comma)).trim().replace(/\s+/gu, " ");
|
|
42
|
+
return {
|
|
43
|
+
family: name ? {
|
|
44
|
+
name,
|
|
45
|
+
quoted: false
|
|
46
|
+
} : null,
|
|
47
|
+
rest: trimmed.slice(comma + 1)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Every family of a CSS `font-family` list in order, without quotes and escapes, as
|
|
52
|
+
* `parseCSSFontFamily` reads the first.
|
|
53
|
+
*/
|
|
54
|
+
function parseCSSFontFamilies(value) {
|
|
55
|
+
const families = [];
|
|
56
|
+
for (let next = nextFontFamily(value ?? ""); next; next = nextFontFamily(next.rest)) if (next.family) families.push(next.family);
|
|
57
|
+
return families;
|
|
58
|
+
}
|
|
59
|
+
function firstFontFamily(value) {
|
|
60
|
+
return parseCSSFontFamilies(value)[0] ?? null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The real family a family of a `font-family` list names: a generic one, such as `sans-serif`,
|
|
64
|
+
* mapped to a real one, and any other name as it is. A quoted name is never a generic.
|
|
65
|
+
*/
|
|
66
|
+
function cssFontFamilyName(family) {
|
|
67
|
+
if (family.quoted) return family.name;
|
|
68
|
+
return CSS_GENERIC_FONT_FAMILIES[family.name.toLowerCase()] ?? family.name;
|
|
69
|
+
}
|
|
23
70
|
/**
|
|
24
71
|
* The first family of a CSS `font-family` list, without its quotes and escapes. A family is a quoted
|
|
25
72
|
* string or unquoted words, which join with single spaces; a quote opens a string only at the start
|
|
26
73
|
* of the family, so a name list such as draw.io's keeps `O'Reilly`. Null for an empty list.
|
|
27
74
|
*/
|
|
28
75
|
function parseCSSFontFamily(value) {
|
|
29
|
-
|
|
30
|
-
if (!text) return null;
|
|
31
|
-
const quote = text[0];
|
|
32
|
-
if (quote === "\"" || quote === "'") return decodeCSSEscapes(text.slice(1, unescapedIndexOf(text, quote, 1))) || null;
|
|
33
|
-
return decodeCSSEscapes(text.slice(0, unescapedIndexOf(text, ",", 0))).trim().replace(/\s+/gu, " ") || null;
|
|
76
|
+
return firstFontFamily(value)?.name ?? null;
|
|
34
77
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
78
|
+
const SANS_SERIF_FAMILY = DEFAULT_FONT_FAMILY;
|
|
79
|
+
/** The families FigJam sets its Bookish and Technical typefaces in, which every font provider serves. */
|
|
80
|
+
const SERIF_FAMILY = "Merriweather";
|
|
81
|
+
const MONOSPACE_FAMILY = "Roboto Mono";
|
|
82
|
+
/**
|
|
83
|
+
* Real families for CSS generic families, which name no font but whatever the browser picks for
|
|
84
|
+
* them, and for `-apple-system` and `BlinkMacSystemFont`, which browsers read as `system-ui`.
|
|
85
|
+
* Keyed by lowercase name; the rest of the generics, such as `cursive`, draw in the sans-serif one.
|
|
86
|
+
*/
|
|
87
|
+
const CSS_GENERIC_FONT_FAMILIES = {
|
|
88
|
+
serif: SERIF_FAMILY,
|
|
89
|
+
"ui-serif": SERIF_FAMILY,
|
|
90
|
+
monospace: MONOSPACE_FAMILY,
|
|
91
|
+
"ui-monospace": MONOSPACE_FAMILY,
|
|
92
|
+
"sans-serif": SANS_SERIF_FAMILY,
|
|
93
|
+
"ui-sans-serif": SANS_SERIF_FAMILY,
|
|
94
|
+
"ui-rounded": SANS_SERIF_FAMILY,
|
|
95
|
+
"system-ui": SANS_SERIF_FAMILY,
|
|
96
|
+
"-apple-system": SANS_SERIF_FAMILY,
|
|
97
|
+
blinkmacsystemfont: SANS_SERIF_FAMILY,
|
|
98
|
+
cursive: SANS_SERIF_FAMILY,
|
|
99
|
+
fantasy: SANS_SERIF_FAMILY,
|
|
100
|
+
math: SANS_SERIF_FAMILY,
|
|
101
|
+
fangsong: SANS_SERIF_FAMILY
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* The family a CSS `font-family` list sets text in: its first family, with a generic family
|
|
105
|
+
* such as `sans-serif` or `system-ui` mapped to a real one. A quoted name is a family's own name,
|
|
106
|
+
* never a generic. Null for an empty list.
|
|
107
|
+
*/
|
|
108
|
+
function resolveCSSFontFamily(value) {
|
|
109
|
+
const family = firstFontFamily(value);
|
|
110
|
+
return family ? cssFontFamilyName(family) : null;
|
|
44
111
|
}
|
|
45
112
|
/**
|
|
46
113
|
* A CSS `line-height` as a text length: a unitless multiple, `em`, or a percent follows the font size,
|
|
@@ -71,6 +138,6 @@ function parseCSSLetterSpacing(value) {
|
|
|
71
138
|
return pixels === null ? void 0 : pixelLength(pixels);
|
|
72
139
|
}
|
|
73
140
|
//#endregion
|
|
74
|
-
export { parseCSSFontFamily, parseCSSLetterSpacing, parseCSSLineHeight };
|
|
141
|
+
export { CSS_GENERIC_FONT_FAMILIES, cssFontFamilyName, parseCSSFontFamilies, parseCSSFontFamily, parseCSSLetterSpacing, parseCSSLineHeight, resolveCSSFontFamily };
|
|
75
142
|
|
|
76
143
|
//# sourceMappingURL=text.js.map
|
package/dist/css/text.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","names":[],"sources":["../../src/css/text.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"text.js","names":[],"sources":["../../src/css/text.ts"],"sourcesContent":["import { DEFAULT_FONT_FAMILY } from '../constants'\nimport { percentLength, pixelLength } from '../text/lengths'\nimport type { TextLength } from '../types'\nimport { dimension, parseCSSNumber } from './values'\n\n/** A CSS escape: hex digits and one optional whitespace, an escaped line break, or any one character. */\nconst CSS_ESCAPE = /\\\\(?:([0-9a-fA-F]{1,6})(?:\\r\\n|[ \\t\\n\\r\\f])?|(?:\\r\\n|[\\n\\r\\f])|([\\s\\S]))?/gu\n\n/** The largest Unicode code point. */\nconst MAX_CODE_POINT = 0x10ffff\n\n/** Text with its CSS escapes decoded; an escaped line break continues the line. */\nfunction decodeCSSEscapes(text: string): string {\n return text.replace(CSS_ESCAPE, (_escape, hex: string | undefined, char: string | undefined) => {\n if (hex === undefined) return char ?? ''\n const codePoint = Number.parseInt(hex, 16)\n const surrogate = codePoint >= 0xd800 && codePoint <= 0xdfff\n return codePoint === 0 || surrogate || codePoint > MAX_CODE_POINT\n ? '\\uFFFD'\n : String.fromCodePoint(codePoint)\n })\n}\n\n/** The index of the first unescaped `stop` character from `from`, or the text's length. */\nfunction unescapedIndexOf(text: string, stop: string, from: number): number {\n let index = from\n while (index < text.length && text[index] !== stop) index += text[index] === '\\\\' ? 2 : 1\n return index\n}\n\n/** A family of a CSS `font-family` list: its name, and whether it was a quoted string. */\nexport interface CSSFontFamily {\n name: string\n quoted: boolean\n}\n\n/** The family at the start of `text` and where the next one starts, or null when none is left. */\nfunction nextFontFamily(text: string): { family: CSSFontFamily | null; rest: string } | null {\n const trimmed = text.trimStart()\n if (!trimmed) return null\n const quote = trimmed[0]\n if (quote === '\"' || quote === \"'\") {\n const end = unescapedIndexOf(trimmed, quote, 1)\n const name = decodeCSSEscapes(trimmed.slice(1, end))\n const comma = unescapedIndexOf(trimmed, ',', end)\n return { family: name ? { name, quoted: true } : null, rest: trimmed.slice(comma + 1) }\n }\n const comma = unescapedIndexOf(trimmed, ',', 0)\n const name = decodeCSSEscapes(trimmed.slice(0, comma)).trim().replace(/\\s+/gu, ' ')\n return { family: name ? { name, quoted: false } : null, rest: trimmed.slice(comma + 1) }\n}\n\n/**\n * Every family of a CSS `font-family` list in order, without quotes and escapes, as\n * `parseCSSFontFamily` reads the first.\n */\nexport function parseCSSFontFamilies(value: string | undefined): CSSFontFamily[] {\n const families: CSSFontFamily[] = []\n for (let next = nextFontFamily(value ?? ''); next; next = nextFontFamily(next.rest)) {\n if (next.family) families.push(next.family)\n }\n return families\n}\n\nfunction firstFontFamily(value: string | undefined): CSSFontFamily | null {\n return parseCSSFontFamilies(value)[0] ?? null\n}\n\n/**\n * The real family a family of a `font-family` list names: a generic one, such as `sans-serif`,\n * mapped to a real one, and any other name as it is. A quoted name is never a generic.\n */\nexport function cssFontFamilyName(family: CSSFontFamily): string {\n if (family.quoted) return family.name\n return CSS_GENERIC_FONT_FAMILIES[family.name.toLowerCase()] ?? family.name\n}\n\n/**\n * The first family of a CSS `font-family` list, without its quotes and escapes. A family is a quoted\n * string or unquoted words, which join with single spaces; a quote opens a string only at the start\n * of the family, so a name list such as draw.io's keeps `O'Reilly`. Null for an empty list.\n */\nexport function parseCSSFontFamily(value: string | undefined): string | null {\n return firstFontFamily(value)?.name ?? null\n}\n\nconst SANS_SERIF_FAMILY = DEFAULT_FONT_FAMILY\n/** The families FigJam sets its Bookish and Technical typefaces in, which every font provider serves. */\nconst SERIF_FAMILY = 'Merriweather'\nconst MONOSPACE_FAMILY = 'Roboto Mono'\n\n/**\n * Real families for CSS generic families, which name no font but whatever the browser picks for\n * them, and for `-apple-system` and `BlinkMacSystemFont`, which browsers read as `system-ui`.\n * Keyed by lowercase name; the rest of the generics, such as `cursive`, draw in the sans-serif one.\n */\nexport const CSS_GENERIC_FONT_FAMILIES: Readonly<Record<string, string>> = {\n serif: SERIF_FAMILY,\n 'ui-serif': SERIF_FAMILY,\n monospace: MONOSPACE_FAMILY,\n 'ui-monospace': MONOSPACE_FAMILY,\n 'sans-serif': SANS_SERIF_FAMILY,\n 'ui-sans-serif': SANS_SERIF_FAMILY,\n 'ui-rounded': SANS_SERIF_FAMILY,\n 'system-ui': SANS_SERIF_FAMILY,\n '-apple-system': SANS_SERIF_FAMILY,\n blinkmacsystemfont: SANS_SERIF_FAMILY,\n cursive: SANS_SERIF_FAMILY,\n fantasy: SANS_SERIF_FAMILY,\n math: SANS_SERIF_FAMILY,\n fangsong: SANS_SERIF_FAMILY\n}\n\n/**\n * The family a CSS `font-family` list sets text in: its first family, with a generic family\n * such as `sans-serif` or `system-ui` mapped to a real one. A quoted name is a family's own name,\n * never a generic. Null for an empty list.\n */\nexport function resolveCSSFontFamily(value: string | undefined): string | null {\n const family = firstFontFamily(value)\n return family ? cssFontFamilyName(family) : null\n}\n\n/**\n * A CSS `line-height` as a text length: a unitless multiple, `em`, or a percent follows the font size,\n * as a percent does in Figma; pixels stay pixels; `normal` is automatic, a null. Undefined for a\n * value it cannot read.\n */\nexport function parseCSSLineHeight(value: string | undefined): TextLength | null | undefined {\n const trimmed = value?.trim()\n if (!trimmed) return undefined\n if (trimmed === 'normal') return null\n const parsed = dimension(trimmed)\n if (parsed?.unit === '' || parsed?.unit === 'em') return percentLength(parsed.number * 100)\n if (parsed?.unit === '%') return percentLength(parsed.number)\n const pixels = parseCSSNumber(trimmed)\n return pixels === null ? undefined : pixelLength(pixels)\n}\n\n/**\n * A CSS `letter-spacing` as a text length: `em` is a percent of the font size, pixels stay pixels,\n * and `normal` is none. Undefined for a value it cannot read.\n */\nexport function parseCSSLetterSpacing(value: string | undefined): TextLength | undefined {\n const trimmed = value?.trim()\n if (!trimmed) return undefined\n if (trimmed === 'normal') return pixelLength(0)\n const parsed = dimension(trimmed)\n if (parsed?.unit === 'em') return percentLength(parsed.number * 100)\n const pixels = parseCSSNumber(trimmed)\n return pixels === null ? undefined : pixelLength(pixels)\n}\n"],"mappings":";;;;;AAMA,MAAM,aAAa;;AAGnB,MAAM,iBAAiB;;AAGvB,SAAS,iBAAiB,MAAsB;CAC9C,OAAO,KAAK,QAAQ,aAAa,SAAS,KAAyB,SAA6B;EAC9F,IAAI,QAAQ,KAAA,GAAW,OAAO,QAAQ;EACtC,MAAM,YAAY,OAAO,SAAS,KAAK,EAAE;EAEzC,OAAO,cAAc,KADH,aAAa,SAAU,aAAa,SACf,YAAY,iBAC/C,MACA,OAAO,cAAc,SAAS;CACpC,CAAC;AACH;;AAGA,SAAS,iBAAiB,MAAc,MAAc,MAAsB;CAC1E,IAAI,QAAQ;CACZ,OAAO,QAAQ,KAAK,UAAU,KAAK,WAAW,MAAM,SAAS,KAAK,WAAW,OAAO,IAAI;CACxF,OAAO;AACT;;AASA,SAAS,eAAe,MAAqE;CAC3F,MAAM,UAAU,KAAK,UAAU;CAC/B,IAAI,CAAC,SAAS,OAAO;CACrB,MAAM,QAAQ,QAAQ;CACtB,IAAI,UAAU,QAAO,UAAU,KAAK;EAClC,MAAM,MAAM,iBAAiB,SAAS,OAAO,CAAC;EAC9C,MAAM,OAAO,iBAAiB,QAAQ,MAAM,GAAG,GAAG,CAAC;EACnD,MAAM,QAAQ,iBAAiB,SAAS,KAAK,GAAG;EAChD,OAAO;GAAE,QAAQ,OAAO;IAAE;IAAM,QAAQ;GAAK,IAAI;GAAM,MAAM,QAAQ,MAAM,QAAQ,CAAC;EAAE;CACxF;CACA,MAAM,QAAQ,iBAAiB,SAAS,KAAK,CAAC;CAC9C,MAAM,OAAO,iBAAiB,QAAQ,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,KA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AS,MAAM,OAAO,cAAc,OAAO,SAAS,GAAG;CAC1F,IAAI,QAAQ,SAAS,KAAK,OAAO,cAAc,OAAO,MAAM;CAC5D,MAAM,SAAS,eAAe,OAAO;CACrC,OAAO,WAAW,OAAO,KAAA,IAAY,YAAY,MAAM;AACzD;;;;;AAMA,SAAgB,sBAAsB,OAAmD;CACvF,MAAM,UAAU,OAAO,KAAK;CAC5B,IAAI,CAAC,SAAS,OAAO,KAAA;CACrB,IAAI,YAAY,UAAU,OAAO,YAAY,CAAC;CAC9C,MAAM,SAAS,UAAU,OAAO;CAChC,IAAI,QAAQ,SAAS,MAAM,OAAO,cAAc,OAAO,SAAS,GAAG;CACnE,MAAM,SAAS,eAAe,OAAO;CACrC,OAAO,WAAW,OAAO,KAAA,IAAY,YAAY,MAAM;AACzD"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Vector } from "../primitives/index.js";
|
|
2
|
+
import { Mat3 } from "../matrix.js";
|
|
3
|
+
//#region src/css/transform.d.ts
|
|
4
|
+
/** The size percentages in a transform resolve against: the element's border box. */
|
|
5
|
+
interface CSSTransformBox {
|
|
6
|
+
width: number;
|
|
7
|
+
height: number;
|
|
8
|
+
}
|
|
9
|
+
/** The properties that transform an element, as CSS applies them: translate, rotate, scale, transform. */
|
|
10
|
+
interface CSSTransformStyle {
|
|
11
|
+
transform?: string;
|
|
12
|
+
translate?: string;
|
|
13
|
+
rotate?: string;
|
|
14
|
+
scale?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A CSS `transform` as a 2D matrix about the transform origin, with percentages resolved against
|
|
18
|
+
* `box`. The identity for `none`; null when it turns out of the plane or cannot be read.
|
|
19
|
+
*/
|
|
20
|
+
declare function parseCSSTransform(value: string | undefined, box: CSSTransformBox): Mat3 | null;
|
|
21
|
+
/**
|
|
22
|
+
* The 2D matrix about the transform origin that an element's `translate`, `rotate`, `scale`, and
|
|
23
|
+
* `transform` make together, applied in that order as CSS applies them. Null when one of them
|
|
24
|
+
* turns out of the plane or cannot be read.
|
|
25
|
+
*/
|
|
26
|
+
declare function parseCSSTransforms(style: CSSTransformStyle, box: CSSTransformBox): Mat3 | null;
|
|
27
|
+
/**
|
|
28
|
+
* Where a CSS `transform-origin` puts the point an element transforms about, from its border
|
|
29
|
+
* box's top left corner. Its center when it is unset or cannot be read.
|
|
30
|
+
*/
|
|
31
|
+
declare function parseCSSTransformOrigin(value: string | undefined, box: CSSTransformBox): Vector;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { CSSTransformBox, CSSTransformStyle, parseCSSTransform, parseCSSTransformOrigin, parseCSSTransforms };
|
|
34
|
+
//# sourceMappingURL=transform.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform.d.ts","names":[],"sources":["../../src/css/transform.ts"],"mappings":";;;;UAOiB;EACf;EACA;;;UAIe;EACf;EACA;EACA;EACA;;;;;;iBAgKc,kBAAkB,2BAA2B,KAAK,kBAAkB;;;;;;iBAoCpE,mBAAmB,OAAO,mBAAmB,KAAK,kBAAkB;;;;;iBAiCpE,wBAAwB,2BAA2B,KAAK,kBAAkB"}
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import Matrix from "../matrix.js";
|
|
2
|
+
import { dimension, splitCSSValue } from "./values.js";
|
|
3
|
+
import valueParser from "postcss-value-parser";
|
|
4
|
+
//#region src/css/transform.ts
|
|
5
|
+
const ANGLE_UNITS = {
|
|
6
|
+
deg: Math.PI / 180,
|
|
7
|
+
rad: 1,
|
|
8
|
+
grad: Math.PI / 200,
|
|
9
|
+
turn: Math.PI * 2
|
|
10
|
+
};
|
|
11
|
+
/** An angle in radians; a bare zero counts, as CSS allows. Null for anything else. */
|
|
12
|
+
function angle(value) {
|
|
13
|
+
const parsed = dimension(value.trim());
|
|
14
|
+
if (!parsed) return null;
|
|
15
|
+
if (parsed.unit === "") return parsed.number === 0 ? 0 : null;
|
|
16
|
+
const radians = ANGLE_UNITS[parsed.unit];
|
|
17
|
+
return radians === void 0 ? null : parsed.number * radians;
|
|
18
|
+
}
|
|
19
|
+
/** A length in pixels, a percentage of `extent`, or null for units this cannot resolve. */
|
|
20
|
+
function length(value, extent) {
|
|
21
|
+
const parsed = dimension(value.trim());
|
|
22
|
+
if (!parsed) return null;
|
|
23
|
+
if (parsed.unit === "%") return parsed.number / 100 * extent;
|
|
24
|
+
return parsed.unit === "px" || parsed.unit === "" && parsed.number === 0 ? parsed.number : null;
|
|
25
|
+
}
|
|
26
|
+
/** A plain number, or a percentage as a fraction, as `scale` takes them. */
|
|
27
|
+
function factor(value) {
|
|
28
|
+
const parsed = dimension(value.trim());
|
|
29
|
+
if (!parsed) return null;
|
|
30
|
+
if (parsed.unit === "%") return parsed.number / 100;
|
|
31
|
+
return parsed.unit === "" ? parsed.number : null;
|
|
32
|
+
}
|
|
33
|
+
function skewed(x, y) {
|
|
34
|
+
return [
|
|
35
|
+
1,
|
|
36
|
+
Math.tan(x),
|
|
37
|
+
0,
|
|
38
|
+
Math.tan(y),
|
|
39
|
+
1,
|
|
40
|
+
0,
|
|
41
|
+
0,
|
|
42
|
+
0,
|
|
43
|
+
1
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
/** The 2D matrix `matrix(a, b, c, d, e, f)` names. */
|
|
47
|
+
function affine([a, b, c, d, e, f]) {
|
|
48
|
+
return [
|
|
49
|
+
a,
|
|
50
|
+
c,
|
|
51
|
+
e,
|
|
52
|
+
b,
|
|
53
|
+
d,
|
|
54
|
+
f,
|
|
55
|
+
0,
|
|
56
|
+
0,
|
|
57
|
+
1
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
function translation(x, y, box) {
|
|
61
|
+
const dx = length(x, box.width);
|
|
62
|
+
const dy = y === void 0 ? 0 : length(y, box.height);
|
|
63
|
+
return dx === null || dy === null ? null : Matrix.translated(dx, dy);
|
|
64
|
+
}
|
|
65
|
+
function scaling(x, y) {
|
|
66
|
+
const sx = factor(x);
|
|
67
|
+
const sy = y === void 0 ? sx : factor(y);
|
|
68
|
+
return sx === null || sy === null ? null : Matrix.scaled(sx, sy);
|
|
69
|
+
}
|
|
70
|
+
function numbers(values) {
|
|
71
|
+
const parsed = values.map(Number);
|
|
72
|
+
return parsed.every(Number.isFinite) ? parsed : null;
|
|
73
|
+
}
|
|
74
|
+
function rotation(value) {
|
|
75
|
+
const radians = value === void 0 ? null : angle(value);
|
|
76
|
+
return radians === null ? null : Matrix.rotated(radians);
|
|
77
|
+
}
|
|
78
|
+
function skewing(x, y) {
|
|
79
|
+
const ax = x === void 0 ? 0 : angle(x);
|
|
80
|
+
const ay = y === void 0 ? 0 : angle(y);
|
|
81
|
+
return ax === null || ay === null ? null : skewed(ax, ay);
|
|
82
|
+
}
|
|
83
|
+
/** The 2D transform functions by lowercase name; 3D turns and perspective are left out. */
|
|
84
|
+
const TRANSFORM_FUNCTIONS = {
|
|
85
|
+
matrix: {
|
|
86
|
+
arity: [6, 6],
|
|
87
|
+
build: (args) => {
|
|
88
|
+
const m = numbers(args);
|
|
89
|
+
return m && affine(m);
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
matrix3d: {
|
|
93
|
+
arity: [16, 16],
|
|
94
|
+
build: (args) => {
|
|
95
|
+
const m = numbers(args);
|
|
96
|
+
return m && inPlane(m) ? affine([
|
|
97
|
+
m[0],
|
|
98
|
+
m[1],
|
|
99
|
+
m[4],
|
|
100
|
+
m[5],
|
|
101
|
+
m[12],
|
|
102
|
+
m[13]
|
|
103
|
+
]) : null;
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
translate: {
|
|
107
|
+
arity: [1, 2],
|
|
108
|
+
build: (args, box) => translation(args[0], args.at(1), box)
|
|
109
|
+
},
|
|
110
|
+
translate3d: {
|
|
111
|
+
arity: [3, 3],
|
|
112
|
+
build: (args, box) => depth(args[2]) ? translation(args[0], args[1], box) : null
|
|
113
|
+
},
|
|
114
|
+
translatex: {
|
|
115
|
+
arity: [1, 1],
|
|
116
|
+
build: (args, box) => translation(args[0], void 0, box)
|
|
117
|
+
},
|
|
118
|
+
translatey: {
|
|
119
|
+
arity: [1, 1],
|
|
120
|
+
build: (args, box) => translation("0", args[0], box)
|
|
121
|
+
},
|
|
122
|
+
rotate: {
|
|
123
|
+
arity: [1, 1],
|
|
124
|
+
build: (args) => rotation(args[0])
|
|
125
|
+
},
|
|
126
|
+
rotatez: {
|
|
127
|
+
arity: [1, 1],
|
|
128
|
+
build: (args) => rotation(args[0])
|
|
129
|
+
},
|
|
130
|
+
scale: {
|
|
131
|
+
arity: [1, 2],
|
|
132
|
+
build: (args) => scaling(args[0], args.at(1))
|
|
133
|
+
},
|
|
134
|
+
scale3d: {
|
|
135
|
+
arity: [3, 3],
|
|
136
|
+
build: (args) => factor(args[2]) === null ? null : scaling(args[0], args[1])
|
|
137
|
+
},
|
|
138
|
+
scalex: {
|
|
139
|
+
arity: [1, 1],
|
|
140
|
+
build: (args) => scaling(args[0], "1")
|
|
141
|
+
},
|
|
142
|
+
scaley: {
|
|
143
|
+
arity: [1, 1],
|
|
144
|
+
build: (args) => scaling("1", args[0])
|
|
145
|
+
},
|
|
146
|
+
skew: {
|
|
147
|
+
arity: [1, 2],
|
|
148
|
+
build: (args) => skewing(args[0], args.at(1))
|
|
149
|
+
},
|
|
150
|
+
skewx: {
|
|
151
|
+
arity: [1, 1],
|
|
152
|
+
build: (args) => skewing(args[0], void 0)
|
|
153
|
+
},
|
|
154
|
+
skewy: {
|
|
155
|
+
arity: [1, 1],
|
|
156
|
+
build: (args) => skewing(void 0, args[0])
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
/**
|
|
160
|
+
* Whether a `matrix3d`, column by column, keeps the plane where it was: no turn out of it and no
|
|
161
|
+
* perspective. Depth, which moves nothing on the plane without perspective, may change.
|
|
162
|
+
*/
|
|
163
|
+
function inPlane(m) {
|
|
164
|
+
return [
|
|
165
|
+
m[2],
|
|
166
|
+
m[3],
|
|
167
|
+
m[6],
|
|
168
|
+
m[7],
|
|
169
|
+
m[8],
|
|
170
|
+
m[9],
|
|
171
|
+
m[11]
|
|
172
|
+
].every((value) => value === 0) && m[15] === 1;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Whether a z offset is a length, which a percentage never is. It moves nothing on the plane without perspective, which a
|
|
176
|
+
* layer cannot hold, so it is read and dropped.
|
|
177
|
+
*/
|
|
178
|
+
function depth(value) {
|
|
179
|
+
return !value.trim().endsWith("%") && length(value, 0) !== null;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* A function's arguments, which CSS separates with commas: null when one is missing, or two
|
|
183
|
+
* stand without a comma between them.
|
|
184
|
+
*/
|
|
185
|
+
function functionArguments(nodes) {
|
|
186
|
+
const parts = nodes.filter((node) => node.type !== "space" && node.type !== "comment");
|
|
187
|
+
const args = [];
|
|
188
|
+
for (const [index, node] of parts.entries()) {
|
|
189
|
+
const expectsArgument = index % 2 === 0;
|
|
190
|
+
if (expectsArgument && node.type !== "word") return null;
|
|
191
|
+
if (!expectsArgument && (node.type !== "div" || node.value !== ",")) return null;
|
|
192
|
+
if (expectsArgument) args.push(node.value);
|
|
193
|
+
}
|
|
194
|
+
return parts.length % 2 === 1 ? args : null;
|
|
195
|
+
}
|
|
196
|
+
/** One transform function as a matrix; null for one this does not know or cannot read. */
|
|
197
|
+
function transformFunction(name, args, box) {
|
|
198
|
+
const fn = Object.hasOwn(TRANSFORM_FUNCTIONS, name) ? TRANSFORM_FUNCTIONS[name] : void 0;
|
|
199
|
+
if (!fn || args.length < fn.arity[0] || args.length > fn.arity[1]) return null;
|
|
200
|
+
return fn.build(args, box);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* A CSS `transform` as a 2D matrix about the transform origin, with percentages resolved against
|
|
204
|
+
* `box`. The identity for `none`; null when it turns out of the plane or cannot be read.
|
|
205
|
+
*/
|
|
206
|
+
function parseCSSTransform(value, box) {
|
|
207
|
+
const text = value?.trim();
|
|
208
|
+
if (!text || text === "none") return Matrix.identity();
|
|
209
|
+
let matrix = Matrix.identity();
|
|
210
|
+
for (const node of valueParser(text).nodes) {
|
|
211
|
+
if (node.type === "space" || node.type === "comment") continue;
|
|
212
|
+
if (node.type !== "function") return null;
|
|
213
|
+
const args = functionArguments(node.nodes);
|
|
214
|
+
const step = args && transformFunction(node.value.toLowerCase(), args, box);
|
|
215
|
+
if (!step) return null;
|
|
216
|
+
matrix = Matrix.multiply(matrix, step);
|
|
217
|
+
}
|
|
218
|
+
return matrix;
|
|
219
|
+
}
|
|
220
|
+
/** The `rotate` property: an angle, or one about the z axis. Null when it turns out of the plane. */
|
|
221
|
+
function rotateProperty(parts) {
|
|
222
|
+
const last = parts.at(-1);
|
|
223
|
+
const axis = parts.slice(0, -1).join(" ").toLowerCase();
|
|
224
|
+
const radians = last === void 0 ? null : angle(last);
|
|
225
|
+
if (radians === null || ![
|
|
226
|
+
"",
|
|
227
|
+
"z",
|
|
228
|
+
"0 0 1"
|
|
229
|
+
].includes(axis)) return null;
|
|
230
|
+
return Matrix.rotated(radians);
|
|
231
|
+
}
|
|
232
|
+
/** A transform property's value as a matrix; the identity when it is unset or `none`. */
|
|
233
|
+
function property(value, parse) {
|
|
234
|
+
const text = value?.trim();
|
|
235
|
+
if (!text || text === "none") return Matrix.identity();
|
|
236
|
+
return parse(splitCSSValue(text));
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* The 2D matrix about the transform origin that an element's `translate`, `rotate`, `scale`, and
|
|
240
|
+
* `transform` make together, applied in that order as CSS applies them. Null when one of them
|
|
241
|
+
* turns out of the plane or cannot be read.
|
|
242
|
+
*/
|
|
243
|
+
function parseCSSTransforms(style, box) {
|
|
244
|
+
const steps = [
|
|
245
|
+
property(style.translate, (parts) => {
|
|
246
|
+
const [x, y, z] = [
|
|
247
|
+
parts.at(0),
|
|
248
|
+
parts.at(1),
|
|
249
|
+
parts.at(2)
|
|
250
|
+
];
|
|
251
|
+
if (x === void 0 || parts.length > 3 || z !== void 0 && !depth(z)) return null;
|
|
252
|
+
return translation(x, y, box);
|
|
253
|
+
}),
|
|
254
|
+
property(style.rotate, rotateProperty),
|
|
255
|
+
property(style.scale, (parts) => {
|
|
256
|
+
const [x, y, z] = [
|
|
257
|
+
parts.at(0),
|
|
258
|
+
parts.at(1),
|
|
259
|
+
parts.at(2)
|
|
260
|
+
];
|
|
261
|
+
if (x === void 0 || parts.length > 3 || z !== void 0 && factor(z) === null) return null;
|
|
262
|
+
return scaling(x, y);
|
|
263
|
+
}),
|
|
264
|
+
parseCSSTransform(style.transform, box)
|
|
265
|
+
];
|
|
266
|
+
if (steps.some((step) => step === null)) return null;
|
|
267
|
+
return Matrix.multiply(...steps.filter((step) => step !== null));
|
|
268
|
+
}
|
|
269
|
+
const ORIGIN_KEYWORDS = {
|
|
270
|
+
left: {
|
|
271
|
+
axis: "x",
|
|
272
|
+
fraction: 0
|
|
273
|
+
},
|
|
274
|
+
right: {
|
|
275
|
+
axis: "x",
|
|
276
|
+
fraction: 1
|
|
277
|
+
},
|
|
278
|
+
top: {
|
|
279
|
+
axis: "y",
|
|
280
|
+
fraction: 0
|
|
281
|
+
},
|
|
282
|
+
bottom: {
|
|
283
|
+
axis: "y",
|
|
284
|
+
fraction: 1
|
|
285
|
+
},
|
|
286
|
+
center: {
|
|
287
|
+
axis: "either",
|
|
288
|
+
fraction: .5
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* Where a CSS `transform-origin` puts the point an element transforms about, from its border
|
|
293
|
+
* box's top left corner. Its center when it is unset or cannot be read.
|
|
294
|
+
*/
|
|
295
|
+
function parseCSSTransformOrigin(value, box) {
|
|
296
|
+
const center = {
|
|
297
|
+
x: box.width / 2,
|
|
298
|
+
y: box.height / 2
|
|
299
|
+
};
|
|
300
|
+
const tokens = splitCSSValue(value?.trim() ?? "");
|
|
301
|
+
const depthOk = tokens.length < 3 || tokens.length === 3 && depth(tokens[2]);
|
|
302
|
+
if (tokens.length === 0 || !depthOk) return center;
|
|
303
|
+
const [first = "center", second = "center"] = tokens.slice(0, 2).map((part) => part.toLowerCase());
|
|
304
|
+
const [x, y] = ORIGIN_KEYWORDS[first]?.axis === "y" || ORIGIN_KEYWORDS[second]?.axis === "x" ? [second, first] : [first, second];
|
|
305
|
+
const resolve = (part, extent, axis) => {
|
|
306
|
+
const keyword = ORIGIN_KEYWORDS[part];
|
|
307
|
+
if (!keyword) return length(part, extent);
|
|
308
|
+
return keyword.axis === axis || keyword.axis === "either" ? keyword.fraction * extent : null;
|
|
309
|
+
};
|
|
310
|
+
const ox = resolve(x, box.width, "x");
|
|
311
|
+
const oy = resolve(y, box.height, "y");
|
|
312
|
+
return ox === null || oy === null ? center : {
|
|
313
|
+
x: ox,
|
|
314
|
+
y: oy
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
//#endregion
|
|
318
|
+
export { parseCSSTransform, parseCSSTransformOrigin, parseCSSTransforms };
|
|
319
|
+
|
|
320
|
+
//# sourceMappingURL=transform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform.js","names":[],"sources":["../../src/css/transform.ts"],"sourcesContent":["import valueParser from 'postcss-value-parser'\n\nimport Matrix, { type Mat3 } from '../matrix'\nimport type { Vector } from '../primitives'\nimport { dimension, splitCSSValue } from './values'\n\n/** The size percentages in a transform resolve against: the element's border box. */\nexport interface CSSTransformBox {\n width: number\n height: number\n}\n\n/** The properties that transform an element, as CSS applies them: translate, rotate, scale, transform. */\nexport interface CSSTransformStyle {\n transform?: string\n translate?: string\n rotate?: string\n scale?: string\n}\n\nconst ANGLE_UNITS: Partial<Record<string, number>> = {\n deg: Math.PI / 180,\n rad: 1,\n grad: Math.PI / 200,\n turn: Math.PI * 2\n}\n\n/** An angle in radians; a bare zero counts, as CSS allows. Null for anything else. */\nfunction angle(value: string): number | null {\n const parsed = dimension(value.trim())\n if (!parsed) return null\n if (parsed.unit === '') return parsed.number === 0 ? 0 : null\n const radians = ANGLE_UNITS[parsed.unit]\n return radians === undefined ? null : parsed.number * radians\n}\n\n/** A length in pixels, a percentage of `extent`, or null for units this cannot resolve. */\nfunction length(value: string, extent: number): number | null {\n const parsed = dimension(value.trim())\n if (!parsed) return null\n if (parsed.unit === '%') return (parsed.number / 100) * extent\n return parsed.unit === 'px' || (parsed.unit === '' && parsed.number === 0) ? parsed.number : null\n}\n\n/** A plain number, or a percentage as a fraction, as `scale` takes them. */\nfunction factor(value: string): number | null {\n const parsed = dimension(value.trim())\n if (!parsed) return null\n if (parsed.unit === '%') return parsed.number / 100\n return parsed.unit === '' ? parsed.number : null\n}\n\nfunction skewed(x: number, y: number): Mat3 {\n return [1, Math.tan(x), 0, Math.tan(y), 1, 0, 0, 0, 1]\n}\n\n/** The 2D matrix `matrix(a, b, c, d, e, f)` names. */\nfunction affine([a, b, c, d, e, f]: number[]): Mat3 {\n return [a, c, e, b, d, f, 0, 0, 1]\n}\n\nfunction translation(x: string, y: string | undefined, box: CSSTransformBox): Mat3 | null {\n const dx = length(x, box.width)\n const dy = y === undefined ? 0 : length(y, box.height)\n return dx === null || dy === null ? null : Matrix.translated(dx, dy)\n}\n\nfunction scaling(x: string, y: string | undefined): Mat3 | null {\n const sx = factor(x)\n const sy = y === undefined ? sx : factor(y)\n return sx === null || sy === null ? null : Matrix.scaled(sx, sy)\n}\n\nfunction numbers(values: string[]): number[] | null {\n const parsed = values.map(Number)\n return parsed.every(Number.isFinite) ? parsed : null\n}\n\nfunction rotation(value: string | undefined): Mat3 | null {\n const radians = value === undefined ? null : angle(value)\n return radians === null ? null : Matrix.rotated(radians)\n}\n\nfunction skewing(x: string | undefined, y: string | undefined): Mat3 | null {\n const ax = x === undefined ? 0 : angle(x)\n const ay = y === undefined ? 0 : angle(y)\n return ax === null || ay === null ? null : skewed(ax, ay)\n}\n\n/** A transform function: how many arguments it takes, and the matrix they make. */\ninterface TransformFunction {\n arity: readonly [min: number, max: number]\n build: (args: string[], box: CSSTransformBox) => Mat3 | null\n}\n\n/** The 2D transform functions by lowercase name; 3D turns and perspective are left out. */\nconst TRANSFORM_FUNCTIONS: Partial<Record<string, TransformFunction>> = {\n matrix: {\n arity: [6, 6],\n build: (args) => {\n const m = numbers(args)\n return m && affine(m)\n }\n },\n matrix3d: {\n arity: [16, 16],\n build: (args) => {\n const m = numbers(args)\n return m && inPlane(m) ? affine([m[0], m[1], m[4], m[5], m[12], m[13]]) : null\n }\n },\n translate: { arity: [1, 2], build: (args, box) => translation(args[0], args.at(1), box) },\n translate3d: {\n arity: [3, 3],\n build: (args, box) => (depth(args[2]) ? translation(args[0], args[1], box) : null)\n },\n translatex: { arity: [1, 1], build: (args, box) => translation(args[0], undefined, box) },\n translatey: { arity: [1, 1], build: (args, box) => translation('0', args[0], box) },\n rotate: { arity: [1, 1], build: (args) => rotation(args[0]) },\n rotatez: { arity: [1, 1], build: (args) => rotation(args[0]) },\n scale: { arity: [1, 2], build: (args) => scaling(args[0], args.at(1)) },\n scale3d: {\n arity: [3, 3],\n build: (args) => (factor(args[2]) === null ? null : scaling(args[0], args[1]))\n },\n scalex: { arity: [1, 1], build: (args) => scaling(args[0], '1') },\n scaley: { arity: [1, 1], build: (args) => scaling('1', args[0]) },\n skew: { arity: [1, 2], build: (args) => skewing(args[0], args.at(1)) },\n skewx: { arity: [1, 1], build: (args) => skewing(args[0], undefined) },\n skewy: { arity: [1, 1], build: (args) => skewing(undefined, args[0]) }\n}\n\n/**\n * Whether a `matrix3d`, column by column, keeps the plane where it was: no turn out of it and no\n * perspective. Depth, which moves nothing on the plane without perspective, may change.\n */\nfunction inPlane(m: number[]): boolean {\n const outOfPlane = [m[2], m[3], m[6], m[7], m[8], m[9], m[11]]\n return outOfPlane.every((value) => value === 0) && m[15] === 1\n}\n\n/**\n * Whether a z offset is a length, which a percentage never is. It moves nothing on the plane without perspective, which a\n * layer cannot hold, so it is read and dropped.\n */\nfunction depth(value: string): boolean {\n return !value.trim().endsWith('%') && length(value, 0) !== null\n}\n\n/**\n * A function's arguments, which CSS separates with commas: null when one is missing, or two\n * stand without a comma between them.\n */\nfunction functionArguments(nodes: valueParser.Node[]): string[] | null {\n const parts = nodes.filter((node) => node.type !== 'space' && node.type !== 'comment')\n const args: string[] = []\n for (const [index, node] of parts.entries()) {\n const expectsArgument = index % 2 === 0\n if (expectsArgument && node.type !== 'word') return null\n if (!expectsArgument && (node.type !== 'div' || node.value !== ',')) return null\n if (expectsArgument) args.push(node.value)\n }\n return parts.length % 2 === 1 ? args : null\n}\n\n/** One transform function as a matrix; null for one this does not know or cannot read. */\nfunction transformFunction(name: string, args: string[], box: CSSTransformBox): Mat3 | null {\n // Own names only, so `constructor` or `__proto__` reads as unknown instead of a prototype member.\n const fn = Object.hasOwn(TRANSFORM_FUNCTIONS, name) ? TRANSFORM_FUNCTIONS[name] : undefined\n if (!fn || args.length < fn.arity[0] || args.length > fn.arity[1]) return null\n return fn.build(args, box)\n}\n\n/**\n * A CSS `transform` as a 2D matrix about the transform origin, with percentages resolved against\n * `box`. The identity for `none`; null when it turns out of the plane or cannot be read.\n */\nexport function parseCSSTransform(value: string | undefined, box: CSSTransformBox): Mat3 | null {\n const text = value?.trim()\n if (!text || text === 'none') return Matrix.identity()\n let matrix = Matrix.identity()\n for (const node of valueParser(text).nodes) {\n if (node.type === 'space' || node.type === 'comment') continue\n if (node.type !== 'function') return null\n const args = functionArguments(node.nodes)\n const step = args && transformFunction(node.value.toLowerCase(), args, box)\n if (!step) return null\n matrix = Matrix.multiply(matrix, step)\n }\n return matrix\n}\n\n/** The `rotate` property: an angle, or one about the z axis. Null when it turns out of the plane. */\nfunction rotateProperty(parts: string[]): Mat3 | null {\n const last = parts.at(-1)\n const axis = parts.slice(0, -1).join(' ').toLowerCase()\n const radians = last === undefined ? null : angle(last)\n if (radians === null || !['', 'z', '0 0 1'].includes(axis)) return null\n return Matrix.rotated(radians)\n}\n\n/** A transform property's value as a matrix; the identity when it is unset or `none`. */\nfunction property(value: string | undefined, parse: (parts: string[]) => Mat3 | null): Mat3 | null {\n const text = value?.trim()\n if (!text || text === 'none') return Matrix.identity()\n return parse(splitCSSValue(text))\n}\n\n/**\n * The 2D matrix about the transform origin that an element's `translate`, `rotate`, `scale`, and\n * `transform` make together, applied in that order as CSS applies them. Null when one of them\n * turns out of the plane or cannot be read.\n */\nexport function parseCSSTransforms(style: CSSTransformStyle, box: CSSTransformBox): Mat3 | null {\n const steps = [\n property(style.translate, (parts) => {\n const [x, y, z] = [parts.at(0), parts.at(1), parts.at(2)]\n if (x === undefined || parts.length > 3 || (z !== undefined && !depth(z))) return null\n return translation(x, y, box)\n }),\n property(style.rotate, rotateProperty),\n property(style.scale, (parts) => {\n const [x, y, z] = [parts.at(0), parts.at(1), parts.at(2)]\n if (x === undefined || parts.length > 3 || (z !== undefined && factor(z) === null)) {\n return null\n }\n return scaling(x, y)\n }),\n parseCSSTransform(style.transform, box)\n ]\n if (steps.some((step) => step === null)) return null\n return Matrix.multiply(...steps.filter((step) => step !== null))\n}\n\nconst ORIGIN_KEYWORDS: Partial<Record<string, { axis: 'x' | 'y' | 'either'; fraction: number }>> = {\n left: { axis: 'x', fraction: 0 },\n right: { axis: 'x', fraction: 1 },\n top: { axis: 'y', fraction: 0 },\n bottom: { axis: 'y', fraction: 1 },\n center: { axis: 'either', fraction: 0.5 }\n}\n\n/**\n * Where a CSS `transform-origin` puts the point an element transforms about, from its border\n * box's top left corner. Its center when it is unset or cannot be read.\n */\nexport function parseCSSTransformOrigin(value: string | undefined, box: CSSTransformBox): Vector {\n const center = { x: box.width / 2, y: box.height / 2 }\n const tokens = splitCSSValue(value?.trim() ?? '')\n // A third value is the depth, which must be a length; anything past it is not an origin.\n const depthOk = tokens.length < 3 || (tokens.length === 3 && depth(tokens[2]))\n if (tokens.length === 0 || !depthOk) return center\n const parts = tokens.slice(0, 2)\n // A vertical keyword first, as in `top left`, names the y offset.\n const [first = 'center', second = 'center'] = parts.map((part) => part.toLowerCase())\n const swapped = ORIGIN_KEYWORDS[first]?.axis === 'y' || ORIGIN_KEYWORDS[second]?.axis === 'x'\n const [x, y] = swapped ? [second, first] : [first, second]\n const resolve = (part: string, extent: number, axis: 'x' | 'y') => {\n const keyword = ORIGIN_KEYWORDS[part]\n if (!keyword) return length(part, extent)\n return keyword.axis === axis || keyword.axis === 'either' ? keyword.fraction * extent : null\n }\n const ox = resolve(x, box.width, 'x')\n const oy = resolve(y, box.height, 'y')\n return ox === null || oy === null ? center : { x: ox, y: oy }\n}\n"],"mappings":";;;;AAoBA,MAAM,cAA+C;CACnD,KAAK,KAAK,KAAK;CACf,KAAK;CACL,MAAM,KAAK,KAAK;CAChB,MAAM,KAAK,KAAK;AAClB;;AAGA,SAAS,MAAM,OAA8B;CAC3C,MAAM,SAAS,UAAU,MAAM,KAAK,CAAC;CACrC,IAAI,CAAC,QAAQ,OAAO;CACpB,IAAI,OAAO,SAAS,IAAI,OAAO,OAAO,WAAW,IAAI,IAAI;CACzD,MAAM,UAAU,YAAY,OAAO;CACnC,OAAO,YAAY,KAAA,IAAY,OAAO,OAAO,SAAS;AACxD;;AAGA,SAAS,OAAO,OAAe,QAA+B;CAC5D,MAAM,SAAS,UAAU,MAAM,KAAK,CAAC;CACrC,IAAI,CAAC,QAAQ,OAAO;CACpB,IAAI,OAAO,SAAS,KAAK,OAAQ,OAAO,SAAS,MAAO;CACxD,OAAO,OAAO,SAAS,QAAS,OAAO,SAAS,MAAM,OAAO,WAAW,IAAK,OAAO,SAAS;AAC/F;;AAGA,SAAS,OAAO,OAA8B;CAC5C,MAAM,SAAS,UAAU,MAAM,KAAK,CAAC;CACrC,IAAI,CAAC,QAAQ,OAAO;CACpB,IAAI,OAAO,SAAS,KAAK,OAAO,OAAO,SAAS;CAChD,OAAO,OAAO,SAAS,KAAK,OAAO,SAAS;AAC9C;AAEA,SAAS,OAAO,GAAW,GAAiB;CAC1C,OAAO;EAAC;EAAG,KAAK,IAAI,CAAC;EAAG;EAAG,KAAK,IAAI,CAAC;EAAG;EAAG;EAAG;EAAG;EAAG;C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AAS,OAAO;CAG5C,MAAM,CAAC,QAAQ,UAAU,SAAS,YAFpB,OAAO,MAAM,GAAG,CAEoB,CAAC,CAAC,KAAK,SAAS,KAAK,YAAY,CAAC;CAEpF,MAAM,CAAC,GAAG,KADM,gBAAgB,MAAM,EAAE,SAAS,OAAO,gBAAgB,OAAO,EAAE,SAAS,MACjE,CAAC,QAAQ,KAAK,IAAI,CAAC,OAAO,MAAM;CACzD,MAAM,WAAW,MAAc,QAAgB,SAAoB;EACjE,MAAM,UAAU,gBAAgB;EAChC,IAAI,CAAC,SAAS,OAAO,OAAO,MAAM,MAAM;EACxC,OAAO,QAAQ,SAAS,QAAQ,QAAQ,SAAS,WAAW,QAAQ,WAAW,SAAS;CAC1F;CACA,MAAM,KAAK,QAAQ,GAAG,IAAI,OAAO,GAAG;CACpC,MAAM,KAAK,QAAQ,GAAG,IAAI,QAAQ,GAAG;CACrC,OAAO,OAAO,QAAQ,OAAO,OAAO,SAAS;EAAE,GAAG;EAAI,GAAG;CAAG;AAC9D"}
|
package/dist/css/values.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"values.d.ts","names":[],"sources":["../../src/css/values.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"values.d.ts","names":[],"sources":["../../src/css/values.ts"],"mappings":";;;;;;iBAoBgB,eAAe;;iBAkBf,cAAc,4BAA4B;;iBAO1C,cAAc"}
|
package/dist/css/values.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import { tryParseColor } from "../color/index.js";
|
|
2
2
|
import valueParser from "postcss-value-parser";
|
|
3
3
|
//#region src/css/values.ts
|
|
4
|
+
/** A CSS dimension's number and lowercase unit, or null. */
|
|
5
|
+
function dimension(value) {
|
|
6
|
+
const parsed = valueParser.unit(value);
|
|
7
|
+
if (!parsed) return null;
|
|
8
|
+
const number = Number(parsed.number);
|
|
9
|
+
return Number.isFinite(number) ? {
|
|
10
|
+
number,
|
|
11
|
+
unit: parsed.unit.toLowerCase()
|
|
12
|
+
} : null;
|
|
13
|
+
}
|
|
4
14
|
/** Pixels in a rem, as browsers use by default. */
|
|
5
15
|
const ROOT_FONT_SIZE = 16;
|
|
6
16
|
/**
|
|
@@ -31,6 +41,6 @@ function splitCSSValue(value) {
|
|
|
31
41
|
return valueParser(value).nodes.filter((node) => node.type !== "space" && node.type !== "comment").map((node) => valueParser.stringify(node));
|
|
32
42
|
}
|
|
33
43
|
//#endregion
|
|
34
|
-
export { parseCSSColor, parseCSSNumber, splitCSSValue };
|
|
44
|
+
export { dimension, parseCSSColor, parseCSSNumber, splitCSSValue };
|
|
35
45
|
|
|
36
46
|
//# sourceMappingURL=values.js.map
|
package/dist/css/values.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"values.js","names":[],"sources":["../../src/css/values.ts"],"sourcesContent":["import valueParser from 'postcss-value-parser'\n\nimport { tryParseColor } from '../color'\nimport type { Color } from '../primitives'\n\n/** Pixels in a rem, as browsers use by default. */\nconst ROOT_FONT_SIZE = 16\n\n/**\n * A CSS number in pixels: unitless, `px`, or `rem`. Null for `auto`, for units that depend on\n * what the value applies to (`%`, `em`, `vh`), and for anything that is not a number.\n */\nexport function parseCSSNumber(value: string | undefined): number | null {\n if (!value) return null\n const dimension = valueParser.unit(value.trim())\n if (!dimension) return null\n const number = Number(dimension.number)\n if (!Number.isFinite(number)) return null\n switch (dimension.unit.toLowerCase()) {\n case '':\n case 'px':\n return number\n case 'rem':\n return number * ROOT_FONT_SIZE\n default:\n return null\n }\n}\n\n/** A CSS color that paints something; null for an invalid or fully transparent one. */\nexport function parseCSSColor(value: string | undefined): Color | null {\n if (!value) return null\n const color = tryParseColor(value.trim())\n return color && color.a > 0 ? color : null\n}\n\n/** The space-separated parts of a CSS value, keeping functions such as `rgb(0, 0, 0)` whole. */\nexport function splitCSSValue(value: string): string[] {\n return valueParser(value)\n .nodes.filter((node) => node.type !== 'space' && node.type !== 'comment')\n .map((node) => valueParser.stringify(node))\n}\n"],"mappings":";;;;AAMA,MAAM,iBAAiB;;;;;AAMvB,SAAgB,eAAe,OAA0C;CACvE,IAAI,CAAC,OAAO,OAAO;CACnB,MAAM,YAAY,YAAY,KAAK,MAAM,KAAK,CAAC;CAC/C,IAAI,CAAC,WAAW,OAAO;CACvB,MAAM,SAAS,OAAO,UAAU,MAAM;CACtC,IAAI,CAAC,OAAO,SAAS,MAAM,GAAG,OAAO;CACrC,QAAQ,UAAU,KAAK,YAAY,GAAnC;EACE,KAAK;EACL,KAAK,MACH,OAAO;EACT,KAAK,OACH,OAAO,SAAS;EAClB,SACE,OAAO;CACX;AACF;;AAGA,SAAgB,cAAc,OAAyC;CACrE,IAAI,CAAC,OAAO,OAAO;CACnB,MAAM,QAAQ,cAAc,MAAM,KAAK,CAAC;CACxC,OAAO,SAAS,MAAM,IAAI,IAAI,QAAQ;AACxC;;AAGA,SAAgB,cAAc,OAAyB;CACrD,OAAO,YAAY,KAAK,CAAC,CACtB,MAAM,QAAQ,SAAS,KAAK,SAAS,WAAW,KAAK,SAAS,SAAS,CAAC,CACxE,KAAK,SAAS,YAAY,UAAU,IAAI,CAAC;AAC9C"}
|
|
1
|
+
{"version":3,"file":"values.js","names":[],"sources":["../../src/css/values.ts"],"sourcesContent":["import valueParser from 'postcss-value-parser'\n\nimport { tryParseColor } from '../color'\nimport type { Color } from '../primitives'\n\n/** A CSS dimension's number and lowercase unit, or null. */\nexport function dimension(value: string): { number: number; unit: string } | null {\n const parsed = valueParser.unit(value)\n if (!parsed) return null\n const number = Number(parsed.number)\n return Number.isFinite(number) ? { number, unit: parsed.unit.toLowerCase() } : null\n}\n\n/** Pixels in a rem, as browsers use by default. */\nconst ROOT_FONT_SIZE = 16\n\n/**\n * A CSS number in pixels: unitless, `px`, or `rem`. Null for `auto`, for units that depend on\n * what the value applies to (`%`, `em`, `vh`), and for anything that is not a number.\n */\nexport function parseCSSNumber(value: string | undefined): number | null {\n if (!value) return null\n const dimension = valueParser.unit(value.trim())\n if (!dimension) return null\n const number = Number(dimension.number)\n if (!Number.isFinite(number)) return null\n switch (dimension.unit.toLowerCase()) {\n case '':\n case 'px':\n return number\n case 'rem':\n return number * ROOT_FONT_SIZE\n default:\n return null\n }\n}\n\n/** A CSS color that paints something; null for an invalid or fully transparent one. */\nexport function parseCSSColor(value: string | undefined): Color | null {\n if (!value) return null\n const color = tryParseColor(value.trim())\n return color && color.a > 0 ? color : null\n}\n\n/** The space-separated parts of a CSS value, keeping functions such as `rgb(0, 0, 0)` whole. */\nexport function splitCSSValue(value: string): string[] {\n return valueParser(value)\n .nodes.filter((node) => node.type !== 'space' && node.type !== 'comment')\n .map((node) => valueParser.stringify(node))\n}\n"],"mappings":";;;;AAMA,SAAgB,UAAU,OAAwD;CAChF,MAAM,SAAS,YAAY,KAAK,KAAK;CACrC,IAAI,CAAC,QAAQ,OAAO;CACpB,MAAM,SAAS,OAAO,OAAO,MAAM;CACnC,OAAO,OAAO,SAAS,MAAM,IAAI;EAAE;EAAQ,MAAM,OAAO,KAAK,YAAY;CAAE,IAAI;AACjF;;AAGA,MAAM,iBAAiB;;;;;AAMvB,SAAgB,eAAe,OAA0C;CACvE,IAAI,CAAC,OAAO,OAAO;CACnB,MAAM,YAAY,YAAY,KAAK,MAAM,KAAK,CAAC;CAC/C,IAAI,CAAC,WAAW,OAAO;CACvB,MAAM,SAAS,OAAO,UAAU,MAAM;CACtC,IAAI,CAAC,OAAO,SAAS,MAAM,GAAG,OAAO;CACrC,QAAQ,UAAU,KAAK,YAAY,GAAnC;EACE,KAAK;EACL,KAAK,MACH,OAAO;EACT,KAAK,OACH,OAAO,SAAS;EAClB,SACE,OAAO;CACX;AACF;;AAGA,SAAgB,cAAc,OAAyC;CACrE,IAAI,CAAC,OAAO,OAAO;CACnB,MAAM,QAAQ,cAAc,MAAM,KAAK,CAAC;CACxC,OAAO,SAAS,MAAM,IAAI,IAAI,QAAQ;AACxC;;AAGA,SAAgB,cAAc,OAAyB;CACrD,OAAO,YAAY,KAAK,CAAC,CACtB,MAAM,QAAQ,SAAS,KAAK,SAAS,WAAW,KAAK,SAAS,SAAS,CAAC,CACxE,KAAK,SAAS,YAAY,UAAU,IAAI,CAAC;AAC9C"}
|