forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_parse_sync_test.ts
More file actions
1676 lines (1454 loc) · 50.8 KB
/
Copy path_parse_sync_test.ts
File metadata and controls
1676 lines (1454 loc) · 50.8 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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018-2026 the Deno authors. MIT license.
import { assertEquals, assertThrows } from "@std/assert";
import { parseSync } from "./_parse_sync.ts";
import { XmlSyntaxError } from "./types.ts";
// =============================================================================
// XML Declaration Variations
// =============================================================================
Deno.test("parseSync() handles declaration with single quotes", () => {
const doc = parseSync("<?xml version='1.0' encoding='UTF-8'?><root/>");
assertEquals(doc.declaration?.version, "1.0");
assertEquals(doc.declaration?.encoding, "UTF-8");
});
Deno.test("parseSync() handles declaration with standalone", () => {
const doc = parseSync('<?xml version="1.0" standalone="yes"?><root/>');
assertEquals(doc.declaration?.version, "1.0");
assertEquals(doc.declaration?.standalone, "yes");
});
// =============================================================================
// DOCTYPE Handling
// =============================================================================
Deno.test("parseSync() rejects DOCTYPE by default", () => {
assertThrows(
() => parseSync("<!DOCTYPE root><root/>"),
XmlSyntaxError,
"DOCTYPE declarations are not allowed",
);
});
Deno.test("parseSync() handles DOCTYPE with internal subset", () => {
const doc = parseSync(
`<!DOCTYPE root [
<!ELEMENT root (#PCDATA)>
]><root/>`,
{ disallowDoctype: false },
);
assertEquals(doc.root.name.local, "root");
});
Deno.test("parseSync() handles DOCTYPE with nested brackets in internal subset", () => {
// This tests the nested bracket depth tracking
const doc = parseSync(
`<!DOCTYPE root [
<!-- Comment with [brackets] inside -->
<!ENTITY test "[nested [brackets] here]">
]><root/>`,
{ disallowDoctype: false },
);
assertEquals(doc.root.name.local, "root");
});
// =============================================================================
// Empty Text Node Handling
// =============================================================================
Deno.test("parseSync() handles adjacent tags without text between them", () => {
// This tests the empty text check
const doc = parseSync("<root><a/><b/></root>");
assertEquals(doc.root.children.length, 2);
if (doc.root.children[0]!.type === "element") {
assertEquals(doc.root.children[0]!.name.local, "a");
}
if (doc.root.children[1]!.type === "element") {
assertEquals(doc.root.children[1]!.name.local, "b");
}
});
// =============================================================================
// Error Handling: End of Input
// =============================================================================
Deno.test("parseSync() throws on unexpected end after <", () => {
assertThrows(
() => parseSync("<root><"),
XmlSyntaxError,
"Unexpected end of input",
);
});
Deno.test("parseSync() throws on unexpected end in start tag", () => {
// Input ends after whitespace in tag, before any attribute or closing
assertThrows(
() => parseSync("<root "),
XmlSyntaxError,
"Unexpected end of input in start tag",
);
});
// =============================================================================
// Error Handling: End Tags
// =============================================================================
Deno.test("parseSync() throws on missing > in end tag", () => {
assertThrows(
() => parseSync("<root></root"),
XmlSyntaxError,
);
});
Deno.test("parseSync() throws on unexpected closing tag", () => {
assertThrows(
() => parseSync("</orphan>"),
XmlSyntaxError,
"Unexpected closing tag",
);
});
Deno.test("parseSync() throws on mismatched closing tag", () => {
assertThrows(
() => parseSync("<root></wrong>"),
XmlSyntaxError,
"Mismatched closing tag",
);
});
Deno.test("parseSync() throws on mismatched namespaced closing tag", () => {
assertThrows(
() => parseSync('<ns:root xmlns:ns="http://example.com"></ns:wrong>'),
XmlSyntaxError,
"Mismatched closing tag: expected </ns:root>",
);
});
// =============================================================================
// Namespace URI Resolution
// =============================================================================
Deno.test("parseSync() resolves namespace URI for prefixed element", () => {
const doc = parseSync(
'<ns:root xmlns:ns="http://example.com"><ns:child/></ns:root>',
);
assertEquals(doc.root.name.prefix, "ns");
assertEquals(doc.root.name.uri, "http://example.com");
const child = doc.root.children[0];
assertEquals(child?.type, "element");
if (child?.type === "element") {
assertEquals(child.name.prefix, "ns");
assertEquals(child.name.uri, "http://example.com");
}
});
Deno.test("parseSync() resolves xml namespace URI implicitly", () => {
const doc = parseSync('<root xml:lang="en"/>');
// The xml prefix is always bound to the XML namespace
assertEquals(doc.root.name.uri, undefined); // root has no prefix
});
Deno.test("parseSync() does not assign URI to unprefixed elements", () => {
const doc = parseSync("<root><child/></root>");
assertEquals(doc.root.name.prefix, undefined);
assertEquals(doc.root.name.uri, undefined);
});
Deno.test("parseSync() handles multiple namespace prefixes", () => {
const doc = parseSync(`
<root xmlns:a="http://a.example.com" xmlns:b="http://b.example.com">
<a:elem1/>
<b:elem2/>
</root>
`);
const children = doc.root.children.filter((c) => c.type === "element");
assertEquals(children.length, 2);
const elem1 = children[0];
const elem2 = children[1];
if (elem1?.type === "element" && elem2?.type === "element") {
assertEquals(elem1.name.prefix, "a");
assertEquals(elem1.name.uri, "http://a.example.com");
assertEquals(elem2.name.prefix, "b");
assertEquals(elem2.name.uri, "http://b.example.com");
}
});
// =============================================================================
// Error Handling: Unclosed Elements
// =============================================================================
Deno.test("parseSync() throws on unclosed element", () => {
assertThrows(
() => parseSync("<root><child>"),
XmlSyntaxError,
"Unclosed element",
);
});
Deno.test("parseSync() throws on unclosed namespaced element", () => {
assertThrows(
() => parseSync('<ns:root xmlns:ns="http://example.com">'),
XmlSyntaxError,
"Unclosed element <ns:root>",
);
});
// =============================================================================
// Error Handling: Unterminated Constructs
// =============================================================================
Deno.test("parseSync() throws on unterminated comment", () => {
assertThrows(
() => parseSync("<root><!-- unterminated"),
XmlSyntaxError,
"Unterminated comment",
);
});
Deno.test("parseSync() throws on -- inside comment", () => {
assertThrows(
() => parseSync("<root><!-- a--b --></root>"),
XmlSyntaxError,
"Cannot use '--' within comments",
);
});
Deno.test("parseSync() throws on unterminated CDATA", () => {
assertThrows(
() => parseSync("<root><![CDATA[unterminated"),
XmlSyntaxError,
"Unterminated CDATA",
);
});
Deno.test("parseSync() throws on unterminated processing instruction", () => {
assertThrows(
() => parseSync("<?xml version='1.0'"),
XmlSyntaxError,
"Unterminated processing instruction",
);
});
// =============================================================================
// Error Handling: Invalid Characters
// =============================================================================
Deno.test("parseSync() throws on unexpected character after <", () => {
assertThrows(
() => parseSync("<root><@invalid/></root>"),
XmlSyntaxError,
"Unexpected character",
);
});
Deno.test("parseSync() throws on unexpected character in start tag", () => {
assertThrows(
() => parseSync("<root @/>"),
XmlSyntaxError,
"Unexpected character",
);
});
// =============================================================================
// Error Handling: Attribute Errors
// =============================================================================
Deno.test("parseSync() throws on < in attribute value", () => {
assertThrows(
() => parseSync('<root attr="<"/>'),
XmlSyntaxError,
"Cannot use '<' in attribute value",
);
});
Deno.test("parseSync() throws on unterminated attribute value", () => {
assertThrows(
() => parseSync('<root attr="unterminated'),
XmlSyntaxError,
"Unterminated attribute value",
);
});
Deno.test("parseSync() throws on missing quote for attribute value", () => {
assertThrows(
() => parseSync("<root attr=unquoted/>"),
XmlSyntaxError,
"Expected quote to start attribute value",
);
});
Deno.test("parseSync() throws on missing = after attribute name", () => {
assertThrows(
() => parseSync('<root attr "value"/>'),
XmlSyntaxError,
"Expected '=' after attribute name",
);
});
// =============================================================================
// Error Handling: Self-Closing Tags
// =============================================================================
Deno.test("parseSync() throws on missing > after / in self-closing tag", () => {
assertThrows(
() => parseSync("<root/x"),
XmlSyntaxError,
"Expected '>' after '/' in self-closing tag",
);
});
// =============================================================================
// Error Handling: Markup Declarations
// =============================================================================
Deno.test("parseSync() throws on unsupported markup declaration", () => {
assertThrows(
() => parseSync("<root><!INVALID></root>"),
XmlSyntaxError,
"Unsupported markup declaration",
);
});
// =============================================================================
// Comment with newlines (line position tracking)
// =============================================================================
Deno.test("parseSync() tracks line position in multi-line comments", () => {
// Tests newline tracking inside comments
const doc = parseSync(`<root><!-- line1
line2
line3 --><item/></root>`);
assertEquals(doc.root.children.length, 2);
// First child is comment, second is element
if (doc.root.children[0]!.type === "comment") {
assertEquals(doc.root.children[0]!.text, " line1\nline2\nline3 ");
}
});
// =============================================================================
// Multi-line CDATA position tracking
// =============================================================================
Deno.test("parseSync() tracks line position in multi-line CDATA", () => {
// Tests newline tracking inside CDATA
const doc = parseSync(`<root><![CDATA[line1
line2
line3]]></root>`);
assertEquals(doc.root.children.length, 1);
if (doc.root.children[0]!.type === "cdata") {
assertEquals(doc.root.children[0]!.text, "line1\nline2\nline3");
}
});
Deno.test("parseSync() tracks line position in multi-line processing instruction", () => {
// Tests newline tracking inside PI content
// PI content with newlines - should not throw
const doc = parseSync(`<?xml version="1.0"?>
<?target content
with newlines
here?>
<root/>`);
assertEquals(doc.root.name.local, "root");
assertEquals(doc.declaration?.version, "1.0");
});
// =============================================================================
// Empty text fast path test
// =============================================================================
Deno.test("parseSync() handles adjacent elements with no text between", () => {
// Tests line 178: empty text fast path when text.length === 0
const doc = parseSync("<root><a></a><b></b></root>");
assertEquals(doc.root.children.length, 2);
// Verify no empty text nodes were created
assertEquals(
doc.root.children.every((c) => c.type === "element"),
true,
);
});
// =============================================================================
// Additional Coverage Tests
// =============================================================================
Deno.test("parseSync() handles CRLF line endings", () => {
// Tests line ending normalization (CRLF -> LF)
const doc = parseSync("<root>\r\n <child/>\r\n</root>");
assertEquals(doc.root.children.length, 3); // whitespace, child, whitespace
});
Deno.test("parseSync() handles CR line endings", () => {
// Tests line ending normalization (CR -> LF)
const doc = parseSync("<root>\r <child/>\r</root>");
assertEquals(doc.root.children.length, 3);
});
Deno.test("parseSync() with trackPosition false reports zero positions", () => {
// Tests that position is 0,0,0 when trackPosition is false
try {
parseSync("<root><unclosed>", { trackPosition: false });
} catch (e) {
if (e instanceof XmlSyntaxError) {
assertEquals(e.line, 0);
assertEquals(e.column, 0);
assertEquals(e.offset, 0);
return;
}
}
throw new Error("Expected XmlSyntaxError");
});
Deno.test("parseSync() extracts standalone yes from declaration", () => {
// Tests standalone attribute extraction
const doc = parseSync('<?xml version="1.0" standalone="yes"?><root/>');
assertEquals(doc.declaration?.standalone, "yes");
});
Deno.test("parseSync() extracts standalone no from declaration", () => {
const doc = parseSync('<?xml version="1.0" standalone="no"?><root/>');
assertEquals(doc.declaration?.standalone, "no");
});
Deno.test("parseSync() extracts standalone with single quotes", () => {
const doc = parseSync("<?xml version='1.0' standalone='yes'?><root/>");
assertEquals(doc.declaration?.standalone, "yes");
});
// =============================================================================
// DTD ENTITY Declaration Tests
// =============================================================================
Deno.test("parseSync() handles ENTITY with SYSTEM identifier", () => {
const doc = parseSync(
`<!DOCTYPE root [
<!ENTITY external SYSTEM "http://example.com/entity.xml">
]><root/>`,
{ disallowDoctype: false },
);
assertEquals(doc.root.name.local, "root");
});
Deno.test("parseSync() handles ENTITY with PUBLIC identifier", () => {
const doc = parseSync(
`<!DOCTYPE root [
<!ENTITY external PUBLIC "-//Example//EN" "http://example.com/entity.xml">
]><root/>`,
{ disallowDoctype: false },
);
assertEquals(doc.root.name.local, "root");
});
Deno.test("parseSync() handles ENTITY with NDATA declaration", () => {
const doc = parseSync(
`<!DOCTYPE root [
<!ENTITY logo SYSTEM "logo.png" NDATA png>
]><root/>`,
{ disallowDoctype: false },
);
assertEquals(doc.root.name.local, "root");
});
Deno.test("parseSync() handles parameter entity declaration", () => {
const doc = parseSync(
`<!DOCTYPE root [
<!ENTITY % common "INCLUDE">
]><root/>`,
{ disallowDoctype: false },
);
assertEquals(doc.root.name.local, "root");
});
Deno.test("parseSync() throws on parameter entity with NDATA", () => {
assertThrows(
() =>
parseSync(
`<!DOCTYPE root [
<!ENTITY % logo SYSTEM "logo.png" NDATA png>
]><root/>`,
{ disallowDoctype: false },
),
XmlSyntaxError,
"Parameter entities cannot have NDATA declarations",
);
});
Deno.test("parseSync() throws on lowercase system keyword", () => {
assertThrows(
() =>
parseSync(
`<!DOCTYPE root [
<!ENTITY test system "test.xml">
]><root/>`,
{ disallowDoctype: false },
),
XmlSyntaxError,
"must be uppercase 'SYSTEM'",
);
});
Deno.test("parseSync() throws on lowercase public keyword", () => {
assertThrows(
() =>
parseSync(
`<!DOCTYPE root [
<!ENTITY test public "-//Test//EN" "test.xml">
]><root/>`,
{ disallowDoctype: false },
),
XmlSyntaxError,
"must be uppercase 'PUBLIC'",
);
});
// =============================================================================
// DTD Declaration Validation Tests
// =============================================================================
Deno.test("parseSync() handles ELEMENT declaration", () => {
const doc = parseSync(
`<!DOCTYPE root [
<!ELEMENT root (item*)>
]><root/>`,
{ disallowDoctype: false },
);
assertEquals(doc.root.name.local, "root");
});
Deno.test("parseSync() handles ATTLIST declaration", () => {
const doc = parseSync(
`<!DOCTYPE root [
<!ATTLIST root id ID #IMPLIED>
]><root/>`,
{ disallowDoctype: false },
);
assertEquals(doc.root.name.local, "root");
});
Deno.test("parseSync() handles NOTATION declaration", () => {
const doc = parseSync(
`<!DOCTYPE root [
<!NOTATION png SYSTEM "image/png">
]><root/>`,
{ disallowDoctype: false },
);
assertEquals(doc.root.name.local, "root");
});
Deno.test("parseSync() handles comment in internal subset", () => {
const doc = parseSync(
`<!DOCTYPE root [
<!-- This is a comment in the DTD -->
]><root/>`,
{ disallowDoctype: false },
);
assertEquals(doc.root.name.local, "root");
});
Deno.test("parseSync() handles PI in internal subset", () => {
const doc = parseSync(
`<!DOCTYPE root [
<?target content?>
]><root/>`,
{ disallowDoctype: false },
);
assertEquals(doc.root.name.local, "root");
});
Deno.test("parseSync() handles parameter entity reference in internal subset", () => {
const doc = parseSync(
`<!DOCTYPE root [
%common;
]><root/>`,
{ disallowDoctype: false },
);
assertEquals(doc.root.name.local, "root");
});
Deno.test("parseSync() throws on unknown DTD declaration type", () => {
assertThrows(
() =>
parseSync(
`<!DOCTYPE root [
<!INVALID test>
]><root/>`,
{ disallowDoctype: false },
),
XmlSyntaxError,
"Unknown DTD declaration type",
);
});
Deno.test("parseSync() throws on conditional section in internal subset", () => {
// Conditional sections start with <![INCLUDE or <![IGNORE
// The parser sees <![, expects CDATA[, but gets something else
assertThrows(
() =>
parseSync(
`<!DOCTYPE root [
<![INCLUDE[
<!ELEMENT root (#PCDATA)>
]]>
]><root/>`,
{ disallowDoctype: false },
),
XmlSyntaxError,
// The parser sees <! followed by [ which is the conditional section marker
"Unknown DTD declaration type",
);
});
// =============================================================================
// Content Before/After Root Tests
// =============================================================================
Deno.test("parseSync() throws on text before root element", () => {
assertThrows(
() => parseSync("text<root/>"),
XmlSyntaxError,
"Cannot have content before the root element",
);
});
Deno.test("parseSync() throws on text after root element", () => {
assertThrows(
() => parseSync("<root/>text"),
XmlSyntaxError,
"Cannot have content after the root element",
);
});
Deno.test("parseSync() throws on entity reference before root", () => {
assertThrows(
() => parseSync("&<root/>"),
XmlSyntaxError,
"Cannot use character/entity references in prolog",
);
});
Deno.test("parseSync() throws on entity reference after root", () => {
assertThrows(
() => parseSync("<root/>&"),
XmlSyntaxError,
"Cannot use character/entity references in prolog/epilog",
);
});
Deno.test("parseSync() throws on CDATA before root element", () => {
assertThrows(
() => parseSync("<![CDATA[test]]><root/>"),
XmlSyntaxError,
"Cannot have CDATA section before the root element",
);
});
Deno.test("parseSync() throws on CDATA after root element", () => {
assertThrows(
() => parseSync("<root/><![CDATA[test]]>"),
XmlSyntaxError,
"Cannot have CDATA section after the root element",
);
});
Deno.test("parseSync() throws on multiple root elements", () => {
assertThrows(
() => parseSync("<root1/><root2/>"),
XmlSyntaxError,
"Only one root element is allowed",
);
});
// =============================================================================
// Namespace Tests
// =============================================================================
Deno.test("parseSync() throws on unbound namespace prefix in element", () => {
assertThrows(
() => parseSync("<ns:root/>"),
XmlSyntaxError,
"Unbound namespace prefix 'ns'",
);
});
Deno.test("parseSync() throws on unbound namespace prefix in attribute", () => {
assertThrows(
() => parseSync("<root ns:attr='value'/>"),
XmlSyntaxError,
"Unbound namespace prefix 'ns'",
);
});
Deno.test("parseSync() throws on duplicate attribute", () => {
assertThrows(
() => parseSync('<root attr="1" attr="2"/>'),
XmlSyntaxError,
"Duplicate attribute 'attr'",
);
});
Deno.test("parseSync() throws on duplicate expanded attribute names", () => {
assertThrows(
() =>
parseSync(
'<root xmlns:a="http://example.com" xmlns:b="http://example.com" a:attr="1" b:attr="2"/>',
),
XmlSyntaxError,
"Duplicate expanded attribute name",
);
});
Deno.test("parseSync() throws on element using xmlns prefix", () => {
assertThrows(
() => parseSync('<xmlns:test xmlns:xmlns="http://example.com"/>'),
XmlSyntaxError,
"Element name cannot use the 'xmlns' prefix",
);
});
Deno.test("parseSync() handles namespace scope restoration on element close", () => {
// Tests that namespace bindings are properly restored when elements close
const doc = parseSync(`
<root xmlns:ns="http://outer.com">
<child xmlns:ns="http://inner.com">
<ns:inner/>
</child>
<ns:outer/>
</root>
`);
// The inner ns:inner element should have inner URI
const children = doc.root.children.filter((c) => c.type === "element");
const child = children[0];
if (child?.type === "element") {
const innerEl = child.children.find((c) => c.type === "element");
if (innerEl?.type === "element") {
assertEquals(innerEl.name.uri, "http://inner.com");
}
}
// The outer ns:outer element should have outer URI
const outer = children[1];
if (outer?.type === "element") {
assertEquals(outer.name.uri, "http://outer.com");
}
});
// =============================================================================
// QName Validation Tests
// =============================================================================
Deno.test("parseSync() throws on QName starting with colon", () => {
assertThrows(
() => parseSync('<:element xmlns=":""/>'),
XmlSyntaxError,
"QName cannot start with ':'",
);
});
Deno.test("parseSync() throws on QName ending with colon", () => {
assertThrows(
() => parseSync('<element: xmlns:element="http://example.com"/>'),
XmlSyntaxError,
"QName cannot end with ':'",
);
});
Deno.test("parseSync() throws on QName with multiple colons", () => {
assertThrows(
() => parseSync('<a:b:c xmlns:a="http://a.com" xmlns:b="http://b.com"/>'),
XmlSyntaxError,
"QName cannot contain multiple ':'",
);
});
Deno.test("parseSync() throws on attribute QName starting with colon", () => {
assertThrows(
() => parseSync('<root :attr="value"/>'),
XmlSyntaxError,
"QName cannot start with ':'",
);
});
// =============================================================================
// Processing Instruction Tests
// =============================================================================
Deno.test("parseSync() throws on PI target containing colon", () => {
assertThrows(
() => parseSync("<?ns:target content?><root/>"),
XmlSyntaxError,
"Cannot use ':' in processing instruction target",
);
});
Deno.test("parseSync() throws on XML declaration not at start", () => {
assertThrows(
() => parseSync('<!-- comment --><?xml version="1.0"?><root/>'),
XmlSyntaxError,
"XML declaration must appear at the start of the document",
);
});
Deno.test("parseSync() throws on BOM before root element", () => {
// BOM (U+FEFF) is treated as content before root element which is invalid
// XML 1.0 §2.8 only allows whitespace, comments, PIs, and DOCTYPE before root
assertThrows(
() => parseSync('\uFEFF<?xml version="1.0"?><root/>'),
XmlSyntaxError,
"Cannot have content before the root element",
);
});
// =============================================================================
// Comment Validation Tests
// =============================================================================
Deno.test("parseSync() throws on dash immediately before --> in comment", () => {
assertThrows(
() => parseSync("<root><!-- test---></root>"),
XmlSyntaxError,
"Cannot use '-' immediately before '-->'",
);
});
// =============================================================================
// Text Content Validation Tests
// =============================================================================
Deno.test("parseSync() throws on ]]> in text content", () => {
assertThrows(
() => parseSync("<root>]]></root>"),
XmlSyntaxError,
"Cannot use ']]>' in text content",
);
});
Deno.test("parseSync() throws on illegal XML character in text", () => {
assertThrows(
() => parseSync("<root>\x00</root>"),
XmlSyntaxError,
"Illegal XML character U+0000",
);
});
Deno.test("parseSync() throws on illegal XML character in comment", () => {
assertThrows(
() => parseSync("<root><!-- \x01 --></root>"),
XmlSyntaxError,
"Illegal XML character U+0001",
);
});
Deno.test("parseSync() throws on illegal XML character in CDATA", () => {
assertThrows(
() => parseSync("<root><![CDATA[\x02]]></root>"),
XmlSyntaxError,
"Illegal XML character U+0002",
);
});
// =============================================================================
// No Root Element Test
// =============================================================================
Deno.test("parseSync() throws on empty document", () => {
assertThrows(
() => parseSync(""),
XmlSyntaxError,
"No root element found",
);
});
Deno.test("parseSync() throws on document with only whitespace", () => {
assertThrows(
() => parseSync(" \n "),
XmlSyntaxError,
"No root element found",
);
});
Deno.test("parseSync() throws on document with only declaration", () => {
assertThrows(
() => parseSync('<?xml version="1.0"?>'),
XmlSyntaxError,
"No root element found",
);
});
// =============================================================================
// Invalid Character in End Tag Tests
// =============================================================================
Deno.test("parseSync() throws on invalid character in end tag name", () => {
// The parser reads "r" then hits "@" which terminates the name
// Then expects '>' but finds 'o'
assertThrows(
() => parseSync("<root></r@ot>"),
XmlSyntaxError,
"Expected '>' in end tag",
);
});
// =============================================================================
// Self-closing root element with namespace
// =============================================================================
Deno.test("parseSync() handles self-closing root with namespace bindings", () => {
const doc = parseSync('<ns:root xmlns:ns="http://example.com"/>');
assertEquals(doc.root.name.prefix, "ns");
assertEquals(doc.root.name.uri, "http://example.com");
});
// =============================================================================
// Additional Coverage: DTD Internal Subset Edge Cases
// =============================================================================
Deno.test("parseSync() throws on unexpected end of input in DTD after <", () => {
assertThrows(
() => parseSync("<!DOCTYPE root [<", { disallowDoctype: false }),
XmlSyntaxError,
"Unexpected end of input in DTD",
);
});
Deno.test("parseSync() throws on unexpected character after < in DTD internal subset", () => {
assertThrows(
() => parseSync("<!DOCTYPE root [<x]><root/>", { disallowDoctype: false }),
XmlSyntaxError,
"Unexpected character 'x' after '<' in DTD",
);
});
Deno.test("parseSync() throws on conditional section [ in internal subset", () => {
// A bare '[' character (not as part of <![) is not allowed
assertThrows(
() => parseSync("<!DOCTYPE root [ [ ]><root/>", { disallowDoctype: false }),
XmlSyntaxError,
"Conditional sections (INCLUDE/IGNORE) are not allowed in internal DTD subset",
);
});
Deno.test("parseSync() throws on unexpected character in DTD internal subset", () => {
// Characters like '@' are not valid in the internal subset
assertThrows(
() => parseSync("<!DOCTYPE root [ @ ]><root/>", { disallowDoctype: false }),
XmlSyntaxError,
"Unexpected character '@' in DTD internal subset",
);
});
Deno.test("parseSync() throws on unterminated DTD internal subset", () => {
assertThrows(
() =>
parseSync("<!DOCTYPE root [ <!ELEMENT root (#PCDATA)>", {
disallowDoctype: false,
}),
XmlSyntaxError,
"Unterminated DTD internal subset",
);
});
// =============================================================================
// Additional Coverage: DTD Comment Edge Cases
// =============================================================================
Deno.test("parseSync() throws on single dash starting comment in DTD", () => {
// <!- is not a valid comment start, must be <!--
assertThrows(
() =>
parseSync("<!DOCTYPE root [<!->]><root/>", { disallowDoctype: false }),
XmlSyntaxError,
"Expected '--' to start comment in DTD",
);
});
Deno.test("parseSync() throws on -- not followed by > in DTD comment", () => {
assertThrows(
() =>
parseSync("<!DOCTYPE root [<!-- comment --x -->]><root/>", {
disallowDoctype: false,
}),
XmlSyntaxError,
"Cannot use '--' within XML comments",
);
});
Deno.test("parseSync() throws on unterminated comment in DTD", () => {
assertThrows(
() =>
parseSync("<!DOCTYPE root [<!-- unterminated", {
disallowDoctype: false,
}),
XmlSyntaxError,
"Unterminated comment in DTD",
);