@rohal12/spindle 0.64.10 → 0.64.12
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/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +291 -443
- package/dist/pkg/play.js +2 -1
- package/dist/pkg/story-variables.js +400 -520
- package/dist/pkg/tooling.js +2 -0
- package/package.json +4 -4
- package/src/completion-context.ts +111 -23
- package/src/format/segment.ts +34 -36
- package/src/js-lexer.ts +187 -64
- package/src/markup/html-integration.tsx +101 -0
- package/src/markup/render.tsx +67 -30
- package/src/play/session.ts +9 -1
- package/src/tooling.ts +63 -19
- package/src/twine-editor/highlight.ts +10 -11
- package/types/tooling.d.ts +42 -0
package/src/js-lexer.ts
CHANGED
|
@@ -97,6 +97,7 @@ interface ParserState {
|
|
|
97
97
|
start: number;
|
|
98
98
|
end: number;
|
|
99
99
|
exprAllowed: boolean;
|
|
100
|
+
operandHere: boolean;
|
|
100
101
|
lastTokEnd: number;
|
|
101
102
|
context: unknown[];
|
|
102
103
|
nextToken(): void;
|
|
@@ -162,14 +163,6 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
162
163
|
typeBeforeLast: TokenType = tt.eof;
|
|
163
164
|
/** Look ahead after a `%name` starting a line (off in look-aheads). */
|
|
164
165
|
lookahead = true;
|
|
165
|
-
/**
|
|
166
|
-
* Where a `%` is a transient (true) or the modulo operator (false)
|
|
167
|
-
* whatever the tokens before it (see parseCode).
|
|
168
|
-
*/
|
|
169
|
-
percentAt?: ReadonlyMap<number, boolean>;
|
|
170
|
-
/** Where it read a `%name` as a transient. */
|
|
171
|
-
transientsRead?: Set<number>;
|
|
172
|
-
|
|
173
166
|
readToken(code: number): void {
|
|
174
167
|
const self = this as unknown as ParserState;
|
|
175
168
|
// acorn updates it only when parsing; its guesses read it when
|
|
@@ -191,15 +184,12 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
191
184
|
const afterPrefix =
|
|
192
185
|
self.type === tt.incDec &&
|
|
193
186
|
(this.operandBeforeLast || this.breakBeforeLast);
|
|
194
|
-
const forced = code === 37 ? this.percentAt?.get(at) : undefined;
|
|
195
187
|
if (
|
|
196
188
|
code === 64 ||
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
(this.lookahead && transientAssignment(self, end))))
|
|
189
|
+
self.exprAllowed ||
|
|
190
|
+
afterPrefix ||
|
|
191
|
+
(this.lookahead && transientAssignment(self, end))
|
|
201
192
|
) {
|
|
202
|
-
if (code === 37) this.transientsRead?.add(at);
|
|
203
193
|
self.pos = end;
|
|
204
194
|
self.finishToken(tt.name, self.input.charAt(at) + name);
|
|
205
195
|
return;
|
|
@@ -230,6 +220,7 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
230
220
|
// or an object key (`class D { function; }`, `{ class: 1 }`)
|
|
231
221
|
const self = this as unknown as ParserState;
|
|
232
222
|
const prevType = self.type;
|
|
223
|
+
const beforePreviousValue = this.prevValue;
|
|
233
224
|
// The word before, unless it is a member name (`a.static`, `a?.let`),
|
|
234
225
|
// which no contextual keyword is
|
|
235
226
|
this.prevValue = isMemberDot(this.typeBeforeLast) ? undefined : self.value;
|
|
@@ -240,7 +231,8 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
240
231
|
// acorn takes a `function` or `class` for a statement's (whose `}` a
|
|
241
232
|
// regex may follow) after the `:` of a conditional, and `async function`
|
|
242
233
|
// always. Both are expressions where an operand is (`a ? b : class {} / 2`,
|
|
243
|
-
// `x = async function () {} / 2`)
|
|
234
|
+
// `x = async function () {} / 2`), including after contextual operators
|
|
235
|
+
// such as `of` and `await`: read them as after what comes before.
|
|
244
236
|
if (!property && (type === tt._function || type === tt._class)) {
|
|
245
237
|
const afterAsync =
|
|
246
238
|
type === tt._function &&
|
|
@@ -248,8 +240,19 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
248
240
|
this.prevValue === 'async' &&
|
|
249
241
|
!this.breakHere;
|
|
250
242
|
const before = afterAsync ? this.typeBeforeLast : prevType;
|
|
243
|
+
// `yield` ends at a line break; the following declaration then
|
|
244
|
+
// closes a statement, so a `%name` after it starts a new expression.
|
|
245
|
+
// For `async function`, retain the operand context from before
|
|
246
|
+
// `async`: parseIdent has already consumed it as an identifier.
|
|
247
|
+
const contextualOperand =
|
|
248
|
+
before === tt.name &&
|
|
249
|
+
(afterAsync ? this.operandBeforeLast : self.exprAllowed) &&
|
|
250
|
+
((afterAsync ? beforePreviousValue : this.prevValue) !== 'yield' ||
|
|
251
|
+
!(afterAsync ? this.breakBeforeLast : this.breakHere));
|
|
251
252
|
self.type =
|
|
252
|
-
before === tt.colon && this.colonEndsTernary
|
|
253
|
+
(before === tt.colon && this.colonEndsTernary) || contextualOperand
|
|
254
|
+
? tt.parenL
|
|
255
|
+
: before;
|
|
253
256
|
}
|
|
254
257
|
this.typeBeforeLast = prevType;
|
|
255
258
|
// @ts-expect-error acorn internals
|
|
@@ -270,6 +273,31 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
270
273
|
}
|
|
271
274
|
}
|
|
272
275
|
|
|
276
|
+
parseIdent(liberal?: boolean): AnyNode {
|
|
277
|
+
// The parser knows this token is an identifier, even when the
|
|
278
|
+
// tokenizer guessed `of` was a for-of separator. Set its context
|
|
279
|
+
// before consuming it, so the following `%` reads as modulo.
|
|
280
|
+
(this as unknown as ParserState).exprAllowed = false;
|
|
281
|
+
// @ts-expect-error acorn internals
|
|
282
|
+
return super.parseIdent(liberal);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
parseForIn(node: AnyNode, init: AnyNode): AnyNode {
|
|
286
|
+
// Conversely, this `of` really is a separator. In `const of of
|
|
287
|
+
// %list`, the preceding identifier can mislead the token-only guess.
|
|
288
|
+
(this as unknown as ParserState).exprAllowed = true;
|
|
289
|
+
// @ts-expect-error acorn internals
|
|
290
|
+
return super.parseForIn(node, init);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
parseAwait(forInit?: boolean): AnyNode {
|
|
294
|
+
// `await` is also tokenized as a name; here the parser knows an
|
|
295
|
+
// operand follows, including a transient such as `await %result`.
|
|
296
|
+
(this as unknown as ParserState).exprAllowed = true;
|
|
297
|
+
// @ts-expect-error acorn internals
|
|
298
|
+
return super.parseAwait(forInit);
|
|
299
|
+
}
|
|
300
|
+
|
|
273
301
|
braceIsBlock(prevType: TokenType): boolean {
|
|
274
302
|
if (prevType === tt.colon && this.colonEndsTernary) return false;
|
|
275
303
|
if (prevType === tt.braceR && this.closedBlock) return true;
|
|
@@ -343,7 +371,15 @@ function tokenizerAt(
|
|
|
343
371
|
goal: JsGoal,
|
|
344
372
|
{ onComment, afterOperand = false }: TokenizerOptions = {},
|
|
345
373
|
): ParserState {
|
|
346
|
-
|
|
374
|
+
// Consumers use absolute offsets, never Acorn's token/node locations.
|
|
375
|
+
// Its error locations are computed separately from the source. Supplying
|
|
376
|
+
// a local starting location avoids its backwards newline-prefix search
|
|
377
|
+
// for every expression on a long passage line.
|
|
378
|
+
const options = {
|
|
379
|
+
...OPTIONS,
|
|
380
|
+
startLocation: { line: 1, column: 0 },
|
|
381
|
+
onComment,
|
|
382
|
+
};
|
|
347
383
|
const p = new SigilParser(options, src, start) as unknown as ParserState;
|
|
348
384
|
if (goal === 'expression') p.type = tt.parenL;
|
|
349
385
|
if (afterOperand) p.exprAllowed = false;
|
|
@@ -533,27 +569,11 @@ function parseWithAst(
|
|
|
533
569
|
src: string,
|
|
534
570
|
goal: JsGoal,
|
|
535
571
|
): { out: ParsedCode; ast: AnyNode } {
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
for (;;) {
|
|
542
|
-
const transientsRead = new Set<number>();
|
|
543
|
-
try {
|
|
544
|
-
const ast = parseAst(src, goal, percentAt, transientsRead);
|
|
545
|
-
const out: ParsedCode = { refs: [], strings: [] };
|
|
546
|
-
walk(ast, src, out, false, false);
|
|
547
|
-
out.refs.sort((a, b) => a.start - b.start);
|
|
548
|
-
return { out, ast };
|
|
549
|
-
} catch (error) {
|
|
550
|
-
const pos = (error as { pos?: number }).pos ?? -1;
|
|
551
|
-
if (!(error instanceof SyntaxError) || percentAt.has(pos)) throw error;
|
|
552
|
-
TRANS_NAME_RE.lastIndex = pos + 1;
|
|
553
|
-
if (src.charAt(pos) !== '%' || !TRANS_NAME_RE.test(src)) throw error;
|
|
554
|
-
percentAt.set(pos, !transientsRead.has(pos));
|
|
555
|
-
}
|
|
556
|
-
}
|
|
572
|
+
const ast = parseAst(src, goal);
|
|
573
|
+
const out: ParsedCode = { refs: [], strings: [] };
|
|
574
|
+
walk(ast, src, out, false, false);
|
|
575
|
+
out.refs.sort((a, b) => a.start - b.start);
|
|
576
|
+
return { out, ast };
|
|
557
577
|
}
|
|
558
578
|
|
|
559
579
|
/** The variable at the root of a reference path, and whether it has members. */
|
|
@@ -633,24 +653,12 @@ export function readMemberPath(
|
|
|
633
653
|
return { path, end };
|
|
634
654
|
}
|
|
635
655
|
|
|
636
|
-
/**
|
|
637
|
-
|
|
638
|
-
* noting where it read a transient in `transientsRead`.
|
|
639
|
-
*/
|
|
640
|
-
function parseAst(
|
|
641
|
-
src: string,
|
|
642
|
-
goal: JsGoal,
|
|
643
|
-
percentAt: ReadonlyMap<number, boolean>,
|
|
644
|
-
transientsRead: Set<number>,
|
|
645
|
-
): AnyNode {
|
|
656
|
+
/** Parse `src` as `goal`, using the parser's context for `%name`. */
|
|
657
|
+
function parseAst(src: string, goal: JsGoal): AnyNode {
|
|
646
658
|
try {
|
|
647
659
|
const p = tokenizerAt(src, 0, goal) as ParserState & {
|
|
648
660
|
parse(): AnyNode;
|
|
649
|
-
percentAt?: ReadonlyMap<number, boolean>;
|
|
650
|
-
transientsRead?: Set<number>;
|
|
651
661
|
};
|
|
652
|
-
p.percentAt = percentAt;
|
|
653
|
-
p.transientsRead = transientsRead;
|
|
654
662
|
if (goal === 'statements') return p.parse();
|
|
655
663
|
// As acorn's parseExpressionAt, and then the code must end
|
|
656
664
|
p.nextToken();
|
|
@@ -940,36 +948,58 @@ export function findCodeEnd(
|
|
|
940
948
|
start: number,
|
|
941
949
|
{ goal = 'expression', stop, onRunOut }: FindCodeEndOptions = {},
|
|
942
950
|
): number {
|
|
943
|
-
|
|
944
|
-
|
|
951
|
+
let verifyStop = false;
|
|
952
|
+
const end = scanCodeEnd(src, start, goal, stop, onRunOut, () => {
|
|
953
|
+
verifyStop = true;
|
|
954
|
+
});
|
|
955
|
+
if (end < 0 || verifyStop) {
|
|
956
|
+
const parsed = parsedCodeEnd(src, start, goal, stop);
|
|
957
|
+
if (parsed >= 0) return parsed;
|
|
958
|
+
}
|
|
959
|
+
return end;
|
|
945
960
|
}
|
|
946
961
|
|
|
947
962
|
/**
|
|
948
963
|
* Where the code from `start` ends, as the parser finds it: the `}` it
|
|
949
|
-
* stops at, or -1. The token scan of
|
|
950
|
-
* opens a regex from the tokens before
|
|
951
|
-
*
|
|
952
|
-
*
|
|
964
|
+
* stops at (or the `{` matching `stop`), or -1. The token scan of
|
|
965
|
+
* `findCodeEnd` guesses whether a `/` opens a regex from the tokens before
|
|
966
|
+
* it, and class fields inside template literals can mislead it. The parser
|
|
967
|
+
* knows the context. Asked when the scan finds no end or a stop that might
|
|
968
|
+
* still be template text.
|
|
953
969
|
*/
|
|
954
970
|
export function parsedCodeEnd(
|
|
955
971
|
src: string,
|
|
956
972
|
start: number,
|
|
957
973
|
goal: JsGoal,
|
|
974
|
+
stop?: (index: number) => boolean,
|
|
958
975
|
): number {
|
|
959
976
|
const p = tokenizerAt(src, start, goal) as ParserState & {
|
|
960
977
|
parse(): unknown;
|
|
961
978
|
};
|
|
979
|
+
const stopped = {};
|
|
980
|
+
let stoppedAt = -1;
|
|
981
|
+
if (stop) {
|
|
982
|
+
const nextToken = p.nextToken;
|
|
983
|
+
p.nextToken = () => {
|
|
984
|
+
nextToken.call(p);
|
|
985
|
+
if (p.type === tt.braceL && stop(p.start)) {
|
|
986
|
+
stoppedAt = p.start;
|
|
987
|
+
throw stopped;
|
|
988
|
+
}
|
|
989
|
+
};
|
|
990
|
+
}
|
|
962
991
|
try {
|
|
963
992
|
if (goal === 'statements') {
|
|
964
993
|
p.parse();
|
|
965
994
|
} else {
|
|
966
995
|
p.nextToken();
|
|
967
996
|
p.parseExpression();
|
|
968
|
-
if (p.type === tt.braceR) return p.start;
|
|
997
|
+
if (!stop && p.type === tt.braceR) return p.start;
|
|
969
998
|
}
|
|
970
999
|
} catch (error) {
|
|
1000
|
+
if (error === stopped) return stoppedAt;
|
|
971
1001
|
const pos = (error as { pos?: number }).pos ?? -1;
|
|
972
|
-
if (src.charAt(pos) === '}') return pos;
|
|
1002
|
+
if (!stop && src.charAt(pos) === '}') return pos;
|
|
973
1003
|
}
|
|
974
1004
|
return -1;
|
|
975
1005
|
}
|
|
@@ -981,6 +1011,7 @@ function scanCodeEnd(
|
|
|
981
1011
|
goal: JsGoal,
|
|
982
1012
|
stop: ((index: number) => boolean) | undefined,
|
|
983
1013
|
onRunOut: (() => void) | undefined,
|
|
1014
|
+
onTemplateStop: () => void,
|
|
984
1015
|
): number {
|
|
985
1016
|
/** No end: the code runs to the end of `src`. */
|
|
986
1017
|
const ranOut = () => {
|
|
@@ -993,6 +1024,7 @@ function scanCodeEnd(
|
|
|
993
1024
|
let from = start;
|
|
994
1025
|
let afterOperand = false;
|
|
995
1026
|
let templateSlip = false;
|
|
1027
|
+
let interpolated = false;
|
|
996
1028
|
for (;;) {
|
|
997
1029
|
let resumeAt = -1;
|
|
998
1030
|
for (const tok of tokens(src, from, goal, { afterOperand })) {
|
|
@@ -1044,10 +1076,17 @@ function scanCodeEnd(
|
|
|
1044
1076
|
t === tt.num;
|
|
1045
1077
|
prevEnd = tok.end;
|
|
1046
1078
|
if (t === tt.braceL) {
|
|
1047
|
-
if (stop?.(tok.start))
|
|
1079
|
+
if (stop?.(tok.start)) {
|
|
1080
|
+
// A misleading keyword can leave the tokenizer's template
|
|
1081
|
+
// context early. Let the parser verify this possible stop once.
|
|
1082
|
+
if (interpolated) onTemplateStop();
|
|
1083
|
+
return tok.start;
|
|
1084
|
+
}
|
|
1048
1085
|
stack.push('{');
|
|
1049
|
-
} else if (t === tt.dollarBraceL)
|
|
1050
|
-
|
|
1086
|
+
} else if (t === tt.dollarBraceL) {
|
|
1087
|
+
interpolated = true;
|
|
1088
|
+
stack.push('${');
|
|
1089
|
+
} else if (t === tt.parenL) stack.push('(');
|
|
1051
1090
|
else if (t === tt.bracketL) stack.push('[');
|
|
1052
1091
|
else if (t === tt.parenR || t === tt.bracketR) {
|
|
1053
1092
|
if (last(stack) === (t === tt.parenR ? '(' : '[')) stack.pop();
|
|
@@ -1063,6 +1102,8 @@ function scanCodeEnd(
|
|
|
1063
1102
|
}
|
|
1064
1103
|
|
|
1065
1104
|
interface Tok {
|
|
1105
|
+
/** Tokenizer expected an operand before this token. */
|
|
1106
|
+
operand?: boolean;
|
|
1066
1107
|
type: TokenType;
|
|
1067
1108
|
start: number;
|
|
1068
1109
|
end: number;
|
|
@@ -1146,7 +1187,13 @@ function* tokens(
|
|
|
1146
1187
|
yield { error: error as Error, pos };
|
|
1147
1188
|
return;
|
|
1148
1189
|
}
|
|
1149
|
-
yield {
|
|
1190
|
+
yield {
|
|
1191
|
+
type: p.type,
|
|
1192
|
+
start: p.start,
|
|
1193
|
+
end: p.end,
|
|
1194
|
+
value: p.value,
|
|
1195
|
+
operand: p.operandHere,
|
|
1196
|
+
};
|
|
1150
1197
|
if (p.type === tt.eof) return;
|
|
1151
1198
|
}
|
|
1152
1199
|
}
|
|
@@ -1376,3 +1423,79 @@ export function scanStringLiteral(
|
|
|
1376
1423
|
}
|
|
1377
1424
|
return { end: src.length, closed: false };
|
|
1378
1425
|
}
|
|
1426
|
+
|
|
1427
|
+
/** Identifier replacement boundaries in decoded code (UTF-16). */
|
|
1428
|
+
export interface CompletionIdentifier extends Span {
|
|
1429
|
+
kind: 'root' | 'member';
|
|
1430
|
+
/** Decoded name, excluding a root's sigil. */
|
|
1431
|
+
name: string;
|
|
1432
|
+
/** Start of the preceding `.` or `?.`, for bracket-access edits. */
|
|
1433
|
+
dotStart?: number;
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
/** Read completion boundaries with the same tokenizer as runtime code. */
|
|
1437
|
+
export function completionIdentifier(
|
|
1438
|
+
source: string,
|
|
1439
|
+
cursor: number,
|
|
1440
|
+
goal: JsGoal = 'expression',
|
|
1441
|
+
): CompletionIdentifier | undefined {
|
|
1442
|
+
let previous: Tok | undefined;
|
|
1443
|
+
for (const token of tokens(source, 0, goal)) {
|
|
1444
|
+
if ('error' in token) {
|
|
1445
|
+
// A bare local sigil is temporarily invalid JavaScript while typing.
|
|
1446
|
+
if (source[token.pos] === '@' && cursor === token.pos + 1)
|
|
1447
|
+
return { kind: 'root', name: '', start: token.pos, end: cursor };
|
|
1448
|
+
return;
|
|
1449
|
+
}
|
|
1450
|
+
const member =
|
|
1451
|
+
previous && isMemberDot(previous.type) ? previous : undefined;
|
|
1452
|
+
if (token.start <= cursor && cursor <= token.end) {
|
|
1453
|
+
if (member && (token.type === tt.name || token.type.keyword)) {
|
|
1454
|
+
return {
|
|
1455
|
+
kind: 'member',
|
|
1456
|
+
name: token.value as string,
|
|
1457
|
+
start: token.start,
|
|
1458
|
+
end: token.end,
|
|
1459
|
+
dotStart: member.start,
|
|
1460
|
+
};
|
|
1461
|
+
}
|
|
1462
|
+
if (token.type === tt.name && !member) {
|
|
1463
|
+
const value = token.value as string;
|
|
1464
|
+
const sigil = sigilAt(source, token.start, value);
|
|
1465
|
+
if (
|
|
1466
|
+
(sigil || value === '$' || value === '_') &&
|
|
1467
|
+
!keyPosition(source, token.end, previous?.type)
|
|
1468
|
+
)
|
|
1469
|
+
return {
|
|
1470
|
+
kind: 'root',
|
|
1471
|
+
name: value.slice(1),
|
|
1472
|
+
start: token.start,
|
|
1473
|
+
end: token.end,
|
|
1474
|
+
};
|
|
1475
|
+
}
|
|
1476
|
+
if (
|
|
1477
|
+
token.type === tt.modulo &&
|
|
1478
|
+
source.slice(token.start, token.end) === '%' &&
|
|
1479
|
+
cursor === token.end &&
|
|
1480
|
+
token.operand
|
|
1481
|
+
)
|
|
1482
|
+
return { kind: 'root', name: '', start: token.start, end: token.end };
|
|
1483
|
+
}
|
|
1484
|
+
if (isMemberDot(token.type) && token.end === cursor) {
|
|
1485
|
+
// Prefer a following identifier's entire suffix, even at its start.
|
|
1486
|
+
previous = token;
|
|
1487
|
+
continue;
|
|
1488
|
+
}
|
|
1489
|
+
if (member && member.end === cursor && token.start >= cursor)
|
|
1490
|
+
return {
|
|
1491
|
+
kind: 'member',
|
|
1492
|
+
name: '',
|
|
1493
|
+
start: cursor,
|
|
1494
|
+
end: cursor,
|
|
1495
|
+
dotStart: member.start,
|
|
1496
|
+
};
|
|
1497
|
+
if (token.start > cursor || token.type === tt.eof) return;
|
|
1498
|
+
previous = token;
|
|
1499
|
+
}
|
|
1500
|
+
return undefined;
|
|
1501
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createPortal,
|
|
3
|
+
h,
|
|
4
|
+
type ComponentChildren,
|
|
5
|
+
type RefObject,
|
|
6
|
+
} from 'preact';
|
|
7
|
+
import { useLayoutEffect, useRef, useState } from 'preact/hooks';
|
|
8
|
+
|
|
9
|
+
export function isHtmlEncoding(value: unknown): boolean {
|
|
10
|
+
return (
|
|
11
|
+
typeof value === 'string' &&
|
|
12
|
+
/^(?:text\/html|application\/xhtml\+xml)$/i.test(value)
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function authorElement(
|
|
17
|
+
tag: string,
|
|
18
|
+
attributes: Record<string, unknown>,
|
|
19
|
+
children: ComponentChildren,
|
|
20
|
+
math: boolean,
|
|
21
|
+
) {
|
|
22
|
+
return math && tag === 'annotation-xml' ? (
|
|
23
|
+
<HtmlAnnotation
|
|
24
|
+
key={attributes.key as string | number | undefined}
|
|
25
|
+
attributes={attributes}
|
|
26
|
+
>
|
|
27
|
+
{children}
|
|
28
|
+
</HtmlAnnotation>
|
|
29
|
+
) : (
|
|
30
|
+
h(tag, attributes, children)
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Preact handles MathML text integration points, but does not yet switch the
|
|
36
|
+
* namespace for HTML-encoded annotation-xml. A portal's container can supply
|
|
37
|
+
* that namespace without adding a wrapper or changing the MathML element.
|
|
38
|
+
*/
|
|
39
|
+
function HtmlAnnotation({
|
|
40
|
+
attributes,
|
|
41
|
+
children,
|
|
42
|
+
}: {
|
|
43
|
+
attributes: Record<string, unknown>;
|
|
44
|
+
children: ComponentChildren;
|
|
45
|
+
}) {
|
|
46
|
+
const elementRef = useRef<Element>(null);
|
|
47
|
+
const [container, setContainer] = useState<
|
|
48
|
+
| (Pick<
|
|
49
|
+
Element,
|
|
50
|
+
| 'nodeType'
|
|
51
|
+
| 'parentNode'
|
|
52
|
+
| 'firstChild'
|
|
53
|
+
| 'childNodes'
|
|
54
|
+
| 'ownerDocument'
|
|
55
|
+
| 'insertBefore'
|
|
56
|
+
| 'appendChild'
|
|
57
|
+
| 'removeChild'
|
|
58
|
+
> & { namespaceURI: string })
|
|
59
|
+
| null
|
|
60
|
+
>(null);
|
|
61
|
+
useLayoutEffect(() => {
|
|
62
|
+
const element = elementRef.current!;
|
|
63
|
+
setContainer({
|
|
64
|
+
nodeType: 1,
|
|
65
|
+
namespaceURI: 'http://www.w3.org/1999/xhtml',
|
|
66
|
+
ownerDocument: element.ownerDocument,
|
|
67
|
+
get parentNode() {
|
|
68
|
+
return element.parentNode;
|
|
69
|
+
},
|
|
70
|
+
get firstChild() {
|
|
71
|
+
return element.firstChild;
|
|
72
|
+
},
|
|
73
|
+
get childNodes() {
|
|
74
|
+
return element.childNodes;
|
|
75
|
+
},
|
|
76
|
+
insertBefore: element.insertBefore.bind(element),
|
|
77
|
+
appendChild: element.appendChild.bind(element),
|
|
78
|
+
removeChild: element.removeChild.bind(element),
|
|
79
|
+
});
|
|
80
|
+
}, []);
|
|
81
|
+
return (
|
|
82
|
+
<>
|
|
83
|
+
{h(
|
|
84
|
+
'annotation-xml',
|
|
85
|
+
{
|
|
86
|
+
...attributes,
|
|
87
|
+
ref: (element: Element | null) => {
|
|
88
|
+
elementRef.current = element;
|
|
89
|
+
const forwarded = attributes.ref as
|
|
90
|
+
RefObject<Element | null> | undefined;
|
|
91
|
+
if (forwarded) forwarded.current = element;
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
isHtmlEncoding(attributes.encoding) ? undefined : children,
|
|
95
|
+
)}
|
|
96
|
+
{container &&
|
|
97
|
+
isHtmlEncoding(attributes.encoding) &&
|
|
98
|
+
createPortal(children, container)}
|
|
99
|
+
</>
|
|
100
|
+
);
|
|
101
|
+
}
|