-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhandler.ts
433 lines (400 loc) · 13.4 KB
/
handler.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
424
425
426
427
428
429
430
431
432
433
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.
import {
XMLParseContext,
XMLParseEvent,
XMLParseError,
ElementInfo,
} from './context.ts';
const NAME_HEAD = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/
const NAME_BODY = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD\u00B7\u0300-\u036F\u203F-\u2040.\d-]/
function isWhitespace(c: string): c is (' ' | '\n' | '\r' | '\t') {
return c === ' ' || c === '\n' || c === '\r' || c === '\t';
}
function isQuote(c: string): c is ('"' | '\'') {
return c === '"' || c === '\'';
}
export function resolveEntity(text: string): string {
let result = text;
[
{ reg: /&/g, ch: '&' },
{ reg: />/g, ch: '>' },
{ reg: /</g, ch: '<' },
{ reg: /"/g, ch: '"' },
{ reg: /'/g, ch: '\'' },
].forEach(({ reg, ch }) => {
result = result.replace(reg, ch);
});
return result;
}
// BEFORE_DOCUMENT; FOUND_LT, Error
export function handleBeforeDocument(cx: XMLParseContext, c: string): XMLParseEvent[] {
if (c === '<') {
cx.state = 'FOUND_LT';
} else {
if (!isWhitespace(c)) {
throw new XMLParseError('Non-whitespace before document.', cx);
}
}
return [];
}
// GENERAL_STUFF; FOUND_LT
export function handleGeneralStuff(cx: XMLParseContext, c: string): XMLParseEvent[] {
if (c === '<') {
cx.state = 'FOUND_LT';
} else {
cx.appendMemento(c);
}
return [];
}
// FOUND_LT; SGML_DECL, START_TAG, END_TAG, PROC_INST, Error
export function handleFoundLT(cx: XMLParseContext, c: string): XMLParseEvent[] {
let events: XMLParseEvent[] = [];
const text = resolveEntity(cx.memento).trim();
cx.clearMemento();
if (text) {
events = [['text', text, new ElementInfo(cx.peekElement()!), false]];
}
if (!isWhitespace(c)) {
if (c === '?') {
cx.state = 'PROC_INST';
} else if (c === '!') {
cx.state = 'SGML_DECL';
} else if (NAME_HEAD.test(c)) {
cx.appendMemento(c);
cx.state = 'START_TAG';
} else if (c === '/') {
cx.state = 'END_TAG';
} else {
throw new XMLParseError('Unencoded <', cx);
}
}
return events;
}
// PROC_INST; PROC_INST_ENDING
export function handleProcInst(cx: XMLParseContext, c: string): XMLParseEvent[] {
if (c === '?') {
cx.state = 'PROC_INST_ENDING';
} else {
cx.appendMemento(c);
}
return [];
}
// PROC_INST_ENDING; processing_instruction & GENERAL_STUFF, PROC_INST
export function handleProcInstEnding(cx: XMLParseContext, c: string): XMLParseEvent[] {
let events: XMLParseEvent[] = [];
if (c === '>') {
events = [['processing_instruction', cx.memento]];
cx.clearMemento();
cx.state = cx.elementLength > 0 ? 'GENERAL_STUFF' : 'BEFORE_DOCUMENT';
} else {
cx.appendMemento(`?${c}`);
cx.state = 'PROC_INST';
}
return events;
}
// SGML_DECL; CDATA, COMMENT, DOCTYPE, GENERAL_STUFF, Error
export function handleSgmlDecl(cx: XMLParseContext, c: string): XMLParseEvent[] {
let events: XMLParseEvent[] = [];
const sgmlCmd = `${cx.memento}${c}`.toUpperCase();
if (sgmlCmd === '[CDATA[') {
cx.clearMemento();
cx.state = 'CDATA';
} else if (sgmlCmd === '--') {
cx.clearMemento();
cx.state = 'COMMENT';
} else if (sgmlCmd === 'DOCTYPE') {
if (cx.elementLength > 0) {
throw new XMLParseError('Inappropriately located doctype declaration', cx);
}
cx.clearMemento();
cx.state = 'DOCTYPE';
} else if (c === '>') {
events = [['sgml_declaration', cx.memento]];
cx.clearMemento();
cx.state = cx.elementLength > 0 ? 'GENERAL_STUFF' : 'BEFORE_DOCUMENT';
} else {
cx.appendMemento(c);
}
return events;
}
// CDATA; CDATA_ENDING
export function handleCdata(cx: XMLParseContext, c: string): XMLParseEvent[] {
if (c === ']') {
cx.state = 'CDATA_ENDING';
} else {
cx.appendMemento(c);
}
return [];
}
// CDATA_ENDING; CDATA_ENDING_2, CDATA
export function handleCdataEnding(cx: XMLParseContext, c: string): XMLParseEvent[] {
if (c === ']') {
cx.state = 'CDATA_ENDING_2';
} else {
cx.appendMemento(`]${c}`);
cx.state = 'CDATA';
}
return [];
}
// CDATA_ENDING_2; text & GENERAL_STUFF, CDATA
export function handleCdataEnding2(cx: XMLParseContext, c: string): XMLParseEvent[] {
let events: XMLParseEvent[] = [];
if (c === '>') {
if (cx.memento) {
events = [['text', cx.memento, new ElementInfo(cx.peekElement()!), true]];
cx.clearMemento();
}
cx.state = 'GENERAL_STUFF';
} else if (c === ']') {
cx.appendMemento(']');
} else {
cx.appendMemento(`]]${c}`);
cx.state = 'CDATA';
}
return events;
}
// COMMENT; COMMENT_ENDING
export function handleComment(cx: XMLParseContext, c: string): XMLParseEvent[] {
if (c === '-') {
cx.state = 'COMMENT_ENDING';
} else {
cx.appendMemento(c);
}
return [];
}
// COMMENT_ENDING; COMMENT_ENDING2, COMMENT
export function handleCommentEnding(cx: XMLParseContext, c: string): XMLParseEvent[] {
if (c === '-') {
cx.state = 'COMMENT_ENDING_2';
} else {
cx.appendMemento(`-${c}`);
cx.state = 'COMMENT';
}
return [];
}
// COMMENT_ENDING_2; comment & GENERAL_STUFF, COMMENT
export function handleCommentEnding2(cx: XMLParseContext, c: string): XMLParseEvent[] {
let events: XMLParseEvent[] = [];
if (c === '>') {
const comment = cx.memento;
if (comment) {
events = [['comment', comment]];
}
cx.clearMemento();
cx.state = 'GENERAL_STUFF';
} else {
cx.appendMemento(`--${c}`);
cx.state = 'COMMENT';
}
return events;
}
// DOCTYPE; doctype & BEFORE_DOCUMENT
export function handleDoctype(cx: XMLParseContext, c: string): XMLParseEvent[] {
let events: XMLParseEvent[] = [];
if (c === '>') {
events = [['doctype', cx.memento]];
cx.clearMemento();
cx.state = 'BEFORE_DOCUMENT';
} else {
cx.appendMemento(c);
}
return events;
}
function emitStartElement(cx: XMLParseContext): XMLParseEvent[] {
const events: XMLParseEvent[] = [];
if (cx.elementLength === 1) {
events.push(['start_document']);
}
const element = cx.peekElement()!;
for (const { ns, uri } of element.prefixMappings) {
cx.registerNamespace(ns, uri);
events.push(['start_prefix_mapping', ns, uri]);
}
// Setting Namespace URI to this element and all attributes.
element.uri = cx.getNamespaceURI(element.prefix);
for (const attribute of element.attributes) {
attribute.uri = cx.getNamespaceURI(attribute.prefix);
}
events.push(['start_element', new ElementInfo(element)]);
return events;
}
// START_TAG; start_element & GENERAL_STUFF, EMPTY_ELEMENT_TAG, START_TAG_STUFF, Error
export function handleStartTag(cx: XMLParseContext, c: string): XMLParseEvent[] {
let events: XMLParseEvent[] = [];
if (NAME_BODY.test(c)) {
cx.appendMemento(c);
} else {
cx.newElement(cx.memento);
cx.clearMemento();
if (c === '>') {
events = emitStartElement(cx);
cx.state = 'GENERAL_STUFF';
} else if (c === '/') {
cx.state = 'EMPTY_ELEMENT_TAG';
} else {
if (!isWhitespace(c)) {
throw new XMLParseError('Invalid character in element name', cx);
}
cx.state = 'START_TAG_STUFF';
}
}
return events;
}
// ELEMENT_STUFF; start_element & GENERAL_STUFF, EMPTY_ELEMENT_TAG, ATTRIBUTE_NAME, Error
export function handleStartTagStuff(cx: XMLParseContext, c: string): XMLParseEvent[] {
let events: XMLParseEvent[] = [];
if (!isWhitespace(c)) {
if (c === '>') {
events = emitStartElement(cx);
cx.state = 'GENERAL_STUFF';
} else if (c === '/') {
cx.state = 'EMPTY_ELEMENT_TAG';
} else if (NAME_HEAD.test(c)) {
cx.appendMemento(c);
cx.state = 'ATTRIBUTE_NAME';
} else {
throw new XMLParseError('Invalid attribute name', cx);
}
}
return events;
}
function emitEndElement(cx: XMLParseContext, qName: string): XMLParseEvent[] {
let events: XMLParseEvent[] = [];
const element = cx.popElement()!;
if (element.qName !== qName) {
throw new XMLParseError(`Illegal element structure, ${element.qName} & ${qName}`, cx);
}
events = [['end_element', new ElementInfo(element)]];
for(const { ns, uri } of element.prefixMappings) {
events.push(['end_prefix_mapping', ns, uri]);
}
return events;
}
// EMPTY_ELEMENT_TAG; start_element & end_element & GENERAL_STUFF, start_element & end_element & end_document & AFTER_DOCUMENT, Error
export function handleEmptyElementTag(cx: XMLParseContext, c: string): XMLParseEvent[] {
let events: XMLParseEvent[] = [];
if (c !== '>') {
throw new XMLParseError('Forward-slash in start-tag not followed by >', cx);
}
const element = cx.peekElement()!;
element.emptyElement = true;
events = emitStartElement(cx).concat(emitEndElement(cx, element.qName));
if (cx.elementLength === 0) {
events.push(['end_document']);
cx.state = 'AFTER_DOCUMENT';
} else {
cx.state = 'GENERAL_STUFF';
}
return events;
}
function newAttribute(cx: XMLParseContext) {
const qName = cx.memento;
cx.clearMemento();
cx.peekElement()!.newAttribute(qName);
cx.state = 'ATTRIBUTE_EQUAL';
}
// ATTRIBUTE_NAME; ATTRIBUTE_EQUAL, ATTRIBUTE_NAME_SAW_WHITE, Error
export function handleAttributeName(cx: XMLParseContext, c: string): XMLParseEvent[] {
if (NAME_BODY.test(c)) {
cx.appendMemento(c);
} else if (isWhitespace(c)) {
cx.state = 'ATTRIBUTE_NAME_SAW_WHITE';
} else if (c === '=') {
newAttribute(cx);
} else {
throw new XMLParseError(c === '>' ? 'Attribute without value' : 'Invalid attribute name', cx);
}
return [];
}
// ATTRIBUTE_NAME_SAW_WHITE; ATTRIBUTE_EQUAL, Error
export function handleAttributeNameSawWhite(cx: XMLParseContext, c: string): XMLParseEvent[] {
if (c === '=') {
newAttribute(cx);
} else if (!isWhitespace(c)) {
throw new XMLParseError('Attribute without value', cx);
}
return [];
}
// ATTRIBUTE_EQUAL; ATTRIBUTE_VALUE_START, Error
export function handleAttributeEqual(cx: XMLParseContext, c: string): XMLParseEvent[] {
// skip whitespace
if (!isWhitespace(c)) {
if (isQuote(c)) {
cx.quote = c;
cx.state = 'ATTRIBUTE_VALUE_START';
} else {
throw new XMLParseError('Unquoted attribute value', cx);
}
}
return [];
}
// ATTRIBUTE_VALUE_START; ATTRIBUTE_VALUE_END
export function handleAttributeValueStart(cx: XMLParseContext, c: string): XMLParseEvent[] {
if (c === cx.quote) {
const value = cx.memento;
cx.clearMemento();
cx.peekElement()!.peekAttribute()!.value = resolveEntity(value);
cx.quote = '';
cx.state = 'ATTRIBUTE_VALUE_END';
} else {
cx.appendMemento(c);
}
return [];
}
// ATTRIBUTE_VALUE_END; START_TAG_STUFF, EMPTY_ELEMENT_TAG, start_element & GENERAL_STUFF, Error
export function handleAttributeValueEnd(cx: XMLParseContext, c: string): XMLParseEvent[] {
let events: XMLParseEvent[] = [];
if (isWhitespace(c)) {
cx.state = 'START_TAG_STUFF';
} else if (c === '/') {
cx.state = 'EMPTY_ELEMENT_TAG';
} else if (c === '>') {
events = emitStartElement(cx);
cx.state = 'GENERAL_STUFF';
} else {
throw new XMLParseError('Invalid attribute name', cx);
}
return events;
}
function closeElement(cx: XMLParseContext): XMLParseEvent[] {
const events = emitEndElement(cx, cx.memento);
cx.clearMemento();
if (cx.elementLength === 0) {
events.push(['end_document']);
cx.state = 'AFTER_DOCUMENT';
} else {
cx.state = 'GENERAL_STUFF';
}
return events;
}
// END_TAG; END_TAG_SAW_WHITE, AFTER_DOCUMENT, GENERAL_STUFF, Error
export function handleEndTag(cx: XMLParseContext, c: string): XMLParseEvent[] {
let events: XMLParseEvent[] = [];
if (NAME_BODY.test(c)) {
cx.appendMemento(c);
} else if (c === '>') {
events = closeElement(cx);
} else if (isWhitespace(c)) {
cx.state = 'END_TAG_SAW_WHITE';
} else {
throw new XMLParseError('Invalid element name', cx);
}
return events;
}
// END_TAG_SAW_WHITE; GENERAL_STUFF, AFTER_DOCUMENT, Error
export function handleEndTagSawWhite(cx: XMLParseContext, c: string): XMLParseEvent[] {
let events: XMLParseEvent[] = [];
if (c === '>') {
events = closeElement(cx);
} else if (!isWhitespace(c)) {
throw new XMLParseError('Invalid characters in end-tag', cx);
}
return events;
}
// AFTER_DOCUMENT; Error
export function handleAfterDocument(cx: XMLParseContext, c: string): XMLParseEvent[] {
if (!isWhitespace(c)) {
throw new XMLParseError('Non-whitespace after document.', cx);
}
return [];
}