-
Notifications
You must be signed in to change notification settings - Fork 0
/
createMfmlParser.ts
423 lines (334 loc) · 11.8 KB
/
createMfmlParser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import {IElementNode, ITextNode, Node, NodeType} from './parser-types';
import {IMessageFormatParserOptions, parseMessageFormat} from './parseMessageFormat';
import {createSaxParser, IParserOptions} from 'tag-soup';
import {isContainerNode, isTextNode} from './node-utils';
import {die} from '../misc';
export interface IMfmlParserOptions extends IMessageFormatParserOptions, IParserOptions {
}
export type MfmlParser = (input: string) => Node;
/**
* Creates an ICU MessageFormat + XML/HTML DOM parser.
*/
export function createMfmlParser(options: IMfmlParserOptions = {}): MfmlParser {
const {decodeText, decodeAttribute} = options;
const saxParserOptions: IParserOptions = {
...options,
decodeText: undefined,
decodeAttribute: undefined,
};
// The stack of nested element nodes
let ancestorNodesCount = 0;
let ancestorNodes: Array<IElementNode> = [];
// The children of the root node
let rootChildren: Array<Node>;
// The list of all nodes in parent-first order
let linearNodes: Array<Node>;
// The index of the next node among all nodes that must be parsed
let linearIndex = 0;
const saxParser = createSaxParser({
startTag(token) {
const tagStart = token.start;
const tagEnd = token.end;
// Lookup a text node that includes the start tag markup
linearIndex = findNodeIndex(linearNodes, linearIndex, tagStart, token.nameEnd);
// The text node that contains the start tag name
const textNode = linearNodes[linearIndex];
if (!isTextNode(textNode)) {
die('Unexpected start tag', tagStart);
}
const elementNode: Node = {
nodeType: NodeType.ELEMENT,
tagName: token.name,
parent: textNode.parent,
children: [],
attributes: [],
start: tagStart,
end: tagEnd,
};
// Put a container element on stack
if (!token.selfClosing) {
ancestorNodes[ancestorNodesCount++] = elementNode;
}
const splitEnd = Math.min(textNode.end, tagEnd);
const siblingNodes = textNode.parent?.children || rootChildren;
let siblingIndex = siblingNodes.indexOf(textNode);
const offset = splitTextNode(linearNodes, linearIndex, siblingNodes, siblingIndex, textNode, tagStart, splitEnd, elementNode);
linearIndex += offset + 1;
// Sibling index at which attributes may start
const attrSiblingIndex = siblingIndex += offset;
// Collect attributes
const attributes = token.attributes;
for (let i = 0; i < attributes.length; ++i) {
const attrToken = attributes[i];
const attrValue = attrToken.rawValue;
const attrValueStart = attrToken.valueStart;
const attrValueEnd = attrToken.valueEnd;
const attrChildren: Array<Node> = [];
const attrNode: Node = {
nodeType: NodeType.ATTRIBUTE,
name: attrToken.name,
children: attrChildren,
parent: elementNode,
start: attrToken.start,
end: attrToken.end,
};
elementNode.attributes.push(attrNode);
// Attr has no value
if (attrValue == null) {
continue;
}
// Attr has a plain text value
if (attrValueEnd <= linearNodes[linearIndex].start) {
attrChildren.push({
nodeType: NodeType.TEXT,
value: decodeAttribute != null ? decodeAttribute(attrValue) : attrValue,
parent: attrNode,
start: attrValueStart,
end: attrValueEnd,
});
continue;
}
while (linearIndex < linearNodes.length) {
const node = linearNodes[linearIndex];
const nodeStart = node.start;
const nodeEnd = node.end;
// MessageFormat node is too far ahead
if (attrValueEnd <= nodeStart) {
break;
}
// Skip nested MessageFormat nodes
if (textNode.parent !== node.parent) {
++linearIndex;
continue;
}
// MessageFormat node is contained by the attr value
if (attrValueStart <= nodeStart && nodeEnd <= attrValueEnd) {
// Leading text in attr value
if (attrChildren.length === 0 && nodeStart !== attrValueStart) {
attrChildren.push({
nodeType: NodeType.TEXT,
value: attrValue.substr(0, nodeStart - attrValueStart),
parent: attrNode,
start: attrValueStart,
end: nodeStart,
});
}
attrChildren.push(node);
node.parent = attrNode;
++linearIndex;
++siblingIndex;
continue;
}
// Trailing text
if (isTextNode(node) && attrValueStart < nodeStart && attrValueEnd < nodeEnd) {
attrChildren.push({
nodeType: NodeType.TEXT,
value: attrValue.substr(nodeStart - attrValueStart),
parent: attrNode,
start: nodeStart,
end: attrValueEnd,
});
break;
}
die('Unexpected attribute syntax', nodeStart);
}
if (decodeAttribute != null) {
decodeTextNodes(attrChildren, decodeAttribute);
}
}
// Remove MessageFormat nodes that are now part of attr children
if (attrSiblingIndex !== siblingIndex) {
siblingNodes.splice(attrSiblingIndex + 1, siblingIndex - attrSiblingIndex);
}
// Exit if the MessageFormat text node fully contains the start tag
if (tagEnd === splitEnd) {
return;
}
// Remove remaining chars of the start tag from the consequent text node
const tailNode = linearNodes[linearIndex];
if (isTextNode(tailNode) && tailNode.start < tagEnd && tailNode.end >= tagEnd) {
tailNode.value = tailNode.value.substring(tagEnd - tailNode.start);
tailNode.start = tagEnd;
return;
}
die('Incorrect attribute syntax', tailNode.start);
},
endTag(token) {
const tagStart = token.start;
const tagEnd = token.end;
linearIndex = findNodeIndex(linearNodes, linearIndex, tagStart, tagEnd);
if (linearIndex === -1) {
if (tagStart !== tagEnd) {
die('Incorrect end tag syntax', tagStart);
}
linearIndex = linearNodes.length;
}
// The node after which the end tag must be inserted
const anchorNode = linearNodes[linearIndex];
// The element node that was created when the start tag was read
const elementNode = ancestorNodes[ancestorNodesCount - 1];
const siblingNodes = elementNode.parent?.children || rootChildren;
const elementIndex = siblingNodes.indexOf(elementNode);
if (elementIndex === -1) {
die('Unexpected syntax', tagStart);
}
let siblingIndex = anchorNode != null ? siblingNodes.indexOf(anchorNode) : -1;
// Truncate ancestor stack as the element was closed
--ancestorNodesCount;
elementNode.end = tagEnd;
if (isTextNode(anchorNode)) {
if (siblingIndex === -1) {
die('End tag is nested incorrectly', tagStart);
}
// Remove end tag markup from the text node
const offset = splitTextNode(linearNodes, linearIndex, siblingNodes, siblingIndex, anchorNode, tagStart, tagEnd, null);
siblingIndex += offset;
linearIndex += offset;
} else if (tagStart === tagEnd) {
siblingIndex = anchorNode != null ? siblingIndex : siblingNodes.length;
if (siblingIndex === -1) {
die('Unexpected syntax', tagStart);
}
}
const childrenCount = siblingIndex - elementIndex - 1;
if (childrenCount === 0) {
return;
}
const children = elementNode.children = siblingNodes.splice(elementIndex + 1, childrenCount);
for (let i = 0; i < children.length; ++i) {
children[i].parent = elementNode;
}
},
}, saxParserOptions);
return (input) => {
rootChildren = parseMessageFormat(input, options);
linearNodes = [];
linearIndex = 0;
linearizeNodes(rootChildren, linearNodes);
saxParser.reset();
try {
saxParser.parse(input);
} finally {
ancestorNodesCount = 0;
ancestorNodes.length = 0;
linearNodes.length = 0;
}
if (decodeText != null) {
decodeTextNodes(rootChildren, decodeText);
}
if (rootChildren.length === 1) {
return rootChildren[0];
}
const rootNode: Node = {
nodeType: NodeType.FRAGMENT,
children: rootChildren,
parent: null,
start: 0,
end: input.length,
};
for (let i = 0; i < rootChildren.length; ++i) {
rootChildren[i].parent = rootNode;
}
return rootNode;
};
}
/**
* Traverse `nodes` and add them to `linearNodes` in the order of occurrence.
*/
function linearizeNodes(nodes: Array<Node>, linearNodes: Array<Node>): void {
for (let i = 0; i < nodes.length; ++i) {
const node = nodes[i];
linearNodes.push(node);
if (isContainerNode(node)) {
linearizeNodes(node.children, linearNodes);
}
}
}
/**
* Returns the index of the node among `nodes` (starting from `index`) that contains a `[start, end]` char range.
* If node wasn't found then -1 is returned.
*/
function findNodeIndex(nodes: Array<Node>, index: number, start: number, end: number): number {
for (let i = index; i < nodes.length; ++i) {
const node = nodes[i];
const nodeStart = node.start;
const nodeEnd = node.end;
if (end <= nodeStart) {
break;
}
if (start === end && nodeStart === start || isTextNode(node) && nodeStart <= start && end <= nodeEnd) {
return i;
}
}
return -1;
}
/**
* Rewrites values of descendant text nodes using `decoder`.
*/
function decodeTextNodes(nodes: Array<Node>, decoder: (name: string) => string): void {
for (let i = 0; i < nodes.length; ++i) {
const node = nodes[i];
if (isTextNode(node)) {
node.value = decoder(node.value);
continue;
}
if (isContainerNode(node)) {
decodeTextNodes(node.children, decoder);
}
}
}
/**
* Splits text node in two by char range and inserts a `node` between these new parts.
*/
function splitTextNode(linearNodes: Array<Node>, linearIndex: number, siblingNodes: Array<Node>, siblingIndex: number, textNode: ITextNode, start: number, end: number, node: Node | null): number {
const textStart = textNode.start;
const textEnd = textNode.end;
const textValue = textNode.value;
// Replace the text node with node
if (textStart === start && textEnd === end) {
if (node) {
linearNodes[linearIndex] = node;
siblingNodes[siblingIndex] = node;
} else {
linearNodes.splice(linearIndex, 1);
siblingNodes.splice(siblingIndex, 1);
}
return 0;
}
// Node goes before the text node
if (textStart === start) {
if (node) {
linearNodes.splice(linearIndex, 0, node);
siblingNodes.splice(siblingIndex, 0, node);
}
textNode.value = textValue.substring(end - textStart);
textNode.start = end;
return 0;
}
textNode.value = textValue.substring(0, start - textStart);
textNode.end = start;
// Node goes after the text node
if (textEnd === end) {
if (node) {
linearNodes.splice(linearIndex + 1, 0, node);
siblingNodes.splice(siblingIndex + 1, 0, node);
}
return 1;
}
// Node splits the text node in two
const tailNode: ITextNode = {
nodeType: NodeType.TEXT,
value: textValue.substring(end - textStart),
parent: textNode.parent,
start: textStart + end - textStart,
end: textEnd,
};
if (node) {
linearNodes.splice(linearIndex + 1, 0, node, tailNode);
siblingNodes.splice(siblingIndex + 1, 0, node, tailNode);
} else {
linearNodes.splice(linearIndex + 1, 0, tailNode);
siblingNodes.splice(siblingIndex + 1, 0, tailNode);
}
return 1;
}