-
Notifications
You must be signed in to change notification settings - Fork 28
/
test.js
327 lines (296 loc) · 12.3 KB
/
test.js
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
const tXml = require('.');
const assert = require('assert');
const fs = require('fs');
const files = {
commented: __dirname + '/test/examples/commented.svg',
commentOnly: __dirname + '/test/examples/commentOnly.svg',
twoComments: __dirname + '/test/examples/twocomments.svg',
tagesschauRSS: __dirname + '/test/examples/tagesschau.rss',
wordpadDocxDocument: __dirname+'/test/examples/wordpad.docx.document.xml',
};
assert(tXml, 'tXml is available');
assert(Array.isArray(tXml.parse('')), 'tXml don\'t return an empty array for an empty string');
/******************************************************************************************************
*
******************************************************************************************************/
assert.deepStrictEqual(tXml.parse('<test>'), [{ tagName: 'test', attributes: {}, children: [] }], 'not even passed the simplest test');
assert.deepStrictEqual(tXml.parse('<test att="v">'), [{ "tagName": "test", "attributes": { "att": "v" }, "children": [] }], 'a single attribute');
assert.deepStrictEqual(
tXml.parse('<test att="v" att2="two">'),
[{ "tagName": "test", "attributes": { "att": "v", "att2": "two" }, "children": [] }],
'multiple attributes'
);
assert.deepStrictEqual(tXml.parse('childTest'), ["childTest"], 'a single text');
assert.deepStrictEqual(tXml.parse('<test>childTest'), [{ "tagName": "test", "attributes": {}, "children": ["childTest"] }], 'a single child text');
assert.deepStrictEqual(tXml.parse('<test></test>'), [{ "tagName": "test", "attributes": {}, "children": [] }], 'simple closingTag');
assert.deepStrictEqual(
tXml.parse('<test><cc></cc><cc></cc></test>'),
[{ "tagName": "test", "attributes": {}, "children": [{ "tagName": "cc", "attributes": {}, "children": [] }, { "tagName": "cc", "attributes": {}, "children": [] }] }],
'two childNodes'
);
assert.deepStrictEqual(
tXml.parse('<!-- some comment --><test><cc c="d"><!-- some comment --></cc><!-- some comment --><cc>value<!-- some comment --></cc></test><!-- ending with not closing comment'),
[{ "tagName": "test", "attributes": {}, "children": [{ "tagName": "cc", "children": [], "attributes": { "c": "d" } }, { "tagName": "cc", "attributes": {}, "children": ["value"] }] }],
'simple childNodes + attribute + childText + ignore comments'
);
assert.deepStrictEqual(
tXml.parse('<!DOCTYPE html><test><cc></cc><cc></cc></test>'),
['!DOCTYPE html',{ "tagName": "test", "attributes": {}, "children": [{ "tagName": "cc", "attributes": {}, "children": [] }, { "tagName": "cc", "attributes": {}, "children": [] }] }],
'two childNodes + ignore doctype declaration'
);
assert.deepStrictEqual(tXml.parse(`<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
<!ENTITY ns_extend "http://ns.adobe.com/Extensibility/1.0/">
<!ENTITY ns_ai "http://ns.adobe.com/AdobeIllustrator/10.0/">
<!ENTITY ns_graphs "http://ns.adobe.com/Graphs/1.0/">
]>
<svg xmlns:x="&ns_extend;" xmlns:i="&ns_ai;" xmlns:graph="&ns_graphs;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" width="583.029px" height="45px" viewBox="0 0 583.029 45" enable-background="new 0 0 583.029 45" xml:space="preserve">
<input id="test">
</svg>`),
[
{
"tagName": "?xml",
"attributes": {
"version": "1.0",
"encoding": "utf-8"
},
"children": []
},
"!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\" [\n<!ENTITY ns_extend \"http://ns.adobe.com/Extensibility/1.0/\">\n<!ENTITY ns_ai \"http://ns.adobe.com/AdobeIllustrator/10.0/\">\n<!ENTITY ns_graphs \"http://ns.adobe.com/Graphs/1.0/\">\n]",
{
"tagName": "svg",
"attributes": {
"xmlns:x": "&ns_extend;",
"xmlns:i": "&ns_ai;",
"xmlns:graph": "&ns_graphs;",
"xmlns": "http://www.w3.org/2000/svg",
"xmlns:xlink": "http://www.w3.org/1999/xlink",
"version": "1.1",
"x": "0px",
"y": "0px",
"width": "583.029px",
"height": "45px",
"viewBox": "0 0 583.029 45",
"enable-background": "new 0 0 583.029 45",
"xml:space": "preserve"
},
"children": [
{
"tagName": "input",
"attributes": {
"id": "test"
},
"children": []
}
]
}
]);
/******************************************************************************************************
* use options
******************************************************************************************************/
assert.deepStrictEqual(
tXml.parse('<test><cc></cc><cc></cc></test>', { filter: function (element) { return element.tagName.toLowerCase() == 'cc'; } }),
[{ "tagName": "cc", "attributes": {}, "children": [] }, { "tagName": "cc", "attributes": {}, "children": [] }],
'get all "cc" nodes'
);
assert.deepStrictEqual(
JSON.stringify(tXml.simplify(tXml.parse('<test><cc>one</cc>test<cc f="test"><sub>3</sub>two</cc><dd></dd></test>'))),
JSON.stringify({ "test": { "cc": ["one", { "sub": "3", "_attributes": { "f": "test" } }], "dd": "" } }),
'simplify'
);
assert.deepStrictEqual(
tXml.parse('<test><style>*{some:10px;}/* <tag> comment */</style></test>'),
[{ "tagName": "test", "attributes": {}, "children": [{ "tagName": "style", "attributes": {}, "children": ["*{some:10px;}/* <tag> comment */"] }] }],
'css with tag as comment'
);
assert.deepStrictEqual(tXml.parse('<style>p { color: "red" }</style>'), [
{
tagName: 'style',
attributes: {},
children: ['p { color: "red" }'] // note here the closing bracket is missing
}
], 'do not cut off the last character');
assert.deepStrictEqual(
tXml.parse('<test><script>$("<div>")</script></test>'),
[{ "tagName": "test", "attributes": {}, "children": [{ "tagName": "script", "attributes": {}, "children": ["$(\"<div>\")"] }] }],
'js creating some tags'
);
var x = '<test a="value"><child a=\'g"g\'>text</child></test>'
assert(x === tXml.stringify(tXml.parse(x)), 'optimal xml string need to keep the same');
var xShould = [{ "tagName": "h1", "attributes": { "class": "test package-name other-class test2" }, "children": [] }];
var x = tXml.getElementsByClassName('<html><head></head><body><h1 class="test package-name other-class test2"></h1></body></html>', 'package-name');
assert.deepStrictEqual(x, xShould, 'find elements by class')
// re-stringify an attribute without value
var s = "<test><something flag></something></test>";
assert.deepStrictEqual(tXml.stringify(tXml.parse(s)), s, 'problem with attribute without value');
assert(tXml.stringify(undefined) === '', 'stringify ignore null values');
assert(tXml.toContentString(tXml.parse('<test>f<case number="2">f</case>f</test>')) === "f f f")
assert.deepStrictEqual(
tXml.getElementById('<test><child id="theId">found</child></test>', 'theId'),
{ "tagName": "child", "attributes": { "id": "theId" }, "children": ["found"] }, 'id not found')
assert.deepStrictEqual(tXml.parse(`<xml><![CDATA[some data]]></xml>`),
[{ tagName: 'xml', attributes: {}, children: ['some data'] }],
'cdata'
);
assert.deepStrictEqual(tXml.simplifyLostLess(tXml.parse(`<question>
<text><![CDATA[<b>Question 1 Text</b>]]> </text>
<answers>
<text correct="1">1Answer 1 Text</text>
<text correct="0">1Answer 2 Text</text>
</answers>
</question>
<question>
<text><![CDATA[<b>Question 2 Text</b>]]> </text>
<answers>
<text correct="1">2Answer 1 Text</text>
<text correct="0">2Answer 2 Text</text>
</answers>
</question>`)),
{
"question": [
{
"text": [
"<b>Question 1 Text</b>"
],
"answers": [
{
"text": [
{
"_attributes": {
"correct": "1"
},
"value": "1Answer 1 Text"
},
{
"_attributes": {
"correct": "0"
},
"value": "1Answer 2 Text"
}
]
}
]
},
{
"text": [
"<b>Question 2 Text</b>"
],
"answers": [
{
"text": [
{
"_attributes": {
"correct": "1"
},
"value": "2Answer 1 Text"
},
{
"_attributes": {
"correct": "0"
},
"value": "2Answer 2 Text"
}
]
}
]
}
]
},
'simplifyLostLess'
);
assert.deepStrictEqual(
tXml.parse(`<test><!-- test --></test>`, { keepComments: true }),
[ { tagName: 'test', attributes: {}, children: [ '<!-- test -->' ] } ],
'keepComments'
);
assert.deepStrictEqual(
tXml.parse(`<test><!-- test --><!-- test2 --></test>`, { keepComments: true }),
[ { tagName: 'test', attributes: {}, children: [ '<!-- test -->', '<!-- test2 -->' ] } ],
'keep two comments'
);
assert.deepStrictEqual(
tXml.parse(`<test><!--></test>`, { keepComments: true }),
[ { tagName: 'test', attributes: {}, children: [ '<!-->' ] } ],
'keep two comments'
);
const svgWithCommentString = fs.readFileSync(files.commented).toString();
assert.deepStrictEqual(
tXml.parse(svgWithCommentString),
[{tagName:"svg",attributes:{height:"200",width:"500"},children:[{tagName:"polyline",attributes:{points:"20,20 40,25 60,40 80,120 120,140 200,180",style:"fill:none;stroke:black;stroke-width:3"},children:[]}]}],
'svg with comment'
);
assert.deepStrictEqual(tXml.parse(`<!-- Test -->
<svg height="200" width="500">
<polyline points="20,20 40,25 60,40 80,120 120,140 200,180" style="fill:none;stroke:black;stroke-width:3" />
</svg>`, { keepComments: true }),
[
'<!-- Test -->',
{
tagName: 'svg',
attributes: { height: '200', width: '500' },
children: [{
tagName: 'polyline',
attributes: {
points: '20,20 40,25 60,40 80,120 120,140 200,180',
style: 'fill:none;stroke:black;stroke-width:3'
},
children: []
}]
}
]
);
assert.deepStrictEqual(tXml.parse('<![CDATA[nothing]]>'), ['nothing'], 'parse CDATA');
assert.deepStrictEqual(tXml.parse('<![CDATA[nothing'), ['nothing'], 'parse unclosed CDATA');
var noError = false;
try{
tXml.parse('<user><name>robert</firstName><user>');
noError = true;
}catch(e){}
assert.deepStrictEqual(noError, false, 'throw when the closeTag is wrong')
assert.deepStrictEqual(tXml.simplifyLostLess([]), {}, 'empty nodes simplify to empty object')
assert.deepStrictEqual(tXml.simplifyLostLess(['3']), '3', 'string list becomes the stirng')
assert.deepStrictEqual(tXml.simplifyLostLess(['1',2]), {}, 'ignore non objects')
assert.deepStrictEqual(tXml.filter([{}],()=>true), [{}], 'allow nodes without children')
const wordpadDoc = fs.readFileSync(files.wordpadDocxDocument).toString();
assert.deepStrictEqual(
tXml.filter(
tXml.parse(wordpadDoc, { keepWhitespace: true }),
(n) => n.tagName === 'w:t'
)[1].children[0],
' '
);
//this should run without Error: Issue #24
tXml.parse('<?xml version="1.0"?><methodCall>TEST</methodCall>', { simplify: true });
// https://github.com/TobiasNickel/tXml/issues/14
testAsync().catch(err=>console.log(err));
async function testAsync(){
const xmlStreamCommentedSvg = fs.createReadStream(files.commented)
.pipe(tXml.transformStream(0));
let numberOfElements = 0;
for await(let element of xmlStreamCommentedSvg) {
numberOfElements++;
}
assert.strictEqual(numberOfElements, 1, 'expect to find one element in commented.svg')
const xmlStreamTwoCommentedSvg = fs.createReadStream(files.twoComments)
.pipe(tXml.transformStream('', { keepComments: true }));
numberOfElements = 0;
for await(let element of xmlStreamTwoCommentedSvg) {
numberOfElements++;
}
assert.strictEqual(numberOfElements, 3, 'expect to find two comments and one element in twoComments.svg')
const xmlStreamCommentOnlySvg = fs.createReadStream(files.commentOnly)
.pipe(tXml.transformStream('', { keepComments: 0 }));
numberOfElements = 0;
for await(let element of xmlStreamCommentOnlySvg) {
numberOfElements++;
}
assert.strictEqual(numberOfElements, 0, 'do not find unclosed comments')
const xmlStreamLongXML = fs.createReadStream(__dirname + '/long.xml')
.pipe(tXml.transformStream(5));
numberOfElements = 0;
for await(let element of xmlStreamLongXML) {
numberOfElements++;
}
assert.strictEqual(numberOfElements, 10000000, 'expected to find many');
}