@dou-ui/code 0.0.0-stage → 0.1.0-alpha.1
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/LICENSE +21 -0
- package/README.md +10 -2
- package/dist/_virtual/_rolldown/runtime.js +12 -0
- package/dist/assets/default-BZBMZKf_.js.map +1 -0
- package/dist/code-diff.js +6 -0
- package/dist/code-diff.js.map +1 -0
- package/dist/code-diff.vue_vue_type_script_setup_true_lang.js +792 -0
- package/dist/code-diff.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/code-editor.js +6 -0
- package/dist/code-editor.js.map +1 -0
- package/dist/code-editor.vue_vue_type_script_setup_true_lang.js +5078 -0
- package/dist/code-editor.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/code-input.js +6 -0
- package/dist/code-input.js.map +1 -0
- package/dist/code-input.vue_vue_type_script_setup_true_lang.js +237 -0
- package/dist/code-input.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/code-keys.js +42 -0
- package/dist/code-keys.js.map +1 -0
- package/dist/code-keys.vue_vue_type_script_setup_true_lang.js +244 -0
- package/dist/code-keys.vue_vue_type_script_setup_true_lang.js.map +1 -0
- package/dist/code-keys2.js +6 -0
- package/dist/code-keys2.js.map +1 -0
- package/dist/collaboration/index.js +255 -0
- package/dist/collaboration/index.js.map +1 -0
- package/dist/completion/match.js +55 -0
- package/dist/completion/match.js.map +1 -0
- package/dist/completion/snippet.js +71 -0
- package/dist/completion/snippet.js.map +1 -0
- package/dist/core/brackets.js +55 -0
- package/dist/core/brackets.js.map +1 -0
- package/dist/core/changes.js +185 -0
- package/dist/core/changes.js.map +1 -0
- package/dist/core/chars.js +130 -0
- package/dist/core/chars.js.map +1 -0
- package/dist/core/commands.js +910 -0
- package/dist/core/commands.js.map +1 -0
- package/dist/core/diff.js +215 -0
- package/dist/core/diff.js.map +1 -0
- package/dist/core/folding.js +87 -0
- package/dist/core/folding.js.map +1 -0
- package/dist/core/history.js +74 -0
- package/dist/core/history.js.map +1 -0
- package/dist/core/keymap.js +326 -0
- package/dist/core/keymap.js.map +1 -0
- package/dist/core/search.js +91 -0
- package/dist/core/search.js.map +1 -0
- package/dist/core/selection.js +88 -0
- package/dist/core/selection.js.map +1 -0
- package/dist/core/text.js +320 -0
- package/dist/core/text.js.map +1 -0
- package/dist/core/transaction.js +98 -0
- package/dist/core/transaction.js.map +1 -0
- package/dist/extension.js +16 -0
- package/dist/extension.js.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/language/highlighter.js +336 -0
- package/dist/language/highlighter.js.map +1 -0
- package/dist/language/language.js +138 -0
- package/dist/language/language.js.map +1 -0
- package/dist/language/stream.js +112 -0
- package/dist/language/stream.js.map +1 -0
- package/dist/language/to-html.js +36 -0
- package/dist/language/to-html.js.map +1 -0
- package/dist/language/worker-client.js +198 -0
- package/dist/language/worker-client.js.map +1 -0
- package/dist/language/worker-protocol.js +48 -0
- package/dist/language/worker-protocol.js.map +1 -0
- package/dist/languages/css.js +128 -0
- package/dist/languages/css.js.map +1 -0
- package/dist/languages/html.js +175 -0
- package/dist/languages/html.js.map +1 -0
- package/dist/languages/javascript.js +300 -0
- package/dist/languages/javascript.js.map +1 -0
- package/dist/languages/json.js +45 -0
- package/dist/languages/json.js.map +1 -0
- package/dist/languages/markdown.js +82 -0
- package/dist/languages/markdown.js.map +1 -0
- package/dist/languages/python.js +88 -0
- package/dist/languages/python.js.map +1 -0
- package/dist/languages/shell.js +153 -0
- package/dist/languages/shell.js.map +1 -0
- package/dist/languages/sql.js +91 -0
- package/dist/languages/sql.js.map +1 -0
- package/dist/languages/yaml.js +85 -0
- package/dist/languages/yaml.js.map +1 -0
- package/dist/locale.js +158 -0
- package/dist/locale.js.map +1 -0
- package/dist/lsp/index.js +715 -0
- package/dist/lsp/index.js.map +1 -0
- package/dist/lsp/markup.js +139 -0
- package/dist/lsp/markup.js.map +1 -0
- package/dist/render-part.js +14 -0
- package/dist/render-part.js.map +1 -0
- package/dist/shiki/index.js +140 -0
- package/dist/shiki/index.js.map +1 -0
- package/dist/style.css +1 -0
- package/dist/style.d.ts +1 -0
- package/dist/style.js +4 -0
- package/dist/types/code-diff.vue.d.ts +45 -0
- package/dist/types/code-editor.vue.d.ts +43 -0
- package/dist/types/code-input.vue.d.ts +32 -0
- package/dist/types/code-keys.d.ts +9 -0
- package/dist/types/code-keys.vue.d.ts +8 -0
- package/dist/types/collaboration/index.d.ts +36 -0
- package/dist/types/completion/match.d.ts +13 -0
- package/dist/types/completion/snippet.d.ts +17 -0
- package/dist/types/core/brackets.d.ts +15 -0
- package/dist/types/core/changes.d.ts +52 -0
- package/dist/types/core/chars.d.ts +24 -0
- package/dist/types/core/commands.d.ts +199 -0
- package/dist/types/core/diff.d.ts +52 -0
- package/dist/types/core/folding.d.ts +23 -0
- package/dist/types/core/history.d.ts +24 -0
- package/dist/types/core/keymap.d.ts +29 -0
- package/dist/types/core/search.d.ts +37 -0
- package/dist/types/core/selection.d.ts +28 -0
- package/dist/types/core/text.d.ts +79 -0
- package/dist/types/core/transaction.d.ts +65 -0
- package/dist/types/extension.d.ts +321 -0
- package/dist/types/index.d.ts +159 -0
- package/dist/types/language/highlighter.d.ts +99 -0
- package/dist/types/language/language.d.ts +100 -0
- package/dist/types/language/stream.d.ts +56 -0
- package/dist/types/language/to-html.d.ts +17 -0
- package/dist/types/language/worker-client.d.ts +52 -0
- package/dist/types/language/worker-protocol.d.ts +50 -0
- package/dist/types/languages/css.d.ts +17 -0
- package/dist/types/languages/html.d.ts +34 -0
- package/dist/types/languages/javascript.d.ts +29 -0
- package/dist/types/languages/json.d.ts +7 -0
- package/dist/types/languages/markdown.d.ts +17 -0
- package/dist/types/languages/python.d.ts +9 -0
- package/dist/types/languages/shell.d.ts +11 -0
- package/dist/types/languages/sql.d.ts +9 -0
- package/dist/types/languages/yaml.d.ts +14 -0
- package/dist/types/locale.d.ts +79 -0
- package/dist/types/lsp/index.d.ts +119 -0
- package/dist/types/lsp/markup.d.ts +14 -0
- package/dist/types/render-part.d.ts +14 -0
- package/dist/types/shiki/index.d.ts +63 -0
- package/dist/types/types.d.ts +267 -0
- package/dist/types/view/input.d.ts +12 -0
- package/dist/types/view/layout.d.ts +55 -0
- package/dist/types/view/measure.d.ts +22 -0
- package/dist/types/view/viewport.d.ts +16 -0
- package/dist/types/view/viewport.test.d.ts +1 -0
- package/dist/types/worker/default.d.ts +1 -0
- package/dist/types/worker/index.d.ts +34 -0
- package/dist/view/input.js +27 -0
- package/dist/view/input.js.map +1 -0
- package/dist/view/layout.js +194 -0
- package/dist/view/layout.js.map +1 -0
- package/dist/view/measure.js +104 -0
- package/dist/view/measure.js.map +1 -0
- package/dist/view/viewport.js +31 -0
- package/dist/view/viewport.js.map +1 -0
- package/dist/worker/default.js +20 -0
- package/dist/worker/default.js.map +1 -0
- package/dist/worker/index.js +139 -0
- package/dist/worker/index.js.map +1 -0
- package/package.json +88 -4
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","names":[],"sources":["../../src/language/stream.ts"],"sourcesContent":["/**\n * A cursor over one line, handed to a language's `token` function. The\n * function reads one token from `pos` onward (at least one character) and\n * returns its kind; whatever it read becomes that token.\n */\nexport class CodeStream {\n /** Where the current token starts. */\n start = 0\n /** Where reading continues. */\n pos = 0\n /** Where this stream ends; a nesting language ends it early, e.g. at `</script>`. */\n end: number\n\n constructor(\n /** The whole line. */\n readonly string: string,\n /** Columns per tab stop, for `indentation()`. */\n readonly tabSize = 2,\n ) {\n this.end = string.length\n }\n\n /** Whether the line is read to its end. */\n eol(): boolean {\n return this.pos >= this.end\n }\n\n /** Whether the stream is at the line's first character that isn't whitespace. */\n sol(): boolean {\n for (let i = this.pos - 1; i >= 0; i--) {\n const code = this.string.charCodeAt(i)\n // as `\\s`: tab to carriage return and space, other spaces (the CJK one too) by the regex\n if (code === 32 || (code >= 9 && code <= 13)) continue\n if (code < 128 || !SPACE.test(this.string[i]!)) return false\n }\n return true\n }\n\n /** The next character, without reading it. */\n peek(): string | undefined {\n return this.pos < this.end ? this.string[this.pos] : undefined\n }\n\n /** Reads and returns the next character. */\n next(): string | undefined {\n return this.pos < this.end ? this.string[this.pos++] : undefined\n }\n\n /** Reads the next character if it is `match` (a character, a set as a regex, or a test). */\n eat(match: string | RegExp | ((ch: string) => boolean)): string | undefined {\n const ch = this.peek()\n if (ch === undefined) return undefined\n const ok =\n typeof match === 'string'\n ? ch === match\n : match instanceof RegExp\n ? match.test(ch)\n : match(ch)\n if (!ok) return undefined\n this.pos++\n return ch\n }\n\n /** Reads characters while they match; returns whether it read any. */\n eatWhile(match: string | RegExp | ((ch: string) => boolean)): boolean {\n const from = this.pos\n while (this.eat(match) !== undefined) {\n // keep reading\n }\n return this.pos > from\n }\n\n /** Reads whitespace. */\n eatSpace(): boolean {\n return this.eatWhile(isSpace)\n }\n\n /** Reads to the end of the stream. */\n skipToEnd(): void {\n this.pos = this.end\n }\n\n /** Reads up to (not including) the next `text`; returns whether it was found. */\n skipTo(text: string): boolean {\n const at = this.string.indexOf(text, this.pos)\n if (at < 0 || at >= this.end) return false\n this.pos = at\n return true\n }\n\n /**\n * Reads `pattern` if the line continues with it. A string matches as is\n * (`caseInsensitive` ignores case); a regex must start with `^` and\n * returns its match. `consume: false` only looks.\n */\n match(pattern: string, consume?: boolean, caseInsensitive?: boolean): boolean\n match(pattern: RegExp, consume?: boolean): RegExpMatchArray | null\n match(\n pattern: string | RegExp,\n consume = true,\n caseInsensitive = false,\n ): boolean | RegExpMatchArray | null {\n if (typeof pattern === 'string') {\n if (this.pos + pattern.length > this.end) return false\n const ok = caseInsensitive\n ? this.string.slice(this.pos, this.pos + pattern.length).toLowerCase() ===\n pattern.toLowerCase()\n : this.string.startsWith(pattern, this.pos)\n if (ok && consume) this.pos += pattern.length\n return ok\n }\n // Matched against the rest of the line: slicing a string doesn't copy it, and a\n // pattern written inside a tokenizer (a new object each time) needs no preparing.\n const rest =\n this.pos || this.end < this.string.length\n ? this.string.slice(this.pos, this.end)\n : this.string\n if (pattern.global || pattern.sticky) pattern.lastIndex = 0\n let found: RegExpMatchArray | null = pattern.exec(rest)\n if (pattern.global || pattern.sticky) pattern.lastIndex = 0\n if (found && found.index !== 0) found = null\n if (!found) return null\n if (consume) this.pos += found[0].length\n return found\n }\n\n /** Steps back `n` characters. */\n backUp(n: number): void {\n this.pos -= n\n }\n\n /** The text of the current token so far. */\n current(): string {\n return this.string.slice(this.start, this.pos)\n }\n\n /** The line's indentation in columns. */\n indentation(): number {\n let col = 0\n for (const ch of this.string) {\n if (ch === ' ') col++\n else if (ch === '\\t') col = (Math.floor(col / this.tabSize) + 1) * this.tabSize\n else break\n }\n return col\n }\n\n /** The text after the stream, to the line's end (also past `end`). */\n rest(): string {\n return this.string.slice(this.pos)\n }\n}\n\nconst SPACE = /\\s/\n\nexport const isSpace = (ch: string) => ch === ' ' || ch === '\\t'\n"],"mappings":";;;;;;AAKA,IAAa,aAAb,MAAwB;CAUX;CAEA;;CAVX,QAAQ;;CAER,MAAM;;CAEN;CAEA,YAEE,QAEA,UAAmB,GACnB;EAHS,KAAA,SAAA;EAEA,KAAA,UAAA;EAET,KAAK,MAAM,OAAO;CACpB;;CAGA,MAAe;EACb,OAAO,KAAK,OAAO,KAAK;CAC1B;;CAGA,MAAe;EACb,KAAK,IAAI,IAAI,KAAK,MAAM,GAAG,KAAK,GAAG,KAAK;GACtC,MAAM,OAAO,KAAK,OAAO,WAAW,CAAC;GAErC,IAAI,SAAS,MAAO,QAAQ,KAAK,QAAQ,IAAK;GAC9C,IAAI,OAAO,OAAO,CAAC,MAAM,KAAK,KAAK,OAAO,EAAG,GAAG,OAAO;EACzD;EACA,OAAO;CACT;;CAGA,OAA2B;EACzB,OAAO,KAAK,MAAM,KAAK,MAAM,KAAK,OAAO,KAAK,OAAO,KAAA;CACvD;;CAGA,OAA2B;EACzB,OAAO,KAAK,MAAM,KAAK,MAAM,KAAK,OAAO,KAAK,SAAS,KA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AI,SAAS,MAAM,UAAU,GAAG,QAAQ;EACxC,IAAI,CAAC,OAAO,OAAO;EACnB,IAAI,SAAS,KAAK,OAAO,MAAM,EAAE,CAAC;EAClC,OAAO;CACT;;CAGA,OAAO,GAAiB;EACtB,KAAK,OAAO;CACd;;CAGA,UAAkB;EAChB,OAAO,KAAK,OAAO,MAAM,KAAK,OAAO,KAAK,GAAG;CAC/C;;CAGA,cAAsB;EACpB,IAAI,MAAM;EACV,KAAK,MAAM,MAAM,KAAK,QACpB,IAAI,OAAO,KAAK;OACX,IAAI,OAAO,KAAM,OAAO,KAAK,MAAM,MAAM,KAAK,OAAO,IAAI,KAAK,KAAK;OACnE;EAEP,OAAO;CACT;;CAGA,OAAe;EACb,OAAO,KAAK,OAAO,MAAM,KAAK,GAAG;CACnC;AACF;AAEA,IAAM,QAAQ;AAEd,IAAa,WAAW,OAAe,OAAO,OAAO,OAAO"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { languageFor, lineParts, tokenizeLine } from "./language.js";
|
|
2
|
+
// src/language/to-html.ts
|
|
3
|
+
var escape = (text) => text.replace(/[&<>"]/g, (ch) => ch === "&" ? "&" : ch === "<" ? "<" : ch === ">" ? ">" : """);
|
|
4
|
+
/**
|
|
5
|
+
* Highlights a whole text as HTML: one `<span class="line">` per line,
|
|
6
|
+
* tokens as `<span class="dou-syntax-…">`, the same classes and colors as
|
|
7
|
+
* the code editor.
|
|
8
|
+
*/
|
|
9
|
+
function highlightToHtml(code, language, tabSize = 2) {
|
|
10
|
+
const state = language.startState();
|
|
11
|
+
return code.split(/\r\n?|\n/).map((text) => {
|
|
12
|
+
const tokens = tokenizeLine(language, text, state, tabSize);
|
|
13
|
+
let html = "";
|
|
14
|
+
for (const [part, type] of lineParts(text, tokens)) html += type ? `<span class="dou-syntax-${type}">${escape(part)}</span>` : escape(part);
|
|
15
|
+
return `<span class="line">${html}</span>`;
|
|
16
|
+
}).join("\n");
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A highlighter for `DouCodeBlock` (its `highlight` prop, or once for the
|
|
20
|
+
* whole app in the ConfigProvider's `defaults`) that knows `languages`.
|
|
21
|
+
* A block in another language shows as plain text.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* const highlight = createCodeHighlighter([javascript, typescript, css])
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
function createCodeHighlighter(languages) {
|
|
28
|
+
return (code, lang) => {
|
|
29
|
+
const language = languageFor(languages, lang);
|
|
30
|
+
if (language) return highlightToHtml(code, language);
|
|
31
|
+
return code.split(/\r\n?|\n/).map((text) => `<span class="line">${escape(text)}</span>`).join("\n");
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export { createCodeHighlighter, highlightToHtml };
|
|
35
|
+
|
|
36
|
+
//# sourceMappingURL=to-html.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to-html.js","names":[],"sources":["../../src/language/to-html.ts"],"sourcesContent":["import { type CodeLanguage, languageFor, lineParts, tokenizeLine } from './language'\n\nconst escape = (text: string) =>\n text.replace(/[&<>\"]/g, (ch) =>\n ch === '&' ? '&' : ch === '<' ? '<' : ch === '>' ? '>' : '"',\n )\n\n/**\n * Highlights a whole text as HTML: one `<span class=\"line\">` per line,\n * tokens as `<span class=\"dou-syntax-…\">`, the same classes and colors as\n * the code editor.\n */\nexport function highlightToHtml(code: string, language: CodeLanguage, tabSize = 2): string {\n const state = language.startState()\n return code\n .split(/\\r\\n?|\\n/)\n .map((text) => {\n const tokens = tokenizeLine(language, text, state, tabSize)\n let html = ''\n for (const [part, type] of lineParts(text, tokens))\n html += type ? `<span class=\"dou-syntax-${type}\">${escape(part)}</span>` : escape(part)\n return `<span class=\"line\">${html}</span>`\n })\n .join('\\n')\n}\n\n/**\n * A highlighter for `DouCodeBlock` (its `highlight` prop, or once for the\n * whole app in the ConfigProvider's `defaults`) that knows `languages`.\n * A block in another language shows as plain text.\n *\n * ```ts\n * const highlight = createCodeHighlighter([javascript, typescript, css])\n * ```\n */\nexport function createCodeHighlighter(languages: readonly CodeLanguage[]) {\n return (code: string, lang: string): string => {\n const language = languageFor(languages, lang)\n if (language) return highlightToHtml(code, language)\n return code\n .split(/\\r\\n?|\\n/)\n .map((text) => `<span class=\"line\">${escape(text)}</span>`)\n .join('\\n')\n }\n}\n"],"mappings":";;AAEA,IAAM,UAAU,SACd,KAAK,QAAQ,YAAY,OACvB,OAAO,MAAM,UAAU,OAAO,MAAM,SAAS,OAAO,MAAM,SAAS,QACrE;;;;;;AAOF,SAAgB,gBAAgB,MAAc,UAAwB,UAAU,GAAW;CACzF,MAAM,QAAQ,SAAS,WAAW;CAClC,OAAO,KACJ,MAAM,UAAU,CAAC,CACjB,KAAK,SAAS;EACb,MAAM,SAAS,aAAa,UAAU,MAAM,OAAO,OAAO;EAC1D,IAAI,OAAO;EACX,KAAK,MAAM,CAAC,MAAM,SAAS,UAAU,MAAM,MAAM,GAC/C,QAAQ,OAAO,2BAA2B,KAAK,IAAI,OAAO,IAAI,EAAE,WAAW,OAAO,IAAI;EACxF,OAAO,sBAAsB,KAAK;CACpC,CAAC,CAAC,CACD,KAAK,IAAI;AACd;;;;;;;;;;AAWA,SAAgB,sBAAsB,WAAoC;CACxE,QAAQ,MAAc,SAAyB;EAC7C,MAAM,WAAW,YAAY,WAAW,IAAI;EAC5C,IAAI,UAAU,OAAO,gBAAgB,MAAM,QAAQ;EACnD,OAAO,KACJ,MAAM,UAAU,CAAC,CACjB,KAAK,SAAS,sBAAsB,OAAO,IAAI,EAAE,QAAQ,CAAC,CAC1D,KAAK,IAAI;CACd;AACF"}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { decodeState, languagesIn } from "./worker-protocol.js";
|
|
2
|
+
// src/language/worker-client.ts
|
|
3
|
+
var nextId = 1;
|
|
4
|
+
/** Lines sent to the worker in one message, so sending a long text never holds up the page long. */
|
|
5
|
+
var PIECE = 2e4;
|
|
6
|
+
/**
|
|
7
|
+
* The editor's side of a highlighting worker (`serveCodeHighlighting`).
|
|
8
|
+
* The text goes over the first time states far ahead are wanted (most
|
|
9
|
+
* texts never need it), in pieces between other work; every change
|
|
10
|
+
* follows it.
|
|
11
|
+
*/
|
|
12
|
+
var SyntaxWorkerClient = class {
|
|
13
|
+
worker;
|
|
14
|
+
language;
|
|
15
|
+
text;
|
|
16
|
+
tabSize;
|
|
17
|
+
handlers;
|
|
18
|
+
id = nextId++;
|
|
19
|
+
/** Changes sent so far; the worker's answers name how many it had taken in. */
|
|
20
|
+
version = 0;
|
|
21
|
+
asking = false;
|
|
22
|
+
closed = false;
|
|
23
|
+
/** Sending the text: messages wait until it is all there. */
|
|
24
|
+
sending = false;
|
|
25
|
+
opened = false;
|
|
26
|
+
queue = [];
|
|
27
|
+
timer = null;
|
|
28
|
+
languages;
|
|
29
|
+
constructor(worker, language, text, tabSize, handlers) {
|
|
30
|
+
this.worker = worker;
|
|
31
|
+
this.language = language;
|
|
32
|
+
this.text = text;
|
|
33
|
+
this.tabSize = tabSize;
|
|
34
|
+
this.handlers = handlers;
|
|
35
|
+
this.languages = languagesIn(language);
|
|
36
|
+
worker.addEventListener("error", this.fail);
|
|
37
|
+
}
|
|
38
|
+
fail = (event) => {
|
|
39
|
+
if (this.closed) return;
|
|
40
|
+
this.dispose();
|
|
41
|
+
this.handlers.failed(event.error ?? event.message ?? event);
|
|
42
|
+
};
|
|
43
|
+
post(message) {
|
|
44
|
+
if (this.sending) {
|
|
45
|
+
this.queue.push(message);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
this.worker.postMessage(message);
|
|
49
|
+
}
|
|
50
|
+
open() {
|
|
51
|
+
this.opened = true;
|
|
52
|
+
this.worker.addEventListener("message", this.listen);
|
|
53
|
+
const nested = [...this.languages.keys()].filter((name) => name !== this.language.name);
|
|
54
|
+
this.post({
|
|
55
|
+
t: "open",
|
|
56
|
+
id: this.id,
|
|
57
|
+
language: this.language.name,
|
|
58
|
+
nested,
|
|
59
|
+
tabSize: this.tabSize
|
|
60
|
+
});
|
|
61
|
+
this.sending = true;
|
|
62
|
+
const doc = this.text();
|
|
63
|
+
let from = 1;
|
|
64
|
+
const next = () => {
|
|
65
|
+
this.timer = null;
|
|
66
|
+
if (this.closed) return;
|
|
67
|
+
const to = Math.min(doc.lines, from + PIECE - 1);
|
|
68
|
+
const done = to >= doc.lines;
|
|
69
|
+
this.worker.postMessage({
|
|
70
|
+
t: "text",
|
|
71
|
+
id: this.id,
|
|
72
|
+
lines: doc.lineTexts(from, to),
|
|
73
|
+
done
|
|
74
|
+
});
|
|
75
|
+
from = to + 1;
|
|
76
|
+
if (!done) {
|
|
77
|
+
this.timer = setTimeout(next, 0);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.sending = false;
|
|
81
|
+
const queue = this.queue;
|
|
82
|
+
this.queue = [];
|
|
83
|
+
for (const message of queue) this.post(message);
|
|
84
|
+
};
|
|
85
|
+
next();
|
|
86
|
+
}
|
|
87
|
+
listen = (event) => {
|
|
88
|
+
const message = event.data;
|
|
89
|
+
if (this.closed || !message || message.id !== this.id) return;
|
|
90
|
+
if (message.t === "unsupported") {
|
|
91
|
+
this.dispose();
|
|
92
|
+
this.handlers.unsupported(message.missing);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (message.t !== "states") return;
|
|
96
|
+
this.asking = false;
|
|
97
|
+
if (message.version !== this.version) return;
|
|
98
|
+
const points = [];
|
|
99
|
+
for (const [line, state] of message.points) {
|
|
100
|
+
const decoded = decodeState(state, (name) => this.languages.get(name));
|
|
101
|
+
if (decoded === void 0) {
|
|
102
|
+
this.dispose();
|
|
103
|
+
this.handlers.unsupported();
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
points.push([line, decoded]);
|
|
107
|
+
}
|
|
108
|
+
this.handlers.states(points);
|
|
109
|
+
};
|
|
110
|
+
/** Passes on a change made to the text, once the worker has it. */
|
|
111
|
+
change(changes) {
|
|
112
|
+
if (this.closed || !this.opened || changes.empty) return;
|
|
113
|
+
this.version++;
|
|
114
|
+
this.post({
|
|
115
|
+
t: "change",
|
|
116
|
+
id: this.id,
|
|
117
|
+
changes: changes.changes.map((change) => [
|
|
118
|
+
change.from,
|
|
119
|
+
change.to,
|
|
120
|
+
change.insert
|
|
121
|
+
]),
|
|
122
|
+
length: changes.length
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/** Asks for the states from line index `from` on, once the worker has read up to `until`. */
|
|
126
|
+
want(from, until) {
|
|
127
|
+
if (this.closed || this.asking) return;
|
|
128
|
+
if (!this.opened) this.open();
|
|
129
|
+
this.asking = true;
|
|
130
|
+
this.post({
|
|
131
|
+
t: "read",
|
|
132
|
+
id: this.id,
|
|
133
|
+
from,
|
|
134
|
+
until
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
dispose() {
|
|
138
|
+
if (this.closed) return;
|
|
139
|
+
this.closed = true;
|
|
140
|
+
if (this.timer) clearTimeout(this.timer);
|
|
141
|
+
this.worker.removeEventListener("error", this.fail);
|
|
142
|
+
if (!this.opened) return;
|
|
143
|
+
this.worker.removeEventListener("message", this.listen);
|
|
144
|
+
this.sending = false;
|
|
145
|
+
this.queue = [];
|
|
146
|
+
this.post({
|
|
147
|
+
t: "close",
|
|
148
|
+
id: this.id
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
/** DouCodeEditor's own worker, with the built-in languages: one for all editors, ended when none uses it. */
|
|
153
|
+
var own = null;
|
|
154
|
+
async function ownWorker() {
|
|
155
|
+
const { default: CodeWorker } = await import("../worker/default.js");
|
|
156
|
+
if (!own) {
|
|
157
|
+
const entry = {
|
|
158
|
+
worker: new CodeWorker(),
|
|
159
|
+
users: 0
|
|
160
|
+
};
|
|
161
|
+
entry.worker.addEventListener("error", () => own === entry && (own = null));
|
|
162
|
+
own = entry;
|
|
163
|
+
}
|
|
164
|
+
const entry = own;
|
|
165
|
+
entry.users++;
|
|
166
|
+
let released = false;
|
|
167
|
+
return {
|
|
168
|
+
worker: entry.worker,
|
|
169
|
+
release() {
|
|
170
|
+
if (released) return;
|
|
171
|
+
released = true;
|
|
172
|
+
if (--entry.users > 0) return;
|
|
173
|
+
entry.worker.terminate();
|
|
174
|
+
if (own === entry) own = null;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/** Editors using each worker made by a `worker` function: one handed to several ends with the last. */
|
|
179
|
+
var holders = /* @__PURE__ */ new Map();
|
|
180
|
+
/** Counts an editor using `worker`; the returned function lets go, ending the worker after the last. */
|
|
181
|
+
function holdWorker(worker) {
|
|
182
|
+
holders.set(worker, (holders.get(worker) ?? 0) + 1);
|
|
183
|
+
let released = false;
|
|
184
|
+
return () => {
|
|
185
|
+
if (released) return;
|
|
186
|
+
released = true;
|
|
187
|
+
const left = holders.get(worker) - 1;
|
|
188
|
+
if (left > 0) {
|
|
189
|
+
holders.set(worker, left);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
holders.delete(worker);
|
|
193
|
+
worker.terminate();
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
export { SyntaxWorkerClient, holdWorker, ownWorker };
|
|
197
|
+
|
|
198
|
+
//# sourceMappingURL=worker-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-client.js","names":[],"sources":["../../src/language/worker-client.ts"],"sourcesContent":["import type { ChangeSet } from '../core/changes'\nimport type { CodeText } from '../core/text'\nimport type { CodeLanguage } from './language'\nimport { decodeState, type FromWorker, languagesIn, type ToWorker } from './worker-protocol'\n\nlet nextId = 1\n\n/** Lines sent to the worker in one message, so sending a long text never holds up the page long. */\nconst PIECE = 20_000\n\n/**\n * The editor's side of a highlighting worker (`serveCodeHighlighting`).\n * The text goes over the first time states far ahead are wanted (most\n * texts never need it), in pieces between other work; every change\n * follows it.\n */\nexport class SyntaxWorkerClient {\n private readonly id = nextId++\n /** Changes sent so far; the worker's answers name how many it had taken in. */\n private version = 0\n private asking = false\n private closed = false\n /** Sending the text: messages wait until it is all there. */\n private sending = false\n private opened = false\n private queue: ToWorker[] = []\n private timer: ReturnType<typeof setTimeout> | null = null\n private readonly languages: Map<string, CodeLanguage>\n\n constructor(\n private readonly worker: Worker,\n private readonly language: CodeLanguage,\n /** The text as it is now. */\n private readonly text: () => CodeText,\n private readonly tabSize: number,\n private readonly handlers: {\n states: (points: Array<[line: number, state: unknown]>) => void\n /** The worker hasn't the language named `missing`, or can't send its states (no `missing`). */\n unsupported: (missing?: string) => void\n /** The worker could not run (it failed to load, or threw). */\n failed: (error: unknown) => void\n },\n ) {\n this.languages = languagesIn(language)\n worker.addEventListener('error', this.fail)\n }\n\n private fail = (event: Event) => {\n if (this.closed) return\n this.dispose()\n this.handlers.failed((event as ErrorEvent).error ?? (event as ErrorEvent).message ?? event)\n }\n\n private post(message: ToWorker) {\n if (this.sending) {\n this.queue.push(message)\n return\n }\n // oxlint-disable-next-line unicorn/require-post-message-target-origin -- a worker takes no origin\n this.worker.postMessage(message)\n }\n\n private open() {\n this.opened = true\n this.worker.addEventListener('message', this.listen)\n const nested = [...this.languages.keys()].filter((name) => name !== this.language.name)\n this.post({\n t: 'open',\n id: this.id,\n language: this.language.name,\n nested,\n tabSize: this.tabSize,\n })\n this.sending = true\n const doc = this.text()\n let from = 1\n const next = () => {\n this.timer = null\n if (this.closed) return\n const to = Math.min(doc.lines, from + PIECE - 1)\n const done = to >= doc.lines\n // oxlint-disable-next-line unicorn/require-post-message-target-origin -- a worker takes no origin\n this.worker.postMessage({ t: 'text', id: this.id, lines: doc.lineTexts(from, to), done })\n from = to + 1\n if (!done) {\n this.timer = setTimeout(next, 0)\n return\n }\n this.sending = false\n const queue = this.queue\n this.queue = []\n for (const message of queue) this.post(message)\n }\n next()\n }\n\n private listen = (event: MessageEvent) => {\n const message = event.data as FromWorker\n if (this.closed || !message || message.id !== this.id) return\n if (message.t === 'unsupported') {\n this.dispose()\n this.handlers.unsupported(message.missing)\n return\n }\n if (message.t !== 'states') return\n this.asking = false\n // Changed since it was asked: what it read is for another text; asking again gets the new one.\n if (message.version !== this.version) return\n const points: Array<[number, unknown]> = []\n for (const [line, state] of message.points) {\n const decoded = decodeState(state, (name) => this.languages.get(name))\n if (decoded === undefined) {\n this.dispose()\n this.handlers.unsupported()\n return\n }\n points.push([line, decoded])\n }\n this.handlers.states(points)\n }\n\n /** Passes on a change made to the text, once the worker has it. */\n change(changes: ChangeSet): void {\n if (this.closed || !this.opened || changes.empty) return\n this.version++\n this.post({\n t: 'change',\n id: this.id,\n changes: changes.changes.map((change) => [change.from, change.to, change.insert]),\n length: changes.length,\n })\n }\n\n /** Asks for the states from line index `from` on, once the worker has read up to `until`. */\n want(from: number, until: number): void {\n if (this.closed || this.asking) return\n if (!this.opened) this.open()\n this.asking = true\n this.post({ t: 'read', id: this.id, from, until })\n }\n\n dispose(): void {\n if (this.closed) return\n this.closed = true\n if (this.timer) clearTimeout(this.timer)\n this.worker.removeEventListener('error', this.fail)\n if (!this.opened) return\n this.worker.removeEventListener('message', this.listen)\n this.sending = false\n this.queue = []\n this.post({ t: 'close', id: this.id })\n }\n}\n\n/** DouCodeEditor's own worker, with the built-in languages: one for all editors, ended when none uses it. */\nlet own: { worker: Worker; users: number } | null = null\n\nexport async function ownWorker(): Promise<{ worker: Worker; release: () => void }> {\n const { default: CodeWorker } = await import('../worker/default?worker&inline')\n if (!own) {\n const entry = { worker: new CodeWorker(), users: 0 }\n // a worker that failed isn't handed out again: the next editor starts another\n entry.worker.addEventListener('error', () => own === entry && (own = null))\n own = entry\n }\n const entry = own\n entry.users++\n let released = false\n return {\n worker: entry.worker,\n release() {\n if (released) return\n released = true\n if (--entry.users > 0) return\n entry.worker.terminate()\n if (own === entry) own = null\n },\n }\n}\n\n/** Editors using each worker made by a `worker` function: one handed to several ends with the last. */\nconst holders = new Map<Worker, number>()\n\n/** Counts an editor using `worker`; the returned function lets go, ending the worker after the last. */\nexport function holdWorker(worker: Worker): () => void {\n holders.set(worker, (holders.get(worker) ?? 0) + 1)\n let released = false\n return () => {\n if (released) return\n released = true\n const left = holders.get(worker)! - 1\n if (left > 0) {\n holders.set(worker, left)\n return\n }\n holders.delete(worker)\n worker.terminate()\n }\n}\n"],"mappings":";;AAKA,IAAI,SAAS;;AAGb,IAAM,QAAQ;;;;;;;AAQd,IAAa,qBAAb,MAAgC;CAcX;CACA;CAEA;CACA;CACA;CAlBnB,KAAsB;;CAEtB,UAAkB;CAClB,SAAiB;CACjB,SAAiB;;CAEjB,UAAkB;CAClB,SAAiB;CACjB,QAA4B,CAAC;CAC7B,QAAsD;CACtD;CAEA,YACE,QACA,UAEA,MACA,SACA,UAOA;EAZiB,KAAA,SAAA;EACA,KAAA,WAAA;EAEA,KAAA,OAAA;EACA,KAAA,UAAA;EACA,KAAA,WAAA;EAQjB,KAAK,YAAY,YAAY,QAAQ;EACrC,OAAO,iBAAiB,SAAS,KAAK,IAAI;CAC5C;CAEA,QAAgB,UAAiB;EAC/B,IAAI,KAAK,QAAQ;EACjB,KAAK,QAAQ;EACb,KAAK,SAAS,OAAQ,MAAqB,SAAU,MAAqB,WAAW,KAAK;CAC5F;CAEA,KAAa,SAAmB;EAC9B,IAAI,KAAK,SAAS;GAChB,KAAK,MAAM,KAAK,OAAO;GACvB;EACF;EAEA,KAAK,OAAO,YAAY,OAAO;CACjC;CAEA,OAAe;EACb,KAAK,SAAS;EACd,KAAK,OAAO,iBAAiB,WAAW,KAAK,MAAM;EACnD,MAAM,SAAS,CAAC,GAAG,KAAK,UAAU,KAAK,CAAC,CAAC,CAAC,QAAQ,SAAS,SAAS,KAAK,SAAS,IAAI;EACtF,KAAK,KAAK;GACR,GAAG;GACH,IAAI,KAAK;GACT,UAAU,KAAK,SAAS;GACxB;GACA,SAAS,KAAK;EAChB,CAAC;EACD,KAAK,UAAU;EACf,MAAM,MAAM,KAAK,KAAK;EACtB,IAAI,OAAO;EACX,MAAM,aAAa;GACjB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AI,WAAW;CACf,aAAa;EACX,IAAI,UAAU;EACd,WAAW;EACX,MAAM,OAAO,QAAQ,IAAI,MAAM,IAAK;EACpC,IAAI,OAAO,GAAG;GACZ,QAAQ,IAAI,QAAQ,IAAI;GACxB;EACF;EACA,QAAQ,OAAO,MAAM;EACrB,OAAO,UAAU;CACnB;AACF"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// src/language/worker-protocol.ts
|
|
2
|
+
var LANGUAGE = "$codeLanguage";
|
|
3
|
+
var isLanguage = (value) => typeof value.token === "function" && typeof value.startState === "function";
|
|
4
|
+
/** A state as plain data, its languages replaced by their names. */
|
|
5
|
+
function encodeState(value) {
|
|
6
|
+
if (typeof value !== "object" || value === null) return value;
|
|
7
|
+
if (Array.isArray(value)) return value.map(encodeState);
|
|
8
|
+
if (isLanguage(value)) return { [LANGUAGE]: value.name };
|
|
9
|
+
const out = {};
|
|
10
|
+
for (const key in value) out[key] = encodeState(value[key]);
|
|
11
|
+
return out;
|
|
12
|
+
}
|
|
13
|
+
/** A state back from plain data; `undefined` when it names a language `find` doesn't know. */
|
|
14
|
+
function decodeState(value, find) {
|
|
15
|
+
if (typeof value !== "object" || value === null) return value;
|
|
16
|
+
if (Array.isArray(value)) {
|
|
17
|
+
const out = [];
|
|
18
|
+
for (const item of value) {
|
|
19
|
+
const decoded = decodeState(item, find);
|
|
20
|
+
if (decoded === void 0 && item !== void 0) return void 0;
|
|
21
|
+
out.push(decoded);
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
const record = value;
|
|
26
|
+
if (typeof record[LANGUAGE] === "string") return find(record[LANGUAGE]);
|
|
27
|
+
const out = {};
|
|
28
|
+
for (const key in record) {
|
|
29
|
+
const decoded = decodeState(record[key], find);
|
|
30
|
+
if (decoded === void 0 && record[key] !== void 0) return void 0;
|
|
31
|
+
out[key] = decoded;
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
/** A language and every language it may hold, by name. */
|
|
36
|
+
function languagesIn(language) {
|
|
37
|
+
const found = /* @__PURE__ */ new Map();
|
|
38
|
+
const visit = (item) => {
|
|
39
|
+
if (found.has(item.name)) return;
|
|
40
|
+
found.set(item.name, item);
|
|
41
|
+
for (const inner of item.nested ?? []) visit(inner);
|
|
42
|
+
};
|
|
43
|
+
visit(language);
|
|
44
|
+
return found;
|
|
45
|
+
}
|
|
46
|
+
export { decodeState, encodeState, languagesIn };
|
|
47
|
+
|
|
48
|
+
//# sourceMappingURL=worker-protocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-protocol.js","names":[],"sources":["../../src/language/worker-protocol.ts"],"sourcesContent":["import type { CodeLanguage } from './language'\n\n/*\n * Messages between the editor and a highlighting worker, and how states\n * cross over: languages inside a state (the script in an HTML file) go by\n * name, everything else as plain data.\n */\n\n/** A change as sent: `[from, to, insert]`. */\nexport type WireChange = [from: number, to: number, insert: string]\n\nexport type ToWorker =\n /** `nested`: the names of the languages it may hold, which the worker's language must hold too. */\n | { t: 'open'; id: number; language: string; nested: string[]; tabSize: number }\n /** The text, in pieces of whole lines; `done` on the last. */\n | { t: 'text'; id: number; lines: string[]; done: boolean }\n | { t: 'change'; id: number; changes: WireChange[]; length: number }\n | { t: 'read'; id: number; from: number; until: number }\n | { t: 'close'; id: number }\n\nexport type FromWorker =\n | { t: 'states'; id: number; version: number; points: Array<[line: number, state: unknown]> }\n /** The worker hasn't the language named `missing`, or (without it) can't send its states. */\n | { t: 'unsupported'; id: number; missing?: string }\n\nconst LANGUAGE = '$codeLanguage'\n\nconst isLanguage = (value: object): value is CodeLanguage =>\n typeof (value as CodeLanguage).token === 'function' &&\n typeof (value as CodeLanguage).startState === 'function'\n\n/** A state as plain data, its languages replaced by their names. */\nexport function encodeState(value: unknown): unknown {\n if (typeof value !== 'object' || value === null) return value\n if (Array.isArray(value)) return value.map(encodeState)\n if (isLanguage(value)) return { [LANGUAGE]: value.name }\n const out: Record<string, unknown> = {}\n for (const key in value) out[key] = encodeState((value as Record<string, unknown>)[key])\n return out\n}\n\n/** A state back from plain data; `undefined` when it names a language `find` doesn't know. */\nexport function decodeState(\n value: unknown,\n find: (name: string) => CodeLanguage | undefined,\n): unknown {\n if (typeof value !== 'object' || value === null) return value\n if (Array.isArray(value)) {\n const out: unknown[] = []\n for (const item of value) {\n const decoded = decodeState(item, find)\n if (decoded === undefined && item !== undefined) return undefined\n out.push(decoded)\n }\n return out\n }\n const record = value as Record<string, unknown>\n if (typeof record[LANGUAGE] === 'string') return find(record[LANGUAGE])\n const out: Record<string, unknown> = {}\n for (const key in record) {\n const decoded = decodeState(record[key], find)\n if (decoded === undefined && record[key] !== undefined) return undefined\n out[key] = decoded\n }\n return out\n}\n\n/** A language and every language it may hold, by name. */\nexport function languagesIn(language: CodeLanguage): Map<string, CodeLanguage> {\n const found = new Map<string, CodeLanguage>()\n const visit = (item: CodeLanguage) => {\n if (found.has(item.name)) return\n found.set(item.name, item)\n for (const inner of item.nested ?? []) visit(inner)\n }\n visit(language)\n return found\n}\n"],"mappings":";AAyBA,IAAM,WAAW;AAEjB,IAAM,cAAc,UAClB,OAAQ,MAAuB,UAAU,cACzC,OAAQ,MAAuB,eAAe;;AAGhD,SAAgB,YAAY,OAAyB;CACnD,IAAI,OAAO,UAAU,YAAY,UAAU,MAAM,OAAO;CACxD,IAAI,MAAM,QAAQ,KAAK,GAAG,OAAO,MAAM,IAAI,WAAW;CACtD,IAAI,WAAW,KAAK,GAAG,OAAO,GAAG,WAAW,MAAM,KAAK;CACvD,MAAM,MAA+B,CAAC;CACtC,KAAK,MAAM,OAAO,OAAO,IAAI,OAAO,YAAa,MAAkC,IAAI;CACvF,OAAO;AACT;;AAGA,SAAgB,YACd,OACA,MACS;CACT,IAAI,OAAO,UAAU,YAAY,UAAU,MAAM,OAAO;CACxD,IAAI,MAAM,QAAQ,KAAK,GAAG;EACxB,MAAM,MAAiB,CAAC;EACxB,KAAK,MAAM,QAAQ,OAAO;GACxB,MAAM,UAAU,YAAY,MAAM,IAAI;GACtC,IAAI,YAAY,KAAA,KAAa,SAAS,KAAA,GAAW,OAAO,KAAA;GACxD,IAAI,KAAK,OAAO;EAClB;EACA,OAAO;CACT;CACA,MAAM,SAAS;CACf,IAAI,OAAO,OAAO,cAAc,UAAU,OAAO,KAAK,OAAO,SAAS;CACtE,MAAM,MAA+B,CAAC;CACtC,KAAK,MAAM,OAAO,QAAQ;EACxB,MAAM,UAAU,YAAY,OAAO,MAAM,IAAI;EAC7C,IAAI,YAAY,KAAA,KAAa,OAAO,SAAS,KAAA,GAAW,OAAO,KAAA;EAC/D,IAAI,OAAO;CACb;CACA,OAAO;AACT;;AAGA,SAAgB,YAAY,UAAmD;CAC7E,MAAM,wBAAQ,IAAI,IAA0B;CAC5C,MAAM,SAAS,SAAuB;EACpC,IAAI,MAAM,IAAI,KAAK,IAAI,GAAG;EAC1B,MAAM,IAAI,KAAK,MAAM,IAAI;EACzB,KAAK,MAAM,SAAS,KAAK,UAAU,CAAC,GAAG,MAAM,KAAK;CACpD;CACA,MAAM,QAAQ;CACd,OAAO;AACT"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { defineCodeLanguage } from "../language/language.js";
|
|
2
|
+
// src/languages/css.ts
|
|
3
|
+
/**
|
|
4
|
+
* Whether a name at the cursor inside a block is a property: `name:` with
|
|
5
|
+
* a `;`, a `}` or the line's end before any `{` (`a:hover {` is a nested
|
|
6
|
+
* selector).
|
|
7
|
+
*/
|
|
8
|
+
var isProperty = (stream) => !!stream.match(/^[\w-]+\s*:(?!:)[^{;}]*(?:[;}]|$)/, false);
|
|
9
|
+
function readString(stream, quote) {
|
|
10
|
+
let escaped = false;
|
|
11
|
+
let ch;
|
|
12
|
+
while ((ch = stream.next()) !== void 0) {
|
|
13
|
+
if (ch === quote && !escaped) return;
|
|
14
|
+
escaped = !escaped && ch === "\\";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** Makes CSS or a nesting dialect such as SCSS (`//` comments, `$` variables). */
|
|
18
|
+
function createCss(name, aliases, scss = false) {
|
|
19
|
+
return defineCodeLanguage({
|
|
20
|
+
name,
|
|
21
|
+
aliases,
|
|
22
|
+
startState: () => ({
|
|
23
|
+
comment: false,
|
|
24
|
+
depth: 0,
|
|
25
|
+
value: false,
|
|
26
|
+
parens: 0,
|
|
27
|
+
at: false
|
|
28
|
+
}),
|
|
29
|
+
copyState: (state) => ({ ...state }),
|
|
30
|
+
sameState: (a, b) => a.comment === b.comment && a.depth === b.depth && a.value === b.value && a.parens === b.parens && a.at === b.at,
|
|
31
|
+
token(stream, state) {
|
|
32
|
+
if (state.comment || stream.match("/*")) {
|
|
33
|
+
state.comment = !stream.skipTo("*/");
|
|
34
|
+
if (state.comment) stream.skipToEnd();
|
|
35
|
+
else stream.pos += 2;
|
|
36
|
+
return "comment";
|
|
37
|
+
}
|
|
38
|
+
if (stream.eatSpace()) return null;
|
|
39
|
+
if (scss && stream.match("//")) {
|
|
40
|
+
stream.skipToEnd();
|
|
41
|
+
return "comment";
|
|
42
|
+
}
|
|
43
|
+
const ch = stream.peek();
|
|
44
|
+
if (ch === "\"" || ch === "'") {
|
|
45
|
+
stream.next();
|
|
46
|
+
readString(stream, ch);
|
|
47
|
+
return "string";
|
|
48
|
+
}
|
|
49
|
+
if (ch === "@") {
|
|
50
|
+
stream.next();
|
|
51
|
+
stream.match(/^[\w-]+/);
|
|
52
|
+
state.at = true;
|
|
53
|
+
return "keyword";
|
|
54
|
+
}
|
|
55
|
+
if (ch === "{") {
|
|
56
|
+
stream.next();
|
|
57
|
+
state.depth++;
|
|
58
|
+
state.value = false;
|
|
59
|
+
state.at = false;
|
|
60
|
+
return "punctuation";
|
|
61
|
+
}
|
|
62
|
+
if (ch === "}") {
|
|
63
|
+
stream.next();
|
|
64
|
+
state.depth = Math.max(0, state.depth - 1);
|
|
65
|
+
state.value = false;
|
|
66
|
+
return "punctuation";
|
|
67
|
+
}
|
|
68
|
+
if (ch === ";") {
|
|
69
|
+
stream.next();
|
|
70
|
+
state.value = false;
|
|
71
|
+
state.at = false;
|
|
72
|
+
return "punctuation";
|
|
73
|
+
}
|
|
74
|
+
if (ch === "(") {
|
|
75
|
+
stream.next();
|
|
76
|
+
state.parens++;
|
|
77
|
+
return "punctuation";
|
|
78
|
+
}
|
|
79
|
+
if (ch === ")") {
|
|
80
|
+
stream.next();
|
|
81
|
+
state.parens = Math.max(0, state.parens - 1);
|
|
82
|
+
return "punctuation";
|
|
83
|
+
}
|
|
84
|
+
if (stream.match(/^--[\w-]*/) || scss && stream.match(/^\$[\w-]+/)) {
|
|
85
|
+
if (!state.value && stream.match(/^\s*:/, false)) return "property";
|
|
86
|
+
return "variable";
|
|
87
|
+
}
|
|
88
|
+
if (state.value || state.at || state.parens) {
|
|
89
|
+
if (stream.match(/^#[\da-fA-F]{3,8}\b/)) return "number";
|
|
90
|
+
if (stream.match(/^[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?(?:%|[a-zA-Z]+)?/)) return "number";
|
|
91
|
+
if (stream.match("!important")) return "keyword";
|
|
92
|
+
if (stream.match(/^[\w-]+(?=\()/)) return "function";
|
|
93
|
+
if (stream.match(/^[\w-]+/)) {
|
|
94
|
+
if (state.at && /^(?:and|or|not|only|screen|print|all)$/.test(stream.current())) return "keyword";
|
|
95
|
+
return state.value ? "variable" : state.at ? "variable" : "property";
|
|
96
|
+
}
|
|
97
|
+
} else {
|
|
98
|
+
if (state.depth > 0 && isProperty(stream)) {
|
|
99
|
+
stream.match(/^[\w-]+/);
|
|
100
|
+
return "property";
|
|
101
|
+
}
|
|
102
|
+
if (stream.match(/^[.#][\w-]+/)) return "type";
|
|
103
|
+
if (stream.match(/^::?[\w-]+/)) return "keyword";
|
|
104
|
+
if (stream.match(/^\[[^\]]*\]?/)) return "attribute";
|
|
105
|
+
if (stream.match(/^[a-zA-Z][\w-]*/) || stream.eat("*")) return "tag";
|
|
106
|
+
if (scss && stream.eat("&")) return "keyword";
|
|
107
|
+
}
|
|
108
|
+
if (ch === ":") {
|
|
109
|
+
stream.next();
|
|
110
|
+
if (state.depth > 0 && !state.at) state.value = true;
|
|
111
|
+
return "punctuation";
|
|
112
|
+
}
|
|
113
|
+
if (stream.eat(/[,>+~/*=]/)) return "operator";
|
|
114
|
+
stream.next();
|
|
115
|
+
return null;
|
|
116
|
+
},
|
|
117
|
+
comment: scss ? {
|
|
118
|
+
line: "//",
|
|
119
|
+
block: ["/*", "*/"]
|
|
120
|
+
} : { block: ["/*", "*/"] },
|
|
121
|
+
autoClose: "([{'\""
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
var css = createCss("css", []);
|
|
125
|
+
var scss = createCss("scss", ["less", "postcss"], true);
|
|
126
|
+
export { createCss, css, scss };
|
|
127
|
+
|
|
128
|
+
//# sourceMappingURL=css.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"css.js","names":[],"sources":["../../src/languages/css.ts"],"sourcesContent":["import { type CodeLanguage, type CodeTokenType, defineCodeLanguage } from '../language/language'\nimport type { CodeStream } from '../language/stream'\n\nexport interface CssState {\n /** Inside a `/* … *\\/` comment. */\n comment: boolean\n /** Open braces. */\n depth: number\n /** After a property's `:`, before its `;`: reading a value. */\n value: boolean\n /** Open parentheses in a value or selector, e.g. `rgb(…)` or `:is(…)`. */\n parens: number\n /** Inside an at-rule's prelude, e.g. `@media (…)`. */\n at: boolean\n}\n\n/**\n * Whether a name at the cursor inside a block is a property: `name:` with\n * a `;`, a `}` or the line's end before any `{` (`a:hover {` is a nested\n * selector).\n */\nconst isProperty = (stream: CodeStream) =>\n !!stream.match(/^[\\w-]+\\s*:(?!:)[^{;}]*(?:[;}]|$)/, false)\n\nfunction readString(stream: CodeStream, quote: string) {\n let escaped = false\n let ch: string | undefined\n while ((ch = stream.next()) !== undefined) {\n if (ch === quote && !escaped) return\n escaped = !escaped && ch === '\\\\'\n }\n}\n\n/** Makes CSS or a nesting dialect such as SCSS (`//` comments, `$` variables). */\nexport function createCss(name: string, aliases: string[], scss = false): CodeLanguage<CssState> {\n return defineCodeLanguage<CssState>({\n name,\n aliases,\n startState: () => ({ comment: false, depth: 0, value: false, parens: 0, at: false }),\n copyState: (state) => ({ ...state }),\n sameState: (a, b) =>\n a.comment === b.comment &&\n a.depth === b.depth &&\n a.value === b.value &&\n a.parens === b.parens &&\n a.at === b.at,\n token(stream, state): CodeTokenType | null {\n if (state.comment || stream.match('/*')) {\n state.comment = !stream.skipTo('*/')\n if (state.comment) stream.skipToEnd()\n else stream.pos += 2\n return 'comment'\n }\n if (stream.eatSpace()) return null\n if (scss && stream.match('//')) {\n stream.skipToEnd()\n return 'comment'\n }\n const ch = stream.peek()!\n if (ch === '\"' || ch === \"'\") {\n stream.next()\n readString(stream, ch)\n return 'string'\n }\n if (ch === '@') {\n stream.next()\n stream.match(/^[\\w-]+/)\n state.at = true\n return 'keyword'\n }\n if (ch === '{') {\n stream.next()\n state.depth++\n state.value = false\n state.at = false\n return 'punctuation'\n }\n if (ch === '}') {\n stream.next()\n state.depth = Math.max(0, state.depth - 1)\n state.value = false\n return 'punctuation'\n }\n if (ch === ';') {\n stream.next()\n state.value = false\n state.at = false\n return 'punctuation'\n }\n if (ch === '(') {\n stream.next()\n state.parens++\n return 'punctuation'\n }\n if (ch === ')') {\n stream.next()\n state.parens = Math.max(0, state.parens - 1)\n return 'punctuation'\n }\n if (stream.match(/^--[\\w-]*/) || (scss && stream.match(/^\\$[\\w-]+/))) {\n if (!state.value && stream.match(/^\\s*:/, false)) return 'property'\n return 'variable'\n }\n if (state.value || state.at || state.parens) {\n if (stream.match(/^#[\\da-fA-F]{3,8}\\b/)) return 'number'\n if (stream.match(/^[+-]?(?:\\d+\\.?\\d*|\\.\\d+)(?:e[+-]?\\d+)?(?:%|[a-zA-Z]+)?/)) return 'number'\n if (stream.match('!important')) return 'keyword'\n if (stream.match(/^[\\w-]+(?=\\()/)) return 'function'\n if (stream.match(/^[\\w-]+/)) {\n if (state.at && /^(?:and|or|not|only|screen|print|all)$/.test(stream.current()))\n return 'keyword'\n return state.value ? 'variable' : state.at ? 'variable' : 'property'\n }\n } else {\n if (state.depth > 0 && isProperty(stream)) {\n stream.match(/^[\\w-]+/)\n return 'property'\n }\n if (stream.match(/^[.#][\\w-]+/)) return 'type'\n if (stream.match(/^::?[\\w-]+/)) return 'keyword'\n if (stream.match(/^\\[[^\\]]*\\]?/)) return 'attribute'\n if (stream.match(/^[a-zA-Z][\\w-]*/) || stream.eat('*')) return 'tag'\n if (scss && stream.eat('&')) return 'keyword'\n }\n if (ch === ':') {\n stream.next()\n if (state.depth > 0 && !state.at) state.value = true\n return 'punctuation'\n }\n if (stream.eat(/[,>+~/*=]/)) return 'operator'\n stream.next()\n return null\n },\n comment: scss ? { line: '//', block: ['/*', '*/'] } : { block: ['/*', '*/'] },\n autoClose: '([{\\'\"',\n })\n}\n\nexport const css = createCss('css', [])\nexport const scss = createCss('scss', ['less', 'postcss'], true)\n"],"mappings":";;;;;;;AAqBA,IAAM,cAAc,WAClB,CAAC,CAAC,OAAO,MAAM,qCAAqC,KAAK;AAE3D,SAAS,WAAW,QAAoB,OAAe;CACrD,IAAI,UAAU;CACd,IAAI;CACJ,QAAQ,KAAK,OAAO,KAAK,OAAO,KAAA,GAAW;EACzC,IAAI,OAAO,SAAS,CAAC,SAAS;EAC9B,UAAU,CAAC,WAAW,OAAO;CAC/B;AACF;;AAGA,SAAgB,UAAU,MAAc,SAAmB,OAAO,OAA+B;CAC/F,OAAO,mBAA6B;EAClC;EACA;EACA,mBAAmB;GAAE,SAAS;GAAO,OAAO;GAAG,OAAO;GAAO,QAAQ;GAAG,IAAI;EAAM;EAClF,YAAY,WAAW,EAAE,GAAG,MAAM;EAClC,YAAY,GAAG,MACb,EAAE,YAAY,EAAE,WAChB,EAAE,UAAU,EAAE,SACd,EAAE,UAAU,EAAE,SACd,EAAE,WAAW,EAAE,UACf,EAAE,OAAO,EAAE;EACb,MAAM,QAA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,MAAM,qBAAqB,GAAG,OAAO;IAChD,IAAI,OAAO,MAAM,yDAAyD,GAAG,OAAO;IACpF,IAAI,OAAO,MAAM,YAAY,GAAG,OAAO;IACvC,IAAI,OAAO,MAAM,eAAe,GAAG,OAAO;IAC1C,IAAI,OAAO,MAAM,SAAS,GAAG;KAC3B,IAAI,MAAM,MAAM,yCAAyC,KAAK,OAAO,QAAQ,CAAC,GAC5E,OAAO;KACT,OAAO,MAAM,QAAQ,aAAa,MAAM,KAAK,aAAa;IAC5D;GACF,OAAO;IACL,IAAI,MAAM,QAAQ,KAAK,WAAW,MAAM,GAAG;KACzC,OAAO,MAAM,SAAS;KACtB,OAAO;IACT;IACA,IAAI,OAAO,MAAM,aAAa,GAAG,OAAO;IACxC,IAAI,OAAO,MAAM,YAAY,GAAG,OAAO;IACvC,IAAI,OAAO,MAAM,cAAc,GAAG,OAAO;IACzC,IAAI,OAAO,MAAM,iBAAiB,KAAK,OAAO,IAAI,GAAG,GAAG,OAAO;IAC/D,IAAI,QAAQ,OAAO,IAAI,GAAG,GAAG,OAAO;GACtC;GACA,IAAI,OAAO,KAAK;IACd,OAAO,KAAK;IACZ,IAAI,MAAM,QAAQ,KAAK,CAAC,MAAM,IAAI,MAAM,QAAQ;IAChD,OAAO;GACT;GACA,IAAI,OAAO,IAAI,WAAW,GAAG,OAAO;GACpC,OAAO,KAAK;GACZ,OAAO;EACT;EACA,SAAS,OAAO;GAAE,MAAM;GAAM,OAAO,CAAC,MAAM,IAAI;EAAE,IAAI,EAAE,OAAO,CAAC,MAAM,IAAI,EAAE;EAC5E,WAAW;CACb,CAAC;AACH;AAEA,IAAa,MAAM,UAAU,OAAO,CAAC,CAAC;AACtC,IAAa,OAAO,UAAU,QAAQ,CAAC,QAAQ,SAAS,GAAG,IAAI"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { defineCodeLanguage } from "../language/language.js";
|
|
2
|
+
import { javascript, typescript } from "./javascript.js";
|
|
3
|
+
import { css, scss } from "./css.js";
|
|
4
|
+
// src/languages/html.ts
|
|
5
|
+
var SCRIPT_TYPES = /^(?:|module|text\/javascript|application\/javascript|js|ts|typescript)$/;
|
|
6
|
+
function scriptLanguage(lang) {
|
|
7
|
+
return /^(?:ts|typescript|tsx)$/.test(lang) ? typescript : javascript;
|
|
8
|
+
}
|
|
9
|
+
function styleLanguage(lang) {
|
|
10
|
+
return /^(?:scss|sass|less|postcss)$/.test(lang) ? scss : css;
|
|
11
|
+
}
|
|
12
|
+
/** Whether an attribute's value is an expression in a Vue template. */
|
|
13
|
+
var isDirective = (attr) => /^(?:v-[\w-]+|[:@#][^=]*)/.test(attr);
|
|
14
|
+
function enter(state, language, end, block) {
|
|
15
|
+
state.inner = {
|
|
16
|
+
language,
|
|
17
|
+
state: language.startState(),
|
|
18
|
+
end,
|
|
19
|
+
block
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
var lastLine = "";
|
|
23
|
+
var lastLowered = "";
|
|
24
|
+
/** The line in lower case, made once for all the tokens read from it. */
|
|
25
|
+
function lowered(line) {
|
|
26
|
+
if (line !== lastLine) {
|
|
27
|
+
lastLine = line;
|
|
28
|
+
lastLowered = line.toLowerCase();
|
|
29
|
+
}
|
|
30
|
+
return lastLowered;
|
|
31
|
+
}
|
|
32
|
+
/** Reads one token of the inner language, stopping where it ends. */
|
|
33
|
+
function inner(stream, state) {
|
|
34
|
+
const at = state.inner;
|
|
35
|
+
const found = at.end.startsWith("<") ? lowered(stream.string).indexOf(at.end, stream.pos) : stream.string.indexOf(at.end, stream.pos);
|
|
36
|
+
const index = found < 0 ? -1 : found - stream.pos;
|
|
37
|
+
if (index === 0) {
|
|
38
|
+
state.inner = null;
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const end = stream.end;
|
|
42
|
+
if (index > 0) stream.end = stream.pos + index;
|
|
43
|
+
const type = at.language.token(stream, at.state);
|
|
44
|
+
stream.end = end;
|
|
45
|
+
if (stream.pos === stream.start) stream.next();
|
|
46
|
+
return type;
|
|
47
|
+
}
|
|
48
|
+
function tag(stream, state, options) {
|
|
49
|
+
if (stream.eatSpace()) return null;
|
|
50
|
+
if (stream.match("/>") || stream.eat(">")) {
|
|
51
|
+
const name = state.tag;
|
|
52
|
+
state.in = "text";
|
|
53
|
+
if (stream.current() === ">") {
|
|
54
|
+
if (name === "script" && SCRIPT_TYPES.test(state.lang)) enter(state, scriptLanguage(state.lang), "<\/script", true);
|
|
55
|
+
else if (name === "style") enter(state, styleLanguage(state.lang), "</style", true);
|
|
56
|
+
}
|
|
57
|
+
state.lang = "";
|
|
58
|
+
return "tag";
|
|
59
|
+
}
|
|
60
|
+
if (stream.eat("=")) return "operator";
|
|
61
|
+
const quote = stream.eat(/["']/);
|
|
62
|
+
if (quote) {
|
|
63
|
+
if (options.vue && isDirective(state.attr)) {
|
|
64
|
+
enter(state, typescript, quote, false);
|
|
65
|
+
return "string";
|
|
66
|
+
}
|
|
67
|
+
return attrValue(stream, state, quote);
|
|
68
|
+
}
|
|
69
|
+
const name = stream.match(/^[^\s=>/"']+/);
|
|
70
|
+
if (name) {
|
|
71
|
+
state.attr = name[0].toLowerCase();
|
|
72
|
+
return "attribute";
|
|
73
|
+
}
|
|
74
|
+
stream.next();
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
/** Reads an attribute value up to its closing quote, which may be on a later line. */
|
|
78
|
+
function attrValue(stream, state, quote) {
|
|
79
|
+
const close = stream.string.indexOf(quote, stream.pos);
|
|
80
|
+
const value = stream.string.slice(stream.pos, close < 0 ? void 0 : close);
|
|
81
|
+
if (state.attr === "lang" || state.attr === "type") state.lang += value.trim().toLowerCase();
|
|
82
|
+
if (close < 0) {
|
|
83
|
+
stream.skipToEnd();
|
|
84
|
+
state.quote = quote;
|
|
85
|
+
} else {
|
|
86
|
+
stream.pos = close + 1;
|
|
87
|
+
state.quote = "";
|
|
88
|
+
}
|
|
89
|
+
return "string";
|
|
90
|
+
}
|
|
91
|
+
function text(stream, state, options) {
|
|
92
|
+
if (stream.match("<!--")) {
|
|
93
|
+
state.comment = true;
|
|
94
|
+
return comment(stream, state);
|
|
95
|
+
}
|
|
96
|
+
if (stream.match(/^<![^>]*>?/)) return "meta";
|
|
97
|
+
if (stream.match(/^<\/[\w:-]*\s*>?/)) return "tag";
|
|
98
|
+
const open = stream.match(/^<([a-zA-Z][\w:.-]*)/);
|
|
99
|
+
if (open) {
|
|
100
|
+
state.in = "tag";
|
|
101
|
+
state.tag = open[1].toLowerCase();
|
|
102
|
+
state.attr = "";
|
|
103
|
+
state.lang = "";
|
|
104
|
+
return "tag";
|
|
105
|
+
}
|
|
106
|
+
if (options.vue && stream.match("{{")) {
|
|
107
|
+
enter(state, typescript, "}}", false);
|
|
108
|
+
return "punctuation";
|
|
109
|
+
}
|
|
110
|
+
if (stream.match(/^&(?:#\d+|#x[\da-fA-F]+|[a-zA-Z]\w*);/)) return "meta";
|
|
111
|
+
stream.next();
|
|
112
|
+
stream.eatWhile(options.vue ? /[^<&{]/ : /[^<&]/);
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
function comment(stream, state) {
|
|
116
|
+
if (stream.skipTo("-->")) {
|
|
117
|
+
stream.pos += 3;
|
|
118
|
+
state.comment = false;
|
|
119
|
+
} else stream.skipToEnd();
|
|
120
|
+
return "comment";
|
|
121
|
+
}
|
|
122
|
+
/** Makes HTML, or a Vue single-file component. */
|
|
123
|
+
function createHtml(name, aliases, options = {}) {
|
|
124
|
+
return defineCodeLanguage({
|
|
125
|
+
name,
|
|
126
|
+
aliases,
|
|
127
|
+
nested: [
|
|
128
|
+
javascript,
|
|
129
|
+
typescript,
|
|
130
|
+
css,
|
|
131
|
+
scss
|
|
132
|
+
],
|
|
133
|
+
startState: () => ({
|
|
134
|
+
in: "text",
|
|
135
|
+
tag: "",
|
|
136
|
+
attr: "",
|
|
137
|
+
lang: "",
|
|
138
|
+
comment: false,
|
|
139
|
+
quote: "",
|
|
140
|
+
inner: null
|
|
141
|
+
}),
|
|
142
|
+
copyState: (state) => ({
|
|
143
|
+
...state,
|
|
144
|
+
inner: state.inner && {
|
|
145
|
+
...state.inner,
|
|
146
|
+
state: state.inner.language.copyState(state.inner.state)
|
|
147
|
+
}
|
|
148
|
+
}),
|
|
149
|
+
sameState: (a, b) => a.in === b.in && a.tag === b.tag && a.attr === b.attr && a.lang === b.lang && a.comment === b.comment && a.quote === b.quote && (a.inner === b.inner || !!a.inner && !!b.inner && a.inner.language === b.inner.language && a.inner.end === b.inner.end && a.inner.language.sameState(a.inner.state, b.inner.state)),
|
|
150
|
+
token(stream, state) {
|
|
151
|
+
if (state.inner) {
|
|
152
|
+
const type = inner(stream, state);
|
|
153
|
+
if (type !== void 0) return type;
|
|
154
|
+
if (stream.match("}}")) return "punctuation";
|
|
155
|
+
if (stream.eat(/["']/)) return "string";
|
|
156
|
+
}
|
|
157
|
+
if (state.comment) return comment(stream, state);
|
|
158
|
+
if (state.quote) return attrValue(stream, state, state.quote);
|
|
159
|
+
return state.in === "tag" ? tag(stream, state, options) : text(stream, state, options);
|
|
160
|
+
},
|
|
161
|
+
comment: { block: ["<!--", "-->"] },
|
|
162
|
+
autoClose: "([{\"'",
|
|
163
|
+
indentAfter: /<(?!\/|!|(?:area|base|br|col|embed|hr|img|input|link|meta|source|track|wbr)\b)[\w:-]+(?:\s[^<>]*)?(?<!\/)>\s*$/i,
|
|
164
|
+
languageAt: (state) => state.inner?.block ? state.inner.language : null
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
var html = createHtml("html", [
|
|
168
|
+
"htm",
|
|
169
|
+
"xml",
|
|
170
|
+
"svg"
|
|
171
|
+
]);
|
|
172
|
+
var vue = createHtml("vue", [], { vue: true });
|
|
173
|
+
export { createHtml, html, vue };
|
|
174
|
+
|
|
175
|
+
//# sourceMappingURL=html.js.map
|