svelte-highlight 7.23.2 → 7.25.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/AnsiOutput.svelte +33 -6
- package/AnsiOutput.svelte.d.ts +8 -0
- package/CodeToolbar.svelte +144 -0
- package/CodeToolbar.svelte.d.ts +125 -0
- package/CodeWindow.svelte +19 -0
- package/CodeWindow.svelte.d.ts +10 -0
- package/CopyButton.svelte +107 -3
- package/CopyButton.svelte.d.ts +17 -1
- package/DiffFileList.svelte +523 -0
- package/DiffFileList.svelte.d.ts +94 -0
- package/DiffFileListItem.svelte +44 -0
- package/DiffMinimap.svelte +99 -0
- package/DiffMinimap.svelte.d.ts +17 -0
- package/DiffStats.svelte +43 -0
- package/DiffStats.svelte.d.ts +17 -0
- package/DiffView.svelte +1213 -0
- package/DiffView.svelte.d.ts +76 -0
- package/FileTabs.svelte +2 -5
- package/Highlight.svelte +158 -2
- package/Highlight.svelte.d.ts +50 -2
- package/HighlightAuto.svelte.d.ts +3 -2
- package/HighlightAutoLazy.svelte +112 -0
- package/HighlightAutoLazy.svelte.d.ts +66 -0
- package/HighlightDiff.svelte +162 -0
- package/HighlightDiff.svelte.d.ts +102 -0
- package/HighlightEditable.svelte +65 -756
- package/HighlightEditable.svelte.d.ts +15 -5
- package/HighlightStream.svelte +117 -218
- package/HighlightStream.svelte.d.ts +8 -0
- package/HighlightSvelte.svelte.d.ts +3 -2
- package/HighlightVirtual.svelte +41 -8
- package/HighlightVirtual.svelte.d.ts +8 -0
- package/LangTag.svelte +33 -4
- package/LangTag.svelte.d.ts +6 -2
- package/LineNumbers.svelte +25 -4
- package/LineNumbers.svelte.d.ts +8 -0
- package/MarkdownStream.svelte +1 -0
- package/README.md +735 -18
- package/ToolbarButton.svelte +90 -0
- package/ToolbarButton.svelte.d.ts +106 -0
- package/Typewriter.svelte +59 -10
- package/Typewriter.svelte.d.ts +7 -0
- package/WrapToggle.svelte +34 -0
- package/WrapToggle.svelte.d.ts +39 -0
- package/action.js +7 -3
- package/ansi.d.ts +1 -3
- package/auto-detect.d.ts +52 -0
- package/auto-detect.js +208 -0
- package/code-region.d.ts +7 -0
- package/code-region.js +20 -0
- package/css-highlights.js +115 -0
- package/css-walk.js +50 -0
- package/diff-controller.d.ts +120 -0
- package/diff-controller.js +721 -0
- package/diff-edits.d.ts +101 -0
- package/diff-edits.js +890 -0
- package/diff-html.d.ts +8 -0
- package/diff-html.js +60 -0
- package/diff-languages.d.ts +4 -0
- package/diff-languages.js +73 -0
- package/diff.d.ts +161 -0
- package/diff.js +1111 -0
- package/editable-commands.js +81 -0
- package/editable-css-paint.js +5 -3
- package/editable-dom-paint.js +1 -1
- package/editable-history.js +199 -0
- package/editable-line-dom.js +176 -1
- package/editable-painter.js +356 -0
- package/editable.d.ts +118 -0
- package/editable.js +513 -0
- package/engine.d.ts +1 -1
- package/fence.js +1 -1
- package/highlight-cascade.js +679 -0
- package/highlight-theme.js +6 -47
- package/incremental-tokenize.js +1 -1
- package/index.d.ts +39 -6
- package/index.js +30 -0
- package/languages/abap.d.ts +2 -0
- package/languages/abap.js +2 -0
- package/languages/aliases.js +1 -1
- package/languages/all.js +18 -1
- package/languages/angular.js +1 -1
- package/languages/antlr.d.ts +2 -0
- package/languages/antlr.js +2 -0
- package/languages/apex.d.ts +2 -0
- package/languages/apex.js +2 -0
- package/languages/bash.js +1 -1
- package/languages/cobol.d.ts +2 -0
- package/languages/cobol.js +2 -0
- package/languages/coq.js +1 -1
- package/languages/cpp.js +1 -1
- package/languages/csharp.js +1 -1
- package/languages/css.js +1 -1
- package/languages/cython.d.ts +2 -0
- package/languages/cython.js +2 -0
- package/languages/dart.js +1 -1
- package/languages/detect-index.d.ts +16 -0
- package/languages/detect-index.js +6 -0
- package/languages/fluent.d.ts +2 -0
- package/languages/fluent.js +2 -0
- package/languages/freemarker.d.ts +2 -0
- package/languages/freemarker.js +3 -0
- package/languages/handlebars.js +1 -1
- package/languages/httpfile.d.ts +2 -0
- package/languages/httpfile.js +3 -0
- package/languages/index.d.ts +34 -0
- package/languages/index.js +17 -0
- package/languages/jinja.js +1 -1
- package/languages/json.js +1 -1
- package/languages/kotlin.js +1 -1
- package/languages/lex.d.ts +2 -0
- package/languages/lex.js +3 -0
- package/languages/lisp.js +1 -1
- package/languages/loaders.d.ts +8 -0
- package/languages/loaders.js +387 -0
- package/languages/mathematica.js +1 -1
- package/languages/meson.d.ts +2 -0
- package/languages/meson.js +2 -0
- package/languages/metal.d.ts +2 -0
- package/languages/metal.js +2 -0
- package/languages/mlir.d.ts +2 -0
- package/languages/mlir.js +2 -0
- package/languages/mojo.js +1 -1
- package/languages/nextflow.js +1 -1
- package/languages/objectivec.js +1 -1
- package/languages/pgsql.js +1 -1
- package/languages/php.js +1 -1
- package/languages/po.d.ts +2 -0
- package/languages/po.js +2 -0
- package/languages/riscv.d.ts +2 -0
- package/languages/riscv.js +2 -0
- package/languages/robotframework.d.ts +2 -0
- package/languages/robotframework.js +2 -0
- package/languages/roc.js +1 -1
- package/languages/solidity.js +1 -1
- package/languages/sshconfig.d.ts +2 -0
- package/languages/sshconfig.js +2 -0
- package/languages/svelte.js +1 -1
- package/languages/uiua.js +1 -1
- package/languages/verilog.js +1 -1
- package/languages/vim.js +1 -1
- package/languages/yacc.d.ts +2 -0
- package/languages/yacc.js +3 -0
- package/languages/zig.js +1 -1
- package/package.json +50 -2
- package/preprocessor.js +20 -5
- package/search.js +2 -2
- package/stream-highlighter.js +198 -0
- package/stream.css +16 -0
- package/stream.d.ts +60 -0
- package/stream.js +283 -0
- package/style-registry.js +34 -3
- package/themes/_base-css.d.ts +1 -0
- package/themes/_base-css.js +1 -0
- package/typewriter-units.d.ts +3 -0
- package/typewriter-units.js +43 -0
- package/virtual-window.js +9 -4
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
// Theme colors for Highlight's "css-highlights" engine, resolved per text
|
|
2
|
+
// run the way the browser cascade resolves them for the "dom" engine's
|
|
3
|
+
// `<span>` markup. Each run's full scope chain is matched against the
|
|
4
|
+
// theme's rules (compound, descendant and child selectors, specificity,
|
|
5
|
+
// source order, `!important`, `var()`, inheritance), so colors match the
|
|
6
|
+
// "dom" engine. Only `color` and `background-color` carry over; the CSS
|
|
7
|
+
// Custom Highlight API can't change font weight or style.
|
|
8
|
+
|
|
9
|
+
import { forEachRule, splitTopLevel } from "./css-walk.js";
|
|
10
|
+
import { CLOSE, OPEN, scopeToCssClass } from "./engine.js";
|
|
11
|
+
import { BASE_CSS } from "./themes/_base-css.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {{ tag: string | null; classes: string[] }} Compound
|
|
15
|
+
* @typedef {{ compounds: Compound[]; combinators: string[]; specificity: number }} Selector
|
|
16
|
+
* @typedef {{ prop: string; value: string; important: boolean; order: number }} Declaration
|
|
17
|
+
* @typedef {{ selectors: Selector[]; declarations: Declaration[] }} Rule
|
|
18
|
+
* @typedef {{ tag: string; classes: Set<string> }} ElementShape
|
|
19
|
+
* @typedef {{ key: string; color?: string; background?: string }} RunStyle
|
|
20
|
+
* @typedef {{
|
|
21
|
+
* parent: CascadeNode | null;
|
|
22
|
+
* element: ElementShape | null;
|
|
23
|
+
* isToken: boolean;
|
|
24
|
+
* color: string | null;
|
|
25
|
+
* background: string | null;
|
|
26
|
+
* effectiveBackground: string | null;
|
|
27
|
+
* custom: Record<string, string>;
|
|
28
|
+
* children: Map<string, CascadeNode>;
|
|
29
|
+
* style: RunStyle | null | undefined;
|
|
30
|
+
* }} CascadeNode
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
const COMPOUND = /^(\*|[a-zA-Z][\w-]*)?((?:\.[\w-]+)*)$/;
|
|
34
|
+
const IMPORTANT = /\s*!\s*important\s*$/i;
|
|
35
|
+
const CHILD_COMBINATOR = /\s*>\s*/g;
|
|
36
|
+
const WHITESPACE = /\s+/;
|
|
37
|
+
const SPACE = /\s/;
|
|
38
|
+
const CLASS_WEIGHT = 256;
|
|
39
|
+
|
|
40
|
+
// Above this many cached scope chains, the cache restarts.
|
|
41
|
+
const NODE_LIMIT = 4096;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* `null` for selectors this model can't match exactly (pseudo-classes,
|
|
45
|
+
* pseudo-elements, attributes, ids, sibling combinators). Pseudo-elements
|
|
46
|
+
* such as `::selection` never style plain token text anyway.
|
|
47
|
+
* @param {string} text
|
|
48
|
+
* @returns {Selector | null}
|
|
49
|
+
*/
|
|
50
|
+
export function parseSelector(text) {
|
|
51
|
+
const normalized = text.replace(CHILD_COMBINATOR, " > ").trim();
|
|
52
|
+
if (normalized === "") return null;
|
|
53
|
+
/** @type {Compound[]} */
|
|
54
|
+
const compounds = [];
|
|
55
|
+
/** @type {string[]} */
|
|
56
|
+
const combinators = [];
|
|
57
|
+
let pending = " ";
|
|
58
|
+
let specificity = 0;
|
|
59
|
+
for (const part of normalized.split(WHITESPACE)) {
|
|
60
|
+
if (part === ">") {
|
|
61
|
+
if (compounds.length === 0 || pending === ">") return null;
|
|
62
|
+
pending = ">";
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const match = COMPOUND.exec(part);
|
|
66
|
+
if (!match) return null;
|
|
67
|
+
const tagText = match[1];
|
|
68
|
+
const classes = match[2] ? match[2].slice(1).split(".") : [];
|
|
69
|
+
if (tagText === undefined && classes.length === 0) return null;
|
|
70
|
+
const tag =
|
|
71
|
+
tagText === undefined || tagText === "*" ? null : tagText.toLowerCase();
|
|
72
|
+
if (compounds.length > 0) combinators.push(pending);
|
|
73
|
+
pending = " ";
|
|
74
|
+
compounds.push({ tag, classes });
|
|
75
|
+
specificity += classes.length * CLASS_WEIGHT + (tag === null ? 0 : 1);
|
|
76
|
+
}
|
|
77
|
+
if (pending === ">") return null;
|
|
78
|
+
return { compounds, combinators, specificity };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Declarations that affect color: `color`, `background-color`, the
|
|
83
|
+
* `background` shorthand, and custom properties (for `var()`).
|
|
84
|
+
* @param {string} body
|
|
85
|
+
* @param {{ order: number }} counter
|
|
86
|
+
* @returns {Declaration[]}
|
|
87
|
+
*/
|
|
88
|
+
function colorDeclarations(body, counter) {
|
|
89
|
+
/** @type {Declaration[]} */
|
|
90
|
+
const declarations = [];
|
|
91
|
+
for (const part of splitTopLevel(body, ";")) {
|
|
92
|
+
const colon = part.indexOf(":");
|
|
93
|
+
if (colon === -1) continue;
|
|
94
|
+
const rawProp = part.slice(0, colon).trim();
|
|
95
|
+
const prop = rawProp.startsWith("--") ? rawProp : rawProp.toLowerCase();
|
|
96
|
+
if (
|
|
97
|
+
prop !== "color" &&
|
|
98
|
+
prop !== "background-color" &&
|
|
99
|
+
prop !== "background" &&
|
|
100
|
+
!prop.startsWith("--")
|
|
101
|
+
) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
let value = part.slice(colon + 1);
|
|
105
|
+
const important = IMPORTANT.test(value);
|
|
106
|
+
if (important) value = value.replace(IMPORTANT, "");
|
|
107
|
+
value = prop.startsWith("--") ? value : value.trim();
|
|
108
|
+
if (value === "" && !prop.startsWith("--")) continue;
|
|
109
|
+
declarations.push({ prop, value, important, order: counter.order++ });
|
|
110
|
+
}
|
|
111
|
+
return declarations;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The theme's style rules, in source order, keeping only declarations and
|
|
116
|
+
* selectors that bear on token colors.
|
|
117
|
+
* @param {string} css
|
|
118
|
+
* @returns {Rule[]}
|
|
119
|
+
*/
|
|
120
|
+
export function parseThemeRules(css) {
|
|
121
|
+
/** @type {Rule[]} */
|
|
122
|
+
const rules = [];
|
|
123
|
+
const counter = { order: 0 };
|
|
124
|
+
forEachRule(css, (prelude, body) => {
|
|
125
|
+
const declarations = colorDeclarations(body, counter);
|
|
126
|
+
if (declarations.length === 0) return;
|
|
127
|
+
const selectors = splitTopLevel(prelude, ",")
|
|
128
|
+
.map((selector) => parseSelector(selector))
|
|
129
|
+
.filter((selector) => selector !== null);
|
|
130
|
+
if (selectors.length > 0) rules.push({ selectors, declarations });
|
|
131
|
+
});
|
|
132
|
+
return rules;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @param {Compound} compound
|
|
137
|
+
* @param {ElementShape} element
|
|
138
|
+
*/
|
|
139
|
+
function compoundMatches(compound, element) {
|
|
140
|
+
if (compound.tag !== null && compound.tag !== element.tag) return false;
|
|
141
|
+
for (const name of compound.classes) {
|
|
142
|
+
if (!element.classes.has(name)) return false;
|
|
143
|
+
}
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Right-to-left, with backtracking over descendant combinators.
|
|
149
|
+
* @param {Selector} selector
|
|
150
|
+
* @param {number} index Compound to match against `node`.
|
|
151
|
+
* @param {CascadeNode} node
|
|
152
|
+
* @returns {boolean}
|
|
153
|
+
*/
|
|
154
|
+
function matchesAt(selector, index, node) {
|
|
155
|
+
const compound = /** @type {Compound} */ (selector.compounds[index]);
|
|
156
|
+
if (node.element === null || !compoundMatches(compound, node.element)) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
if (index === 0) return true;
|
|
160
|
+
if (selector.combinators[index - 1] === ">") {
|
|
161
|
+
return node.parent !== null && matchesAt(selector, index - 1, node.parent);
|
|
162
|
+
}
|
|
163
|
+
for (let ancestor = node.parent; ancestor; ancestor = ancestor.parent) {
|
|
164
|
+
if (matchesAt(selector, index - 1, ancestor)) return true;
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Index of the `)` matching the `(` just before `start`, skipping strings.
|
|
171
|
+
* @param {string} value
|
|
172
|
+
* @param {number} start
|
|
173
|
+
*/
|
|
174
|
+
function closingParen(value, start) {
|
|
175
|
+
let depth = 1;
|
|
176
|
+
for (let i = start; i < value.length; i++) {
|
|
177
|
+
const ch = value[i];
|
|
178
|
+
if (ch === "(") depth++;
|
|
179
|
+
else if (ch === ")" && --depth === 0) return i;
|
|
180
|
+
}
|
|
181
|
+
return -1;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Replaces each `var(--name, fallback)` in `value`. `null` when a reference
|
|
186
|
+
* has no value and no fallback: the declaration is then invalid at
|
|
187
|
+
* computed-value time and behaves as `unset`.
|
|
188
|
+
* @param {string} value
|
|
189
|
+
* @param {(name: string) => string | undefined} lookup
|
|
190
|
+
* @returns {string | null}
|
|
191
|
+
*/
|
|
192
|
+
export function substituteVars(value, lookup) {
|
|
193
|
+
let out = "";
|
|
194
|
+
let i = 0;
|
|
195
|
+
for (;;) {
|
|
196
|
+
const start = value.toLowerCase().indexOf("var(", i);
|
|
197
|
+
if (start === -1) return out + value.slice(i);
|
|
198
|
+
const end = closingParen(value, start + 4);
|
|
199
|
+
if (end === -1) return null;
|
|
200
|
+
const inner = value.slice(start + 4, end);
|
|
201
|
+
const comma = splitTopLevel(inner, ",");
|
|
202
|
+
const name = (comma[0] ?? "").trim();
|
|
203
|
+
const fallback =
|
|
204
|
+
comma.length > 1 ? inner.slice(inner.indexOf(",") + 1) : undefined;
|
|
205
|
+
let replacement = lookup(name);
|
|
206
|
+
if (replacement === undefined && fallback !== undefined) {
|
|
207
|
+
const resolved = substituteVars(fallback, lookup);
|
|
208
|
+
if (resolved === null) return null;
|
|
209
|
+
replacement = resolved;
|
|
210
|
+
}
|
|
211
|
+
if (replacement === undefined) return null;
|
|
212
|
+
out += value.slice(i, start) + replacement;
|
|
213
|
+
i = end + 1;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const BACKGROUND_KEYWORDS = new Set([
|
|
218
|
+
"none",
|
|
219
|
+
"repeat",
|
|
220
|
+
"repeat-x",
|
|
221
|
+
"repeat-y",
|
|
222
|
+
"no-repeat",
|
|
223
|
+
"space",
|
|
224
|
+
"round",
|
|
225
|
+
"scroll",
|
|
226
|
+
"fixed",
|
|
227
|
+
"local",
|
|
228
|
+
"center",
|
|
229
|
+
"top",
|
|
230
|
+
"bottom",
|
|
231
|
+
"left",
|
|
232
|
+
"right",
|
|
233
|
+
"border-box",
|
|
234
|
+
"padding-box",
|
|
235
|
+
"content-box",
|
|
236
|
+
"text",
|
|
237
|
+
"auto",
|
|
238
|
+
"cover",
|
|
239
|
+
"contain",
|
|
240
|
+
]);
|
|
241
|
+
const IMAGE_FUNCTION =
|
|
242
|
+
/^(url|image|image-set|cross-fade|element|paint|(repeating-)?(linear|radial|conic)-gradient)\(/i;
|
|
243
|
+
const LENGTH_LIKE = /^[-+.\d]/;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Splits on top-level whitespace and `/`, keeping functions whole.
|
|
247
|
+
* @param {string} value
|
|
248
|
+
*/
|
|
249
|
+
function shorthandTokens(value) {
|
|
250
|
+
/** @type {string[]} */
|
|
251
|
+
const tokens = [];
|
|
252
|
+
let depth = 0;
|
|
253
|
+
let current = "";
|
|
254
|
+
for (const ch of value) {
|
|
255
|
+
if (ch === "(") depth++;
|
|
256
|
+
else if (ch === ")") depth--;
|
|
257
|
+
if (depth === 0 && (ch === "/" || SPACE.test(ch))) {
|
|
258
|
+
if (current) tokens.push(current);
|
|
259
|
+
current = "";
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
current += ch;
|
|
263
|
+
}
|
|
264
|
+
if (current) tokens.push(current);
|
|
265
|
+
return tokens;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* The `background-color` a `background` shorthand sets: its final layer's
|
|
270
|
+
* color, or `transparent` when it names none.
|
|
271
|
+
* @param {string} value
|
|
272
|
+
*/
|
|
273
|
+
export function shorthandBackgroundColor(value) {
|
|
274
|
+
const layers = splitTopLevel(value, ",");
|
|
275
|
+
const last = layers[layers.length - 1] ?? "";
|
|
276
|
+
for (const token of shorthandTokens(last)) {
|
|
277
|
+
const lower = token.toLowerCase();
|
|
278
|
+
if (BACKGROUND_KEYWORDS.has(lower)) continue;
|
|
279
|
+
if (IMAGE_FUNCTION.test(token) || LENGTH_LIKE.test(token)) continue;
|
|
280
|
+
return token;
|
|
281
|
+
}
|
|
282
|
+
return "transparent";
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** @param {string | null} value */
|
|
286
|
+
function keyword(value) {
|
|
287
|
+
return value === null ? "unset" : value.trim().toLowerCase();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* CSS-wide keywords on a custom property. `true`: take the inherited value.
|
|
292
|
+
* `false`: the guaranteed-invalid value.
|
|
293
|
+
*/
|
|
294
|
+
const CSS_WIDE = new Map([
|
|
295
|
+
["inherit", true],
|
|
296
|
+
["unset", true],
|
|
297
|
+
["revert", true],
|
|
298
|
+
["revert-layer", true],
|
|
299
|
+
["initial", false],
|
|
300
|
+
]);
|
|
301
|
+
|
|
302
|
+
const CURRENT_COLOR = /(?<![\w-])currentcolor(?![\w-])/gi;
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Replaces `currentColor` with the inherited color. Inside `::highlight()`,
|
|
306
|
+
* `currentColor` doesn't mean the token's parent color, so it can't stay.
|
|
307
|
+
* @param {string | null} value
|
|
308
|
+
* @param {string | null} color The parent's color, or `null` when it comes
|
|
309
|
+
* from outside the `<pre>`.
|
|
310
|
+
* @param {(color: string | null) => string | null} known Fills in the
|
|
311
|
+
* outside color.
|
|
312
|
+
* @returns {string | null}
|
|
313
|
+
*/
|
|
314
|
+
function withCurrentColor(value, color, known) {
|
|
315
|
+
if (value === null) return value;
|
|
316
|
+
CURRENT_COLOR.lastIndex = 0;
|
|
317
|
+
if (!CURRENT_COLOR.test(value)) return value;
|
|
318
|
+
const resolved = known(color);
|
|
319
|
+
return resolved === null ? value : value.replace(CURRENT_COLOR, resolved);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* @param {Rule[]} rules
|
|
324
|
+
* @param {Record<string, string>} rootCustom Custom properties inherited
|
|
325
|
+
* from outside the `<pre>` (a palette's `--shl-*` vars).
|
|
326
|
+
* @param {(() => string | null) | undefined} outsideColor The color the
|
|
327
|
+
* `<pre>` inherits. Read only when a `currentColor` needs it.
|
|
328
|
+
*/
|
|
329
|
+
function createResolver(rules, rootCustom, outsideColor) {
|
|
330
|
+
/** @type {string | null | undefined} */
|
|
331
|
+
let pageColor;
|
|
332
|
+
/** @param {string | null} color */
|
|
333
|
+
const known = (color) => {
|
|
334
|
+
if (color !== null) return color;
|
|
335
|
+
if (pageColor === undefined) pageColor = outsideColor?.() ?? null;
|
|
336
|
+
return pageColor;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* @param {CascadeNode | null} parent
|
|
341
|
+
* @param {ElementShape} element
|
|
342
|
+
* @param {boolean} isToken
|
|
343
|
+
* @returns {CascadeNode}
|
|
344
|
+
*/
|
|
345
|
+
function compute(parent, element, isToken) {
|
|
346
|
+
/** @type {CascadeNode} */
|
|
347
|
+
const node = {
|
|
348
|
+
parent,
|
|
349
|
+
element,
|
|
350
|
+
isToken,
|
|
351
|
+
color: null,
|
|
352
|
+
background: null,
|
|
353
|
+
effectiveBackground: null,
|
|
354
|
+
custom: parent ? parent.custom : rootCustom,
|
|
355
|
+
children: new Map(),
|
|
356
|
+
style: undefined,
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
/** @type {Map<string, { declaration: Declaration; specificity: number }>} */
|
|
360
|
+
const winners = new Map();
|
|
361
|
+
for (const rule of rules) {
|
|
362
|
+
let specificity = -1;
|
|
363
|
+
for (const selector of rule.selectors) {
|
|
364
|
+
if (
|
|
365
|
+
selector.specificity > specificity &&
|
|
366
|
+
matchesAt(selector, selector.compounds.length - 1, node)
|
|
367
|
+
) {
|
|
368
|
+
specificity = selector.specificity;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
if (specificity < 0) continue;
|
|
372
|
+
for (const declaration of rule.declarations) {
|
|
373
|
+
const key =
|
|
374
|
+
declaration.prop === "background"
|
|
375
|
+
? "background-color"
|
|
376
|
+
: declaration.prop;
|
|
377
|
+
const previous = winners.get(key);
|
|
378
|
+
if (
|
|
379
|
+
previous === undefined ||
|
|
380
|
+
(declaration.important && !previous.declaration.important) ||
|
|
381
|
+
(declaration.important === previous.declaration.important &&
|
|
382
|
+
(specificity > previous.specificity ||
|
|
383
|
+
(specificity === previous.specificity &&
|
|
384
|
+
declaration.order > previous.declaration.order)))
|
|
385
|
+
) {
|
|
386
|
+
winners.set(key, { declaration, specificity });
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const inherited = node.custom;
|
|
392
|
+
/** @type {Map<string, string>} */
|
|
393
|
+
const declaredCustom = new Map();
|
|
394
|
+
for (const [key, { declaration }] of winners) {
|
|
395
|
+
if (key.startsWith("--")) declaredCustom.set(key, declaration.value);
|
|
396
|
+
}
|
|
397
|
+
if (declaredCustom.size > 0) {
|
|
398
|
+
/** @type {Record<string, string>} */
|
|
399
|
+
const custom = {};
|
|
400
|
+
for (const [name, value] of Object.entries(inherited)) {
|
|
401
|
+
if (!declaredCustom.has(name)) custom[name] = value;
|
|
402
|
+
}
|
|
403
|
+
/** @type {Set<string>} */
|
|
404
|
+
const resolving = new Set();
|
|
405
|
+
/**
|
|
406
|
+
* @param {string} name
|
|
407
|
+
* @returns {string | undefined}
|
|
408
|
+
*/
|
|
409
|
+
const resolveCustom = (name) => {
|
|
410
|
+
const raw = declaredCustom.get(name);
|
|
411
|
+
if (raw === undefined) return inherited[name];
|
|
412
|
+
const wide = CSS_WIDE.get(raw.trim().toLowerCase());
|
|
413
|
+
if (wide !== undefined) return wide ? inherited[name] : undefined;
|
|
414
|
+
if (resolving.has(name)) return undefined;
|
|
415
|
+
resolving.add(name);
|
|
416
|
+
const value = substituteVars(raw, resolveCustom);
|
|
417
|
+
resolving.delete(name);
|
|
418
|
+
return value === null || value.trim() === "" ? undefined : value;
|
|
419
|
+
};
|
|
420
|
+
for (const name of declaredCustom.keys()) {
|
|
421
|
+
const value = resolveCustom(name);
|
|
422
|
+
if (value !== undefined) custom[name] = value;
|
|
423
|
+
}
|
|
424
|
+
node.custom = custom;
|
|
425
|
+
}
|
|
426
|
+
const custom = node.custom;
|
|
427
|
+
/** @param {string} name */
|
|
428
|
+
const lookup = (name) => custom[name];
|
|
429
|
+
|
|
430
|
+
const parentColor = parent ? parent.color : null;
|
|
431
|
+
node.color = parentColor;
|
|
432
|
+
const colorWinner = winners.get("color");
|
|
433
|
+
if (colorWinner) {
|
|
434
|
+
// In `color`, `currentColor` means the inherited color, also inside
|
|
435
|
+
// functions like `color-mix()`.
|
|
436
|
+
const value = withCurrentColor(
|
|
437
|
+
substituteVars(colorWinner.declaration.value, lookup),
|
|
438
|
+
parentColor,
|
|
439
|
+
known,
|
|
440
|
+
);
|
|
441
|
+
const lower = keyword(value);
|
|
442
|
+
if (lower === "initial") node.color = "CanvasText";
|
|
443
|
+
else if (
|
|
444
|
+
value !== null &&
|
|
445
|
+
lower !== "inherit" &&
|
|
446
|
+
lower !== "unset" &&
|
|
447
|
+
lower !== "revert" &&
|
|
448
|
+
lower !== "revert-layer" &&
|
|
449
|
+
lower !== "currentcolor"
|
|
450
|
+
) {
|
|
451
|
+
node.color = value.trim();
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
const backgroundWinner = winners.get("background-color");
|
|
456
|
+
if (backgroundWinner) {
|
|
457
|
+
const { declaration } = backgroundWinner;
|
|
458
|
+
let value = withCurrentColor(
|
|
459
|
+
substituteVars(declaration.value, lookup),
|
|
460
|
+
node.color,
|
|
461
|
+
known,
|
|
462
|
+
);
|
|
463
|
+
const lower = keyword(value);
|
|
464
|
+
if (lower === "inherit") {
|
|
465
|
+
node.background = parent ? parent.background : null;
|
|
466
|
+
} else if (lower === "currentcolor") {
|
|
467
|
+
node.background = node.color;
|
|
468
|
+
} else if (
|
|
469
|
+
value !== null &&
|
|
470
|
+
lower !== "unset" &&
|
|
471
|
+
lower !== "initial" &&
|
|
472
|
+
lower !== "revert" &&
|
|
473
|
+
lower !== "revert-layer"
|
|
474
|
+
) {
|
|
475
|
+
if (declaration.prop === "background") {
|
|
476
|
+
value = shorthandBackgroundColor(value);
|
|
477
|
+
}
|
|
478
|
+
node.background =
|
|
479
|
+
value.trim().toLowerCase() === "transparent" ? null : value.trim();
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// Backgrounds don't inherit, but a token's text sits on its nearest
|
|
484
|
+
// painted ancestor span. The `<pre>`/`<code>` boxes paint their own.
|
|
485
|
+
node.effectiveBackground = isToken
|
|
486
|
+
? (node.background ?? parent?.effectiveBackground ?? null)
|
|
487
|
+
: null;
|
|
488
|
+
return node;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const outside = /** @type {CascadeNode} */ ({
|
|
492
|
+
parent: null,
|
|
493
|
+
element: null,
|
|
494
|
+
isToken: false,
|
|
495
|
+
color: null,
|
|
496
|
+
background: null,
|
|
497
|
+
effectiveBackground: null,
|
|
498
|
+
custom: rootCustom,
|
|
499
|
+
children: new Map(),
|
|
500
|
+
style: null,
|
|
501
|
+
});
|
|
502
|
+
const pre = compute(outside, { tag: "pre", classes: new Set() }, false);
|
|
503
|
+
const code = compute(pre, { tag: "code", classes: new Set(["hljs"]) }, false);
|
|
504
|
+
code.style = null;
|
|
505
|
+
let size = 0;
|
|
506
|
+
|
|
507
|
+
return {
|
|
508
|
+
/** The `<code class="hljs">` element: where events start. */
|
|
509
|
+
root: code,
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* The node for a `<span>` with `scope`'s classes inside `parent`.
|
|
513
|
+
* @param {CascadeNode} parent
|
|
514
|
+
* @param {string} scope
|
|
515
|
+
* @returns {CascadeNode}
|
|
516
|
+
*/
|
|
517
|
+
child(parent, scope) {
|
|
518
|
+
let node = parent.children.get(scope);
|
|
519
|
+
if (node === undefined) {
|
|
520
|
+
if (size >= NODE_LIMIT) {
|
|
521
|
+
code.children = new Map();
|
|
522
|
+
size = 0;
|
|
523
|
+
}
|
|
524
|
+
node = compute(
|
|
525
|
+
parent,
|
|
526
|
+
{
|
|
527
|
+
tag: "span",
|
|
528
|
+
classes: new Set(scopeToCssClass(scope, "hljs-").split(" ")),
|
|
529
|
+
},
|
|
530
|
+
true,
|
|
531
|
+
);
|
|
532
|
+
parent.children.set(scope, node);
|
|
533
|
+
size++;
|
|
534
|
+
}
|
|
535
|
+
return node;
|
|
536
|
+
},
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* What a highlight must paint for text directly inside `node`, or
|
|
540
|
+
* `null` when it looks the same as plain text in `<code>`.
|
|
541
|
+
* @param {CascadeNode} node
|
|
542
|
+
* @returns {RunStyle | null}
|
|
543
|
+
*/
|
|
544
|
+
styleOf(node) {
|
|
545
|
+
if (node.style !== undefined) return node.style;
|
|
546
|
+
const color =
|
|
547
|
+
node.color !== null && node.color !== code.color
|
|
548
|
+
? node.color
|
|
549
|
+
: undefined;
|
|
550
|
+
const background = node.effectiveBackground ?? undefined;
|
|
551
|
+
node.style =
|
|
552
|
+
color === undefined && background === undefined
|
|
553
|
+
? null
|
|
554
|
+
: {
|
|
555
|
+
key: `${color ?? ""}\n${background ?? ""}`,
|
|
556
|
+
...(color !== undefined && { color }),
|
|
557
|
+
...(background !== undefined && { background }),
|
|
558
|
+
};
|
|
559
|
+
return node.style;
|
|
560
|
+
},
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** @typedef {ReturnType<typeof createResolver>} ThemeCascade */
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* A resolver for `theme`'s token colors. Theme CSS is matched as written;
|
|
568
|
+
* a `ThemePalette` goes through `themes/base.css`, as the "dom" engine
|
|
569
|
+
* applies it.
|
|
570
|
+
* @param {string | import("./theme.d.ts").ThemePalette} theme
|
|
571
|
+
* @param {() => string | null} [outsideColor] The color the `<pre>`
|
|
572
|
+
* inherits from the page. Needed only by themes that use `currentColor`.
|
|
573
|
+
* @returns {ThemeCascade}
|
|
574
|
+
*/
|
|
575
|
+
export function createThemeCascade(theme, outsideColor) {
|
|
576
|
+
if (typeof theme === "object") {
|
|
577
|
+
// The vars sit on a wrapper with no `--shl-*` vars above it, so a
|
|
578
|
+
// CSS-wide keyword leaves the var unset.
|
|
579
|
+
/** @type {Record<string, string>} */
|
|
580
|
+
const vars = {};
|
|
581
|
+
for (const [name, value] of Object.entries(theme.vars)) {
|
|
582
|
+
if (!CSS_WIDE.has(value.trim().toLowerCase())) vars[name] = value;
|
|
583
|
+
}
|
|
584
|
+
return createResolver(parseThemeRules(BASE_CSS), vars, outsideColor);
|
|
585
|
+
}
|
|
586
|
+
return createResolver(parseThemeRules(theme), {}, outsideColor);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Run styles and their `c<index>` names. Pass the same table to each
|
|
591
|
+
* `cascadeRanges` call to keep names stable across calls.
|
|
592
|
+
* @typedef {{ styles: RunStyle[]; names: Map<string, string> }} RunTable
|
|
593
|
+
*/
|
|
594
|
+
|
|
595
|
+
/** @returns {RunTable} */
|
|
596
|
+
export function createRunTable() {
|
|
597
|
+
return { styles: [], names: new Map() };
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* One range per run of text that shares a style, named `c<index>` into
|
|
602
|
+
* `styles`. Text that looks the same as plain `<code>` text gets no range.
|
|
603
|
+
* Offsets are into the events' concatenated text, as `toRanges` returns.
|
|
604
|
+
* @param {import("./engine.d.ts").ScopeEvent[]} events
|
|
605
|
+
* @param {ThemeCascade} cascade
|
|
606
|
+
* @param {RunTable} [table] Names styles; new styles are appended to it.
|
|
607
|
+
* @returns {{ ranges: import("./engine.d.ts").TokenRange[]; styles: RunStyle[] }}
|
|
608
|
+
*/
|
|
609
|
+
export function cascadeRanges(events, cascade, table = createRunTable()) {
|
|
610
|
+
/** @type {import("./engine.d.ts").TokenRange[]} */
|
|
611
|
+
const ranges = [];
|
|
612
|
+
const { styles, names } = table;
|
|
613
|
+
/** @type {CascadeNode[]} */
|
|
614
|
+
const stack = [cascade.root];
|
|
615
|
+
let top = cascade.root;
|
|
616
|
+
let pos = 0;
|
|
617
|
+
for (const event of events) {
|
|
618
|
+
if (event.t === OPEN) {
|
|
619
|
+
top = cascade.child(top, event.s);
|
|
620
|
+
stack.push(top);
|
|
621
|
+
} else if (event.t === CLOSE) {
|
|
622
|
+
stack.pop();
|
|
623
|
+
top = /** @type {CascadeNode} */ (stack[stack.length - 1]);
|
|
624
|
+
} else {
|
|
625
|
+
const length = event.v.length;
|
|
626
|
+
if (length === 0) continue;
|
|
627
|
+
const style = cascade.styleOf(top);
|
|
628
|
+
if (style !== null) {
|
|
629
|
+
let scope = names.get(style.key);
|
|
630
|
+
if (scope === undefined) {
|
|
631
|
+
scope = `c${styles.length}`;
|
|
632
|
+
names.set(style.key, scope);
|
|
633
|
+
styles.push(style);
|
|
634
|
+
}
|
|
635
|
+
const last = ranges[ranges.length - 1];
|
|
636
|
+
if (last !== undefined && last.end === pos && last.scope === scope) {
|
|
637
|
+
last.end += length;
|
|
638
|
+
} else {
|
|
639
|
+
ranges.push({ start: pos, end: pos + length, scope });
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
pos += length;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
return { ranges, styles };
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* @param {RunStyle[]} styles From `cascadeRanges`.
|
|
650
|
+
* @param {string} prefix Highlight name prefix, e.g. `hljs-highlight-1`.
|
|
651
|
+
* @returns {string} `::highlight(<prefix>-c<i>)` rules, concatenated.
|
|
652
|
+
*/
|
|
653
|
+
export function cascadeHighlightRules(styles, prefix) {
|
|
654
|
+
let rules = "";
|
|
655
|
+
for (const [index, style] of styles.entries()) {
|
|
656
|
+
const declarations = [];
|
|
657
|
+
if (style.color !== undefined) declarations.push(`color:${style.color}`);
|
|
658
|
+
if (style.background !== undefined) {
|
|
659
|
+
declarations.push(`background-color:${style.background}`);
|
|
660
|
+
}
|
|
661
|
+
rules += `::highlight(${prefix}-c${index}){${declarations.join(";")}}`;
|
|
662
|
+
}
|
|
663
|
+
return rules;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* Lives here, not in a component: svelte-preprocess matches a literal
|
|
668
|
+
* style tag inside `<script>` as the component's `<style>` block.
|
|
669
|
+
* @param {RunStyle[]} styles From `cascadeRanges`.
|
|
670
|
+
* @param {string} prefix Highlight name prefix, e.g. `hljs-highlight-1`.
|
|
671
|
+
* @returns {string} `<style>`-wrapped `::highlight(<prefix>-c<i>)` rules, or
|
|
672
|
+
* `""` when there are none.
|
|
673
|
+
*/
|
|
674
|
+
export function cascadeHighlightStyle(styles, prefix) {
|
|
675
|
+
if (styles.length === 0) return "";
|
|
676
|
+
// Theme values can't close the tag early.
|
|
677
|
+
const rules = cascadeHighlightRules(styles, prefix).replaceAll("<", "\\3c ");
|
|
678
|
+
return `<style>${rules}</style>`;
|
|
679
|
+
}
|