-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsax.d.ts
179 lines (179 loc) · 6.5 KB
/
tsax.d.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
export type EventType = "cdata" | "comment" | "doctype" | "endTag" | "eof" | "error" | "processingInstruction" | "singleTag" | "startTag" | "text";
export type Attributes = {
[attributeName: string]: string;
};
export type TSax = {
next: () => EventType;
tagName: () => string | undefined;
localName: () => string | undefined;
prefix: () => string | undefined;
piTarget: () => string | undefined;
text: (raw?: boolean) => string | undefined;
attributes: (raw?: boolean) => Attributes | undefined | "error";
error: () => string | undefined;
};
/**
* @typedef
* {"cdata"|"comment"|"doctype"|"endTag"|"eof"|"error"|"processingInstruction"|"singleTag"|"startTag"|"text"}
* EventType
*/
/** @typedef {{[attributeName: string]: string}} Attributes */
/**
* @typedef {{
* next: () => EventType;
* tagName: () => string | undefined;
* localName: () => string | undefined;
* prefix: () => string | undefined;
* piTarget: () => string | undefined;
* text: (raw?: boolean) => string | undefined;
* attributes: (raw?: boolean) => Attributes | undefined | "error";
* error: () => string | undefined;
*}}
* TSax
*/
/**
* @param {string} S
*/
export function tSax(S: string): {
/**
* This is the main method for interacting with tSax. It consumes the next
* event from the XML string and returns the event type it found. Further
* parsing of data belonging to this event is only done on request, using
* methods `tagName()`, `attributes()`, `rawText()` and so on. This makes
* processing very fast when one is only interested in accessing data of
* some very specific events.
*
* @example <caption>Looking for the next occurrence of a `<foo>`
* element</caption>
* const tsax = tSax(xmlString);
* while (true) {
* switch (tsax.next()) {
* case "element":
* break;
* case "eof":
* case "error":
* throw new Error("Did not find expected element <foo>");
* default:
* continue;
* }
* if (tsax.tagName() === "foo") {
* break;
* }
* }
* console.log(tsax.attributes());
*
*
* @example <caption>Building a DOM tree</caption>
* // Intialize with the root node
* let currentElement = {children: []};
* const elementStack = [];
*
* while (true) {
* switch (tsax.next()) {
* case "startTag":
* const element = {
* tagName: tsax.tagName(),
* attributes: tsax.attributes(),
* children: [],
* };
* currentElement.children.push(element);
* currentElement = element;
* elementStack.push(element);
* break;
* case "endTag":
* currentElement = elementStack.pop();
* if (!currentElement) {
* throw new Error("Too many closing tags");
* }
* break;
* case "text":
* case "cdata":
* currentElement.children.push(tsax.rawText());
* break;
* case "error":
* throw new Error(tsax.error());
* case "eof":
* if (elementStack.length !== 1) {
* throw new Error("Missing closing tags");
* }
* // The root node only has one child, the root element.
* return currentElement.children[0];
* }
* }
* }
*
* @returns {EventType} One of the following event types. Depending on the
* current event, specific methods of the TSax object are avilable to get
* more information about the event.
*
* * `"cdata"`: A CDATA node. Available methods:
* * `rawText()`
* * `"comment"`: A comment node. Available methods:
* * `rawText()`
* * `"doctype"`: A doctype declaration. Available methods:
* * `rawText()`
* * `"endTag"`: An closing tag. Available methods:
* * `localName()`
* * `prefix()`
* * `tagName()`
* * `"eof"`: The end of the file was reached. No methods available.
* * `"error"`: An error occurred during parsing. Available methods:
* * `error()`
* * `"processingInstruction"`: A processing instruction. Available methods:
* * `rawText()`
* * `"singleTag"`: A self closing tag. Available methods:
* * `attributes()`
* * `localName()`
* * `prefix()`
* * `tagName()`
* * `"startTag"`: A start tag. The same methods as for `"singleTag"` are
* available.
* * `"text"`: A text node. Available methods:
* * `rawText()`
*/
next: () => EventType;
/**
* @returns {string|undefined} Tag name of the current event, preserving
* original upper/lower case, including prefix. `undefined` if the current
* event is not a start or end tag.
*/
tagName: () => string | undefined;
localName: () => string | undefined;
prefix: () => string | undefined;
/**
* @returns {string|undefined} The processing instruction target, i.e. the
* "tag name" of a processing instruction. `undefined` if the current event
* is not a processing instruction.
*/
piTarget: () => string | undefined;
/***
* Only available if the current event is `"cdata"`, `"comment"`,
* `"doctype"`, `"processingInstruction"`, or `"text"`. Otherwise,
* `undefined` is returned.
* @param {boolean} [raw] If `true`, will return XML text verbatim. If
* falsy, entities will be resolved.
* @returns {string|undefined} If there was a problem resolving entities,
* `undefined` is returned and an error message can be retrieved with
* `error()`.
*/
text: (raw?: boolean | undefined) => string | undefined;
/**
* @param {boolean} [raw] If `true`, will return attribute values varbatim.
* If falsy, entities will be resolved.
* @returns {Attributes | undefined | "error"} An object mapping attribute
* names to attribute values. Attribute names include prefixes. Namespace
* declarations are treated like regular attributes.
*
* Only available if the current event is `"singleTag"` or `"startTag"`,
* otherwise returns `undefined`.
*
* If an error occurs during attribute parsing, returns `"error"`. To get
* the error message, use the `error()` method.
*/
attributes: (raw?: boolean | undefined) => Attributes | undefined | "error";
/**
* @returns Error message. `undefined` unless the current event is an error
* event.
*/
error: () => string | undefined;
};