-
-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathentities_spec.js
More file actions
745 lines (646 loc) · 23.6 KB
/
Copy pathentities_spec.js
File metadata and controls
745 lines (646 loc) · 23.6 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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
import {XMLParser, XMLBuilder} from "../src/fxp.js";
describe("XMLParser Entities", function() {
it("should parse attributes with valid names, default entities", function() {
const xmlData = `<a:root xmlns:a="urn:none" xmlns:a-b="urn:none">
<a:a attr="2foo&bar'">1</a:a>
<a:b>2</a:b>
<a-b:b-a>2</a-b:b-a>
<a:c>test&\r\nтест<\r\ntest</a:c>
<a:el><![CDATA[<a><<a/><b>2</b>]]]]>\r\n<![CDATA[]]]]><![CDATA[>&]]>a</a:el>
<c:string lang="ru">\
страхования\
» от суммы \
его активов\
</c:string>
</a:root>`;
const expected = {
"a:root": {
"@_xmlns:a": "urn:none",
"@_xmlns:a-b": "urn:none",
"a:a": {
"#text": 1,
"@_attr": "2foo&bar'"
},
"a:b": 2,
"a-b:b-a": 2,
"a:c": "test&\nтест<\ntest",
"a:el": "<a><<a/><b>2</b>]]]]>&a",
"c:string": {
"#text": "страхования » от суммы его активов",
"@_lang": "ru"
}
}
};
const options = {
allowBooleanAttributes: true,
ignoreAttributes: false,
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData, true);
//console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});
it("should parse XML with DOCTYPE without internal DTD", function() {
const xmlData = `<?xml version='1.0' standalone='no'?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
<svg><metadata>test</metadata></svg>`;
const expected = {
"?xml": {
"@_version": "1.0",
"@_standalone": "no"
},
"svg" : {
"metadata": "test"
}
};
const options = {
allowBooleanAttributes: true,
ignoreAttributes: false,
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData, true);
expect(result).toEqual(expected);
});
it("should parse XML with DOCTYPE without internal DTD", function() {
const xmlData = `<?xml version='1.0' standalone='no'?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
<svg>
<metadata>[test]</metadata>
</svg>`;
const expected = {
"?xml": {
"@_version": "1.0",
"@_standalone": "no"
},
"svg" : {
"metadata": "[test]"
}
};
const options = {
allowBooleanAttributes: true,
ignoreAttributes: false,
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData, true);
// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});
it("should error for when any tag is left to close", function(){
const xmlData = `<?xml version="1.0"?><!DOCTYPE `;
expect(() =>{
const parser = new XMLParser();
parser.parse(xmlData);
}).toThrowError("Unclosed DOCTYPE")
})
it("should parse XML with DOCTYPE with valid comment expressions ", function() {
const xmlData = "<?xml version=\"1.0\" standalone=\"yes\" ?>" +
"<!--open the DOCTYPE declaration -" +
" the open square bracket indicates an internal DTD-->" +
"<!DOCTYPE foo [" +
"<!--define the internal DTD-->" +
"<!ELEMENT foo EMPTY>" +
"<!ELEMENT foo ANY>" +
"<!--close the DOCTYPE declaration-->" +
"]>" +
"<foo>Hello World.</foo>";
const expected = {
"?xml": "",
foo: "Hello World."
};
const options = {
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
//console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});
it("should not throw error when DTD comments contain '<' or '>'", function() {
const xmlData = `<!DOCTYPE greeting [<!-- < > < -->]>`;
const parser = new XMLParser();
parser.parse(xmlData);
});
it("should read entity value between correct matching quote char", function() {
const xmlData = `<!DOCTYPE x [ <!ENTITY x 'x">]><!--'> ]>
<X>
<Y/><![CDATA[--><X><Z/><!--]]>-->
</X>`;
const expected = {
X: {
Y: '',
'#text': '--><X><Z/><!---->'
} };
const parser = new XMLParser();
let result = parser.parse(xmlData);
// console.log(result);
expect(result).toEqual(expected);
});
it("should escape regex char from entity name", function() {
const xmlData = `<?xml version="1.0"?>
<!DOCTYPE foo [
<!ENTITY l. "<img src=x onerror=alert(1)>">
]>
<root>
<text>Hello <b>World</b></text>
</root>`;
const expected = {
"?xml": "",
"root": {
"text": "Hello <b>World</b>"
}
};
const options = {
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
//console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});
it("should parse attributes having '>' in value", function() {
const xmlData = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ENTITY nbsp " ">
<!ENTITY writer "Writer: Donald Duck.">
<!ENTITY copyright "Copyright: W3Schools.">
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body attr="&writer;">Don't forget me this weekend!</body>
<footer>&writer; ©right;</footer>
</note> `;
const expected = {
"?xml": {
"version": "1.0",
"encoding": "UTF-8"
},
"note": {
"to": "Tove",
"from": "Jani",
"heading": "Reminder",
"body": {
"#text": "Don't forget me this weekend!",
"attr": "Writer: Donald Duck."
},
"footer": "Writer: Donald Duck. Copyright: W3Schools."
}
};
const options = {
attributeNamePrefix: "",
ignoreAttributes: false,
processEntities: true
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});
it("should parse dynamic entity", function() {
const xmlData = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ENTITY nbsp "writer;">
<!ENTITY writer "Writer: Donald Duck.">
<!ENTITY copyright "Copyright: W3Schools.">
]>
<note>
<heading>Reminder</heading>
<body attr="&writer;">Don't forget me this weekend!</body>
<footer>&writer;& ©right;</footer>
</note> `;
const expected = {
"?xml": {
"version": "1.0",
"encoding": "UTF-8"
},
"note": {
"heading": "Reminder",
"body": {
"#text": "Don't forget me this weekend!",
"attr": "Writer: Donald Duck."
},
"footer": "Writer: Donald Duck.Writer: Donald Duck.Copyright: W3Schools."
}
};
const options = {
attributeNamePrefix: "",
ignoreAttributes: false,
processEntities: true
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});
it("should allow !ATTLIST & !NOTATION", function() {
const xmlData = `<?xml version="1.0"?>
<!DOCTYPE code [
<!ELEMENT code (#PCDATA)>
<!NOTATION vrml PUBLIC "VRML 1.0">
<!NOTATION vrml2 PUBLIC "VRML 1.0" "system">
<!ATTLIST code lang NOTATION (vrml) #REQUIRED>
]>
<code lang="vrml">Some VRML instructions</code>`;
const expected = {
"?xml": {
"version": "1.0"
},
"code": {
"lang": 'vrml',
"#text": 'Some VRML instructions'
}
};
const options = {
attributeNamePrefix: "",
ignoreAttributes: false,
processEntities: true
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});
it("should build by decoding default entities", function() {
const jsObj = {
"note": {
"@heading": "Reminder > \"Alert",
"body": {
"#text": " 3 < 4",
"attr": "Writer: Donald Duck."
},
}
};
const expected = `
<note heading="Reminder > "Alert">
<body>
3 < 4
<attr>Writer: Donald Duck.</attr>
</body>
</note>`;
const options = {
attributeNamePrefix: "@",
ignoreAttributes: false,
// processEntities: true
};
const builder = new XMLBuilder(options);
const result = builder.build(jsObj);
expect(result.replace(/\s+/g, "")).toEqual(expected.replace(/\s+/g, ""));
});
it("should build by decoding default entities in preserve mode", function() {
const jsObj = [
{
"note": [
{
"body": [
{
"#text": "3 < 4"
},
{
"attr": [
{
"#text": "Writer: Donald Duck."
}
]
}
]
}
],
":@": {
"@heading": "Reminder > \"Alert"
}
}
];
const expected = `
<note heading="Reminder > "Alert">
<body>
3 < 4
<attr>Writer: Donald Duck.</attr>
</body>
</note>`;
const options = {
attributeNamePrefix: "@",
ignoreAttributes: false,
preserveOrder: true,
// processEntities: false
};
const parser = new XMLParser(options);
let result = parser.parse(expected);
// console.log(JSON.stringify(result,null,4));
const builder = new XMLBuilder(options);
result = builder.build(jsObj);
// console.log(result);
expect(result.replace(/\s+/g, "")).toEqual(expected.replace(/\s+/g, ""));
});
it("should parse HTML entities when htmlEntities:true", function() {
const xmlData = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ENTITY nbsp "writer;">
<!ENTITY writer "Writer: Donald Duck.">
<!ENTITY copyright "Copyright: W3Schools.">
]>
<note>
<heading>Reminder</heading>
<body attr="&writer;">Don't forget me this weekend!®</body>
<footer>&writer;& ©right;&inr;</footer>
</note> `;
const expected = {
"?xml": {
"version": "1.0",
"encoding": "UTF-8"
},
"note": {
"heading": "Reminder",
"body": {
"#text": "Don't forget me this weekend!®",
"attr": "Writer: Donald Duck."
},
"footer": "Writer: Donald Duck.Writer: Donald Duck.Copyright: W3Schools.₹"
}
};
const options = {
attributeNamePrefix: "",
ignoreAttributes: false,
processEntities: true,
htmlEntities: true
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});
it("should parse HTML numeric entities when htmlEntities:true", function() {
const xmlData = `
<?xml version="1.0" encoding="UTF-8"?>
<note>
<heading>Bear</heading>
<body face="ʕ•ᴥ•ʔ" smile="😊😋">Bears are called Bären in German!</body>
</note> `;
const expected = {
"?xml": {
"version": "1.0",
"encoding": "UTF-8"
},
"note": {
"heading": "Bear",
"body": {
"#text": "Bears are called Bären in German!",
"face": "ʕ•ᴥ•ʔ",
"smile": "\u{1F60A}\u{1F60B}"
}
}
};
const options = {
attributeNamePrefix: "",
ignoreAttributes: false,
processEntities: true,
htmlEntities: true,
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
expect(result).toEqual(expected);
});
it("should skip HTML numeric or hex entities when htmlEntities:true but entity is out of range", function() {
const xmlData = `<root attr="�">�</root>`;
const expected = {
"root": {
"#text": "�",
"attr": "�"
}
};
const options = {
attributeNamePrefix: "",
ignoreAttributes: false,
processEntities: true,
htmlEntities: true,
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
expect(result).toEqual(expected);
});
it("should throw error if an entity name contains special char", function() {
const xmlData = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ENTITY nj$ "writer;">
<!ENTITY wr?er "Writer: Donald Duck.">
]>`;
const options = {
processEntities: true,
};
expect(() =>{
const parser = new XMLParser(options);
parser.parse(xmlData);
}).toThrowError("Invalid entity name nj$")
});
it("should allow localised entity names", function() {
const xmlData = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ENTITY ሀሎ "Amharic hello!">
<!ENTITY Здраво "Macedonian hello.">
]>
<note>
<heading>Reminder</heading>
<body attr="&ሀሎ;">Don't forget me this weekend! &Здраво;</body>
</note> `;
const expected = {
"?xml": {
"version": "1.0",
"encoding": "UTF-8"
},
"note": {
"heading": "Reminder",
"body": {
"#text": "Don't forget me this weekend! Macedonian hello.",
"attr": "Amharic hello!"
}
}
};
const options = {
attributeNamePrefix: "",
ignoreAttributes: false,
processEntities: true,
htmlEntities: true
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});
});
describe("XMLParser External Entities", function() {
it("should throw error when an entity value has '&'", function() {
const parser = new XMLParser();
expect( () => {
parser.addEntity("#xD", "&\r");
}).toThrowError("Entity value can't have '&'");
});
it("should throw error when an entity identifier has '&'", function() {
const parser = new XMLParser();
expect( () => {
parser.addEntity("
", "\r");
}).toThrowError("An entity must be set without '&' and ';'. Eg. use '#xD' for '
'");
});
it("should throw error when an entity identifier has ';'", function() {
const parser = new XMLParser();
expect( () => {
parser.addEntity("#xD;", "\r");
}).toThrowError("An entity must be set without '&' and ';'. Eg. use '#xD' for '
'");
});
it("should set and parse for valid entity set externally", function() {
const xmlData = `<note>&unknown;
last</note> `;
const parser = new XMLParser();
parser.addEntity("#xD", "\r\n");
let result = parser.parse(xmlData);
// console.log(JSON.stringify(result,null,4));
expect(result.note).toEqual(`&unknown;\r\nlast`);
});
it("External Entity can change the behavior of default entities", function() {
const xmlData = `<note>>last</note> `;
const parser = new XMLParser();
parser.addEntity("gt", "<>");
let result = parser.parse(xmlData);
// console.log(JSON.stringify(result,null,4));
expect(result.note).toEqual(`<>last`);
});
it("Same external Entity can be set by multiple times", function() {
const xmlData = `<note>>last</note> `;
const parser = new XMLParser();
parser.addEntity("gt", "<>");
parser.addEntity("gt", "><");
let result = parser.parse(xmlData);
// console.log(JSON.stringify(result,null,4));
expect(result.note).toEqual(`><last`);
});
it("should build by decoding '&' preserve mode", function() {
const jsObj = [
{
"note": [
{
"body": [
{ "#text": "(3 & 4) < 5" },
{ "attr": [ { "#text": "Writer: Donald Duck." } ] }
]
}
],
":@": {
"@heading": "Reminder > \"Alert"
}
}
];
const expected = `
<note heading="Reminder > "Alert">
<body>
(3 & 4) < 5
<attr>Writer: Donald Duck.</attr>
</body>
</note>`;
const options = {
attributeNamePrefix: "@",
ignoreAttributes: false,
preserveOrder: true,
// processEntities: false
};
const parser = new XMLParser(options);
let result = parser.parse(expected);
// console.log(JSON.stringify(result,null,4));
const builder = new XMLBuilder(options);
result = builder.build(jsObj);
// console.log(result);
expect(result.replace(/\s+/g, "")).toEqual(expected.replace(/\s+/g, ""));
});
it("should build by decoding '&'", function() {
const jsObj = {
"note": {
"body": {
"attr": "Writer: Donald Duck.",
"#text": "(3 & 4) < 5"
},
"@heading": "Reminder > \"Alert"
}
};
const expected = `
<note heading="Reminder > "Alert">
<body>
<attr>Writer: Donald Duck.</attr>
(3 & 4) < 5
</body>
</note>`;
const options = {
attributeNamePrefix: "@",
ignoreAttributes: false,
};
const parser = new XMLParser(options);
let result = parser.parse(expected);
// console.log(JSON.stringify(result,null,4));
// expect(expected).toEqual(jsObj);
const builder = new XMLBuilder(options);
const output = builder.build(jsObj);
// console.log(output);
expect(output.replace(/\s+/g, "")).toEqual(expected.replace(/\s+/g, ""));
});
it("should replace '&lt;' with '<'", function() {
const xmlData = `<SimpleScalarPropertiesInputOutput>
<stringValue>&lt;</stringValue>
</SimpleScalarPropertiesInputOutput>`;
const expected = {
"SimpleScalarPropertiesInputOutput": {
"stringValue": "<"
}
};
const options = {
attributeNamePrefix: "",
ignoreAttributes: false,
processEntities: true,
// preserveOrder: true
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
//console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});
it("should support entities with tags in content", function() {
const xmlData = `
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
<!ENTITY Smile "
<rect x='.5' y='.5' width='29' height='39' fill='black' stroke='red'/>
<g transform='translate(0, 5)'>
<circle cx='15' cy='15' r='10' fill='yellow'/>
<circle cx='12' cy='12' r='1.5' fill='black'/>
<circle cx='17' cy='12' r='1.5' fill='black'/>
<path d='M 10 19 L 15 23 20 19' stroke='black' stroke-width='2'/></g>"
>
]>
<svg width="850px" height="700px" version="1.1" xmlns="http://www.w3.org/2000/svg">
<g transform="matrix(16,0,0,16,0,0)">&Smile;</g></svg> `;
const expected = {
"?xml": {
"version": "1.0",
"encoding": "utf-8"
},
"svg": {
"g": {
"#text": " \n \t<rect x='.5' y='.5' width='29' height='39' fill='black' stroke='red'/>\n\t\t<g transform='translate(0, 5)'> \n\t\t\t<circle cx='15' cy='15' r='10' fill='yellow'/>\n\t\t\t<circle cx='12' cy='12' r='1.5' fill='black'/>\n\t\t\t<circle cx='17' cy='12' r='1.5' fill='black'/>\n\t\t\t<path d='M 10 19 L 15 23 20 19' stroke='black' stroke-width='2'/></g>",
"transform": "matrix(16,0,0,16,0,0)"
},
"width": "850px",
"height": "700px",
"version": "1.1",
"xmlns": "http://www.w3.org/2000/svg"
}
};
const options = {
attributeNamePrefix: "",
ignoreAttributes: false,
processEntities: true,
// preserveOrder: true
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);
// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);
});
});