@rohal12/spindle 0.59.22 → 0.59.24
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 +3 -0
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +1707 -1273
- package/dist/pkg/macro-registry.js +575 -0
- package/dist/pkg/story-variables.js +1596 -1239
- package/dist/pkg/tooling.js +24 -16
- package/package.json +2 -1
- package/src/code-check.ts +293 -56
- package/src/components/PassageDialog.tsx +28 -1
- package/src/components/macros/MenubarButtons.tsx +3 -0
- package/src/components/macros/SettingsControls.tsx +10 -6
- package/src/components/macros/Type.tsx +1 -1
- package/src/components/macros/detached-body.tsx +62 -3
- package/src/hooks/use-modal-focus.ts +62 -19
- package/src/markup/ast.ts +88 -1
- package/src/markup/line-column.ts +14 -0
- package/src/markup/pair.ts +331 -0
- package/src/markup/parse.ts +166 -36
- package/src/markup/render.tsx +15 -4
- package/src/markup/spindle.d.peggy.ts +3 -3
- package/src/markup/spindle.peggy +81 -180
- package/src/markup/tokens.ts +49 -9
- package/src/markup/validate.ts +167 -79
- package/src/settings.ts +9 -0
- package/src/static-literal.ts +292 -0
- package/src/story-variables.ts +148 -10
- package/src/tooling.ts +111 -9
- package/src/types-drift-check.ts +52 -0
- package/src/widgets/ast-scanner.ts +16 -4
- package/src/widgets/widget-def.ts +93 -22
- package/types/tooling.d.ts +498 -7
package/src/markup/spindle.peggy
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
// compiled to its parser on load (scripts/peggy.ts); parse.ts wraps it.
|
|
3
3
|
//
|
|
4
4
|
// Start rules:
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
5
|
+
// Tokens the flat token stream (tokens.ts Token), without nesting. The
|
|
6
|
+
// nesting (which closer closes which opener) is pair.ts; the AST
|
|
7
|
+
// is built from the paired tokens (ast.ts).
|
|
8
|
+
// SelectorsPrefix the selectors that start the input.
|
|
9
9
|
//
|
|
10
10
|
// options.text selects text mode (HTML attribute values, labels): no links
|
|
11
11
|
// or HTML, and a backslash run before a brace is paired up.
|
|
@@ -28,7 +28,6 @@ const RB = String.fromCharCode(125);
|
|
|
28
28
|
const { SIGIL_SCOPES, withSelectors } = shared;
|
|
29
29
|
const VOID_TAGS = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']);
|
|
30
30
|
const BRANCH_PARENT = { elseif: 'if', else: 'if', case: 'switch', default: 'switch', next: 'timed' };
|
|
31
|
-
const BRANCHING = new Set(['if', 'switch', 'timed']);
|
|
32
31
|
const LINK_SEPARATORS = [['|', false], ['->', false], ['<-', true]];
|
|
33
32
|
const NO_SELECTORS = {};
|
|
34
33
|
/**
|
|
@@ -56,14 +55,10 @@ function parseLink(inner, at, indexOf) {
|
|
|
56
55
|
const before = part(0, idx);
|
|
57
56
|
const after = part(idx + separator.length, inner.length);
|
|
58
57
|
const [display, target] = targetFirst ? [after, before] : [before, after];
|
|
59
|
-
return { display: display.value, target: target.value, targetStart: target.start, targetEnd: target.end };
|
|
58
|
+
return { display: display.value, displayStart: display.start, displayEnd: display.end, target: target.value, targetStart: target.start, targetEnd: target.end };
|
|
60
59
|
}
|
|
61
60
|
const whole = part(0, inner.length);
|
|
62
|
-
return { display: whole.value, target: whole.value, targetStart: whole.start, targetEnd: whole.end };
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function quoteArg(value) {
|
|
66
|
-
return '"' + value.replace(/[\\"]/g, '\\$&') + '"';
|
|
61
|
+
return { display: whole.value, displayStart: whole.start, displayEnd: whole.end, target: whole.value, targetStart: whole.start, targetEnd: whole.end };
|
|
67
62
|
}
|
|
68
63
|
|
|
69
64
|
function selectorsOf(parts) {
|
|
@@ -82,16 +77,6 @@ function label(tag) {
|
|
|
82
77
|
if (tag.lex === 'html') return (tag.isClose ? '</' : '<') + tag.tag + '>';
|
|
83
78
|
return LB + (tag.isClose ? '/' : '') + tag.name + RB;
|
|
84
79
|
}
|
|
85
|
-
function closerOf(tag) {
|
|
86
|
-
return tag.lex === 'html' ? '</' + tag.tag + '>' : LB + '/' + tag.name + RB;
|
|
87
|
-
}
|
|
88
|
-
function at(loc) {
|
|
89
|
-
return 'line ' + loc.start.line + ', column ' + loc.start.column;
|
|
90
|
-
}
|
|
91
|
-
function sameName(open, close) {
|
|
92
|
-
return open.lex === close.lex && open.lower === close.lower;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
80
|
/** A lexeme: what one token of the flat stream holds, with its span. */
|
|
96
81
|
function lexeme(lex, loc, fields) {
|
|
97
82
|
fields.lex = lex;
|
|
@@ -101,38 +86,22 @@ function lexeme(lex, loc, fields) {
|
|
|
101
86
|
return fields;
|
|
102
87
|
}
|
|
103
88
|
|
|
104
|
-
/** The AST node of a lexeme that stands alone (not a macro or element). */
|
|
105
|
-
function nodeOf(x) {
|
|
106
|
-
switch (x.lex) {
|
|
107
|
-
case 'text':
|
|
108
|
-
return x.comment ? { type: 'text', value: x.value, comment: true } : { type: 'text', value: x.value };
|
|
109
|
-
case 'link':
|
|
110
|
-
return withSelectors({ type: 'macro', name: 'link', rawArgs: quoteArg(x.display) + ' ' + quoteArg(x.target), children: [] }, x);
|
|
111
|
-
case 'variable':
|
|
112
|
-
return withSelectors({ type: 'variable', name: x.name, scope: x.scope }, x);
|
|
113
|
-
case 'expression':
|
|
114
|
-
return withSelectors({ type: 'expression', expression: x.expression }, x);
|
|
115
|
-
default:
|
|
116
|
-
return x; // already a node
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
89
|
/** The token of a lexeme (tokens.ts Token). */
|
|
121
90
|
function tokenOf(x) {
|
|
122
91
|
const span = { start: x.start, end: x.end };
|
|
123
92
|
switch (x.lex) {
|
|
124
93
|
case 'text':
|
|
125
|
-
return { type: 'text', value: x.value, ...span };
|
|
94
|
+
return x.comment ? { type: 'text', value: x.value, comment: true, ...span } : { type: 'text', value: x.value, ...span };
|
|
126
95
|
case 'link':
|
|
127
|
-
return withSelectors({ type: 'link', display: x.display, target: x.target, targetStart: x.targetStart, targetEnd: x.targetEnd, ...span }, x);
|
|
96
|
+
return withSelectors({ type: 'link', display: x.display, displayStart: x.displayStart, displayEnd: x.displayEnd, target: x.target, targetStart: x.targetStart, targetEnd: x.targetEnd, ...x.selectorSpan, ...span }, x);
|
|
128
97
|
case 'variable':
|
|
129
|
-
return withSelectors({ type: 'variable', name: x.name, scope: x.scope, ...span }, x);
|
|
98
|
+
return withSelectors({ type: 'variable', name: x.name, nameStart: x.nameStart, nameEnd: x.nameEnd, scope: x.scope, ...x.selectorSpan, ...span }, x);
|
|
130
99
|
case 'expression':
|
|
131
|
-
return withSelectors({ type: 'expression', expression: x.expression, ...span }, x);
|
|
100
|
+
return withSelectors({ type: 'expression', expression: x.expression, expressionStart: x.expressionStart, expressionEnd: x.expressionEnd, ...x.selectorSpan, ...span }, x);
|
|
132
101
|
case 'macro':
|
|
133
|
-
return withSelectors({ type: 'macro', name: x.name, rawArgs: x.rawArgs, isClose: x.isClose, ...span }, x);
|
|
102
|
+
return withSelectors({ type: 'macro', name: x.name, nameStart: x.nameStart, nameEnd: x.nameEnd, rawArgs: x.rawArgs, argsStart: x.argsStart, argsEnd: x.argsEnd, isClose: x.isClose, ...x.selectorSpan, ...span }, x);
|
|
134
103
|
case 'html':
|
|
135
|
-
return { type: 'html', tag: x.tag, attributes: x.attributes, isClose: x.isClose, isSelfClose: x.isSelfClose, ...span };
|
|
104
|
+
return { type: 'html', tag: x.tag, tagNameStart: x.tagNameStart, tagNameEnd: x.tagNameEnd, attributes: x.attributes, attributeSpans: x.attributeSpans, isClose: x.isClose, isSelfClose: x.isSelfClose, ...span };
|
|
136
105
|
}
|
|
137
106
|
}
|
|
138
107
|
|
|
@@ -140,7 +109,7 @@ function tokenOf(x) {
|
|
|
140
109
|
* Flatten items (lexemes, nodes, arrays of them, nulls) and join adjacent
|
|
141
110
|
* text, except escaped braces and comments, which stay a text item of their own.
|
|
142
111
|
*/
|
|
143
|
-
function joinText(items
|
|
112
|
+
function joinText(items) {
|
|
144
113
|
const out = [];
|
|
145
114
|
let last = null;
|
|
146
115
|
const add = (item) => {
|
|
@@ -155,7 +124,7 @@ function joinText(items, convert) {
|
|
|
155
124
|
last = item;
|
|
156
125
|
};
|
|
157
126
|
add(items);
|
|
158
|
-
return
|
|
127
|
+
return out.map(tokenOf);
|
|
159
128
|
}
|
|
160
129
|
}}
|
|
161
130
|
|
|
@@ -192,17 +161,32 @@ function linkSeparatorIndex(inner, at, separator) {
|
|
|
192
161
|
}
|
|
193
162
|
return -1;
|
|
194
163
|
}
|
|
164
|
+
/**
|
|
165
|
+
* Where the selectors `s` are written, from `start` up to `after`, the end of
|
|
166
|
+
* what Selectors consumed (the one space that may follow them is not part of
|
|
167
|
+
* them): the fields of a token; none without selectors.
|
|
168
|
+
*/
|
|
169
|
+
function selectorSpan(s, start, after) {
|
|
170
|
+
if (s === NO_SELECTORS) return {};
|
|
171
|
+
return { selectorsStart: start, selectorsEnd: input.charAt(after - 1) === ' ' ? after - 1 : after };
|
|
172
|
+
}
|
|
195
173
|
function skipTo(end) {
|
|
196
174
|
skipEnd = end;
|
|
197
175
|
return end >= 0;
|
|
198
176
|
}
|
|
199
|
-
|
|
200
|
-
|
|
177
|
+
/** Fail with `message` at `loc`; `code` tells the kind of error apart (MarkupError). */
|
|
178
|
+
function fail(message, loc, code) {
|
|
179
|
+
try {
|
|
180
|
+
error(message, loc);
|
|
181
|
+
} catch (e) {
|
|
182
|
+
e.code = code;
|
|
183
|
+
throw e;
|
|
184
|
+
}
|
|
201
185
|
}
|
|
202
186
|
function attrs(list) {
|
|
203
187
|
const attributes = {};
|
|
204
188
|
const seen = new Set();
|
|
205
|
-
for (const
|
|
189
|
+
for (const { name, value } of list) {
|
|
206
190
|
const lower = name.toLowerCase();
|
|
207
191
|
if (seen.has(lower)) continue;
|
|
208
192
|
seen.add(lower);
|
|
@@ -210,114 +194,12 @@ function attrs(list) {
|
|
|
210
194
|
}
|
|
211
195
|
return attributes;
|
|
212
196
|
}
|
|
213
|
-
|
|
214
|
-
/** Check the body of a macro or element against its opening tag. */
|
|
215
|
-
function checkBody(open, body) {
|
|
216
|
-
for (const branch of body.branches) {
|
|
217
|
-
const parent = BRANCH_PARENT[branch.tag.lower];
|
|
218
|
-
if (open.lex !== 'macro' || open.lower !== parent) {
|
|
219
|
-
fail(label(branch.tag) + ' must be directly inside ' + LB + parent + RB + ', not inside ' + label(open), branch.tag.loc);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
if (!body.end) {
|
|
223
|
-
fail('Unclosed ' + label(open) + ': no ' + closerOf(open) + ' closes it', open.loc);
|
|
224
|
-
}
|
|
225
|
-
if (!sameName(open, body.end)) {
|
|
226
|
-
fail(label(body.end) + ' found where ' + closerOf(open) + ' should close the ' + label(open) + ' opened at ' + at(open.loc), body.end.loc);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
function macroNode(tag, body) {
|
|
231
|
-
const node = { type: 'macro', name: tag.lower, rawArgs: tag.rawArgs, children: [] };
|
|
232
|
-
if (!body) return withSelectors(node, tag);
|
|
233
|
-
checkBody(tag, body);
|
|
234
|
-
node.children = body.children;
|
|
235
|
-
if (BRANCHING.has(tag.lower)) {
|
|
236
|
-
node.branches = [withSelectors({ rawArgs: tag.rawArgs, children: body.children }, tag)];
|
|
237
|
-
for (const b of body.branches) {
|
|
238
|
-
node.branches.push(withSelectors({ rawArgs: b.tag.rawArgs, children: b.children }, b.tag));
|
|
239
|
-
}
|
|
240
|
-
node.children = [];
|
|
241
|
-
} else {
|
|
242
|
-
withSelectors(node, tag);
|
|
243
|
-
}
|
|
244
|
-
return node;
|
|
245
|
-
}
|
|
246
197
|
}
|
|
247
198
|
|
|
248
|
-
// ---------------------------------------------------------------- structure
|
|
249
|
-
|
|
250
|
-
Markup
|
|
251
|
-
= nodes:Content Tail { return nodes; }
|
|
252
|
-
|
|
253
|
-
/** What ends the top level: the end, or a closer or branch with no opener. */
|
|
254
|
-
Tail
|
|
255
|
-
= !.
|
|
256
|
-
/ tag:BranchTag { fail(label(tag) + ' must be directly inside ' + LB + BRANCH_PARENT[tag.lower] + RB, tag.loc); }
|
|
257
|
-
/ tag:CloseTag { fail(label(tag) + ' closes nothing: no ' + (tag.lex === 'html' ? '<' + tag.tag + '>' : LB + tag.name + RB) + ' is open here', tag.loc); }
|
|
258
|
-
|
|
259
|
-
Content
|
|
260
|
-
= items:Item* { return joinText(items, nodeOf); }
|
|
261
|
-
|
|
262
|
-
Item
|
|
263
|
-
= TextRun
|
|
264
|
-
/ Escape
|
|
265
|
-
/ Comment
|
|
266
|
-
/ LinkLexeme
|
|
267
|
-
/ EmailAutolink
|
|
268
|
-
/ HtmlElement
|
|
269
|
-
/ VoidCloser
|
|
270
|
-
/ Value
|
|
271
|
-
/ MacroElement
|
|
272
|
-
/ Literal
|
|
273
|
-
|
|
274
|
-
MacroElement
|
|
275
|
-
= tag:MacroTag &{ return tag.kind === 'open'; }
|
|
276
|
-
body:( &{ rawName = tag.lower; return hooks.isRaw(rawName); } b:RawBody { return b; }
|
|
277
|
-
/ &{ return hooks.isBlock(tag.lower); } b:BlockBody { return b; }
|
|
278
|
-
/ "" { return null; } )
|
|
279
|
-
{ return macroNode(tag, body); }
|
|
280
|
-
|
|
281
|
-
HtmlElement
|
|
282
|
-
= &{ return !textMode; } open:HtmlOpen
|
|
283
|
-
body:( &{ return !open.isSelfClose; } b:BlockBody { return b; } )?
|
|
284
|
-
{
|
|
285
|
-
const node = { type: 'html', tag: open.tag, attributes: open.attributes, children: [] };
|
|
286
|
-
if (body) {
|
|
287
|
-
checkBody(open, body);
|
|
288
|
-
node.children = body.children;
|
|
289
|
-
}
|
|
290
|
-
return node;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
BlockBody
|
|
294
|
-
= children:Content branches:BranchPart* end:BlockEnd { return { children, branches, end }; }
|
|
295
|
-
|
|
296
|
-
BranchPart
|
|
297
|
-
= tag:BranchTag children:Content { return { tag, children }; }
|
|
298
|
-
|
|
299
|
-
BlockEnd
|
|
300
|
-
= CloseTag
|
|
301
|
-
/ !. { return null; }
|
|
302
|
-
|
|
303
|
-
BranchTag
|
|
304
|
-
= tag:MacroTag &{ return tag.kind === 'branch'; } { return tag; }
|
|
305
|
-
|
|
306
|
-
CloseTag
|
|
307
|
-
= tag:MacroTag &{ return tag.kind === 'close'; } { return tag; }
|
|
308
|
-
/ &{ return !textMode; } tag:HtmlClose { return tag; }
|
|
309
|
-
|
|
310
|
-
/** A raw body ({do}): JavaScript up to its closer, kept as one text node. */
|
|
311
|
-
RawBody
|
|
312
|
-
= start:Here &{ return skipTo(hooks.rawBodyEnd(input, start, rawName)); }
|
|
313
|
-
body:$(.|{ return skipEnd - offset(); }|) end:CloseTag
|
|
314
|
-
{ return { children: body ? [{ type: 'text', value: body }] : [], branches: [], end }; }
|
|
315
|
-
/ "" { return { children: [], branches: [], end: null }; }
|
|
316
|
-
|
|
317
199
|
// ------------------------------------------------------------- flat tokens
|
|
318
200
|
|
|
319
201
|
Tokens
|
|
320
|
-
= items:TokenItem* { return joinText(items
|
|
202
|
+
= items:TokenItem* { return joinText(items); }
|
|
321
203
|
|
|
322
204
|
TokenItem
|
|
323
205
|
= TextRun
|
|
@@ -333,6 +215,11 @@ TokenItem
|
|
|
333
215
|
{ return body ? [tag, body] : tag; }
|
|
334
216
|
/ Literal
|
|
335
217
|
|
|
218
|
+
/** The closing tag that ends a raw body. */
|
|
219
|
+
CloseTag
|
|
220
|
+
= tag:MacroTag &{ return tag.kind === 'close'; } { return tag; }
|
|
221
|
+
/ &{ return !textMode; } tag:HtmlClose { return tag; }
|
|
222
|
+
|
|
336
223
|
/** A raw body and its closer, as tokens; nothing if it is unclosed. */
|
|
337
224
|
RawTokens
|
|
338
225
|
= start:Here &{ return skipTo(hooks.rawBodyEnd(input, start, rawName)); }
|
|
@@ -392,10 +279,12 @@ Literal
|
|
|
392
279
|
/ "[" { return lexeme('text', location(), { value: '[' }); }
|
|
393
280
|
|
|
394
281
|
LinkLexeme
|
|
395
|
-
= &{ return !textMode; } "[[" s:Selectors at:Here inner:$LinkText "]]" {
|
|
396
|
-
|
|
282
|
+
= &{ return !textMode; } "[[" ss:Here s:Selectors at:Here inner:$LinkText "]]" {
|
|
283
|
+
const link = lexeme('link', location(), parseLink(inner, at, (separator) => linkSeparatorIndex(inner, at, separator)));
|
|
284
|
+
link.selectorSpan = selectorSpan(s, ss, at);
|
|
285
|
+
return withSelectors(link, s);
|
|
397
286
|
}
|
|
398
|
-
/ &{ return !textMode; } "[[" { fail('Unclosed link: [[ without ]]', location()); }
|
|
287
|
+
/ &{ return !textMode; } "[[" { fail('Unclosed link: [[ without ]]', location(), 'unclosed-link'); }
|
|
399
288
|
|
|
400
289
|
/**
|
|
401
290
|
* A link's text, up to the first ]]. (Nested [[…]] pairs, as the
|
|
@@ -423,13 +312,17 @@ SelectorChar
|
|
|
423
312
|
|
|
424
313
|
/** A variable `{$a.b}` or expression `{$a + 1}`, `{(…)}`, `{!…}`. */
|
|
425
314
|
Value
|
|
426
|
-
= "{" s:Selectors sigil:[$_@%] name:$[a-zA-Z0-9_.]* "}" {
|
|
427
|
-
|
|
315
|
+
= "{" ss:Here s:Selectors sigil:[$_@%] nameStart:Here name:$[a-zA-Z0-9_.]* "}" {
|
|
316
|
+
const variable = lexeme('variable', location(), { name, nameStart, nameEnd: nameStart + name.length, scope: SIGIL_SCOPES[sigil] });
|
|
317
|
+
variable.selectorSpan = selectorSpan(s, ss, nameStart - 1);
|
|
318
|
+
return withSelectors(variable, s);
|
|
428
319
|
}
|
|
429
|
-
/ "{" s:Selectors &[$_@%(!] expression:CodeToBrace "}" {
|
|
430
|
-
|
|
320
|
+
/ "{" ss:Here s:Selectors after:Here &[$_@%(!] expression:CodeToBrace "}" {
|
|
321
|
+
const node = lexeme('expression', location(), { expression, expressionStart: after, expressionEnd: after + expression.length });
|
|
322
|
+
node.selectorSpan = selectorSpan(s, ss, after);
|
|
323
|
+
return withSelectors(node, s);
|
|
431
324
|
}
|
|
432
|
-
/ "{" Selectors &[$_@%(!] { fail('Unclosed ' + LB + input.substr(location().end.offset, 1) + '…: no ' + RB + ' ends it', location()); }
|
|
325
|
+
/ "{" Selectors &[$_@%(!] { fail('Unclosed ' + LB + input.substr(location().end.offset, 1) + '…: no ' + RB + ' ends it', location(), 'unclosed-expression'); }
|
|
433
326
|
|
|
434
327
|
/** JavaScript up to the } that ends it: where is up to options.hooks. */
|
|
435
328
|
CodeToBrace
|
|
@@ -437,16 +330,21 @@ CodeToBrace
|
|
|
437
330
|
|
|
438
331
|
/** A macro tag: `{name args}`, `{/name}`, with selectors on an opener. */
|
|
439
332
|
MacroTag
|
|
440
|
-
= "{" s:Selectors start:Here close:"/"? name:MacroName nameEnd:Here
|
|
333
|
+
= "{" ss:Here s:Selectors start:Here close:"/"? name:MacroName nameEnd:Here
|
|
441
334
|
tail:( &{ return skipTo(hooks.closeBrace(input, nameEnd, start)); } args:$(.|{ return skipEnd - offset(); }|) "}" { return args; } )? {
|
|
442
335
|
const loc = location();
|
|
443
336
|
const lower = name.toLowerCase();
|
|
444
337
|
const tag = lexeme('macro', loc, { name, lower, rawArgs: '', isClose: !!close });
|
|
445
|
-
|
|
338
|
+
tag.nameStart = nameEnd - name.length;
|
|
339
|
+
tag.nameEnd = nameEnd;
|
|
340
|
+
tag.selectorSpan = selectorSpan(s, ss, start);
|
|
341
|
+
if (tail === null) fail('Unclosed ' + LB + (close ? '/' : '') + name + '…: no ' + RB + ' ends the macro', loc, 'unclosed-macro');
|
|
446
342
|
tag.rawArgs = tail.trim();
|
|
343
|
+
tag.argsStart = tag.rawArgs ? nameEnd + tail.length - tail.trimStart().length : nameEnd;
|
|
344
|
+
tag.argsEnd = tag.argsStart + tag.rawArgs.length;
|
|
447
345
|
if (close) {
|
|
448
|
-
if (s !== NO_SELECTORS) fail('A closing tag takes no selectors: ' + label(tag), loc);
|
|
449
|
-
if (tag.rawArgs) fail('A closing tag takes no arguments: ' + LB + '/' + name + ' ' + tag.rawArgs + RB, loc);
|
|
346
|
+
if (s !== NO_SELECTORS) fail('A closing tag takes no selectors: ' + label(tag), loc, 'closer-with-selectors');
|
|
347
|
+
if (tag.rawArgs) fail('A closing tag takes no arguments: ' + LB + '/' + name + ' ' + tag.rawArgs + RB, loc, 'closer-with-arguments');
|
|
450
348
|
tag.kind = 'close';
|
|
451
349
|
} else {
|
|
452
350
|
withSelectors(tag, s);
|
|
@@ -454,7 +352,7 @@ MacroTag
|
|
|
454
352
|
}
|
|
455
353
|
return tag;
|
|
456
354
|
}
|
|
457
|
-
/ "{" s:Selectors "/" { fail('A closing tag starts with a letter after ' + LB + '/', location()); }
|
|
355
|
+
/ "{" s:Selectors "/" { fail('A closing tag starts with a letter after ' + LB + '/', location(), 'invalid-closer'); }
|
|
458
356
|
|
|
459
357
|
/** A macro name: a letter, then anything up to whitespace or }. */
|
|
460
358
|
MacroName
|
|
@@ -471,27 +369,23 @@ EmailAutolink
|
|
|
471
369
|
&{ return EMAIL_AUTOLINK.test(value); }
|
|
472
370
|
{ return lexeme('text', location(), { value }); }
|
|
473
371
|
|
|
474
|
-
/** A redundant closer of a void element (</br>), which is dropped. */
|
|
475
|
-
VoidCloser
|
|
476
|
-
= &{ return !textMode; } tag:HtmlClose &{ return VOID_TAGS.has(tag.lower); } { return null; }
|
|
477
|
-
|
|
478
372
|
HtmlOpen
|
|
479
|
-
= "<" tag:TagName list:Attribute* _ slash:"/"? ">" {
|
|
373
|
+
= "<" tagStart:Here tag:TagName list:Attribute* _ slash:"/"? ">" {
|
|
480
374
|
const lower = tag.toLowerCase();
|
|
481
|
-
return lexeme('html', location(), { tag, lower, attributes: attrs(list), isClose: false, isSelfClose: !!slash || VOID_TAGS.has(lower) });
|
|
375
|
+
return lexeme('html', location(), { tag, lower, tagNameStart: tagStart, tagNameEnd: tagStart + tag.length, attributes: attrs(list), attributeSpans: list, isClose: false, isSelfClose: !!slash || VOID_TAGS.has(lower) });
|
|
482
376
|
}
|
|
483
377
|
/ "<" tag:TagName &(_c / "/" / ">" / !.) Attribute* _ bad:Here {
|
|
484
378
|
// Only the offset of a location is used (parse.ts reads line and
|
|
485
379
|
// column from it).
|
|
486
380
|
const c = input.charAt(bad);
|
|
487
|
-
if (!c) fail('Unclosed tag <' + tag + ': no > ends it (write < for a < that starts no tag)', location());
|
|
381
|
+
if (!c) fail('Unclosed tag <' + tag + ': no > ends it (write < for a < that starts no tag)', location(), 'unclosed-tag');
|
|
488
382
|
const at = { source: location().source, start: { offset: bad, line: 0, column: 0 }, end: { offset: bad + 1, line: 0, column: 0 } };
|
|
489
|
-
fail('Unexpected ' + JSON.stringify(c) + ' in the tag <' + tag + '>: expected an attribute, or > to end the tag', at);
|
|
383
|
+
fail('Unexpected ' + JSON.stringify(c) + ' in the tag <' + tag + '>: expected an attribute, or > to end the tag', at, 'unexpected-character');
|
|
490
384
|
}
|
|
491
385
|
|
|
492
386
|
HtmlClose
|
|
493
|
-
= "</" tag:TagName _ ">" {
|
|
494
|
-
return lexeme('html', location(), { tag, lower: tag.toLowerCase(), attributes: {}, isClose: true, isSelfClose: false });
|
|
387
|
+
= "</" tagStart:Here tag:TagName _ ">" {
|
|
388
|
+
return lexeme('html', location(), { tag, lower: tag.toLowerCase(), tagNameStart: tagStart, tagNameEnd: tagStart + tag.length, attributes: {}, attributeSpans: [], isClose: true, isSelfClose: false });
|
|
495
389
|
}
|
|
496
390
|
|
|
497
391
|
TagName
|
|
@@ -502,15 +396,22 @@ TagName
|
|
|
502
396
|
* `>`, `/` or `=`; and not `<`, `{` or `}`, which markup can't use there.
|
|
503
397
|
*/
|
|
504
398
|
Attribute
|
|
505
|
-
= _ name:$(!(_c / ["'>/=<{}]) .)+ &{ attrCode = hooks.isCodeAttribute(name); return true; } value:(_ "=" _ v:AttrValue { return v; })? {
|
|
506
|
-
|
|
399
|
+
= _ nameStart:Here name:$(!(_c / ["'>/=<{}]) .)+ &{ attrCode = hooks.isCodeAttribute(name); return true; } value:(_ "=" _ v:AttrValue { return v; })? {
|
|
400
|
+
const attribute = { name, value: value ? value.value : '', nameStart, nameEnd: nameStart + name.length };
|
|
401
|
+
if (value) {
|
|
402
|
+
attribute.valueStart = value.start;
|
|
403
|
+
attribute.valueEnd = value.end;
|
|
404
|
+
if (value.quote) attribute.quote = value.quote;
|
|
405
|
+
}
|
|
406
|
+
return attribute;
|
|
507
407
|
}
|
|
508
408
|
|
|
409
|
+
/** An attribute value: its text, where that is written and its quote, if any. */
|
|
509
410
|
AttrValue
|
|
510
|
-
= '"'
|
|
511
|
-
/ "'"
|
|
512
|
-
/ q:["'] { fail('Unclosed attribute value: the ' + q + ' here has no closing ' + q, location()); }
|
|
513
|
-
/
|
|
411
|
+
= '"' start:Here value:$(ValuePart / [^"])* end:Here '"' { return { value, start, end, quote: '"' }; }
|
|
412
|
+
/ "'" start:Here value:$(ValuePart / [^'])* end:Here "'" { return { value, start, end, quote: "'" }; }
|
|
413
|
+
/ q:["'] { fail('Unclosed attribute value: the ' + q + ' here has no closing ' + q, location(), 'unclosed-attribute'); }
|
|
414
|
+
/ start:Here value:$[^ \t\n\r\x0B\x0C\u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF>]* end:Here { return { value, start, end }; }
|
|
514
415
|
|
|
515
416
|
/** Markup in a quoted value, whose quotes don't end the value. */
|
|
516
417
|
ValuePart
|
package/src/markup/tokens.ts
CHANGED
|
@@ -43,48 +43,88 @@ export function withSelectors<T extends Selectors>(
|
|
|
43
43
|
return target;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Where a part of a token is written: `xStart` up to `xEnd`, for the part
|
|
48
|
+
* named `x` (UTF-16 offsets into the input).
|
|
49
|
+
*/
|
|
50
|
+
export type Spans<Part extends string> = Record<
|
|
51
|
+
`${Part}Start` | `${Part}End`,
|
|
52
|
+
number
|
|
53
|
+
>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Where the selectors of a token are written, without the space that may
|
|
57
|
+
* follow them. Both are absent for a token without selectors.
|
|
58
|
+
*/
|
|
59
|
+
export type SelectorSpan = Partial<Spans<'selectors'>>;
|
|
60
|
+
|
|
46
61
|
/** Where a token is in the input: from `start` up to `end`. */
|
|
47
|
-
interface Span {
|
|
62
|
+
export interface Span {
|
|
48
63
|
start: number;
|
|
49
64
|
end: number;
|
|
50
65
|
}
|
|
51
66
|
|
|
67
|
+
/** A token that may start with `.class#id` selectors. */
|
|
68
|
+
interface SelectorToken extends Span, Selectors, SelectorSpan {}
|
|
69
|
+
|
|
52
70
|
export interface TextToken extends Span {
|
|
53
71
|
type: 'text';
|
|
54
72
|
value: string;
|
|
73
|
+
/** A closed HTML comment: markdown drops it, and so does raw rendering. */
|
|
74
|
+
comment?: true;
|
|
55
75
|
}
|
|
56
76
|
|
|
57
|
-
|
|
77
|
+
/** `display` is where the label is written, `target` the target. */
|
|
78
|
+
export interface LinkToken
|
|
79
|
+
extends SelectorToken, Spans<'display'>, Spans<'target'> {
|
|
58
80
|
type: 'link';
|
|
59
81
|
display: string;
|
|
60
82
|
target: string;
|
|
61
|
-
/** Where the target is written, from `targetStart` to `targetEnd`. */
|
|
62
|
-
targetStart: number;
|
|
63
|
-
targetEnd: number;
|
|
64
83
|
}
|
|
65
84
|
|
|
66
|
-
|
|
85
|
+
/**
|
|
86
|
+
* `name` is written after the `{`, the `/` and the selectors; `args`, where
|
|
87
|
+
* `rawArgs` is, is an empty span at `nameEnd` with no arguments.
|
|
88
|
+
*/
|
|
89
|
+
export interface MacroToken
|
|
90
|
+
extends SelectorToken, Spans<'name'>, Spans<'args'> {
|
|
67
91
|
type: 'macro';
|
|
68
92
|
name: string;
|
|
69
93
|
rawArgs: string;
|
|
70
94
|
isClose: boolean;
|
|
71
95
|
}
|
|
72
96
|
|
|
73
|
-
|
|
97
|
+
/** `name` is written without the sigil. */
|
|
98
|
+
export interface VariableToken extends SelectorToken, Spans<'name'> {
|
|
74
99
|
type: 'variable';
|
|
75
100
|
name: string;
|
|
76
101
|
scope: VariableScope;
|
|
77
102
|
}
|
|
78
103
|
|
|
79
|
-
|
|
104
|
+
/** `expression` is written after the `{` and the selectors. */
|
|
105
|
+
export interface ExpressionToken extends SelectorToken, Spans<'expression'> {
|
|
80
106
|
type: 'expression';
|
|
81
107
|
expression: string;
|
|
82
108
|
}
|
|
83
109
|
|
|
84
|
-
|
|
110
|
+
/**
|
|
111
|
+
* An attribute of an HTML tag as it is written, in order. The value is
|
|
112
|
+
* written without its quotes, and absent with no `=`.
|
|
113
|
+
*/
|
|
114
|
+
export interface AttributeSpan extends Spans<'name'>, Partial<Spans<'value'>> {
|
|
115
|
+
name: string;
|
|
116
|
+
/** The quote around the value, if it has one. */
|
|
117
|
+
quote?: '"' | "'";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** `tagName` is where the tag name is written. */
|
|
121
|
+
export interface HtmlToken extends Span, Spans<'tagName'> {
|
|
85
122
|
type: 'html';
|
|
86
123
|
tag: string;
|
|
124
|
+
/** The attribute values by name; the first of equal names (any case). */
|
|
87
125
|
attributes: Record<string, string>;
|
|
126
|
+
/** Every attribute as written, in order, duplicates included. */
|
|
127
|
+
attributeSpans: AttributeSpan[];
|
|
88
128
|
isClose: boolean;
|
|
89
129
|
isSelfClose: boolean;
|
|
90
130
|
}
|