brumate 0.2.0 → 0.4.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/README.md +2 -2
- package/check.d.ts +49 -0
- package/dist/{boot-D-IdPy7B.d.ts → boot-DLlRmD9z.d.ts} +11 -10
- package/dist/cli.js +275 -15
- package/dist/cli.js.map +1 -1
- package/dist/client-BvDsC6Qr.js +3463 -0
- package/dist/client-BvDsC6Qr.js.map +1 -0
- package/dist/client-DBwL2wKQ.d.ts +669 -0
- package/dist/{client-routes-JTbGtukf.js → client-routes-BtGpYCef.js} +60 -30
- package/dist/client-routes-BtGpYCef.js.map +1 -0
- package/dist/{cookies-Av5WFmcf.js → cookies-Cnbf4w-S.js} +15 -7
- package/dist/cookies-Cnbf4w-S.js.map +1 -0
- package/dist/{defer-D0bo1Gqx.js → defer-CKWZLv2K.js} +8 -4
- package/dist/defer-CKWZLv2K.js.map +1 -0
- package/dist/devtools-4uTPwR_v.js +597 -0
- package/dist/devtools-4uTPwR_v.js.map +1 -0
- package/dist/devtools-panel-Be9pxYgv.js +1994 -0
- package/dist/devtools-panel-Be9pxYgv.js.map +1 -0
- package/dist/events-LgLdp1R7.js +114 -0
- package/dist/events-LgLdp1R7.js.map +1 -0
- package/dist/features-CHY5Ekku.js +30 -0
- package/dist/features-CHY5Ekku.js.map +1 -0
- package/dist/files-WW4RlajL.js +506 -0
- package/dist/files-WW4RlajL.js.map +1 -0
- package/dist/forms-j1xPAR14.d.ts +281 -0
- package/dist/forms-nXK7-Lv_.js +440 -0
- package/dist/forms-nXK7-Lv_.js.map +1 -0
- package/dist/highlight.d.ts +17 -0
- package/dist/highlight.js +278 -0
- package/dist/highlight.js.map +1 -0
- package/dist/html-BtKRyi7g.js +305 -0
- package/dist/html-BtKRyi7g.js.map +1 -0
- package/dist/html-Bw9Qwu6F.d.ts +107 -0
- package/dist/index.d.ts +142 -11
- package/dist/index.js +641 -106
- package/dist/index.js.map +1 -1
- package/dist/{inspect-BJtACnQp.js → inspect-Y6y4uNZp.js} +4333 -681
- package/dist/inspect-Y6y4uNZp.js.map +1 -0
- package/dist/{loader-B18i3_-T.js → loader-BjKGJzLN.js} +21 -14
- package/dist/loader-BjKGJzLN.js.map +1 -0
- package/dist/logo-DQ_OYhBi.js +10 -0
- package/dist/logo-DQ_OYhBi.js.map +1 -0
- package/dist/{navigate-BwuZveDF.js → navigate-CfOr-TRF.js} +130 -24
- package/dist/navigate-CfOr-TRF.js.map +1 -0
- package/dist/nesting-DA5vJw9X.js +265 -0
- package/dist/nesting-DA5vJw9X.js.map +1 -0
- package/dist/produce-B7X9ori9.js +402 -0
- package/dist/produce-B7X9ori9.js.map +1 -0
- package/dist/produce-DIgHBVwL.d.ts +148 -0
- package/dist/{rpc-B-Zkn6Ch.js → rpc-Q-XkLwFh.js} +28 -13
- package/dist/rpc-Q-XkLwFh.js.map +1 -0
- package/dist/runtime/batch.js +9 -7
- package/dist/runtime/batch.js.map +1 -1
- package/dist/runtime/client.d.ts +4 -4
- package/dist/runtime/client.js +4 -4
- package/dist/runtime/devtools.d.ts +172 -5
- package/dist/runtime/devtools.js +3 -1
- package/dist/runtime/env.js +14 -2
- package/dist/runtime/env.js.map +1 -1
- package/dist/runtime/events.d.ts +38 -1
- package/dist/runtime/events.js +1 -1
- package/dist/runtime/forms.d.ts +2 -0
- package/dist/runtime/forms.js +1 -0
- package/dist/runtime/gather.d.ts +25 -0
- package/dist/runtime/gather.js +33 -0
- package/dist/runtime/gather.js.map +1 -0
- package/dist/runtime/hot.d.ts +1 -1
- package/dist/runtime/hot.js +5 -2
- package/dist/runtime/hot.js.map +1 -1
- package/dist/runtime/html.d.ts +2 -2
- package/dist/runtime/html.js +1 -1
- package/dist/runtime/loader.d.ts +2 -2
- package/dist/runtime/loader.js +2 -2
- package/dist/runtime/routes.js +4 -1
- package/dist/runtime/routes.js.map +1 -1
- package/dist/runtime/server.d.ts +5 -4
- package/dist/runtime/server.js +5 -5
- package/dist/runtime/signals.d.ts +75 -2
- package/dist/runtime/signals.js +44 -2
- package/dist/runtime/signals.js.map +1 -1
- package/dist/runtime/static-loader.d.ts +2 -2
- package/dist/runtime/static-loader.js +1 -1
- package/dist/runtime/testing.d.ts +18 -3
- package/dist/runtime/testing.js +150 -33
- package/dist/runtime/testing.js.map +1 -1
- package/dist/runtime/values.d.ts +2 -0
- package/dist/runtime/values.js +24 -0
- package/dist/runtime/values.js.map +1 -0
- package/dist/{server-BA9SJ2Vm.d.ts → server-BvPIg9pT.d.ts} +363 -32
- package/dist/{server-BqfM95r1.js → server-DjyOVJtJ.js} +1309 -220
- package/dist/server-DjyOVJtJ.js.map +1 -0
- package/dist/{static-loader-BzE9SRaj.d.ts → static-loader-BGgdp_Y5.d.ts} +2 -2
- package/dist/static-loader-BZR17hSw.js +312 -0
- package/dist/static-loader-BZR17hSw.js.map +1 -0
- package/dist/{stream-JaMHMdBD.js → stream-BP5AVmg5.js} +182 -17
- package/dist/stream-BP5AVmg5.js.map +1 -0
- package/dist/values-B8C7CLvG.d.ts +63 -0
- package/dist/vite.d.ts +20 -20
- package/dist/vite.js +2031 -539
- package/dist/vite.js.map +1 -1
- package/elements.d.ts +951 -0
- package/forms/forms.brm.ts +141 -0
- package/package.json +41 -3
- package/skills/brumate/SKILL.md +58 -0
- package/skills/brumate/references/errors.md +2078 -0
- package/skills/brumate/references/events.md +117 -0
- package/skills/brumate/references/signals.md +61 -0
- package/skills/brumate/references/state.md +235 -0
- package/skills/brumate/references/syntax.md +250 -0
- package/types.d.ts +4 -4
- package/dist/client-C0UCNL3J.js +0 -1643
- package/dist/client-C0UCNL3J.js.map +0 -1
- package/dist/client-MD0ZmcFR.d.ts +0 -354
- package/dist/client-routes-JTbGtukf.js.map +0 -1
- package/dist/cookies-Av5WFmcf.js.map +0 -1
- package/dist/defer-D0bo1Gqx.js.map +0 -1
- package/dist/devtools-C50VH8gZ.js +0 -113
- package/dist/devtools-C50VH8gZ.js.map +0 -1
- package/dist/devtools-panel-AYjfwPhj.js +0 -435
- package/dist/devtools-panel-AYjfwPhj.js.map +0 -1
- package/dist/events-BKWRqtMg.js +0 -52
- package/dist/events-BKWRqtMg.js.map +0 -1
- package/dist/files-Bnn1JqHx.js +0 -192
- package/dist/files-Bnn1JqHx.js.map +0 -1
- package/dist/html-bHZbkK-h.js +0 -170
- package/dist/html-bHZbkK-h.js.map +0 -1
- package/dist/html-y1q7-t7D.d.ts +0 -60
- package/dist/inspect-BJtACnQp.js.map +0 -1
- package/dist/loader-B18i3_-T.js.map +0 -1
- package/dist/navigate-BwuZveDF.js.map +0 -1
- package/dist/produce-CWfJ4byV.js +0 -167
- package/dist/produce-CWfJ4byV.js.map +0 -1
- package/dist/produce-D4K7qpaL.d.ts +0 -72
- package/dist/rpc-B-Zkn6Ch.js.map +0 -1
- package/dist/server-BqfM95r1.js.map +0 -1
- package/dist/static-loader-DOygOFj5.js +0 -216
- package/dist/static-loader-DOygOFj5.js.map +0 -1
- package/dist/stream-JaMHMdBD.js.map +0 -1
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { always, create_language, embed_grammars, enter, fallback, goto, keyword, leave, match, on, range, to_html, within } from "@twinkleplop/core";
|
|
2
|
+
import { compile, define_grammar } from "@twinkleplop/core/compile";
|
|
3
|
+
import * as TOKENS from "@twinkleplop/core/tokens";
|
|
4
|
+
import { tokenize as tokenize$1 } from "@twinkleplop/css";
|
|
5
|
+
import { tokenize as tokenize$2 } from "@twinkleplop/tsx";
|
|
6
|
+
import { dark, light } from "@twinkleplop/theme-github/tokens";
|
|
7
|
+
//#region src/highlight-keywords.ts
|
|
8
|
+
const BLOCK_KEYWORDS = [
|
|
9
|
+
"if",
|
|
10
|
+
"else if",
|
|
11
|
+
"else",
|
|
12
|
+
"for",
|
|
13
|
+
"repeat",
|
|
14
|
+
"switch",
|
|
15
|
+
"case",
|
|
16
|
+
"default",
|
|
17
|
+
"try",
|
|
18
|
+
"catch",
|
|
19
|
+
"loading",
|
|
20
|
+
"portal",
|
|
21
|
+
"head",
|
|
22
|
+
"client",
|
|
23
|
+
"offscreen",
|
|
24
|
+
"reveal",
|
|
25
|
+
"defer",
|
|
26
|
+
"key",
|
|
27
|
+
"template"
|
|
28
|
+
];
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/highlight.ts
|
|
31
|
+
/**
|
|
32
|
+
* A language of the twinkleplop highlighter (`@twinkleplop/core`) for `.brm` files, for the devtools and the website: HTML-like markup with `{…}`
|
|
33
|
+
* expressions, attribute values without quotes, control flow blocks such as
|
|
34
|
+
* `#if (a) { … }`, dynamic tags `<{tag}>`, and `<script>` and `<style>`
|
|
35
|
+
* blocks. Scripts and expressions are highlighted as TSX, styles as CSS.
|
|
36
|
+
*
|
|
37
|
+
* Like the Svelte grammar of twinkleplop, it marks the raw text of scripts,
|
|
38
|
+
* styles and expressions, and a reclassifier hands each one to its language.
|
|
39
|
+
*/
|
|
40
|
+
const WHITESPACE = [
|
|
41
|
+
" ",
|
|
42
|
+
" ",
|
|
43
|
+
"\n",
|
|
44
|
+
"\r"
|
|
45
|
+
];
|
|
46
|
+
const TAG_NAME_CHARS = range([
|
|
47
|
+
["a", "z"],
|
|
48
|
+
["A", "Z"],
|
|
49
|
+
["0", "9"],
|
|
50
|
+
["-", "-"],
|
|
51
|
+
["_", "_"],
|
|
52
|
+
[":", ":"],
|
|
53
|
+
[".", "."],
|
|
54
|
+
["$", "$"]
|
|
55
|
+
]);
|
|
56
|
+
const WORD_CHARS = range([
|
|
57
|
+
["a", "z"],
|
|
58
|
+
["A", "Z"],
|
|
59
|
+
["0", "9"],
|
|
60
|
+
["_", "_"],
|
|
61
|
+
["$", "$"]
|
|
62
|
+
]);
|
|
63
|
+
const ASCII_ALPHA = range([["a", "z"], ["A", "Z"]]);
|
|
64
|
+
const ATTR_NAME_CHARS = range([
|
|
65
|
+
["a", "z"],
|
|
66
|
+
["A", "Z"],
|
|
67
|
+
["0", "9"],
|
|
68
|
+
["-", "-"],
|
|
69
|
+
["_", "_"],
|
|
70
|
+
["$", "$"]
|
|
71
|
+
]);
|
|
72
|
+
const BARE_VALUE_CHARS = range([
|
|
73
|
+
["!", "!"],
|
|
74
|
+
["#", "&"],
|
|
75
|
+
["*", "."],
|
|
76
|
+
["0", "="],
|
|
77
|
+
["?", "Z"],
|
|
78
|
+
["\\", "\\"],
|
|
79
|
+
["^", "~"]
|
|
80
|
+
]);
|
|
81
|
+
const RAW = TOKENS.raw_svelte_expression;
|
|
82
|
+
const rawSkips = [
|
|
83
|
+
within("\"", "\"", RAW, {
|
|
84
|
+
escape: "\\",
|
|
85
|
+
multiline: true
|
|
86
|
+
}),
|
|
87
|
+
within("'", "'", RAW, {
|
|
88
|
+
escape: "\\",
|
|
89
|
+
multiline: true
|
|
90
|
+
}),
|
|
91
|
+
within("`", "`", RAW, {
|
|
92
|
+
escape: "\\",
|
|
93
|
+
multiline: true
|
|
94
|
+
}),
|
|
95
|
+
within("/*", "*/", RAW, { multiline: true }),
|
|
96
|
+
within("//", "\n", RAW)
|
|
97
|
+
];
|
|
98
|
+
const expressionRules = [
|
|
99
|
+
...rawSkips,
|
|
100
|
+
match("{", RAW, enter("expression_brace")),
|
|
101
|
+
fallback({ token: RAW })
|
|
102
|
+
];
|
|
103
|
+
const tagRules = [
|
|
104
|
+
on(WHITESPACE),
|
|
105
|
+
match("=", TOKENS.operator, enter("attr_value")),
|
|
106
|
+
match("{", TOKENS.expression, enter("expression_body")),
|
|
107
|
+
match("[", RAW, enter("raw_group")),
|
|
108
|
+
match(ATTR_NAME_CHARS, TOKENS.attr_name),
|
|
109
|
+
match(":", TOKENS.attr_name)
|
|
110
|
+
];
|
|
111
|
+
/** Raw code between `start` and `end` that ends the state, as in `<\/script>`. */
|
|
112
|
+
function rawBlock(name, token) {
|
|
113
|
+
return {
|
|
114
|
+
[`${name}_attrs`]: { rules: [
|
|
115
|
+
match(">", TOKENS.punctuation, goto(`${name}_content`)),
|
|
116
|
+
on(WHITESPACE),
|
|
117
|
+
match("=", TOKENS.operator),
|
|
118
|
+
within("\"", "\"", TOKENS.string),
|
|
119
|
+
within("'", "'", TOKENS.string),
|
|
120
|
+
match(ATTR_NAME_CHARS, TOKENS.attr_name)
|
|
121
|
+
] },
|
|
122
|
+
[`${name}_content`]: { rules: [on("</", enter(`${name}_close_probe`)), fallback({ token })] },
|
|
123
|
+
[`${name}_close_probe`]: {
|
|
124
|
+
mode: "probe",
|
|
125
|
+
fallback: `${name}_close_fail`,
|
|
126
|
+
rules: [on(`${name}>`, goto(`${name}_close`)), fallback(goto(`${name}_close_fail`))]
|
|
127
|
+
},
|
|
128
|
+
[`${name}_close_fail`]: { rules: [match("<", token, leave())] },
|
|
129
|
+
[`${name}_close`]: { rules: [
|
|
130
|
+
match("</", TOKENS.punctuation),
|
|
131
|
+
match(name, TOKENS.tag_name),
|
|
132
|
+
match(">", TOKENS.punctuation, goto("content"))
|
|
133
|
+
] }
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
const rawGrammar = define_grammar({
|
|
137
|
+
name: "brm",
|
|
138
|
+
states: {
|
|
139
|
+
content: { rules: [
|
|
140
|
+
within("<!--", "-->", TOKENS.comment),
|
|
141
|
+
match("</", TOKENS.punctuation, enter("close_tag")),
|
|
142
|
+
on("<", enter("lt_probe")),
|
|
143
|
+
on("#", enter("hash_probe")),
|
|
144
|
+
match("{", TOKENS.expression, enter("expression_body")),
|
|
145
|
+
match("}", TOKENS.punctuation),
|
|
146
|
+
fallback({})
|
|
147
|
+
] },
|
|
148
|
+
hash_probe: {
|
|
149
|
+
mode: "probe",
|
|
150
|
+
fallback: "hash_text",
|
|
151
|
+
rules: [on(BLOCK_KEYWORDS, {
|
|
152
|
+
boundary: true,
|
|
153
|
+
...enter("block_start")
|
|
154
|
+
}), fallback(enter("hash_text"))]
|
|
155
|
+
},
|
|
156
|
+
hash_text: { rules: [on("#"), fallback(leave())] },
|
|
157
|
+
block_start: { rules: [match("#", TOKENS.svelte_block, goto("block_keyword"))] },
|
|
158
|
+
block_keyword: { rules: [keyword(["template"], goto("template_name"), TOKENS.svelte_block), match(BLOCK_KEYWORDS, TOKENS.svelte_block, goto("block_header"))] },
|
|
159
|
+
template_name: { rules: [
|
|
160
|
+
on(WHITESPACE),
|
|
161
|
+
match(ATTR_NAME_CHARS, TOKENS.function, goto("template_name_rest")),
|
|
162
|
+
fallback(goto("block_header"))
|
|
163
|
+
] },
|
|
164
|
+
template_name_rest: { rules: [match(ATTR_NAME_CHARS, TOKENS.function), fallback(goto("block_header"))] },
|
|
165
|
+
block_header: { rules: [
|
|
166
|
+
on(WHITESPACE),
|
|
167
|
+
match("(", TOKENS.punctuation, goto("block_header_body")),
|
|
168
|
+
match("{", TOKENS.punctuation, leave()),
|
|
169
|
+
fallback(leave())
|
|
170
|
+
] },
|
|
171
|
+
block_header_body: { rules: [
|
|
172
|
+
...rawSkips,
|
|
173
|
+
match(["(", "["], RAW, enter("raw_group")),
|
|
174
|
+
match(")", TOKENS.punctuation, goto("block_header")),
|
|
175
|
+
keyword(["by"], {}, TOKENS.keyword),
|
|
176
|
+
match(WORD_CHARS, RAW, enter("raw_word")),
|
|
177
|
+
fallback({ token: RAW })
|
|
178
|
+
] },
|
|
179
|
+
raw_word: { rules: [match(WORD_CHARS, RAW), fallback(leave())] },
|
|
180
|
+
lt_probe: {
|
|
181
|
+
mode: "probe",
|
|
182
|
+
fallback: "lt_text",
|
|
183
|
+
rules: [
|
|
184
|
+
on(ASCII_ALPHA, enter("lt_tag")),
|
|
185
|
+
on([">", "{"], enter("lt_tag")),
|
|
186
|
+
fallback(enter("lt_text"))
|
|
187
|
+
]
|
|
188
|
+
},
|
|
189
|
+
lt_tag: { rules: [match("<", TOKENS.punctuation, goto("tag_start"))] },
|
|
190
|
+
lt_text: { rules: [on("<"), fallback(leave())] },
|
|
191
|
+
tag_start: { rules: [
|
|
192
|
+
keyword(["script"], goto("script_attrs"), TOKENS.tag_name),
|
|
193
|
+
keyword(["style"], goto("style_attrs"), TOKENS.tag_name),
|
|
194
|
+
match(">", TOKENS.punctuation, leave()),
|
|
195
|
+
match("{", TOKENS.expression, goto("dynamic_tag")),
|
|
196
|
+
match(TAG_NAME_CHARS, TOKENS.tag_name, goto("tag_open"))
|
|
197
|
+
] },
|
|
198
|
+
dynamic_tag: { rules: [match("}", TOKENS.expression, goto("tag_attrs")), ...expressionRules] },
|
|
199
|
+
tag_open: { rules: [
|
|
200
|
+
match("/>", TOKENS.punctuation, leave()),
|
|
201
|
+
match(">", TOKENS.punctuation, leave()),
|
|
202
|
+
on(WHITESPACE, goto("tag_attrs")),
|
|
203
|
+
match(TAG_NAME_CHARS, TOKENS.tag_name)
|
|
204
|
+
] },
|
|
205
|
+
tag_attrs: { rules: [
|
|
206
|
+
match("/>", TOKENS.punctuation, leave()),
|
|
207
|
+
match(">", TOKENS.punctuation, leave()),
|
|
208
|
+
...tagRules
|
|
209
|
+
] },
|
|
210
|
+
close_tag: { rules: [
|
|
211
|
+
match(">", TOKENS.punctuation, leave()),
|
|
212
|
+
on(WHITESPACE),
|
|
213
|
+
match(TAG_NAME_CHARS, TOKENS.tag_name)
|
|
214
|
+
] },
|
|
215
|
+
attr_value: { rules: [
|
|
216
|
+
within("\"", "\"", TOKENS.string, { multiline: true }),
|
|
217
|
+
within("'", "'", TOKENS.string, { multiline: true }),
|
|
218
|
+
match("{", TOKENS.expression, goto("expression_body")),
|
|
219
|
+
fallback(goto("bare_value"))
|
|
220
|
+
] },
|
|
221
|
+
bare_value: { rules: [
|
|
222
|
+
...rawSkips,
|
|
223
|
+
match(["(", "["], RAW, enter("raw_group")),
|
|
224
|
+
match(BARE_VALUE_CHARS, RAW),
|
|
225
|
+
fallback(leave())
|
|
226
|
+
] },
|
|
227
|
+
raw_group: { rules: [
|
|
228
|
+
...rawSkips,
|
|
229
|
+
match(["(", "["], RAW, enter("raw_group")),
|
|
230
|
+
match([")", "]"], RAW, leave()),
|
|
231
|
+
fallback({ token: RAW })
|
|
232
|
+
] },
|
|
233
|
+
expression_body: { rules: [match("}", TOKENS.expression, leave()), ...expressionRules] },
|
|
234
|
+
expression_brace: { rules: [match("}", RAW, leave()), ...expressionRules] },
|
|
235
|
+
...rawBlock("script", TOKENS.raw_script),
|
|
236
|
+
...rawBlock("style", TOKENS.raw_style)
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
let tsxLanguage;
|
|
240
|
+
let cssLanguage;
|
|
241
|
+
function embedTsx(input) {
|
|
242
|
+
return (tsxLanguage ??= tokenize$2())(input);
|
|
243
|
+
}
|
|
244
|
+
const tokenize = create_language(compile(rawGrammar), [always(embed_grammars({
|
|
245
|
+
[TOKENS.raw_script]: embedTsx,
|
|
246
|
+
[TOKENS.raw_svelte_expression]: embedTsx,
|
|
247
|
+
[TOKENS.raw_style]: (input) => (cssLanguage ??= tokenize$1())(input)
|
|
248
|
+
}), "embed")]);
|
|
249
|
+
/** Returns a function that highlights `.brm` code as HTML. */
|
|
250
|
+
function language(options) {
|
|
251
|
+
const run = tokenize(options);
|
|
252
|
+
return (input, render) => to_html(input, run(input), render);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* The colours of the GitHub theme of twinkleplop as CSS, for code under a
|
|
256
|
+
* `.twinkleplop` element. The devtools put it in their shadow root, where
|
|
257
|
+
* the stylesheets of the page do not reach.
|
|
258
|
+
*/
|
|
259
|
+
function themeCSS(theme = "dark") {
|
|
260
|
+
const palette = theme === "dark" ? dark : light;
|
|
261
|
+
const rules = [];
|
|
262
|
+
for (const [token, color] of Object.entries(palette)) if (token !== "background_color") rules.push(`.twinkleplop .${token} { color: ${color}; }`);
|
|
263
|
+
return rules.join("\n");
|
|
264
|
+
}
|
|
265
|
+
/** Highlights code of a file by its extension: `.brm`, `.css`, or TypeScript and the rest. */
|
|
266
|
+
function highlightFile(file, code, render) {
|
|
267
|
+
const extension = /\.(\w+)(?:\?.*)?$/.exec(file)?.[1] ?? "";
|
|
268
|
+
let run;
|
|
269
|
+
if (extension === "brm") run = brmLanguage ??= tokenize();
|
|
270
|
+
else if (extension === "css") run = cssLanguage ??= tokenize$1();
|
|
271
|
+
else run = tsxLanguage ??= tokenize$2();
|
|
272
|
+
return to_html(code, run(code), render);
|
|
273
|
+
}
|
|
274
|
+
let brmLanguage;
|
|
275
|
+
//#endregion
|
|
276
|
+
export { highlightFile, language, rawGrammar, themeCSS, tokenize };
|
|
277
|
+
|
|
278
|
+
//# sourceMappingURL=highlight.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"highlight.js","names":["tsx","css"],"sources":["../src/highlight-keywords.ts","../src/highlight.ts"],"sourcesContent":["// Sorted longest first by the compiler, so `else if` beats `else`.\n// It lives in its own file, so the public entry does not export it. A test checks\n// the list against the keywords of the parser.\nexport const BLOCK_KEYWORDS: string[] = [\n 'if',\n 'else if',\n 'else',\n 'for',\n 'repeat',\n 'switch',\n 'case',\n 'default',\n 'try',\n 'catch',\n 'loading',\n 'portal',\n 'head',\n 'client',\n 'offscreen',\n 'reveal',\n 'defer',\n 'key',\n 'template',\n];\n","/**\n * A language of the twinkleplop highlighter (`@twinkleplop/core`) for `.brm` files, for the devtools and the website: HTML-like markup with `{…}`\n * expressions, attribute values without quotes, control flow blocks such as\n * `#if (a) { … }`, dynamic tags `<{tag}>`, and `<script>` and `<style>`\n * blocks. Scripts and expressions are highlighted as TSX, styles as CSS.\n *\n * Like the Svelte grammar of twinkleplop, it marks the raw text of scripts,\n * styles and expressions, and a reclassifier hands each one to its language.\n */\nimport {\n always,\n create_language,\n embed_grammars,\n enter,\n fallback,\n goto,\n keyword,\n leave,\n match,\n on,\n range,\n to_html,\n within,\n} from '@twinkleplop/core';\nimport type { LanguageFactory, LanguageOptions, RenderOptions } from '@twinkleplop/core';\nimport type { Grammar } from '@twinkleplop/core/compile';\nimport { compile, define_grammar } from '@twinkleplop/core/compile';\nimport * as TOKENS from '@twinkleplop/core/tokens';\nimport { tokenize as css } from '@twinkleplop/css';\nimport { tokenize as tsx } from '@twinkleplop/tsx';\nimport { dark, light } from '@twinkleplop/theme-github/tokens';\nimport { BLOCK_KEYWORDS } from './highlight-keywords.ts';\n\nconst WHITESPACE = [' ', '\\t', '\\n', '\\r'];\n\nconst TAG_NAME_CHARS = range([\n ['a', 'z'],\n ['A', 'Z'],\n ['0', '9'],\n ['-', '-'],\n ['_', '_'],\n [':', ':'],\n ['.', '.'],\n ['$', '$'],\n]);\n\nconst WORD_CHARS = range([\n ['a', 'z'],\n ['A', 'Z'],\n ['0', '9'],\n ['_', '_'],\n ['$', '$'],\n]);\n\nconst ASCII_ALPHA = range([\n ['a', 'z'],\n ['A', 'Z'],\n]);\n\nconst ATTR_NAME_CHARS = range([\n ['a', 'z'],\n ['A', 'Z'],\n ['0', '9'],\n ['-', '-'],\n ['_', '_'],\n ['$', '$'],\n]);\n\n// The characters of an attribute value without quotes, such as\n// `props.items[0]`. Whitespace, `>` and `/` end it.\nconst BARE_VALUE_CHARS = range([\n ['!', '!'],\n ['#', '&'],\n ['*', '.'],\n ['0', '='],\n ['?', 'Z'],\n ['\\\\', '\\\\'],\n ['^', '~'],\n]);\n\nconst RAW = TOKENS.raw_svelte_expression;\n\n// Strings and comments in raw code, so their brackets do not count.\nconst rawSkips = [\n within('\"', '\"', RAW, { escape: '\\\\', multiline: true }),\n within(\"'\", \"'\", RAW, { escape: '\\\\', multiline: true }),\n within('`', '`', RAW, { escape: '\\\\', multiline: true }),\n within('/*', '*/', RAW, { multiline: true }),\n within('//', '\\n', RAW),\n];\n\n// Inside `{…}`, nested braces are tracked until the brace that closes it.\nconst expressionRules = [\n ...rawSkips,\n match('{', RAW, enter('expression_brace')),\n fallback({ token: RAW }),\n];\n\nconst tagRules = [\n on(WHITESPACE),\n match('=', TOKENS.operator, enter('attr_value')),\n match('{', TOKENS.expression, enter('expression_body')),\n match('[', RAW, enter('raw_group')),\n match(ATTR_NAME_CHARS, TOKENS.attr_name),\n match(':', TOKENS.attr_name),\n];\n\n/** Raw code between `start` and `end` that ends the state, as in `</script>`. */\nfunction rawBlock(name: 'script' | 'style', token: string) {\n return {\n [`${name}_attrs`]: {\n rules: [\n match('>', TOKENS.punctuation, goto(`${name}_content`)),\n on(WHITESPACE),\n match('=', TOKENS.operator),\n within('\"', '\"', TOKENS.string),\n within(\"'\", \"'\", TOKENS.string),\n match(ATTR_NAME_CHARS, TOKENS.attr_name),\n ],\n },\n [`${name}_content`]: {\n rules: [on('</', enter(`${name}_close_probe`)), fallback({ token })],\n },\n [`${name}_close_probe`]: {\n mode: 'probe' as const,\n fallback: `${name}_close_fail`,\n rules: [on(`${name}>`, goto(`${name}_close`)), fallback(goto(`${name}_close_fail`))],\n },\n [`${name}_close_fail`]: { rules: [match('<', token, leave())] },\n [`${name}_close`]: {\n rules: [\n match('</', TOKENS.punctuation),\n match(name, TOKENS.tag_name),\n match('>', TOKENS.punctuation, goto('content')),\n ],\n },\n };\n}\n\nexport const rawGrammar: Grammar = define_grammar({\n name: 'brm',\n states: {\n content: {\n rules: [\n within('<!--', '-->', TOKENS.comment),\n match('</', TOKENS.punctuation, enter('close_tag')),\n on('<', enter('lt_probe')),\n on('#', enter('hash_probe')),\n match('{', TOKENS.expression, enter('expression_body')),\n // The brace that closes a block body.\n match('}', TOKENS.punctuation),\n fallback({}),\n ],\n },\n\n // `#` starts a block only before a known keyword, so `#1` stays text.\n hash_probe: {\n mode: 'probe',\n fallback: 'hash_text',\n rules: [\n on(BLOCK_KEYWORDS, { boundary: true, ...enter('block_start') }),\n fallback(enter('hash_text')),\n ],\n },\n hash_text: { rules: [on('#'), fallback(leave())] },\n block_start: {\n rules: [match('#', TOKENS.svelte_block, goto('block_keyword'))],\n },\n block_keyword: {\n rules: [\n keyword(['template'], goto('template_name'), TOKENS.svelte_block),\n match(BLOCK_KEYWORDS, TOKENS.svelte_block, goto('block_header')),\n ],\n },\n // A range matches one character, so the name continues in its own state.\n template_name: {\n rules: [\n on(WHITESPACE),\n match(ATTR_NAME_CHARS, TOKENS.function, goto('template_name_rest')),\n fallback(goto('block_header')),\n ],\n },\n template_name_rest: {\n rules: [match(ATTR_NAME_CHARS, TOKENS.function), fallback(goto('block_header'))],\n },\n // An optional header in parentheses, then the `{` of the body.\n block_header: {\n rules: [\n on(WHITESPACE),\n match('(', TOKENS.punctuation, goto('block_header_body')),\n match('{', TOKENS.punctuation, leave()),\n fallback(leave()),\n ],\n },\n // `by` in `#for (item of list by item.id)` is a keyword of the block.\n // Words are read whole, so `by` only matches on its own.\n block_header_body: {\n rules: [\n ...rawSkips,\n match(['(', '['], RAW, enter('raw_group')),\n match(')', TOKENS.punctuation, goto('block_header')),\n keyword(['by'], {}, TOKENS.keyword),\n match(WORD_CHARS, RAW, enter('raw_word')),\n fallback({ token: RAW }),\n ],\n },\n raw_word: {\n rules: [match(WORD_CHARS, RAW), fallback(leave())],\n },\n\n // `<` opens a tag before a letter, `>` (a fragment) or `{` (a dynamic tag).\n lt_probe: {\n mode: 'probe',\n fallback: 'lt_text',\n rules: [\n on(ASCII_ALPHA, enter('lt_tag')),\n on(['>', '{'], enter('lt_tag')),\n fallback(enter('lt_text')),\n ],\n },\n lt_tag: { rules: [match('<', TOKENS.punctuation, goto('tag_start'))] },\n lt_text: { rules: [on('<'), fallback(leave())] },\n\n tag_start: {\n rules: [\n keyword(['script'], goto('script_attrs'), TOKENS.tag_name),\n keyword(['style'], goto('style_attrs'), TOKENS.tag_name),\n match('>', TOKENS.punctuation, leave()),\n match('{', TOKENS.expression, goto('dynamic_tag')),\n match(TAG_NAME_CHARS, TOKENS.tag_name, goto('tag_open')),\n ],\n },\n dynamic_tag: {\n rules: [match('}', TOKENS.expression, goto('tag_attrs')), ...expressionRules],\n },\n tag_open: {\n rules: [\n match('/>', TOKENS.punctuation, leave()),\n match('>', TOKENS.punctuation, leave()),\n on(WHITESPACE, goto('tag_attrs')),\n match(TAG_NAME_CHARS, TOKENS.tag_name),\n ],\n },\n tag_attrs: {\n rules: [\n match('/>', TOKENS.punctuation, leave()),\n match('>', TOKENS.punctuation, leave()),\n ...tagRules,\n ],\n },\n close_tag: {\n rules: [\n match('>', TOKENS.punctuation, leave()),\n on(WHITESPACE),\n match(TAG_NAME_CHARS, TOKENS.tag_name),\n ],\n },\n\n // The value after `=`: a string, an object in braces, or code without quotes.\n attr_value: {\n rules: [\n within('\"', '\"', TOKENS.string, { multiline: true }),\n within(\"'\", \"'\", TOKENS.string, { multiline: true }),\n match('{', TOKENS.expression, goto('expression_body')),\n fallback(goto('bare_value')),\n ],\n },\n bare_value: {\n rules: [\n ...rawSkips,\n match(['(', '['], RAW, enter('raw_group')),\n match(BARE_VALUE_CHARS, RAW),\n fallback(leave()),\n ],\n },\n raw_group: {\n rules: [\n ...rawSkips,\n match(['(', '['], RAW, enter('raw_group')),\n match([')', ']'], RAW, leave()),\n fallback({ token: RAW }),\n ],\n },\n\n expression_body: {\n rules: [match('}', TOKENS.expression, leave()), ...expressionRules],\n },\n expression_brace: {\n rules: [match('}', RAW, leave()), ...expressionRules],\n },\n\n ...rawBlock('script', TOKENS.raw_script),\n ...rawBlock('style', TOKENS.raw_style),\n },\n});\n\nlet tsxLanguage: ReturnType<typeof tsx> | undefined;\nlet cssLanguage: ReturnType<typeof css> | undefined;\nfunction embedTsx(input: string) {\n return (tsxLanguage ??= tsx())(input);\n}\n\nexport const tokenize: LanguageFactory = create_language(compile(rawGrammar), [\n always(\n embed_grammars({\n [TOKENS.raw_script]: embedTsx,\n [TOKENS.raw_svelte_expression]: embedTsx,\n [TOKENS.raw_style]: (input: string) => (cssLanguage ??= css())(input),\n }),\n 'embed',\n ),\n]);\n\n/** Returns a function that highlights `.brm` code as HTML. */\nexport function language(options?: LanguageOptions) {\n const run = tokenize(options);\n return (input: string, render?: RenderOptions): string => to_html(input, run(input), render);\n}\n\n/**\n * The colours of the GitHub theme of twinkleplop as CSS, for code under a\n * `.twinkleplop` element. The devtools put it in their shadow root, where\n * the stylesheets of the page do not reach.\n */\nexport function themeCSS(theme: 'dark' | 'light' = 'dark'): string {\n const palette = theme === 'dark' ? dark : light;\n const rules: string[] = [];\n for (const [token, color] of Object.entries(palette)) {\n if (token !== 'background_color') {\n rules.push(`.twinkleplop .${token} { color: ${color}; }`);\n }\n }\n return rules.join('\\n');\n}\n\n/** Highlights code of a file by its extension: `.brm`, `.css`, or TypeScript and the rest. */\nexport function highlightFile(file: string, code: string, render?: RenderOptions): string {\n const extension = /\\.(\\w+)(?:\\?.*)?$/.exec(file)?.[1] ?? '';\n let run: ReturnType<LanguageFactory>;\n if (extension === 'brm') {\n run = brmLanguage ??= tokenize();\n } else if (extension === 'css') {\n run = cssLanguage ??= css();\n } else {\n run = tsxLanguage ??= tsx();\n }\n return to_html(code, run(code), render);\n}\n\nlet brmLanguage: ReturnType<LanguageFactory> | undefined;\n"],"mappings":";;;;;;;AAGA,MAAa,iBAA2B;CACtC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;;;;;;;;;;;ACUA,MAAM,aAAa;CAAC;CAAK;CAAM;CAAM;AAAI;AAEzC,MAAM,iBAAiB,MAAM;CAC3B,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;AACX,CAAC;AAED,MAAM,aAAa,MAAM;CACvB,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;AACX,CAAC;AAED,MAAM,cAAc,MAAM,CACxB,CAAC,KAAK,GAAG,GACT,CAAC,KAAK,GAAG,CACX,CAAC;AAED,MAAM,kBAAkB,MAAM;CAC5B,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;CACT,CAAC,KAAK,GAAG;CAC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K,IAAI,CAAC,GAAG,MAAM;CACzD,IAAI;CACJ,IAAI,cAAc,OAChB,MAAM,gBAAgB,SAAS;MAC1B,IAAI,cAAc,OACvB,MAAM,gBAAgBA,WAAI;MAE1B,MAAM,gBAAgBD,WAAI;CAE5B,OAAO,QAAQ,MAAM,IAAI,IAAI,GAAG,MAAM;AACxC;AAEA,IAAI"}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
//#region src/runtime/html.ts
|
|
2
|
+
const RAW = Symbol.for("brumate.raw");
|
|
3
|
+
var RawHTML = class {
|
|
4
|
+
[RAW];
|
|
5
|
+
constructor(html) {
|
|
6
|
+
this[RAW] = html;
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
function raw(html) {
|
|
10
|
+
return new RawHTML(html);
|
|
11
|
+
}
|
|
12
|
+
function isRaw(value) {
|
|
13
|
+
return typeof value === "object" && value !== null && RAW in value;
|
|
14
|
+
}
|
|
15
|
+
function rawHTML(value) {
|
|
16
|
+
return value[RAW];
|
|
17
|
+
}
|
|
18
|
+
const ESCAPES = {
|
|
19
|
+
"&": "&",
|
|
20
|
+
"<": "<",
|
|
21
|
+
">": ">",
|
|
22
|
+
"\"": """
|
|
23
|
+
};
|
|
24
|
+
function toEscape(char) {
|
|
25
|
+
return ESCAPES[char];
|
|
26
|
+
}
|
|
27
|
+
function escape(value) {
|
|
28
|
+
return /[&<>]/.test(value) ? value.replace(/[&<>]/g, toEscape) : value;
|
|
29
|
+
}
|
|
30
|
+
function escapeAttribute(value) {
|
|
31
|
+
return /[&<"]/.test(value) ? value.replace(/[&<"]/g, toEscape) : value;
|
|
32
|
+
}
|
|
33
|
+
/** True for a promise, or any object with a `then` function, as the server awaits it. */
|
|
34
|
+
function isThenable(value) {
|
|
35
|
+
return value !== null && (typeof value === "object" || typeof value === "function") && typeof value.then === "function";
|
|
36
|
+
}
|
|
37
|
+
/** Renders a value inserted in markup. A promise is not awaited: it renders empty. */
|
|
38
|
+
function insertValue(value) {
|
|
39
|
+
if (isRaw(value)) return value[RAW];
|
|
40
|
+
if (value === null || value === void 0 || typeof value === "boolean" || isThenable(value)) return "";
|
|
41
|
+
if (Array.isArray(value)) {
|
|
42
|
+
let result = "";
|
|
43
|
+
for (const item of value) result += insertValue(item);
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
return escape(String(value));
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Renders a value as the text of an element whose content is text, such as
|
|
50
|
+
* a `<textarea>`, as `insertValue` does but without escaping.
|
|
51
|
+
*/
|
|
52
|
+
function textValue(value) {
|
|
53
|
+
if (isRaw(value)) return value[RAW];
|
|
54
|
+
if (value === null || value === void 0 || typeof value === "boolean" || isThenable(value)) return "";
|
|
55
|
+
if (Array.isArray(value)) {
|
|
56
|
+
let result = "";
|
|
57
|
+
for (const item of value) result += textValue(item);
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
return String(value);
|
|
61
|
+
}
|
|
62
|
+
function toKebabCase(name) {
|
|
63
|
+
return name.startsWith("--") ? name : name.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);
|
|
64
|
+
}
|
|
65
|
+
function classValue(value) {
|
|
66
|
+
if (typeof value !== "object" || value === null) return value ? String(value) : "";
|
|
67
|
+
let text = "";
|
|
68
|
+
if (Array.isArray(value)) {
|
|
69
|
+
for (const item of value) {
|
|
70
|
+
const part = classValue(item);
|
|
71
|
+
if (part !== "") text = text === "" ? part : `${text} ${part}`;
|
|
72
|
+
}
|
|
73
|
+
return text;
|
|
74
|
+
}
|
|
75
|
+
let first = true;
|
|
76
|
+
for (const name in value) if (Object.hasOwn(value, name) && value[name]) {
|
|
77
|
+
text = first ? name : `${text} ${name}`;
|
|
78
|
+
first = false;
|
|
79
|
+
}
|
|
80
|
+
return text;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Renders a `class` attribute from classes that each have a space before
|
|
84
|
+
* them, which the compiler builds from a literal class value. The classes
|
|
85
|
+
* are escaped already. No class renders nothing.
|
|
86
|
+
*/
|
|
87
|
+
function classAttribute(classes) {
|
|
88
|
+
return classes === "" ? "" : ` class="${classes.slice(1)}"`;
|
|
89
|
+
}
|
|
90
|
+
function styleValue(value) {
|
|
91
|
+
if (typeof value === "object" && value !== null) {
|
|
92
|
+
const declarations = [];
|
|
93
|
+
for (const [name, item] of Object.entries(value)) if (item !== null && item !== void 0 && item !== false) declarations.push(`${toKebabCase(name)}:${String(item)}`);
|
|
94
|
+
return declarations.join(";");
|
|
95
|
+
}
|
|
96
|
+
return String(value);
|
|
97
|
+
}
|
|
98
|
+
/** The brand of a binding of `$bind`, under the key of the registry. See `Binding`. */
|
|
99
|
+
const BOUND = Symbol.for("brumate.binding");
|
|
100
|
+
/** The words of an attribute that says `true` or `false` in its value. */
|
|
101
|
+
const TRUE_FALSE = ["true", "false"];
|
|
102
|
+
/**
|
|
103
|
+
* Attributes whose two states are words in the value, not whether the
|
|
104
|
+
* attribute is there, by their words for `true` and `false`. A missing
|
|
105
|
+
* attribute means a third state, such as the default of the element, so
|
|
106
|
+
* `false` must not remove it. `aria-*` and `data-*` are such attributes too.
|
|
107
|
+
*/
|
|
108
|
+
const SWITCHES = /* @__PURE__ */ new Map([
|
|
109
|
+
["contenteditable", TRUE_FALSE],
|
|
110
|
+
["draggable", TRUE_FALSE],
|
|
111
|
+
["spellcheck", TRUE_FALSE],
|
|
112
|
+
["writingsuggestions", TRUE_FALSE],
|
|
113
|
+
["focusable", TRUE_FALSE],
|
|
114
|
+
["translate", ["yes", "no"]],
|
|
115
|
+
["autocomplete", ["on", "off"]],
|
|
116
|
+
["autocorrect", ["on", "off"]]
|
|
117
|
+
]);
|
|
118
|
+
/** The words of `true` and `false` for an attribute that says them in its value, or undefined. */
|
|
119
|
+
function switchWords(name) {
|
|
120
|
+
const lower = name.toLowerCase();
|
|
121
|
+
if (lower.startsWith("aria-") || lower.startsWith("data-")) return TRUE_FALSE;
|
|
122
|
+
return SWITCHES.get(lower);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Returns the text of an attribute, or undefined if the attribute is not
|
|
126
|
+
* rendered.
|
|
127
|
+
*
|
|
128
|
+
* - `null` and `undefined` leave the attribute out.
|
|
129
|
+
* - For an attribute whose value says its state, such as `aria-expanded`,
|
|
130
|
+
* `draggable` or `translate`, `true` and `false` render as its words:
|
|
131
|
+
* `"true"` and `"false"`, or `"yes"` and `"no"`, or `"on"` and `"off"`.
|
|
132
|
+
* - For other attributes, such as `disabled`, `true` renders the attribute
|
|
133
|
+
* without a value, and `false` leaves it out.
|
|
134
|
+
*/
|
|
135
|
+
function attributeText(name, value) {
|
|
136
|
+
if (typeof value === "object" && value !== null && BOUND in value) {
|
|
137
|
+
if (name === "ref") return;
|
|
138
|
+
value = value.value;
|
|
139
|
+
}
|
|
140
|
+
if (value === null || value === void 0 || typeof value === "function" || isThenable(value)) return;
|
|
141
|
+
if (typeof value === "boolean") {
|
|
142
|
+
const words = switchWords(name);
|
|
143
|
+
if (words) return value ? words[0] : words[1];
|
|
144
|
+
return value ? "" : void 0;
|
|
145
|
+
}
|
|
146
|
+
if (name === "class") {
|
|
147
|
+
const text = classValue(value);
|
|
148
|
+
return text === "" ? void 0 : text;
|
|
149
|
+
}
|
|
150
|
+
return name === "style" ? styleValue(value) : String(value);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* A name that HTML reads as one attribute. A spread takes its names from
|
|
154
|
+
* data, so a name with a space, a quote, `>`, `/` or `=` could write other
|
|
155
|
+
* attributes or markup.
|
|
156
|
+
*/
|
|
157
|
+
const ATTRIBUTE_NAME = /^[^\s"'>/=\x00-\x1f\x7f]+$/;
|
|
158
|
+
/** Renders one attribute, with a leading space. A name that is not one attribute renders nothing. */
|
|
159
|
+
function attribute(name, value) {
|
|
160
|
+
const text = attributeText(name, value);
|
|
161
|
+
if (text === void 0 || !ATTRIBUTE_NAME.test(name)) return "";
|
|
162
|
+
return value === true && text === "" ? ` ${name}` : ` ${name}="${escapeAttribute(text)}"`;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Returns the value of a style variable with text around it, such as `12px`
|
|
166
|
+
* for `${size}px`. A value that sets no variable stays as it is.
|
|
167
|
+
*/
|
|
168
|
+
function affix(prefix, value, suffix) {
|
|
169
|
+
return value === null || value === void 0 || value === false ? value : `${prefix}${String(value)}${suffix}`;
|
|
170
|
+
}
|
|
171
|
+
/** Returns a style with custom properties before an element's own style. */
|
|
172
|
+
function vars(variables, own) {
|
|
173
|
+
const parts = [];
|
|
174
|
+
for (const [name, value] of Object.entries(variables)) if (value !== null && value !== void 0 && value !== false) parts.push(`${name}:${String(value)}`);
|
|
175
|
+
const rest = own === null || own === void 0 || own === false ? "" : styleValue(own);
|
|
176
|
+
if (rest) parts.push(rest);
|
|
177
|
+
return parts.join(";");
|
|
178
|
+
}
|
|
179
|
+
/** Returns a `<style>` element for a component's stylesheet. */
|
|
180
|
+
function styleElement(id, css) {
|
|
181
|
+
return `<style b-style="${escapeAttribute(id)}">${css.replace(/<\/style/gi, "<\\/style")}</style>`;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Merges the attributes of an element that has spreads, in their order: a
|
|
185
|
+
* later value wins, except `class`, whose values all apply.
|
|
186
|
+
*/
|
|
187
|
+
/**
|
|
188
|
+
* A value of an attribute that brumate writes, such as the `b-on` of a dynamic tag, which the
|
|
189
|
+
* compiler wraps. An object of attributes, such as a spread, writes a `b-` attribute only with
|
|
190
|
+
* one, so data from outside the app cannot name handlers or scopes.
|
|
191
|
+
*/
|
|
192
|
+
var Internal = class {
|
|
193
|
+
value;
|
|
194
|
+
constructor(value) {
|
|
195
|
+
this.value = value;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
/** Wraps the value of an attribute that brumate writes. See `Internal`. */
|
|
199
|
+
function internal(value) {
|
|
200
|
+
return new Internal(value);
|
|
201
|
+
}
|
|
202
|
+
/** The value of an attribute of an object, or undefined for a `b-` attribute from outside. */
|
|
203
|
+
function ownValue(name, value) {
|
|
204
|
+
if (value instanceof Internal) return value.value;
|
|
205
|
+
return name.startsWith("b-") ? void 0 : value;
|
|
206
|
+
}
|
|
207
|
+
function mergeAttributes(parts, variables) {
|
|
208
|
+
const result = {};
|
|
209
|
+
for (const part of parts) for (const [name, value] of Object.entries(part ?? {})) {
|
|
210
|
+
if (name.startsWith("b-") && !(value instanceof Internal)) continue;
|
|
211
|
+
result[name] = name === "class" && result.class !== void 0 && value !== void 0 ? [result.class, value] : value;
|
|
212
|
+
}
|
|
213
|
+
if (variables) result.style = vars(variables, result.style);
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* The `b-on` entry of a handler that a spread passes on, such as
|
|
218
|
+
* `click:prevent=Parent#h1@3`: its event, the modifiers of its wrappers, its
|
|
219
|
+
* client function and the scope it runs in. A handler that `$handler` made
|
|
220
|
+
* has its arguments after another `@`, encoded so that they hold no space.
|
|
221
|
+
*/
|
|
222
|
+
function handlerEntry(name, ref, scope, modifiers = [], args) {
|
|
223
|
+
const bound = args === void 0 ? "" : `@${encodeURIComponent(JSON.stringify(args))}`;
|
|
224
|
+
return `${[name.slice(2).toLowerCase(), ...modifiers].join(":")}=${ref}@${scope}${bound}`;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* The `b-on` entry of a binding that a spread puts on `value` or `checked`
|
|
228
|
+
* of an element. It names the scope of the slot, or of the setter of
|
|
229
|
+
* `$bind(name, fn)`, after `@`. A box writes on `change`.
|
|
230
|
+
*/
|
|
231
|
+
function bindEntry(name, ref, setter) {
|
|
232
|
+
const event = name === "checked" ? "change" : "input";
|
|
233
|
+
return setter ? `${event}=$set:${setter.ref}@${setter.scope}` : `${event}=$bind#0.${ref.slot}@${ref.scope}`;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* The `b-on` attribute of an element with spreads: the bindings of its
|
|
237
|
+
* spreads, so its handlers read the new value, then its own handlers, then
|
|
238
|
+
* those of its spreads.
|
|
239
|
+
*/
|
|
240
|
+
function onAttribute(on, own) {
|
|
241
|
+
const value = onValue(on, own);
|
|
242
|
+
return value ? ` b-on="${escapeAttribute(value)}"` : "";
|
|
243
|
+
}
|
|
244
|
+
/** The value of `b-on`, in the order of `onAttribute`. */
|
|
245
|
+
function onValue(on, own) {
|
|
246
|
+
const bindings = [];
|
|
247
|
+
const rest = [];
|
|
248
|
+
for (const entry of on) if (/=\$(bind#|set:)/.test(entry)) bindings.push(entry);
|
|
249
|
+
else rest.push(entry);
|
|
250
|
+
const parts = [];
|
|
251
|
+
for (const part of [
|
|
252
|
+
...bindings,
|
|
253
|
+
own,
|
|
254
|
+
...rest
|
|
255
|
+
]) if (part) parts.push(part);
|
|
256
|
+
return parts.join(" ");
|
|
257
|
+
}
|
|
258
|
+
/** Renders an object of attributes. */
|
|
259
|
+
function spread(attributes, names = false) {
|
|
260
|
+
let result = "";
|
|
261
|
+
const written = [];
|
|
262
|
+
for (const [name, value] of Object.entries(attributes)) if (name !== "children" && !/^on[A-Z]/.test(name)) {
|
|
263
|
+
const text = attribute(name, ownValue(name, value));
|
|
264
|
+
if (text) {
|
|
265
|
+
result += text;
|
|
266
|
+
written.push(name);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return names ? `${result} b-s="${written.join(" ")}"` : result;
|
|
270
|
+
}
|
|
271
|
+
const VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
272
|
+
"area",
|
|
273
|
+
"base",
|
|
274
|
+
"br",
|
|
275
|
+
"col",
|
|
276
|
+
"embed",
|
|
277
|
+
"hr",
|
|
278
|
+
"img",
|
|
279
|
+
"input",
|
|
280
|
+
"link",
|
|
281
|
+
"meta",
|
|
282
|
+
"source",
|
|
283
|
+
"track",
|
|
284
|
+
"wbr"
|
|
285
|
+
]);
|
|
286
|
+
/**
|
|
287
|
+
* Renders the attributes of a form that posts to a server function, such as
|
|
288
|
+
* ` action="/_brumate/App_1abc%23save" method="post"`. `method` is the name of
|
|
289
|
+
* the method attribute to add, if the element has none. `build` is the ID
|
|
290
|
+
* of the build of the page, which the URL carries as `__build`, so the
|
|
291
|
+
* server can tell a post from before a deploy without scripts.
|
|
292
|
+
*/
|
|
293
|
+
function actionAttributes(prefix, name, id, method, result, build, key) {
|
|
294
|
+
const methodText = method ? ` ${method}="post"` : "";
|
|
295
|
+
const params = [];
|
|
296
|
+
if (build !== void 0) params.push(`__build=${encodeURIComponent(build)}`);
|
|
297
|
+
if (key !== void 0) params.push(`__key=${key}`);
|
|
298
|
+
const query = params.join("&");
|
|
299
|
+
if (result !== void 0) return ` ${name}="${escapeAttribute(`?__action=${encodeURIComponent(id)}${query ? `&${query}` : ""}`)}"${methodText} b-action="${escapeAttribute(id)}" b-result="${escapeAttribute(result)}"`;
|
|
300
|
+
return ` ${name}="${escapeAttribute(`${prefix}/${encodeURIComponent(id)}${query ? `?${query}` : ""}`)}"${methodText}`;
|
|
301
|
+
}
|
|
302
|
+
//#endregion
|
|
303
|
+
export { styleElement as C, spread as S, vars as T, onAttribute as _, attribute as a, raw as b, classAttribute as c, escapeAttribute as d, handlerEntry as f, mergeAttributes as g, isRaw as h, affix as i, classValue as l, internal as m, VOID_ELEMENTS as n, attributeText as o, insertValue as p, actionAttributes as r, bindEntry as s, Internal as t, escape as u, onValue as v, textValue as w, rawHTML as x, ownValue as y };
|
|
304
|
+
|
|
305
|
+
//# sourceMappingURL=html-BtKRyi7g.js.map
|