@webstudio-is/css-data 0.90.0 → 0.266.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/lib/index.js +17534 -15
- package/lib/types/__generated__/animatable-properties.d.ts +1 -0
- package/lib/types/__generated__/html.d.ts +2 -0
- package/lib/types/__generated__/keyword-values.d.ts +4 -358
- package/lib/types/__generated__/properties.d.ts +10 -3674
- package/lib/types/__generated__/property-value-descriptions.d.ts +7 -5446
- package/lib/types/__generated__/pseudo-classes.d.ts +1 -0
- package/lib/types/__generated__/pseudo-elements.d.ts +1 -0
- package/lib/types/__generated__/pseudo-selector-descriptions.d.ts +2 -0
- package/lib/types/__generated__/shorthand-properties.d.ts +1 -0
- package/lib/types/css-tree.d.ts +772 -0
- package/lib/types/index.d.ts +18 -3680
- package/lib/types/media-condition-simulator.d.ts +7 -0
- package/lib/types/parse-css-value.d.ts +8 -3
- package/lib/types/parse-css.d.ts +75 -6
- package/lib/types/property-parsers/conic-gradient.d.ts +3 -0
- package/lib/types/property-parsers/gradient-utils.d.ts +20 -0
- package/lib/types/property-parsers/grid-template-areas.d.ts +21 -0
- package/lib/types/property-parsers/grid-template-tracks.d.ts +128 -0
- package/lib/types/property-parsers/index.d.ts +6 -2
- package/lib/types/property-parsers/linear-gradient.d.ts +4 -0
- package/lib/types/property-parsers/radial-gradient.d.ts +3 -0
- package/lib/types/property-parsers/types.d.ts +28 -0
- package/lib/types/selector-validation.d.ts +13 -0
- package/lib/types/shorthands.d.ts +2 -0
- package/package.json +29 -33
- package/lib/__generated__/keyword-values.js +0 -5255
- package/lib/__generated__/properties.js +0 -3697
- package/lib/__generated__/property-value-descriptions.js +0 -2740
- package/lib/__generated__/units.js +0 -65
- package/lib/cjs/__generated__/keyword-values.js +0 -5275
- package/lib/cjs/__generated__/properties.js +0 -3717
- package/lib/cjs/__generated__/property-value-descriptions.js +0 -2760
- package/lib/cjs/__generated__/units.js +0 -85
- package/lib/cjs/custom-data.js +0 -64
- package/lib/cjs/html-check.js +0 -25
- package/lib/cjs/html.js +0 -581
- package/lib/cjs/index.js +0 -48
- package/lib/cjs/package.json +0 -1
- package/lib/cjs/parse-css-value.js +0 -152
- package/lib/cjs/parse-css.js +0 -136
- package/lib/cjs/popularity-index.js +0 -4633
- package/lib/cjs/property-parsers/background.js +0 -119
- package/lib/cjs/property-parsers/box-shadow.js +0 -129
- package/lib/cjs/property-parsers/index.js +0 -19
- package/lib/cjs/property-parsers/parsers.js +0 -26
- package/lib/cjs/property-parsers/to-longhand.js +0 -24
- package/lib/cjs/schema.js +0 -135
- package/lib/custom-data.js +0 -44
- package/lib/html-check.js +0 -2
- package/lib/html.js +0 -561
- package/lib/parse-css-value.js +0 -122
- package/lib/parse-css.js +0 -106
- package/lib/popularity-index.js +0 -4613
- package/lib/property-parsers/background.js +0 -89
- package/lib/property-parsers/box-shadow.js +0 -99
- package/lib/property-parsers/index.js +0 -2
- package/lib/property-parsers/parsers.js +0 -6
- package/lib/property-parsers/to-longhand.js +0 -4
- package/lib/schema.js +0 -115
- package/lib/types/custom-data.d.ts +0 -15
- package/lib/types/html-check.d.ts +0 -1
- package/lib/types/html.d.ts +0 -62
- package/lib/types/parse-css-value.test.d.ts +0 -1
- package/lib/types/parse-css.test.d.ts +0 -1
- package/lib/types/popularity-index.d.ts +0 -6
- package/lib/types/property-parsers/background.d.ts +0 -11
- package/lib/types/property-parsers/background.test.d.ts +0 -1
- package/lib/types/property-parsers/box-shadow.d.ts +0 -3
- package/lib/types/property-parsers/box-shadow.test.d.ts +0 -1
- package/lib/types/property-parsers/parsers.d.ts +0 -2
- package/lib/types/property-parsers/to-longhand.d.ts +0 -1
- package/lib/types/schema.d.ts +0 -3243
- package/src/__generated__/keyword-values.ts +0 -5253
- package/src/__generated__/properties.ts +0 -3696
- package/src/__generated__/property-value-descriptions.ts +0 -4008
- package/src/__generated__/units.ts +0 -63
- package/src/custom-data.ts +0 -57
- package/src/html-check.ts +0 -8
- package/src/html.ts +0 -617
- package/src/index.ts +0 -25
- package/src/parse-css-value.test.ts +0 -136
- package/src/parse-css-value.ts +0 -157
- package/src/parse-css.test.ts +0 -101
- package/src/parse-css.ts +0 -137
- package/src/popularity-index.ts +0 -4611
- package/src/property-parsers/README.md +0 -11
- package/src/property-parsers/background.test.ts +0 -184
- package/src/property-parsers/background.ts +0 -147
- package/src/property-parsers/box-shadow.test.ts +0 -294
- package/src/property-parsers/box-shadow.ts +0 -130
- package/src/property-parsers/index.ts +0 -2
- package/src/property-parsers/parsers.ts +0 -5
- package/src/property-parsers/to-longhand.ts +0 -4
- package/src/schema.ts +0 -193
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import * as csstree from "css-tree";
|
|
2
|
-
import { LayersValue, TupleValue } from "../schema";
|
|
3
|
-
import type { InvalidValue, TupleValueItem, Unit } from "../schema";
|
|
4
|
-
import { colord } from "colord";
|
|
5
|
-
|
|
6
|
-
const cssTreeTryParseValue = (input: string) => {
|
|
7
|
-
try {
|
|
8
|
-
const ast = csstree.parse(input, { context: "value" });
|
|
9
|
-
return ast;
|
|
10
|
-
} catch {
|
|
11
|
-
return undefined;
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export const parseBoxShadow = (
|
|
16
|
-
boxShadow: string
|
|
17
|
-
): LayersValue | InvalidValue => {
|
|
18
|
-
let tokenStream = boxShadow.trim();
|
|
19
|
-
tokenStream = tokenStream.endsWith(";")
|
|
20
|
-
? tokenStream.slice(0, -1)
|
|
21
|
-
: tokenStream;
|
|
22
|
-
|
|
23
|
-
const cleanupKeywords = ["box-shadow:"];
|
|
24
|
-
|
|
25
|
-
for (const cleanupKeyword of cleanupKeywords) {
|
|
26
|
-
tokenStream = tokenStream.startsWith(cleanupKeyword)
|
|
27
|
-
? tokenStream.slice(cleanupKeyword.length).trim()
|
|
28
|
-
: tokenStream;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const cssAst = cssTreeTryParseValue(tokenStream);
|
|
32
|
-
|
|
33
|
-
if (cssAst === undefined) {
|
|
34
|
-
return {
|
|
35
|
-
type: "invalid",
|
|
36
|
-
value: boxShadow,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const parsed = csstree.lexer.matchProperty("box-shadow", cssAst);
|
|
41
|
-
if (parsed.error) {
|
|
42
|
-
return {
|
|
43
|
-
type: "invalid",
|
|
44
|
-
value: boxShadow,
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const layers: TupleValue[] = [];
|
|
49
|
-
|
|
50
|
-
csstree.walk(cssAst, (node) => {
|
|
51
|
-
if (node.type === "Value") {
|
|
52
|
-
const children = node.children;
|
|
53
|
-
let layer: csstree.CssNode[] = [];
|
|
54
|
-
|
|
55
|
-
for (const child of children) {
|
|
56
|
-
if (children.last === child) {
|
|
57
|
-
layer.push(child);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (child.type === "Operator" || children.last === child) {
|
|
61
|
-
const shadow: TupleValueItem[] = [];
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow#syntax
|
|
65
|
-
* `inset` and color can be at the start or end and their sequence can be anywhere.
|
|
66
|
-
* The rest need to foolow the sequence all the time
|
|
67
|
-
*/
|
|
68
|
-
|
|
69
|
-
for (let index = 0; index < layer.length; index++) {
|
|
70
|
-
const item = layer[index];
|
|
71
|
-
|
|
72
|
-
if (item.type === "Identifier") {
|
|
73
|
-
shadow.push({
|
|
74
|
-
type: "keyword",
|
|
75
|
-
value: item.name,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (item.type === "Function" || item.type === "Hash") {
|
|
80
|
-
const colorValue = colord(csstree.generate(item));
|
|
81
|
-
if (!colorValue.isValid()) {
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
const rgb = colorValue.toRgb();
|
|
85
|
-
shadow.push({
|
|
86
|
-
type: "rgb",
|
|
87
|
-
alpha: rgb.a,
|
|
88
|
-
r: rgb.r,
|
|
89
|
-
g: rgb.g,
|
|
90
|
-
b: rgb.b,
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (item.type === "Dimension") {
|
|
95
|
-
shadow.push({
|
|
96
|
-
type: "unit",
|
|
97
|
-
value: Number(item.value),
|
|
98
|
-
unit: item.unit as Unit,
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (item.type === "Number") {
|
|
103
|
-
shadow.push({
|
|
104
|
-
type: "unit",
|
|
105
|
-
value: Number(item.value),
|
|
106
|
-
unit: "number",
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
layers.push({
|
|
112
|
-
type: "tuple",
|
|
113
|
-
value: shadow,
|
|
114
|
-
});
|
|
115
|
-
layer = [];
|
|
116
|
-
continue;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
layer.push(child);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
return layers.length > 0
|
|
125
|
-
? {
|
|
126
|
-
type: "layers",
|
|
127
|
-
value: layers,
|
|
128
|
-
}
|
|
129
|
-
: { type: "invalid", value: boxShadow };
|
|
130
|
-
};
|
package/src/schema.ts
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import { units } from "./__generated__/units";
|
|
2
|
-
import type { properties } from "./__generated__/properties";
|
|
3
|
-
import { z } from "zod";
|
|
4
|
-
|
|
5
|
-
type Properties = typeof properties & {
|
|
6
|
-
[custom: CustomProperty]: {
|
|
7
|
-
appliesTo: "allElements";
|
|
8
|
-
initial: string;
|
|
9
|
-
inherited: boolean;
|
|
10
|
-
};
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type StyleProperty = keyof Properties;
|
|
14
|
-
|
|
15
|
-
type CustomProperty = `--${string}`;
|
|
16
|
-
|
|
17
|
-
export type AppliesTo = Properties[StyleProperty]["appliesTo"];
|
|
18
|
-
|
|
19
|
-
export type UnitGroup = keyof typeof units;
|
|
20
|
-
|
|
21
|
-
type UnitEnum = (typeof units)[UnitGroup][number];
|
|
22
|
-
|
|
23
|
-
const Unit = z.union([
|
|
24
|
-
// expected tuple with at least single element
|
|
25
|
-
// so cast to tuple with single union element to get correct inference
|
|
26
|
-
z.enum(Object.values(units).flat() as [UnitEnum]),
|
|
27
|
-
z.literal("number"),
|
|
28
|
-
]);
|
|
29
|
-
|
|
30
|
-
export type Unit = z.infer<typeof Unit>;
|
|
31
|
-
|
|
32
|
-
export const UnitValue = z.object({
|
|
33
|
-
type: z.literal("unit"),
|
|
34
|
-
unit: Unit,
|
|
35
|
-
value: z.number(),
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
export type UnitValue = z.infer<typeof UnitValue>;
|
|
39
|
-
|
|
40
|
-
export const KeywordValue = z.object({
|
|
41
|
-
type: z.literal("keyword"),
|
|
42
|
-
// @todo use exact type
|
|
43
|
-
value: z.string(),
|
|
44
|
-
});
|
|
45
|
-
export type KeywordValue = z.infer<typeof KeywordValue>;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Valid unparsed css value
|
|
49
|
-
**/
|
|
50
|
-
export const UnparsedValue = z.object({
|
|
51
|
-
type: z.literal("unparsed"),
|
|
52
|
-
value: z.string(),
|
|
53
|
-
// For the builder we want to be able to hide background-image
|
|
54
|
-
hidden: z.boolean().optional(),
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
export type UnparsedValue = z.infer<typeof UnparsedValue>;
|
|
58
|
-
|
|
59
|
-
const FontFamilyValue = z.object({
|
|
60
|
-
type: z.literal("fontFamily"),
|
|
61
|
-
value: z.array(z.string()),
|
|
62
|
-
});
|
|
63
|
-
export type FontFamilyValue = z.infer<typeof FontFamilyValue>;
|
|
64
|
-
|
|
65
|
-
const RgbValue = z.object({
|
|
66
|
-
type: z.literal("rgb"),
|
|
67
|
-
r: z.number(),
|
|
68
|
-
g: z.number(),
|
|
69
|
-
b: z.number(),
|
|
70
|
-
alpha: z.number(),
|
|
71
|
-
});
|
|
72
|
-
export type RgbValue = z.infer<typeof RgbValue>;
|
|
73
|
-
|
|
74
|
-
export const ImageValue = z.object({
|
|
75
|
-
type: z.literal("image"),
|
|
76
|
-
value: z.union([
|
|
77
|
-
z.object({ type: z.literal("asset"), value: z.string() }),
|
|
78
|
-
// url is not stored in db and only used by css-engine transformValue
|
|
79
|
-
// to prepare image value for rendering
|
|
80
|
-
z.object({ type: z.literal("url"), url: z.string() }),
|
|
81
|
-
]),
|
|
82
|
-
// For the builder we want to be able to hide images
|
|
83
|
-
hidden: z.boolean().optional(),
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
export type ImageValue = z.infer<typeof ImageValue>;
|
|
87
|
-
|
|
88
|
-
// We want to be able to render the invalid value
|
|
89
|
-
// and show it is invalid visually, without saving it to the db
|
|
90
|
-
export const InvalidValue = z.object({
|
|
91
|
-
type: z.literal("invalid"),
|
|
92
|
-
value: z.string(),
|
|
93
|
-
});
|
|
94
|
-
export type InvalidValue = z.infer<typeof InvalidValue>;
|
|
95
|
-
|
|
96
|
-
const UnsetValue = z.object({
|
|
97
|
-
type: z.literal("unset"),
|
|
98
|
-
value: z.literal(""),
|
|
99
|
-
});
|
|
100
|
-
export type UnsetValue = z.infer<typeof UnsetValue>;
|
|
101
|
-
|
|
102
|
-
export const TupleValueItem = z.union([
|
|
103
|
-
UnitValue,
|
|
104
|
-
KeywordValue,
|
|
105
|
-
UnparsedValue,
|
|
106
|
-
RgbValue,
|
|
107
|
-
]);
|
|
108
|
-
export type TupleValueItem = z.infer<typeof TupleValueItem>;
|
|
109
|
-
|
|
110
|
-
export const TupleValue = z.object({
|
|
111
|
-
type: z.literal("tuple"),
|
|
112
|
-
value: z.array(TupleValueItem),
|
|
113
|
-
hidden: z.boolean().optional(),
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
export type TupleValue = z.infer<typeof TupleValue>;
|
|
117
|
-
|
|
118
|
-
const LayerValueItem = z.union([
|
|
119
|
-
UnitValue,
|
|
120
|
-
KeywordValue,
|
|
121
|
-
UnparsedValue,
|
|
122
|
-
ImageValue,
|
|
123
|
-
TupleValue,
|
|
124
|
-
InvalidValue,
|
|
125
|
-
]);
|
|
126
|
-
|
|
127
|
-
export type LayerValueItem = z.infer<typeof LayerValueItem>;
|
|
128
|
-
// To support background layers https://developer.mozilla.org/en-US/docs/Web/CSS/background
|
|
129
|
-
// and similar comma separated css properties
|
|
130
|
-
// InvalidValue used in case of asset not found
|
|
131
|
-
export const LayersValue = z.object({
|
|
132
|
-
type: z.literal("layers"),
|
|
133
|
-
value: z.array(LayerValueItem),
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
export type LayersValue = z.infer<typeof LayersValue>;
|
|
137
|
-
|
|
138
|
-
const ValidStaticStyleValue = z.union([
|
|
139
|
-
ImageValue,
|
|
140
|
-
LayersValue,
|
|
141
|
-
UnitValue,
|
|
142
|
-
KeywordValue,
|
|
143
|
-
FontFamilyValue,
|
|
144
|
-
RgbValue,
|
|
145
|
-
UnparsedValue,
|
|
146
|
-
TupleValue,
|
|
147
|
-
]);
|
|
148
|
-
|
|
149
|
-
export type ValidStaticStyleValue = z.infer<typeof ValidStaticStyleValue>;
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* All StyleValue types that going to need wrapping into a CSS variable when rendered
|
|
153
|
-
* on canvas inside builder.
|
|
154
|
-
* Values like InvalidValue, UnsetValue, VarValue don't need to be wrapped
|
|
155
|
-
*/
|
|
156
|
-
export const isValidStaticStyleValue = (
|
|
157
|
-
styleValue: StyleValue
|
|
158
|
-
): styleValue is ValidStaticStyleValue => {
|
|
159
|
-
// guard against invalid checks
|
|
160
|
-
const staticStyleValue = styleValue as ValidStaticStyleValue;
|
|
161
|
-
return (
|
|
162
|
-
staticStyleValue.type === "image" ||
|
|
163
|
-
staticStyleValue.type === "layers" ||
|
|
164
|
-
staticStyleValue.type === "unit" ||
|
|
165
|
-
staticStyleValue.type === "keyword" ||
|
|
166
|
-
staticStyleValue.type === "fontFamily" ||
|
|
167
|
-
staticStyleValue.type === "rgb" ||
|
|
168
|
-
staticStyleValue.type === "unparsed" ||
|
|
169
|
-
staticStyleValue.type === "tuple"
|
|
170
|
-
);
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
const VarValue = z.object({
|
|
174
|
-
type: z.literal("var"),
|
|
175
|
-
value: z.string(),
|
|
176
|
-
fallbacks: z.array(ValidStaticStyleValue),
|
|
177
|
-
});
|
|
178
|
-
export type VarValue = z.infer<typeof VarValue>;
|
|
179
|
-
|
|
180
|
-
export const StyleValue = z.union([
|
|
181
|
-
ValidStaticStyleValue,
|
|
182
|
-
InvalidValue,
|
|
183
|
-
UnsetValue,
|
|
184
|
-
VarValue,
|
|
185
|
-
]);
|
|
186
|
-
|
|
187
|
-
export type StyleValue = z.infer<typeof StyleValue>;
|
|
188
|
-
|
|
189
|
-
const Style = z.record(z.string(), StyleValue);
|
|
190
|
-
|
|
191
|
-
export type Style = {
|
|
192
|
-
[property in StyleProperty]?: StyleValue;
|
|
193
|
-
} & { [property: CustomProperty]: StyleValue };
|