-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import * as hCSS from "../../../deps/deno.land/x/hyperactive_css/mod.ts";
export interface Css {
readonly chunks: ReadonlySet<string>;
}
/**
* Just for syntax highlighting.
*/
export function css(
tmpl: readonly string[],
...params: readonly string[]
): Css {
const code = cssTmplBuilder(tmpl, params);
return {
chunks: new Set([code]),
};
}
export function fromString(code: string): Css {
return {
chunks: new Set([code]),
};
}
function cssTmplBuilder(
tmpl: readonly string[],
params: readonly string[],
): string {
if (!tmpl.length) {
return "";
}
const [thead, ...trest] = tmpl;
if (!params.length || trest.length === 0) {
return thead;
}
const [phead, ...prest] = params;
return thead + phead + cssTmplBuilder(trest, prest);
}
export function join(...units: readonly Css[]): Css {
const result = new Set<string>();
// `Set.prototype.union` method is not widely implemented yet.
for (const unit of units) {
for (const chunk of unit.chunks) {
result.add(chunk);
}
}
return {
chunks: result,
};
}
export function serialize(css: Css): string {
return Array.from(css.chunks.values()).join("\n");
}
/**
* Join class names.
*/
export function cx(
...classNames: (string | null | false | undefined)[]
): string {
return classNames.filter((c): c is string => !!c).join(" ");
}
/**
* This function returns an object whose every propery value is
* class name string.
*
* The purpose of this function is to create namespaced (scoped)
* class while keeping short character count for class name strings.
*
* As this function uses indices for class names, removing a name
* from the list may cause broken style if a user loads stale CSS
* and fresh HTML (or vice-versa). In order to avoid this, you can
* replace the obsolete name with `null`, so this function skips
* the index that later names keep its indices.
*
* @example
* const c = buildClasses("foo", ["bar", null, "baz"]);
* assertEquals(c.bar, "foo__0");
* assertEquals(c.baz, "foo__2");
*/
export function buildClasses<Name extends string | null>(
prefix: string,
names: readonly Name[],
): Record<Exclude<Name, null>, string> {
return Object.fromEntries(
names.map((name, i) => name ? [name, `${prefix}__${i.toString(31)}`] : null)
.filter((entry): entry is NonNullable<typeof entry> => !!entry),
);
}
export interface CSSAssets {
paths: readonly string[];
replace(fn: (from: string) => string): string;
}
export function getAssets(code: string): CSSAssets {
const ast = hCSS.parseAStylesheet(code);
const assets = ast.rules.map((rule) => parseRule(rule)).flat().filter(
(token) => {
// URL
if (/^[a-z0-9]+:/.test(token.value)) {
return false;
}
// Absolute URL
if (token.value.startsWith("/")) {
return false;
}
return true;
},
);
return {
paths: assets.map((token) => token.value).filter((x, i, arr) =>
arr.indexOf(x) === i
),
replace(f) {
const ordered = assets.sort((a, b) =>
b.debug.from.offset - a.debug.from.offset
);
let ret = code;
for (const token of ordered) {
if (token instanceof hCSS.StringToken) {
ret = ret.slice(0, token.debug.from.offset + 1) + `"` +
f(token.value) +
`"` + ret.slice(token.debug.to.offset + 1);
} else {
ret = ret.slice(0, token.debug.from.offset + 1) + `url(` +
f(token.value) +
`)` + ret.slice(token.debug.to.offset + 1);
}
}
return ret;
},
};
}
function parseRule(
rule: hCSS.CSSParserRule,
): readonly (hCSS.URLToken | hCSS.StringToken)[] {
if (rule instanceof hCSS.AtRule) {
return [
...rule.rules.map((rule) => parseRule(rule)).flat(),
...rule.declarations.map((rule) => parseRule(rule)).flat(),
];
}
if (rule instanceof hCSS.QualifiedRule) {
return rule.declarations.map((decl) => parseRule(decl)).flat();
}
if (rule instanceof hCSS.Declaration) {
return rule.value.map((value) => parseToken(value)).flat();
}
return [];
}
function parseToken(
token: hCSS.CSSParserToken,
): readonly (hCSS.URLToken | hCSS.StringToken)[] {
if (token instanceof hCSS.URLToken) {
return [token];
}
if (token instanceof hCSS.SimpleBlock) {
return token.value.map((block) => parseToken(block)).flat();
}
if (token instanceof hCSS.Func) {
// hyperactivecss cannot parse `url("...")` as an URL token
if (token.name === "url") {
return token.value.filter((t): t is hCSS.StringToken =>
t instanceof hCSS.StringToken
);
}
return token.value.map((token) => parseToken(token)).flat();
}
return [];
}