forked from libxmljs/libxmljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_parser.ts
More file actions
236 lines (197 loc) · 9.66 KB
/
xml_parser.ts
File metadata and controls
236 lines (197 loc) · 9.66 KB
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
import * as fs from "fs";
import * as libxml from "../index";
import { XMLSaveOptions, XMLElement, XMLParseOptions, XMLStructuredError } from "../index";
module.exports.parse = function (assert: any) {
var filename = __dirname + "/../../test/fixtures/parser.xml";
var str = fs.readFileSync(filename, "utf8").replace(/[\r]+/g, '');
var doc = libxml.parseXml(str);
assert.equal("1.0", doc.version());
assert.equal("UTF-8", doc.encoding());
assert.equal("root", doc.root()?.name());
assert.equal("child", (doc.get("child") as XMLElement).name());
assert.equal("grandchild", ((doc.get("child") as XMLElement).get("grandchild") as XMLElement).name());
assert.equal("with love", (doc.get("child/grandchild") as XMLElement).text());
assert.equal("sibling", (doc.get("sibling") as XMLElement).name());
assert.equal(6, (doc.get("sibling") as XMLElement).line());
assert.equal(3, (doc.get("child") as XMLElement).getAttribute("to")?.line());
assert.equal("with content!", (doc.get("sibling") as XMLElement).text());
assert.equal(str, doc.toString());
assert.done();
};
module.exports.parseWithInvisibleCharacter = function (assert: any) {
var strWithInvisibleCharacter = "\uFEFF<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child>with love</child><sibling>with content!</sibling></root>";
var doc = libxml.parseXml(strWithInvisibleCharacter)
assert.equal("1.0", doc.version());
assert.equal("UTF-8", doc.encoding());
assert.equal("root", doc.root()?.name());
assert.equal("child", (doc.get("child") as XMLElement).name());
assert.equal("with love", (doc.get("child") as XMLElement).text());
assert.equal("sibling", (doc.get("sibling") as XMLElement).name());
assert.equal("with content!", (doc.get("sibling") as XMLElement).text());
assert.done();
}
module.exports.parse_with_flags = function (assert: any) {
const filename = __dirname + "/../../test/fixtures/parser.xml";
const str = fs.readFileSync(filename, "utf8").replace(/[\r]+/g, '');
const doc = libxml.parseXml(str, {replaceEntities: true, validateEntities: true});
assert.equal(18, doc.getParseFlags());
assert.done();
}
module.exports.parseAsync = function (assert: any) {
var filename = __dirname + "/../../test/fixtures/parser.xml";
var str = fs.readFileSync(filename, "utf8").replace(/[\r]+/g, '');
let x = 0;
libxml.parseXmlAsync(str).then((doc) => {
assert.equal(++x, 2);
assert.equal("1.0", doc.version());
assert.equal("UTF-8", doc.encoding());
assert.equal("root", doc.root()?.name());
assert.equal("child", (doc.get("child") as XMLElement).name());
assert.equal("grandchild", ((doc.get("child") as XMLElement).get("grandchild") as XMLElement).name());
assert.equal("with love", (doc.get("child/grandchild") as XMLElement).text());
assert.equal("sibling", (doc.get("sibling") as XMLElement).name());
assert.equal(6, (doc.get("sibling") as XMLElement).line());
assert.equal(3, (doc.get("child") as XMLElement).getAttribute("to")?.line());
assert.equal("with content!", (doc.get("sibling") as XMLElement).text());
assert.equal(str, doc.toString());
assert.done();
});
assert.equal(++x, 1);
};
module.exports.parseAsyncWithInvisibleCharacter = function (assert: any) {
var strWithInvisibleCharacter = "\uFEFF<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><child>with love</child><sibling>with content!</sibling></root>";
let x = 0;
libxml.parseXmlAsync(strWithInvisibleCharacter).then((doc) => {
assert.equal(++x, 2);
assert.equal("1.0", doc.version());
assert.equal("UTF-8", doc.encoding());
assert.equal("root", doc.root()?.name());
assert.equal("child", (doc.get("child") as XMLElement).name());
assert.equal("with love", (doc.get("child") as XMLElement).text());
assert.equal("sibling", (doc.get("sibling") as XMLElement).name());
assert.equal("with content!", (doc.get("sibling") as XMLElement).text());
assert.done();
});
assert.equal(++x, 1);
}
module.exports.parse_async_with_replace = function (assert: any) {
const filename = __dirname + "/../../test/fixtures/parser.xml";
const str = fs.readFileSync(filename, "utf8").replace(/[\r]+/g, '');
let x = 0;
libxml.parseXmlAsync(str, {replaceEntities: true, validateEntities: true}).then((doc) => {
assert.equal(18, doc.getParseFlags());
assert.done();
});
assert.equal(++x, 1);
}
module.exports.parse_buffer = function (assert: any) {
var filename = __dirname + "/../../test/fixtures/parser-utf16.xml";
var buf = fs.readFileSync(filename);
var doc = libxml.parseXml(buf);
assert.equal("1.0", doc.version());
assert.equal("UTF-16", doc.encoding());
assert.equal("root", doc.root()?.name());
assert.done();
};
module.exports.recoverable_parse = function (assert: any) {
var filename = __dirname + "/../../test/fixtures/warnings/ent9.xml";
var str = fs.readFileSync(filename, "utf8");
var doc = libxml.parseXml(str);
assert.equal(1, doc.errors.length);
var err = doc.errors.shift()!;
assert.ok(err instanceof Error);
assert.equal(err.domain, 3);
assert.equal(err.column, 13);
assert.equal(err.line, 1);
assert.equal(err.code, 201);
assert.equal(err.str1, "prefix");
assert.done();
};
module.exports.baseurl_xml = function (assert: any) {
if (/^win/.test(process.platform)) {
// libxml won't resolve the path on Windows
assert.done();
return;
}
var str = '<!DOCTYPE example SYSTEM "baseurl.dtd">\n' + '<example msg="&happy;"/>\n';
// First verify it fails when we don't give baseUrl
var doc = libxml.Document.fromXml(str, {
validateEntities: true,
replaceEntities: true,
});
assert.ok(doc.errors.length > 0);
// Now it should work
var doc = libxml.Document.fromXml(str, {
validateEntities: true,
replaceEntities: true,
baseUrl: __dirname + "/../../test/fixtures/example.xml",
});
assert.equal(doc.errors.length, 0);
assert.done();
};
module.exports.fatal_error = function (assert: any) {
var filename = __dirname + "/../../test/fixtures/errors/comment.xml";
var str = fs.readFileSync(filename, "utf8");
var err: XMLStructuredError | null = null;
try {
libxml.parseXml(str);
} catch (e) {
if (e instanceof XMLStructuredError) {
err = e;
}
}
var errorControl = {
domain: 1,
code: 4,
message: "Start tag expected, '<' not found\n",
level: 3,
file: null,
line: 5,
str1: null,
str2: null,
str3: null,
int1: null,
column: 10,
};
assert.ok(err instanceof Error);
assert.equal(errorControl.code, err?.code);
assert.done();
};
module.exports.parse_options = function (assert: any) {
function test_parser_option(input: any, options: XMLParseOptions, expected: any, saveOptions?: XMLSaveOptions) {
var output = libxml.parseXml(input, options).toString(saveOptions);
output = output.replace(/^<\?xml version="1.0" encoding="UTF-8"\?>\n/, "");
output = output.replace(/\n$/, "");
assert.equal(output, expected);
}
test_parser_option("<x>&</x>", { recover: true }, "<x/>"); // without this option, this document would raise an exception during parsing
test_parser_option(
"<!DOCTYPE x [ <!ENTITY foo 'bar'> ]> <x>&foo;</x>",
{ replaceEntities: true },
'<!DOCTYPE x [\n<!ENTITY foo "bar">\n]>\n<x>bar</x>'
); // foo => bar
test_parser_option("<x> <a>123</a> </x>", {}, "<x> <a>123</a> </x>"); // no indentation even though the toString() default called for formatting
test_parser_option("<x> <a>123</a> </x>", { preserveWhitespace: false }, "<x>\n <a>123</a>\n</x>"); // ah, now we have indentation!
test_parser_option("<x><![CDATA[hi]]></x>", {}, "<x><![CDATA[hi]]></x>"); // normally CDATA stays as CDATA
test_parser_option("<x><![CDATA[hi]]></x>", { preserveCDATA: false }, "<x>hi</x>"); // but here CDATA is removed!
const TAB = " ";
const TEXT_CONTENT = `\n${TAB}${TAB}test test\n${TAB}`;
const ORIGINAL = `<x attr="test">\n\n${TAB}<a>123</a>\n\n${TAB}<b>${TEXT_CONTENT}</b>\n\n</x>`,
XML_FORMATTED = `<x attr="test">\n <a>123</a>\n <b>${TEXT_CONTENT}</b>\n</x>`,
NO_WHITESPACE = `<x attr="test"><a>123</a><b>${TEXT_CONTENT}</b></x>`;
// When `preserveWhitespace: true`, input whitespace should be preserved regardless of toString({ format }) option
test_parser_option(ORIGINAL, { preserveWhitespace: true }, ORIGINAL);
test_parser_option(ORIGINAL, { preserveWhitespace: true }, ORIGINAL, { format: true, type: "html" });
test_parser_option(ORIGINAL, { preserveWhitespace: true }, ORIGINAL, { format: false });
// Input can be formatted when it contains no whitespace
test_parser_option(NO_WHITESPACE, { preserveWhitespace: true }, NO_WHITESPACE, { format: false });
test_parser_option(NO_WHITESPACE, { preserveWhitespace: true }, XML_FORMATTED, { format: true });
// When `preserveWhitespace: false` and toString({ format: true }),
// output should have default libxml formatting applied regardless of input format
test_parser_option(ORIGINAL, { preserveWhitespace: false }, XML_FORMATTED);
test_parser_option(ORIGINAL, { preserveWhitespace: false }, XML_FORMATTED, { format: true });
// When `preserveWhitespace: false` and toString({ format: false }),
// output should have all possible whitespace stripped
test_parser_option(ORIGINAL, { preserveWhitespace: false }, NO_WHITESPACE, { format: false });
assert.done();
};