@cossackframework/renderer 0.8.3 → 0.9.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/README.md +171 -29
- package/agent-docs.json +121 -0
- package/dist/attributes.d.ts +824 -0
- package/dist/attributes.js +1 -0
- package/dist/browser-ssr.d.ts +5 -0
- package/dist/browser-ssr.js +7 -0
- package/dist/browser.d.ts +2 -0
- package/dist/browser.js +11 -0
- package/dist/component-tags.d.ts +69 -0
- package/dist/component-tags.js +106 -0
- package/dist/cossack-element.d.ts +36 -2
- package/dist/cossack-element.js +26 -28
- package/dist/cossack-html.d.ts +23 -5
- package/dist/cossack-html.js +1059 -551
- package/dist/css-result.d.ts +9 -0
- package/dist/css-result.js +14 -0
- package/dist/css-scoping.d.ts +4 -0
- package/dist/css-scoping.js +95 -0
- package/dist/css.d.ts +3 -16
- package/dist/css.js +13 -111
- package/dist/diagnostics.d.ts +85 -0
- package/dist/diagnostics.js +465 -0
- package/dist/directives.d.ts +13 -7
- package/dist/directives.js +7 -7
- package/dist/index.d.ts +2 -0
- package/dist/index.js +9 -7
- package/dist/minify-html.d.ts +22 -0
- package/dist/minify-html.js +123 -18
- package/dist/server.d.ts +3 -1
- package/dist/server.js +6 -4
- package/dist/ssr-form-values.d.ts +2 -0
- package/dist/ssr-form-values.js +72 -0
- package/dist/style-registry.d.ts +42 -0
- package/dist/style-registry.js +73 -0
- package/dist/style-runtime.d.ts +47 -0
- package/dist/style-runtime.js +43 -0
- package/dist/template-analysis.d.ts +224 -0
- package/dist/template-analysis.js +321 -0
- package/dist/template-parse.d.ts +157 -0
- package/dist/template-parse.js +526 -0
- package/dist/template.d.ts +2 -0
- package/dist/template.js +3 -0
- package/package.json +26 -6
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single HTML tokenizer in the renderer.
|
|
3
|
+
*
|
|
4
|
+
* SSR (`renderToString`) and the client (`compileTemplate`) must agree on what
|
|
5
|
+
* every binding in a template is: child content, part of an attribute, text in
|
|
6
|
+
* a raw-text element, or something inside a comment. They used to run four
|
|
7
|
+
* hand-written scanners that disagreed on `<` in text, comments and raw-text
|
|
8
|
+
* elements. One state machine (`step`) now reads HTML the way the HTML
|
|
9
|
+
* tokenizer does. `analyzeTemplate` drives it over template strings to
|
|
10
|
+
* classify bindings; `tokenizeHtml` drives it over rendered markup for the SSR
|
|
11
|
+
* class merge and `minifyHtml`.
|
|
12
|
+
*/
|
|
13
|
+
/** Elements whose content the HTML parser reads as text, not markup. */
|
|
14
|
+
export declare const RAW_TEXT_TAGS: ReadonlySet<string>;
|
|
15
|
+
export type BindingInfo =
|
|
16
|
+
/** Child content. Hydration wraps it in `<!--CRP_i-->`/`<!--/CRP-->`. */
|
|
17
|
+
{
|
|
18
|
+
kind: 'node';
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Inside an attribute value. `name` keeps the author's case (the DOM
|
|
22
|
+
* lowercases HTML attribute names). `start` bindings directly follow
|
|
23
|
+
* `name=` or `name="`; `prefixStart` is the index in the (scoped) string
|
|
24
|
+
* where that `name=` text begins. Other attribute bindings interpolate into
|
|
25
|
+
* a value that already started.
|
|
26
|
+
*
|
|
27
|
+
* `directValue` is true when a `.value` set here (directly or through a
|
|
28
|
+
* spread) can be serialized by SSR as a plain `value` attribute: the element
|
|
29
|
+
* is not a <textarea>/<select> and has no static `value` attribute. Other
|
|
30
|
+
* cases need the serializeFormValues pass.
|
|
31
|
+
*/
|
|
32
|
+
| AttributeBinding
|
|
33
|
+
/**
|
|
34
|
+
* Text content of a raw-text element (`tag` is lowercase). `first` is true
|
|
35
|
+
* when no text precedes the binding inside the element.
|
|
36
|
+
*/
|
|
37
|
+
| {
|
|
38
|
+
kind: 'raw-text';
|
|
39
|
+
tag: string;
|
|
40
|
+
first: boolean;
|
|
41
|
+
}
|
|
42
|
+
/** Inside a comment. The value is dropped by both renderers. */
|
|
43
|
+
| {
|
|
44
|
+
kind: 'comment';
|
|
45
|
+
}
|
|
46
|
+
/** In a tag but not in an attribute value (e.g. `<div ${x}>`). Dropped. */
|
|
47
|
+
| {
|
|
48
|
+
kind: 'element';
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A component tag: `<${Card}` opens one and `</${Card}` closes it. `html`
|
|
52
|
+
* compiles component tags into component() results before any renderer
|
|
53
|
+
* sees the template, so the renderers never receive this kind.
|
|
54
|
+
*/
|
|
55
|
+
| {
|
|
56
|
+
kind: 'tag';
|
|
57
|
+
closing: boolean;
|
|
58
|
+
};
|
|
59
|
+
export interface AttributeBinding {
|
|
60
|
+
kind: 'attribute';
|
|
61
|
+
name: string;
|
|
62
|
+
start: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* True when the binding is the attribute's whole value (`name=${x}` or
|
|
65
|
+
* `name="${x}"`). False when the value also has static text or other
|
|
66
|
+
* bindings (`name="a ${x}"`, `name="${x}${y}"`): then the attribute is
|
|
67
|
+
* text, and every binding in it adds its value as text.
|
|
68
|
+
*/
|
|
69
|
+
whole: boolean;
|
|
70
|
+
quote: '"' | "'" | '';
|
|
71
|
+
prefixStart: number;
|
|
72
|
+
directValue: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Where the binding's element starts: the `<` is at `tagOffset` in the
|
|
75
|
+
* (scoped) string `tagString`. SSR uses it to find the element's markup.
|
|
76
|
+
*/
|
|
77
|
+
tagString: number;
|
|
78
|
+
tagOffset: number;
|
|
79
|
+
/**
|
|
80
|
+
* Where the start tag ends: its `>` is at `tagEndOffset` in the (scoped)
|
|
81
|
+
* string `tagEndString`. Both are -1 when the template ends inside the tag.
|
|
82
|
+
*/
|
|
83
|
+
tagEndString: number;
|
|
84
|
+
tagEndOffset: number;
|
|
85
|
+
}
|
|
86
|
+
/** An attribute that contains bindings, used to drop it whole for `nothing`. */
|
|
87
|
+
export interface AttributeGroup {
|
|
88
|
+
readonly indices: readonly number[];
|
|
89
|
+
/** Static text from the whitespace before the name up to the first binding. */
|
|
90
|
+
readonly prefix: string;
|
|
91
|
+
/** Static text after the last binding that still belongs to the attribute. */
|
|
92
|
+
readonly suffixLength: number;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Everything the renderers need to know about a template's static strings.
|
|
96
|
+
* It never depends on values, so one analysis is shared by every render and
|
|
97
|
+
* every instance of the template. It is deeply frozen: a render that tried to
|
|
98
|
+
* write to it would corrupt every later render, so that throws instead.
|
|
99
|
+
*/
|
|
100
|
+
export interface TemplateAnalysis {
|
|
101
|
+
/** Template strings with the style scope attribute added to static tags. */
|
|
102
|
+
readonly strings: readonly string[];
|
|
103
|
+
readonly bindings: readonly Readonly<BindingInfo>[];
|
|
104
|
+
/**
|
|
105
|
+
* Indexed by binding: the attribute whose first binding has that index, or
|
|
106
|
+
* undefined. Same length as `bindings`.
|
|
107
|
+
*/
|
|
108
|
+
readonly attributeGroups: readonly (AttributeGroup | undefined)[];
|
|
109
|
+
}
|
|
110
|
+
/** @internal Counts analysis runs so tests can verify the cache. */
|
|
111
|
+
export declare const templateAnalysisStats: {
|
|
112
|
+
runs: number;
|
|
113
|
+
};
|
|
114
|
+
/** The shared analysis for a template's strings in a style scope. */
|
|
115
|
+
export declare const getTemplateAnalysis: (input: TemplateStringsArray, scopeId?: string) => TemplateAnalysis;
|
|
116
|
+
/** @internal Tokenizer states, shared with the template parser. */
|
|
117
|
+
export declare const enum State {
|
|
118
|
+
Data = 0,
|
|
119
|
+
TagName = 1,
|
|
120
|
+
BeforeAttrName = 2,
|
|
121
|
+
AttrName = 3,
|
|
122
|
+
AfterAttrName = 4,
|
|
123
|
+
BeforeAttrValue = 5,
|
|
124
|
+
AttrValueQuoted = 6,
|
|
125
|
+
AttrValueUnquoted = 7,
|
|
126
|
+
EndTag = 8,
|
|
127
|
+
Comment = 9,
|
|
128
|
+
BogusComment = 10,
|
|
129
|
+
RawText = 11
|
|
130
|
+
}
|
|
131
|
+
/** @internal */
|
|
132
|
+
export declare const isSpace: (ch: string | undefined) => ch is "\t" | "\n" | "\f" | "\r" | " ";
|
|
133
|
+
/** @internal */
|
|
134
|
+
export interface Tokenizer {
|
|
135
|
+
state: State;
|
|
136
|
+
/** Lowercase name of the most recent start tag. Kept after the tag ends. */
|
|
137
|
+
tagName: string;
|
|
138
|
+
/** Lowercase name of the raw-text element whose text is being read. */
|
|
139
|
+
rawTextTag: string;
|
|
140
|
+
quote: '"' | "'";
|
|
141
|
+
/**
|
|
142
|
+
* The last two characters seen inside a comment, seeded with the opening
|
|
143
|
+
* dashes so `<!-->` and `<!--->` close the way the HTML tokenizer does.
|
|
144
|
+
*/
|
|
145
|
+
commentTail: string;
|
|
146
|
+
}
|
|
147
|
+
/** @internal */
|
|
148
|
+
export declare const createTokenizer: () => Tokenizer;
|
|
149
|
+
/**
|
|
150
|
+
* Advance the tokenizer over `str[j]`. Returns the index of the next character
|
|
151
|
+
* to read: `j + 1`, `j + 4` after `<!--`, the end of a run of characters that
|
|
152
|
+
* cannot change the state (text, raw text, quoted values, comment bodies), or
|
|
153
|
+
* `j` when the character must be read again in the new state (it ended a tag
|
|
154
|
+
* name, an attribute name or an unquoted value). Callers compare the state
|
|
155
|
+
* before and after a step to see what the characters did.
|
|
156
|
+
* @internal
|
|
157
|
+
*/
|
|
158
|
+
export declare const step: (t: Tokenizer, str: string, j: number) => number;
|
|
159
|
+
/**
|
|
160
|
+
* Tokenize template strings and classify every binding.
|
|
161
|
+
*
|
|
162
|
+
* When `scopeId` is given, ` data-cossack-scope="<id>"` is inserted after the
|
|
163
|
+
* name of every opening tag that appears in the static strings (not inside
|
|
164
|
+
* comments or raw text, and never inside nested values).
|
|
165
|
+
*/
|
|
166
|
+
export declare const analyzeTemplate: (input: readonly string[], scopeId?: string) => TemplateAnalysis;
|
|
167
|
+
/** An attribute of a start tag. Offsets index the tokenized source. */
|
|
168
|
+
export interface HtmlAttribute {
|
|
169
|
+
/** As written. */
|
|
170
|
+
readonly name: string;
|
|
171
|
+
readonly nameStart: number;
|
|
172
|
+
readonly nameEnd: number;
|
|
173
|
+
/** Value bounds without quotes; both -1 when there is no `=`. */
|
|
174
|
+
readonly valueStart: number;
|
|
175
|
+
readonly valueEnd: number;
|
|
176
|
+
readonly quote: '"' | "'" | '';
|
|
177
|
+
}
|
|
178
|
+
/** One token of rendered markup. `start`/`end` index the tokenized source. */
|
|
179
|
+
export type HtmlToken = {
|
|
180
|
+
readonly type: 'text';
|
|
181
|
+
readonly start: number;
|
|
182
|
+
readonly end: number;
|
|
183
|
+
}
|
|
184
|
+
/** Text of a raw-text element (`tag` is lowercase). */
|
|
185
|
+
| {
|
|
186
|
+
readonly type: 'raw-text';
|
|
187
|
+
readonly start: number;
|
|
188
|
+
readonly end: number;
|
|
189
|
+
readonly tag: string;
|
|
190
|
+
} | {
|
|
191
|
+
readonly type: 'start-tag';
|
|
192
|
+
readonly start: number;
|
|
193
|
+
readonly end: number;
|
|
194
|
+
/** Lowercase tag name; the source keeps the author's case. */
|
|
195
|
+
readonly name: string;
|
|
196
|
+
readonly nameEnd: number;
|
|
197
|
+
readonly attributes: readonly HtmlAttribute[];
|
|
198
|
+
/** The tag ends with `/>`. */
|
|
199
|
+
readonly selfClosing: boolean;
|
|
200
|
+
/** False when the source ends before the tag's `>`. */
|
|
201
|
+
readonly closed: boolean;
|
|
202
|
+
} | {
|
|
203
|
+
readonly type: 'end-tag';
|
|
204
|
+
readonly start: number;
|
|
205
|
+
readonly end: number;
|
|
206
|
+
readonly name: string;
|
|
207
|
+
readonly nameEnd: number;
|
|
208
|
+
}
|
|
209
|
+
/** `<!--` up to and including `-->`. */
|
|
210
|
+
| {
|
|
211
|
+
readonly type: 'comment';
|
|
212
|
+
readonly start: number;
|
|
213
|
+
readonly end: number;
|
|
214
|
+
}
|
|
215
|
+
/** `<!doctype>`, `<?…>`, `</ …>`, or markup the source ends inside. */
|
|
216
|
+
| {
|
|
217
|
+
readonly type: 'other';
|
|
218
|
+
readonly start: number;
|
|
219
|
+
readonly end: number;
|
|
220
|
+
};
|
|
221
|
+
/** Split rendered markup into tokens, using the same tokenizer as templates. */
|
|
222
|
+
export declare const tokenizeHtml: (source: string) => HtmlToken[];
|
|
223
|
+
/** Call `visit` with each token of rendered markup, in order. */
|
|
224
|
+
export declare const forEachHtmlToken: (source: string, visit: (token: HtmlToken) => void) => void;
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
// @ts-self-types="./template-analysis.d.ts"
|
|
2
|
+
//#region src/template-analysis.ts
|
|
3
|
+
var e = /* @__PURE__ */ new Set([
|
|
4
|
+
"script",
|
|
5
|
+
"style",
|
|
6
|
+
"textarea",
|
|
7
|
+
"title"
|
|
8
|
+
]), t = { runs: 0 }, n = /* @__PURE__ */ new WeakMap(), r = (e, t) => {
|
|
9
|
+
let r = n.get(e);
|
|
10
|
+
r || (r = /* @__PURE__ */ new Map(), n.set(e, r));
|
|
11
|
+
let i = t ?? "", a = r.get(i);
|
|
12
|
+
return a || (a = f(e, t), r.set(i, a)), a;
|
|
13
|
+
}, i = /* @__PURE__ */ function(e) {
|
|
14
|
+
return e[e.Data = 0] = "Data", e[e.TagName = 1] = "TagName", e[e.BeforeAttrName = 2] = "BeforeAttrName", e[e.AttrName = 3] = "AttrName", e[e.AfterAttrName = 4] = "AfterAttrName", e[e.BeforeAttrValue = 5] = "BeforeAttrValue", e[e.AttrValueQuoted = 6] = "AttrValueQuoted", e[e.AttrValueUnquoted = 7] = "AttrValueUnquoted", e[e.EndTag = 8] = "EndTag", e[e.Comment = 9] = "Comment", e[e.BogusComment = 10] = "BogusComment", e[e.RawText = 11] = "RawText", e;
|
|
15
|
+
}({}), a = (e) => e === " " || e === "\n" || e === " " || e === "\r" || e === "\f", o = (e) => !!e && (e >= "a" && e <= "z" || e >= "A" && e <= "Z"), s = () => ({
|
|
16
|
+
state: 0,
|
|
17
|
+
tagName: "",
|
|
18
|
+
rawTextTag: "",
|
|
19
|
+
quote: "\"",
|
|
20
|
+
commentTail: ""
|
|
21
|
+
}), c = (t) => {
|
|
22
|
+
e.has(t.tagName) ? (t.state = 11, t.rawTextTag = t.tagName) : t.state = 0;
|
|
23
|
+
}, l = (e, t, n) => {
|
|
24
|
+
let r = e.indexOf(n, t);
|
|
25
|
+
return r < 0 ? e.length : r;
|
|
26
|
+
}, u = (e, t, n) => {
|
|
27
|
+
for (; t < e.length; t++) {
|
|
28
|
+
let r = e.charCodeAt(t);
|
|
29
|
+
if (r === 32 || r === 9 || r === 10 || r === 12 || r === 13 || r === 47 || r === 62 || r === 61 && n) break;
|
|
30
|
+
}
|
|
31
|
+
return t;
|
|
32
|
+
}, d = (e, t, n) => {
|
|
33
|
+
let r = t[n];
|
|
34
|
+
switch (e.state) {
|
|
35
|
+
case 0: {
|
|
36
|
+
if (r !== "<") return l(t, n, "<");
|
|
37
|
+
let i = t[n + 1];
|
|
38
|
+
if (o(i)) e.state = 1, e.tagName = "";
|
|
39
|
+
else if (i === "/" && o(t[n + 2])) e.state = 8;
|
|
40
|
+
else if (i === "!" && t[n + 2] === "-" && t[n + 3] === "-") return e.state = 9, e.commentTail = "--", n + 4;
|
|
41
|
+
else (i === "!" || i === "?" || i === "/" && t[n + 2] !== void 0) && (e.state = 10);
|
|
42
|
+
return n + 1;
|
|
43
|
+
}
|
|
44
|
+
case 1: {
|
|
45
|
+
let r = u(t, n, !1);
|
|
46
|
+
return r === n ? (e.state = 2, n) : (e.tagName += t.slice(n, r).toLowerCase(), r);
|
|
47
|
+
}
|
|
48
|
+
case 2:
|
|
49
|
+
case 4: return r === ">" ? c(e) : r === "/" ? e.state = 2 : r === "=" && e.state === 4 ? e.state = 5 : a(r) || (e.state = 3), n + 1;
|
|
50
|
+
case 3:
|
|
51
|
+
if (a(r)) e.state = 4;
|
|
52
|
+
else if (r === "/" || r === ">") return e.state = 2, n;
|
|
53
|
+
else if (r === "=") e.state = 5;
|
|
54
|
+
else return u(t, n, !0);
|
|
55
|
+
return n + 1;
|
|
56
|
+
case 5: return r === "\"" || r === "'" ? (e.quote = r, e.state = 6) : r === ">" ? c(e) : a(r) || (e.state = 7), n + 1;
|
|
57
|
+
case 6: return r === e.quote ? (e.state = 2, n + 1) : l(t, n, e.quote);
|
|
58
|
+
case 7: return a(r) || r === ">" || r === "/" && n === 0 && t[1] === ">" ? (e.state = 2, n) : r === "/" ? n + 1 : u(t, n, !1);
|
|
59
|
+
case 8:
|
|
60
|
+
case 10: return r === ">" ? (e.state = 0, n + 1) : l(t, n, ">");
|
|
61
|
+
case 9: {
|
|
62
|
+
if (r === ">" && e.commentTail === "--") return e.state = 0, n + 1;
|
|
63
|
+
let i = Math.max(l(t, n, ">"), n + 1);
|
|
64
|
+
return e.commentTail = (e.commentTail + t.slice(Math.max(n, i - 2), i)).slice(-2), i;
|
|
65
|
+
}
|
|
66
|
+
case 11: {
|
|
67
|
+
if (r !== "<") return l(t, n, "<");
|
|
68
|
+
let i = e.rawTextTag;
|
|
69
|
+
if (t[n + 1] === "/" && t.slice(n + 2, n + 2 + i.length).toLowerCase() === i) {
|
|
70
|
+
let r = t[n + 2 + i.length];
|
|
71
|
+
(r === void 0 || a(r) || r === "/" || r === ">") && (e.state = 8);
|
|
72
|
+
}
|
|
73
|
+
return n + 1;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}, f = (e, n) => {
|
|
77
|
+
t.runs++;
|
|
78
|
+
let r = n ? ` data-cossack-scope="${n}"` : "", i = [], o = [], c = Array.from({ length: Math.max(e.length - 1, 0) }), l = s(), u = !0, f = !0, p = null, m = -1, h = [], g = !1, _ = 0, v = 0;
|
|
79
|
+
for (let t = 0; t < e.length; t++) {
|
|
80
|
+
let n = e[t], s = "", y = (e) => {
|
|
81
|
+
if (p && p.indices.length === 0 && p.name.toLowerCase() === "value" && (g = !0), p && p.indices.length > 0 && p.stringIndex === p.indices[0]) {
|
|
82
|
+
let n = p.indices[p.indices.length - 1], r = t === n + 1 ? e : 0;
|
|
83
|
+
c[p.indices[0]] = Object.freeze({
|
|
84
|
+
indices: Object.freeze(p.indices),
|
|
85
|
+
prefix: p.prefix,
|
|
86
|
+
suffixLength: r
|
|
87
|
+
});
|
|
88
|
+
let i = o[p.indices[0]];
|
|
89
|
+
(p.indices.length > 1 || r !== +!!i.quote) && (i.whole = !1);
|
|
90
|
+
}
|
|
91
|
+
p = null;
|
|
92
|
+
}, b = () => {
|
|
93
|
+
y(0);
|
|
94
|
+
let e = l.tagName !== "textarea" && l.tagName !== "select" && !g;
|
|
95
|
+
for (let n of h) n.directValue = e, n.tagEndString = t, n.tagEndOffset = s.length;
|
|
96
|
+
h = [], l.state === 11 && (u = !0);
|
|
97
|
+
};
|
|
98
|
+
for (let e = 0; e < n.length;) {
|
|
99
|
+
let i = n[e], o = l.state, c = d(l, n, e), x = l.state;
|
|
100
|
+
switch (o) {
|
|
101
|
+
case 0:
|
|
102
|
+
x === 1 && (h = [], g = !1, _ = t, v = s.length);
|
|
103
|
+
break;
|
|
104
|
+
case 1:
|
|
105
|
+
x !== 1 && (s += r, m = -1);
|
|
106
|
+
break;
|
|
107
|
+
case 2:
|
|
108
|
+
case 4:
|
|
109
|
+
if (x === 3) {
|
|
110
|
+
y(e);
|
|
111
|
+
let n = s.length;
|
|
112
|
+
p = {
|
|
113
|
+
name: i,
|
|
114
|
+
stringIndex: t,
|
|
115
|
+
nameStart: n,
|
|
116
|
+
spaceStart: m < 0 ? n : m,
|
|
117
|
+
indices: [],
|
|
118
|
+
prefix: ""
|
|
119
|
+
}, m = -1;
|
|
120
|
+
} else a(i) ? m < 0 && (m = s.length) : i === "/" ? m = -1 : x !== 5 && b();
|
|
121
|
+
break;
|
|
122
|
+
case 3:
|
|
123
|
+
x === 3 ? p && (p.name += n.slice(e, c)) : x === 4 ? m = s.length : x === 2 && (y(e), m = -1);
|
|
124
|
+
break;
|
|
125
|
+
case 5:
|
|
126
|
+
x === 6 ? f = !0 : (x === 0 || x === 11) && (y(e), b());
|
|
127
|
+
break;
|
|
128
|
+
case 6:
|
|
129
|
+
x === 2 ? (y(e + 1), m = -1) : f = !1;
|
|
130
|
+
break;
|
|
131
|
+
case 7:
|
|
132
|
+
x === 2 && (y(e), m = -1);
|
|
133
|
+
break;
|
|
134
|
+
case 11:
|
|
135
|
+
x === 11 && (u = !1);
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
s += n.slice(e, c), e = c;
|
|
139
|
+
}
|
|
140
|
+
if (t < e.length - 1) switch (l.state) {
|
|
141
|
+
case 0:
|
|
142
|
+
n.endsWith("</") ? (o.push({
|
|
143
|
+
kind: "tag",
|
|
144
|
+
closing: !0
|
|
145
|
+
}), l.state = 8) : n.endsWith("<") ? (o.push({
|
|
146
|
+
kind: "tag",
|
|
147
|
+
closing: !1
|
|
148
|
+
}), l.state = 2, l.tagName = "", h = [], g = !1, _ = t, v = s.length - 1, m = -1) : o.push({ kind: "node" });
|
|
149
|
+
break;
|
|
150
|
+
case 9:
|
|
151
|
+
case 10:
|
|
152
|
+
o.push({ kind: "comment" });
|
|
153
|
+
break;
|
|
154
|
+
case 11:
|
|
155
|
+
o.push({
|
|
156
|
+
kind: "raw-text",
|
|
157
|
+
tag: l.rawTextTag,
|
|
158
|
+
first: u
|
|
159
|
+
}), u = !1;
|
|
160
|
+
break;
|
|
161
|
+
case 5:
|
|
162
|
+
case 6:
|
|
163
|
+
case 7: {
|
|
164
|
+
if (!p) {
|
|
165
|
+
o.push({ kind: "element" });
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
let e = p, n = l.state === 6, r = (l.state === 5 || n && f) && e.stringIndex === t;
|
|
169
|
+
e.indices.length === 0 && e.stringIndex === t && (e.prefix = s.slice(e.spaceStart)), e.indices.push(t);
|
|
170
|
+
let i = {
|
|
171
|
+
kind: "attribute",
|
|
172
|
+
name: e.name,
|
|
173
|
+
start: r,
|
|
174
|
+
whole: r,
|
|
175
|
+
quote: n ? l.quote : "",
|
|
176
|
+
prefixStart: r ? e.nameStart : -1,
|
|
177
|
+
directValue: !1,
|
|
178
|
+
tagString: _,
|
|
179
|
+
tagOffset: v,
|
|
180
|
+
tagEndString: -1,
|
|
181
|
+
tagEndOffset: -1
|
|
182
|
+
};
|
|
183
|
+
o.push(i), h.push(i), l.state === 5 && (l.state = 7), f = !1;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
default:
|
|
187
|
+
o.push({ kind: "element" }), (l.state === 3 || l.state === 4) && y(0);
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
i.push(s);
|
|
191
|
+
}
|
|
192
|
+
for (let e of o) Object.freeze(e);
|
|
193
|
+
return Object.freeze({
|
|
194
|
+
strings: Object.freeze(i),
|
|
195
|
+
bindings: Object.freeze(o),
|
|
196
|
+
attributeGroups: Object.freeze(c)
|
|
197
|
+
});
|
|
198
|
+
}, p = (e) => {
|
|
199
|
+
let t = [];
|
|
200
|
+
return m(e, (e) => {
|
|
201
|
+
t.push(e);
|
|
202
|
+
}), t;
|
|
203
|
+
}, m = (e, t) => {
|
|
204
|
+
let n = s(), r = 0, i = -1, o = [], c, l = !1, u = (e, a) => {
|
|
205
|
+
c && c.nameEnd < 0 && (c.nameEnd = e), c && c.valueStart >= 0 && c.valueEnd < 0 && (c.valueEnd = e), t({
|
|
206
|
+
type: "start-tag",
|
|
207
|
+
start: r,
|
|
208
|
+
end: e,
|
|
209
|
+
name: n.tagName,
|
|
210
|
+
nameEnd: i < 0 ? e : i,
|
|
211
|
+
attributes: o,
|
|
212
|
+
selfClosing: a && l,
|
|
213
|
+
closed: a
|
|
214
|
+
});
|
|
215
|
+
};
|
|
216
|
+
for (let s = 0; s < e.length;) {
|
|
217
|
+
let f = e[s], p = n.state, m = d(n, e, s), h = n.state;
|
|
218
|
+
switch (p) {
|
|
219
|
+
case 0:
|
|
220
|
+
h !== 0 && (s > r && t({
|
|
221
|
+
type: "text",
|
|
222
|
+
start: r,
|
|
223
|
+
end: s
|
|
224
|
+
}), r = s, h === 1 && (i = -1, o = [], c = void 0, l = !1));
|
|
225
|
+
break;
|
|
226
|
+
case 11:
|
|
227
|
+
h === 8 && (s > r && t({
|
|
228
|
+
type: "raw-text",
|
|
229
|
+
start: r,
|
|
230
|
+
end: s,
|
|
231
|
+
tag: n.rawTextTag
|
|
232
|
+
}), r = s);
|
|
233
|
+
break;
|
|
234
|
+
case 8:
|
|
235
|
+
if (h === 0) {
|
|
236
|
+
let n = r + 2;
|
|
237
|
+
for (; n < m && !a(e[n]) && e[n] !== "/" && e[n] !== ">";) n++;
|
|
238
|
+
t({
|
|
239
|
+
type: "end-tag",
|
|
240
|
+
start: r,
|
|
241
|
+
end: m,
|
|
242
|
+
name: e.slice(r + 2, n).toLowerCase(),
|
|
243
|
+
nameEnd: n
|
|
244
|
+
}), r = m;
|
|
245
|
+
}
|
|
246
|
+
break;
|
|
247
|
+
case 9:
|
|
248
|
+
case 10:
|
|
249
|
+
h === 0 && (t({
|
|
250
|
+
type: p === 9 ? "comment" : "other",
|
|
251
|
+
start: r,
|
|
252
|
+
end: m
|
|
253
|
+
}), r = m);
|
|
254
|
+
break;
|
|
255
|
+
case 1:
|
|
256
|
+
h !== 1 && (i = s);
|
|
257
|
+
break;
|
|
258
|
+
case 2:
|
|
259
|
+
case 4:
|
|
260
|
+
h === 3 ? (c = {
|
|
261
|
+
name: "",
|
|
262
|
+
nameStart: s,
|
|
263
|
+
nameEnd: -1,
|
|
264
|
+
valueStart: -1,
|
|
265
|
+
valueEnd: -1,
|
|
266
|
+
quote: ""
|
|
267
|
+
}, o.push(c)) : (h === 0 || h === 11) && (u(m, !0), r = m), l = f === "/";
|
|
268
|
+
break;
|
|
269
|
+
case 3:
|
|
270
|
+
h !== 3 && c && (c.nameEnd = s, c.name = e.slice(c.nameStart, s));
|
|
271
|
+
break;
|
|
272
|
+
case 5:
|
|
273
|
+
if (!c) break;
|
|
274
|
+
h === 6 ? (c.quote = n.quote, c.valueStart = s + 1) : h === 7 ? c.valueStart = s : (h === 0 || h === 11) && (c.valueStart = c.valueEnd = s, u(m, !0), r = m);
|
|
275
|
+
break;
|
|
276
|
+
case 6:
|
|
277
|
+
case 7:
|
|
278
|
+
h === 2 && c && (c.valueEnd = s);
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
s = m;
|
|
282
|
+
}
|
|
283
|
+
if (r < e.length) {
|
|
284
|
+
let i = e.length;
|
|
285
|
+
switch (n.state) {
|
|
286
|
+
case 0:
|
|
287
|
+
t({
|
|
288
|
+
type: "text",
|
|
289
|
+
start: r,
|
|
290
|
+
end: i
|
|
291
|
+
});
|
|
292
|
+
break;
|
|
293
|
+
case 11:
|
|
294
|
+
t({
|
|
295
|
+
type: "raw-text",
|
|
296
|
+
start: r,
|
|
297
|
+
end: i,
|
|
298
|
+
tag: n.rawTextTag
|
|
299
|
+
});
|
|
300
|
+
break;
|
|
301
|
+
case 1:
|
|
302
|
+
case 2:
|
|
303
|
+
case 3:
|
|
304
|
+
case 4:
|
|
305
|
+
case 5:
|
|
306
|
+
case 6:
|
|
307
|
+
case 7:
|
|
308
|
+
c && c.name === "" && (c.name = e.slice(c.nameStart, c.nameEnd < 0 ? i : c.nameEnd)), u(i, !1);
|
|
309
|
+
break;
|
|
310
|
+
default:
|
|
311
|
+
t({
|
|
312
|
+
type: "other",
|
|
313
|
+
start: r,
|
|
314
|
+
end: i
|
|
315
|
+
});
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
//#endregion
|
|
321
|
+
export { e as RAW_TEXT_TAGS, i as State, f as analyzeTemplate, s as createTokenizer, m as forEachHtmlToken, r as getTemplateAnalysis, a as isSpace, d as step, t as templateAnalysisStats, p as tokenizeHtml };
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
export interface TemplateLocation {
|
|
2
|
+
readonly string: number;
|
|
3
|
+
readonly offset: number;
|
|
4
|
+
}
|
|
5
|
+
export interface TemplateRange {
|
|
6
|
+
readonly start: TemplateLocation;
|
|
7
|
+
readonly end: TemplateLocation;
|
|
8
|
+
}
|
|
9
|
+
export interface TemplateAttribute {
|
|
10
|
+
/** The name as written, including a `.`, `?` or `@` prefix, or `...`. */
|
|
11
|
+
readonly name: string;
|
|
12
|
+
/** From the first character of the name to the end of the value. */
|
|
13
|
+
readonly range: TemplateRange;
|
|
14
|
+
readonly nameRange: TemplateRange;
|
|
15
|
+
/** False for a bare attribute such as `disabled`. */
|
|
16
|
+
readonly hasValue: boolean;
|
|
17
|
+
readonly quote: '"' | "'" | '';
|
|
18
|
+
/**
|
|
19
|
+
* The value's static text around its bindings, as written (entities are
|
|
20
|
+
* not decoded). Always one more entry than `bindings`.
|
|
21
|
+
*/
|
|
22
|
+
readonly segments: readonly string[];
|
|
23
|
+
readonly bindings: readonly number[];
|
|
24
|
+
}
|
|
25
|
+
export type TemplateNamespace = 'html' | 'svg' | 'mathml';
|
|
26
|
+
interface TagNode {
|
|
27
|
+
readonly attributes: readonly TemplateAttribute[];
|
|
28
|
+
/** Bindings in the start tag outside any attribute value. They render nothing. */
|
|
29
|
+
readonly droppedBindings: readonly number[];
|
|
30
|
+
readonly children: readonly TemplateNode[];
|
|
31
|
+
/** From the start tag's `<` to the end of the end tag, or of the start tag. */
|
|
32
|
+
readonly range: TemplateRange;
|
|
33
|
+
readonly startTag: TemplateRange;
|
|
34
|
+
/** The start tag ends with `/>`. */
|
|
35
|
+
readonly selfClosing: boolean;
|
|
36
|
+
readonly namespace: TemplateNamespace;
|
|
37
|
+
}
|
|
38
|
+
export interface TemplateElement extends TagNode {
|
|
39
|
+
readonly type: 'element';
|
|
40
|
+
/** The tag name as written. */
|
|
41
|
+
readonly tag: string;
|
|
42
|
+
/** The lowercase tag name. */
|
|
43
|
+
readonly name: string;
|
|
44
|
+
/** An end tag closed the element. Void and self-closing elements need none. */
|
|
45
|
+
readonly closed: boolean;
|
|
46
|
+
}
|
|
47
|
+
/** `<${Card} ...>children</${Card}>` or `<${Card} ... />`. */
|
|
48
|
+
export interface TemplateComponent extends TagNode {
|
|
49
|
+
readonly type: 'component';
|
|
50
|
+
/** The binding that holds the component class. */
|
|
51
|
+
readonly binding: number;
|
|
52
|
+
/** The binding of the closing tag; null when self-closing or unclosed. */
|
|
53
|
+
readonly closeBinding: number | null;
|
|
54
|
+
/** Where the closing tag's `</` starts; null when self-closing or unclosed. */
|
|
55
|
+
readonly closeStart: TemplateLocation | null;
|
|
56
|
+
}
|
|
57
|
+
export interface TemplateText {
|
|
58
|
+
readonly type: 'text';
|
|
59
|
+
/** As written: entities are not decoded. */
|
|
60
|
+
readonly text: string;
|
|
61
|
+
readonly range: TemplateRange;
|
|
62
|
+
}
|
|
63
|
+
/** A binding in node position, or in the text of a raw-text element. */
|
|
64
|
+
export interface TemplateBindingNode {
|
|
65
|
+
readonly type: 'binding';
|
|
66
|
+
readonly binding: number;
|
|
67
|
+
/** Text of `<script>`, `<style>`, `<textarea>` or `<title>`. */
|
|
68
|
+
readonly rawText: boolean;
|
|
69
|
+
}
|
|
70
|
+
export interface TemplateComment {
|
|
71
|
+
readonly type: 'comment';
|
|
72
|
+
readonly range: TemplateRange;
|
|
73
|
+
readonly bindings: readonly number[];
|
|
74
|
+
}
|
|
75
|
+
export type TemplateNode = TemplateElement | TemplateComponent | TemplateText | TemplateBindingNode | TemplateComment;
|
|
76
|
+
export type TemplateBindingRole = {
|
|
77
|
+
readonly kind: 'node';
|
|
78
|
+
readonly parent: TemplateElement | TemplateComponent | null;
|
|
79
|
+
} | {
|
|
80
|
+
readonly kind: 'raw-text';
|
|
81
|
+
readonly element: TemplateElement | null;
|
|
82
|
+
} | {
|
|
83
|
+
readonly kind: 'attribute';
|
|
84
|
+
readonly owner: TemplateElement | TemplateComponent;
|
|
85
|
+
readonly attribute: TemplateAttribute;
|
|
86
|
+
} | {
|
|
87
|
+
readonly kind: 'tag';
|
|
88
|
+
readonly component: TemplateComponent;
|
|
89
|
+
readonly closing: boolean;
|
|
90
|
+
}
|
|
91
|
+
/** Inside a comment, a tag outside any attribute value, or an end tag. Renders nothing. */
|
|
92
|
+
| {
|
|
93
|
+
readonly kind: 'dropped';
|
|
94
|
+
readonly reason: 'comment' | 'tag' | 'end-tag';
|
|
95
|
+
};
|
|
96
|
+
export type TemplateProblemCode = 'COSSACK_TEMPLATE_UNTERMINATED' | 'COSSACK_TEMPLATE_DROPPED_BINDING' | 'COSSACK_TEMPLATE_UNCLOSED_ELEMENT' | 'COSSACK_TEMPLATE_UNEXPECTED_END_TAG' | 'COSSACK_TEMPLATE_SELF_CLOSING_ELEMENT' | 'COSSACK_TEMPLATE_UNKNOWN_BINDING_PREFIX' | 'COSSACK_TEMPLATE_PREFIX_WITHOUT_BINDING' | 'COSSACK_TEMPLATE_PARTIAL_BINDING' | 'COSSACK_TEMPLATE_DUPLICATE_ATTRIBUTE' | 'COSSACK_TEMPLATE_UNCLOSED_COMPONENT' | 'COSSACK_TEMPLATE_UNEXPECTED_COMPONENT_END';
|
|
97
|
+
export interface TemplateProblem {
|
|
98
|
+
readonly code: TemplateProblemCode;
|
|
99
|
+
readonly severity: 'error' | 'warning';
|
|
100
|
+
readonly message: string;
|
|
101
|
+
readonly range: TemplateRange;
|
|
102
|
+
}
|
|
103
|
+
export interface ParsedTemplate {
|
|
104
|
+
readonly nodes: readonly TemplateNode[];
|
|
105
|
+
/** The role of each binding, by index. */
|
|
106
|
+
readonly bindings: readonly TemplateBindingRole[];
|
|
107
|
+
readonly problems: readonly TemplateProblem[];
|
|
108
|
+
}
|
|
109
|
+
/** Elements that never have content or an end tag. */
|
|
110
|
+
export declare const VOID_ELEMENTS: ReadonlySet<string>;
|
|
111
|
+
export declare const parseTemplate: (strings: readonly string[], options?: {
|
|
112
|
+
svg?: boolean;
|
|
113
|
+
}) => ParsedTemplate;
|
|
114
|
+
/** The tag being written at a location: an element (with its namespace) or a component tag. */
|
|
115
|
+
export type TemplateContextTag = {
|
|
116
|
+
readonly component: false;
|
|
117
|
+
readonly tag: string;
|
|
118
|
+
readonly name: string;
|
|
119
|
+
readonly namespace: TemplateNamespace;
|
|
120
|
+
} | {
|
|
121
|
+
readonly component: true;
|
|
122
|
+
readonly binding: number;
|
|
123
|
+
};
|
|
124
|
+
/** What an editor is writing at a location in a template. */
|
|
125
|
+
export type TemplateContext = {
|
|
126
|
+
readonly kind: 'text';
|
|
127
|
+
readonly namespace: TemplateNamespace;
|
|
128
|
+
} | {
|
|
129
|
+
readonly kind: 'tag-name';
|
|
130
|
+
readonly prefix: string;
|
|
131
|
+
readonly namespace: TemplateNamespace;
|
|
132
|
+
} | {
|
|
133
|
+
readonly kind: 'attribute-name';
|
|
134
|
+
readonly prefix: string;
|
|
135
|
+
readonly tag: TemplateContextTag;
|
|
136
|
+
readonly attributes: readonly string[];
|
|
137
|
+
} | {
|
|
138
|
+
readonly kind: 'attribute-value';
|
|
139
|
+
readonly name: string;
|
|
140
|
+
readonly prefix: string;
|
|
141
|
+
readonly quote: '"' | "'" | '';
|
|
142
|
+
readonly tag: TemplateContextTag;
|
|
143
|
+
} | {
|
|
144
|
+
readonly kind: 'end-tag';
|
|
145
|
+
readonly prefix: string;
|
|
146
|
+
readonly open: TemplateElement | null;
|
|
147
|
+
} | {
|
|
148
|
+
readonly kind: 'other';
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* The context at `location` in a template's strings, for editor completions:
|
|
152
|
+
* text, a tag name, an attribute name or value, or an end tag.
|
|
153
|
+
*/
|
|
154
|
+
export declare const templateContextAt: (strings: readonly string[], location: TemplateLocation, options?: {
|
|
155
|
+
svg?: boolean;
|
|
156
|
+
}) => TemplateContext;
|
|
157
|
+
export {};
|