xmloxide 0.4.3

A pure Rust reimplementation of libxml2 — memory-safe XML/HTML parsing
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
//! Core XML 1.0 parser state machine.
//!
//! Implements a hand-rolled recursive descent parser for XML 1.0 (Fifth Edition).
//! See <https://www.w3.org/TR/xml/> for the specification.

use std::collections::HashMap;

use crate::error::{ErrorSeverity, ParseError};
use crate::tree::{Attribute, Document, NodeId, NodeKind};
use crate::validation::dtd::{parse_dtd, serialize_dtd, AttributeDecl, AttributeType, EntityKind};

use super::input::{
    find_invalid_xml_char, may_contain_invalid_xml_chars, parse_cdata_content,
    parse_comment_content, parse_pi_content, parse_xml_decl, split_name, split_owned_name,
    validate_pubid, ExternalEntityInfo, NamespaceResolver, ParserInput, XMLNS_NAMESPACE,
    XML_NAMESPACE,
};
use super::ParseOptions;

/// Default maximum amplification factor for entity/attribute expansion.
///
/// libxml2 uses a factor of 5 — if the expanded output exceeds 5x the input
/// size due to default attribute application or entity expansion, the document
/// is rejected as a potential denial-of-service attack.
const DEFAULT_MAX_AMPLIFICATION: usize = 5;

/// The core XML parser.
pub(crate) struct XmlParser<'a> {
    /// Shared low-level input state (position, peek, advance, name parsing, etc.).
    input: ParserInput<'a>,
    /// The document being built.
    doc: Document,
    /// Parser options.
    options: ParseOptions,
    /// Namespace resolver managing the scope stack.
    ns: NamespaceResolver,
    /// DTD attribute type declarations, keyed by `(element_name, attr_name)`.
    /// Used for attribute value normalization of namespace URIs.
    attr_types: HashMap<(String, String), AttributeType>,
    /// DTD attribute default value declarations, keyed by element name.
    /// Used for applying default/fixed attributes from ATTLIST.
    attr_defaults: HashMap<String, Vec<AttributeDecl>>,
    /// Total input size in bytes, used for amplification factor checking.
    input_size: usize,
    /// Running total of bytes added through default attribute expansion.
    expansion_size: usize,
}

impl<'a> XmlParser<'a> {
    pub fn new(input: &'a str, options: &ParseOptions) -> Self {
        let mut pi = ParserInput::new(input);
        pi.set_recover(options.recover);
        pi.set_max_depth(options.max_depth);
        pi.set_max_name_length(options.max_name_length);
        pi.set_max_entity_expansions(options.max_entity_expansions);
        pi.set_entity_resolver(options.entity_resolver.clone());

        // Pre-size the node arena — roughly 1 node per 30 bytes of input.
        let estimated_nodes = (input.len() / 30).max(64);
        Self {
            input: pi,
            doc: Document::with_capacity(estimated_nodes),
            options: options.clone(),
            ns: NamespaceResolver::new(),
            attr_types: HashMap::new(),
            attr_defaults: HashMap::new(),
            input_size: input.len(),
            expansion_size: 0,
        }
    }

    /// Main parse entry point. Parses the entire document.
    pub fn parse(&mut self) -> Result<Document, ParseError> {
        // Parse optional XML declaration — must be at the very start of the
        // document with no leading whitespace (XML 1.0 §2.8).
        if self.input.looking_at(b"<?xml ")
            || self.input.looking_at(b"<?xml\t")
            || self.input.looking_at(b"<?xml\r")
        {
            self.parse_xml_declaration()?;
            // Skip whitespace immediately after the XML declaration — the
            // serializer always emits its own newline after the declaration.
            self.input.skip_whitespace();
        } else if !self.input.at_end() {
            // If there's no XML declaration, skip any leading whitespace.
            // Leading whitespace before a non-declaration is tolerated
            // (it will be handled as misc content).
            let had_leading_ws = self.input.skip_whitespace();
            // But if the whitespace was hiding an XML declaration, that's an error
            if had_leading_ws
                && (self.input.looking_at(b"<?xml ")
                    || self.input.looking_at(b"<?xml\t")
                    || self.input.looking_at(b"<?xml\r"))
            {
                return Err(self
                    .input
                    .fatal("XML declaration must be at the start of the document"));
            }
        }

        // Parse prolog content (comments, PIs, whitespace before root element)
        self.parse_misc(self.doc.root())?;

        // Parse optional DOCTYPE declaration
        if self.input.looking_at(b"<!DOCTYPE") || self.input.looking_at(b"<!doctype") {
            self.parse_doctype(self.doc.root())?;
            self.parse_misc(self.doc.root())?; // more misc after doctype
        }

        // Parse root element (required by XML 1.0 §2.1)
        if self.input.peek() == Some(b'<')
            && self
                .input
                .peek_at(1)
                .is_some_and(|b| b != b'!' && b != b'?')
        {
            self.parse_element(self.doc.root())?;
        } else if self.options.recover {
            self.input
                .push_diagnostic(ErrorSeverity::Error, "missing root element".to_string());
        } else {
            return Err(self.input.fatal("missing root element"));
        }

        // Parse trailing content (comments, PIs after root element)
        self.parse_misc(self.doc.root())?;

        self.input.skip_whitespace();
        if !self.input.at_end() && !self.options.recover {
            return Err(self.input.fatal("content after document element"));
        }

        // Sync diagnostics from input to document before returning.
        self.doc.diagnostics = std::mem::take(&mut self.input.diagnostics);

        Ok(std::mem::take(&mut self.doc))
    }

    // --- XML Declaration ---
    // See XML 1.0 §2.8: [23] XMLDecl

    fn parse_xml_declaration(&mut self) -> Result<(), ParseError> {
        let decl = parse_xml_decl(&mut self.input)?;
        self.doc.version = Some(decl.version);
        self.doc.encoding = decl.encoding;
        self.doc.standalone = decl.standalone;
        Ok(())
    }

    // --- Misc (comments, PIs, whitespace) ---

    fn parse_misc(&mut self, parent: NodeId) -> Result<(), ParseError> {
        loop {
            // Preserve document-level whitespace as text nodes (matches libxml2).
            // libxml2 normalizes prolog/epilog whitespace to a single `\n`
            // regardless of how many blank lines appear in the source.
            let ws = self.input.consume_whitespace();
            if !ws.is_empty() {
                let ws_node = self.doc.create_node(NodeKind::Text {
                    content: "\n".to_string(),
                });
                self.doc.append_child(parent, ws_node);
            }
            if self.input.at_end() {
                break;
            }
            if self.input.looking_at(b"<!--") {
                self.parse_comment(parent)?;
            } else if self.input.looking_at(b"<?") {
                self.parse_processing_instruction(parent)?;
            } else {
                break;
            }
        }
        Ok(())
    }

    // --- DOCTYPE Declaration ---
    // See XML 1.0 §2.8: [28] doctypedecl

    #[allow(clippy::too_many_lines)]
    fn parse_doctype(&mut self, parent: NodeId) -> Result<(), ParseError> {
        // Consume <!DOCTYPE (case-insensitive match already checked by caller)
        self.input.expect_str(b"<!DOCTYPE")?;
        self.input.skip_whitespace_required()?;

        // Read the root element name
        let name = self.input.parse_name()?;

        self.input.skip_whitespace();

        // Check for external ID: SYSTEM or PUBLIC
        let mut system_id = None;
        let mut public_id = None;

        if self.input.looking_at(b"SYSTEM") {
            self.input.expect_str(b"SYSTEM")?;
            self.input.skip_whitespace_required()?;
            system_id = Some(self.input.parse_quoted_value()?);
            self.input.skip_whitespace();
        } else if self.input.looking_at(b"PUBLIC") {
            self.input.expect_str(b"PUBLIC")?;
            self.input.skip_whitespace_required()?;
            let pid = self.input.parse_quoted_value()?;
            // Validate public ID characters per XML 1.0 §2.3 [13].
            if let Some(msg) = validate_pubid(&pid) {
                if self.options.recover {
                    self.input.push_diagnostic(ErrorSeverity::Warning, msg);
                } else {
                    return Err(self.input.fatal(msg));
                }
            }
            public_id = Some(pid);
            self.input.skip_whitespace_required()?;
            system_id = Some(self.input.parse_quoted_value()?);
            self.input.skip_whitespace();
        }

        // Flag whether there's an external DTD subset. Per XML 1.0 §4.1 WFC:
        // Entity Declared, undeclared entities are not WF errors when the
        // document references an unread external DTD subset.
        if system_id.is_some() || public_id.is_some() {
            self.input.has_external_dtd = true;
        }

        // Parse optional internal subset: [ ... ]
        let mut internal_subset = None;
        if self.input.peek() == Some(b'[') {
            self.input.advance(1);
            let start = self.input.pos();

            // Scan to matching ']', tracking depth for bracket chars inside
            // entity values. Quoted strings and comments are skipped to avoid
            // misinterpreting brackets or apostrophes in comments.
            let mut depth: u32 = 1;
            while !self.input.at_end() && depth > 0 {
                if self.input.looking_at(b"<!--") {
                    // Skip XML comments (may contain apostrophes/quotes)
                    self.input.advance(4);
                    while !self.input.at_end() && !self.input.looking_at(b"-->") {
                        self.input.advance(1);
                    }
                    if !self.input.at_end() {
                        self.input.advance(3); // consume -->
                    }
                } else if let Some(b'"' | b'\'') = self.input.peek() {
                    let quote = self.input.peek().unwrap_or(b'"');
                    self.input.advance(1);
                    while !self.input.at_end() && self.input.peek() != Some(quote) {
                        self.input.advance(1);
                    }
                    if !self.input.at_end() {
                        self.input.advance(1); // closing quote
                    }
                } else if self.input.peek() == Some(b'[') {
                    depth += 1;
                    self.input.advance(1);
                } else if self.input.peek() == Some(b']') {
                    depth -= 1;
                    self.input.advance(1);
                } else {
                    self.input.advance(1);
                }
            }
            if depth > 0 {
                return Err(self
                    .input
                    .fatal("unexpected end of input in internal subset"));
            }

            // Extract the internal subset text (between '[' and ']').
            let end = self.input.pos() - 1; // exclude the closing ']'
            let subset_text = std::str::from_utf8(self.input.slice(start, end))
                .ok()
                .map(str::to_string);

            if let Some(subset_text) = subset_text {
                // Detect parameter entity references in the internal subset.
                // Per XML 1.0 §4.1 WFC: Entity Declared, their presence
                // means undeclared general entities are not WF errors.
                if subset_text.contains('%') {
                    self.input.has_pe_references = true;
                }

                match parse_dtd(&subset_text) {
                    Ok(dtd) => {
                        // Wire entity declarations into the parser input
                        // for entity reference resolution.
                        for (ent_name, ent_decl) in &dtd.entities {
                            match &ent_decl.kind {
                                EntityKind::Internal(value) => {
                                    self.input
                                        .entity_map
                                        .insert(ent_name.clone(), value.clone());
                                }
                                EntityKind::External {
                                    system_id,
                                    public_id,
                                } => {
                                    self.input.entity_external.insert(
                                        ent_name.clone(),
                                        ExternalEntityInfo {
                                            system_id: system_id.clone(),
                                            public_id: public_id.clone(),
                                        },
                                    );
                                }
                            }
                        }

                        // Wire attribute type declarations for namespace
                        // URI normalization (XML 1.0 §3.3.3).
                        for (element_name, attrs) in &dtd.attributes {
                            for attr_decl in attrs {
                                self.attr_types.insert(
                                    (element_name.clone(), attr_decl.attribute_name.clone()),
                                    attr_decl.attribute_type.clone(),
                                );
                            }
                        }

                        // Store attribute default values for later application.
                        for (element_name, attrs) in &dtd.attributes {
                            for attr_decl in attrs {
                                self.attr_defaults
                                    .entry(element_name.clone())
                                    .or_default()
                                    .push(attr_decl.clone());
                            }
                        }

                        // Re-serialize the DTD from parsed structures for
                        // consistent formatting (matches libxml2 behavior).
                        let serialized = serialize_dtd(&dtd);
                        if !serialized.is_empty() {
                            internal_subset = Some(serialized);
                        }
                    }
                    Err(e) => {
                        if self.options.recover {
                            self.input.push_diagnostic(
                                ErrorSeverity::Warning,
                                format!("error parsing DTD internal subset: {}", e.message),
                            );
                        } else {
                            return Err(self
                                .input
                                .fatal(format!("error in DTD internal subset: {}", e.message)));
                        }
                    }
                }
            }

            self.input.skip_whitespace();
        }

        self.input.expect_byte(b'>')?;

        let doctype_id = self.doc.create_node(NodeKind::DocumentType {
            name,
            system_id,
            public_id,
            internal_subset,
        });
        self.doc.append_child(parent, doctype_id);
        Ok(())
    }

    // --- Elements ---
    // See XML 1.0 §3.1: [40] STag, [42] ETag, [44] EmptyElemTag

    #[allow(clippy::too_many_lines)]
    fn parse_element(&mut self, parent: NodeId) -> Result<NodeId, ParseError> {
        self.input.increment_depth()?;
        self.input.expect_byte(b'<')?;
        let name = self.input.parse_name()?;
        let mut attributes = Vec::new();

        // Parse attributes
        loop {
            let had_ws = self.input.skip_whitespace();
            if self.input.peek() == Some(b'>') || self.input.looking_at(b"/>") {
                break;
            }
            if !had_ws {
                return Err(self.input.fatal("whitespace required between attributes"));
            }
            let attr = self.parse_attribute()?;
            attributes.push(attr);
        }

        // Check for duplicate attributes (XML 1.0 §3.1 WFC: Unique Att Spec)
        // Skip for 0 or 1 attributes (no duplicates possible).
        if attributes.len() >= 2 {
            // O(n²) comparison avoids HashSet allocation for small attribute lists.
            let mut found_dup = false;
            'outer: for i in 1..attributes.len() {
                for j in 0..i {
                    if attributes[i].name == attributes[j].name
                        && attributes[i].prefix == attributes[j].prefix
                    {
                        let full_name = if let Some(ref pfx) = attributes[i].prefix {
                            format!("{pfx}:{}", attributes[i].name)
                        } else {
                            attributes[i].name.clone()
                        };
                        if self.options.recover {
                            self.input.push_diagnostic(
                                ErrorSeverity::Error,
                                format!("duplicate attribute: '{full_name}'"),
                            );
                            found_dup = true;
                        } else {
                            return Err(self
                                .input
                                .fatal(format!("duplicate attribute: '{full_name}'")));
                        }
                        break 'outer;
                    }
                }
            }
            let _ = found_dup; // suppress unused warning
        }

        // --- Apply #FIXED default attributes from DTD ATTLIST declarations ---
        // Per XML 1.0 §3.3.2, if an attribute declared with #FIXED is not
        // present on the element, the parser must add it with the declared
        // default value. #DEFAULT attributes are tracked for amplification
        // factor checking but not inserted into the tree (matching libxml2).
        // Namespace declarations (xmlns, xmlns:prefix) are inserted before
        // other attributes to match libxml2's attribute ordering.
        if let Some(defaults) = if self.attr_defaults.is_empty() {
            None
        } else {
            self.attr_defaults.get(&name).cloned()
        } {
            let mut insert_pos = 0; // insertion point for namespace declarations
            for attr_decl in &defaults {
                let (default_value, is_fixed) = match &attr_decl.default {
                    crate::validation::dtd::AttributeDefault::Fixed(v) => (Some(v.clone()), true),
                    crate::validation::dtd::AttributeDefault::Default(v) => {
                        (Some(v.clone()), false)
                    }
                    _ => (None, false),
                };
                if let Some(value) = default_value {
                    // Check if the attribute is already present by comparing
                    // prefix:local components directly, avoiding format!/clone.
                    let attr_name = &attr_decl.attribute_name;
                    let (decl_pfx, decl_local) = split_name(attr_name);
                    let already_present = attributes
                        .iter()
                        .any(|a| a.name == decl_local && a.prefix.as_deref() == decl_pfx);
                    if !already_present {
                        // Track expansion for amplification factor check
                        // (both #FIXED and #DEFAULT contribute to expansion)
                        self.expansion_size += attr_name.len() + value.len();

                        // Only insert #FIXED attributes into the tree
                        if is_fixed {
                            let (decl_prefix, decl_local) = split_name(attr_name);
                            let attr = Attribute {
                                name: decl_local.to_string(),
                                value,
                                prefix: decl_prefix.map(String::from),
                                namespace: None,
                                raw_value: None,
                            };
                            let is_ns_decl =
                                attr_name == "xmlns" || attr_name.starts_with("xmlns:");
                            if is_ns_decl {
                                attributes.insert(insert_pos, attr);
                                insert_pos += 1;
                            } else {
                                attributes.push(attr);
                            }
                        }
                    }
                }
            }

            // Check amplification factor: reject if default attribute
            // expansion would exceed the input size by more than the
            // maximum factor (matching libxml2's xmlCtxtSetMaxAmplification).
            if self.expansion_size > self.input_size.saturating_mul(DEFAULT_MAX_AMPLIFICATION) {
                return Err(self
                    .input
                    .fatal("maximum entity amplification factor exceeded"));
            }
        }

        // --- Namespace processing (Namespaces in XML 1.0 section 3) ---

        // Check for namespace declarations to skip namespace scope push/pop
        // when not needed. Skip the scan entirely when there are no attributes.
        let has_ns_decls = !attributes.is_empty()
            && attributes.iter().any(|a| {
                a.prefix.as_deref() == Some("xmlns") || (a.prefix.is_none() && a.name == "xmlns")
            });
        if has_ns_decls {
            self.ns.push_scope();
        }

        // Split into prefix and local name for namespace processing.
        let (prefix, local_name) = split_name(&name);

        // Validate QName syntax: check for multiple colons (the local part
        // should not contain a colon after split_name).
        if prefix.is_some() && local_name.contains(':') {
            let msg = "QName contains multiple colons";
            if self.options.recover {
                self.input
                    .push_diagnostic(ErrorSeverity::Error, msg.to_string());
            } else {
                return Err(self.input.fatal(msg));
            }
        }

        // Reject element names with "xmlns" prefix (Namespaces in XML 1.0 §3).
        if prefix == Some("xmlns") {
            if self.options.recover {
                self.input.push_diagnostic(
                    ErrorSeverity::Error,
                    "elements must not have the prefix 'xmlns'".to_string(),
                );
            } else {
                return Err(self
                    .input
                    .fatal("elements must not have the prefix 'xmlns'"));
            }
        }

        // Scan attributes for namespace declarations and bind them,
        // with validation of namespace constraints.
        // In recovery mode, some invalid attributes may be stripped.
        let mut strip_attr_indices: Vec<usize> = Vec::new();
        if has_ns_decls {
            for (attr_idx, attr) in attributes.iter().enumerate() {
                if attr.prefix.as_deref() == Some("xmlns") {
                    // Prefixed namespace declaration: xmlns:prefix="uri"
                    let declared_prefix = &attr.name;

                    // Validate QName: the local part (declared_prefix) must be
                    // a non-empty NCName (no colon, not empty). An empty local
                    // part means the attribute was `xmlns:` with nothing after
                    // the colon, which is not a valid QName.
                    if declared_prefix.is_empty() {
                        let msg = "namespace prefix must not be empty (invalid QName 'xmlns:')";
                        if self.options.recover {
                            self.input
                                .push_diagnostic(ErrorSeverity::Error, msg.to_string());
                            continue;
                        }
                        return Err(self.input.fatal(msg));
                    }
                    if declared_prefix.contains(':') {
                        let msg = "QName contains multiple colons";
                        if self.options.recover {
                            self.input
                                .push_diagnostic(ErrorSeverity::Error, msg.to_string());
                        } else {
                            return Err(self.input.fatal(msg));
                        }
                    }

                    // Normalize namespace URI based on DTD-declared attribute
                    // type. For non-CDATA types (e.g., NMTOKEN), whitespace is
                    // collapsed per XML 1.0 §3.3.3. Construct the full attribute
                    // name only when attr_types is non-empty (DTD present).
                    let ns_value = if self.attr_types.is_empty() {
                        attr.value.clone()
                    } else {
                        let attr_qname = format!("xmlns:{declared_prefix}");
                        self.normalize_attr_value_by_type(&name, &attr_qname, &attr.value)
                    };

                    // XML 1.0 Namespaces: cannot unbind a prefix (xmlns:prefix="").
                    if ns_value.is_empty() {
                        if self.options.recover {
                            self.input.push_diagnostic(
                                ErrorSeverity::Error,
                                format!("namespace prefix '{declared_prefix}' cannot be undeclared in XML 1.0"),
                            );
                        } else {
                            return Err(self.input.fatal(format!(
                                "namespace prefix '{declared_prefix}' cannot be undeclared in XML 1.0"
                            )));
                        }
                    }

                    // Cannot declare the 'xmlns' prefix itself.
                    if declared_prefix == "xmlns" {
                        if self.options.recover {
                            self.input.push_diagnostic(
                                ErrorSeverity::Error,
                                "the 'xmlns' prefix must not be declared".to_string(),
                            );
                        } else {
                            return Err(self
                                .input
                                .fatal("the 'xmlns' prefix must not be declared"));
                        }
                    }

                    // 'xml' prefix must map to the XML namespace URI and vice versa.
                    if declared_prefix == "xml" && ns_value != XML_NAMESPACE {
                        if self.options.recover {
                            self.input.push_diagnostic(
                                ErrorSeverity::Error,
                                "the 'xml' prefix must be bound to the XML namespace".to_string(),
                            );
                            // In recovery mode, strip the invalid rebinding
                            // (matches libxml2: output is <tst/> not <tst xmlns:xml="..."/>).
                            strip_attr_indices.push(attr_idx);
                            continue;
                        }
                        return Err(self
                            .input
                            .fatal("the 'xml' prefix must be bound to the XML namespace"));
                    }

                    // No other prefix may be bound to the XML namespace URI.
                    if declared_prefix != "xml" && ns_value == XML_NAMESPACE {
                        if self.options.recover {
                            self.input.push_diagnostic(
                                ErrorSeverity::Error,
                                "only the 'xml' prefix may be bound to the XML namespace"
                                    .to_string(),
                            );
                        } else {
                            return Err(self
                                .input
                                .fatal("only the 'xml' prefix may be bound to the XML namespace"));
                        }
                    }

                    // No prefix may be bound to the xmlns namespace URI.
                    if ns_value == XMLNS_NAMESPACE {
                        if self.options.recover {
                            self.input.push_diagnostic(
                                ErrorSeverity::Error,
                                "the xmlns namespace must not be bound to any prefix".to_string(),
                            );
                        } else {
                            return Err(self
                                .input
                                .fatal("the xmlns namespace must not be bound to any prefix"));
                        }
                    }

                    self.ns.bind(Some(attr.name.clone()), ns_value);
                } else if attr.prefix.is_none() && attr.name == "xmlns" {
                    // Default namespace declaration: xmlns="uri"

                    // Normalize namespace URI based on DTD-declared attribute type.
                    let ns_value = if self.attr_types.is_empty() {
                        attr.value.clone()
                    } else {
                        self.normalize_attr_value_by_type(&name, "xmlns", &attr.value)
                    };

                    // Cannot bind default namespace to the XML or xmlns namespace URIs.
                    if ns_value == XML_NAMESPACE {
                        if self.options.recover {
                            self.input.push_diagnostic(
                                ErrorSeverity::Error,
                                "the xml namespace must not be declared as the default namespace"
                                    .to_string(),
                            );
                        } else {
                            return Err(self.input.fatal(
                                "the xml namespace must not be declared as the default namespace",
                            ));
                        }
                    }
                    if ns_value == XMLNS_NAMESPACE {
                        if self.options.recover {
                            self.input.push_diagnostic(
                                ErrorSeverity::Error,
                                "the xmlns namespace must not be declared as the default namespace"
                                    .to_string(),
                            );
                        } else {
                            return Err(self.input.fatal(
                                "the xmlns namespace must not be declared as the default namespace",
                            ));
                        }
                    }
                    self.ns.bind(None, ns_value);
                } else if attr.prefix.is_some() && attr.name.contains(':') {
                    // Validate QName syntax for prefixed non-namespace attributes.
                    let msg = "QName contains multiple colons";
                    if self.options.recover {
                        self.input
                            .push_diagnostic(ErrorSeverity::Error, msg.to_string());
                    } else {
                        return Err(self.input.fatal(msg));
                    }
                }
            }
        } else {
            // No namespace declarations — only validate QName syntax for
            // prefixed attributes (checking for multiple colons).
            for attr in &attributes {
                if attr.prefix.is_some() && attr.name.contains(':') {
                    let msg = "QName contains multiple colons";
                    if self.options.recover {
                        self.input
                            .push_diagnostic(ErrorSeverity::Error, msg.to_string());
                    } else {
                        return Err(self.input.fatal(msg));
                    }
                }
            }
        }

        // Resolve the element's namespace URI from its prefix.
        let elem_ns = self.ns.resolve(prefix).map(String::from);

        // Check for unbound element prefix.
        if let Some(pfx) = prefix {
            if pfx != "xml" && elem_ns.is_none() {
                if self.options.recover {
                    self.input.push_diagnostic(
                        ErrorSeverity::Error,
                        format!("unbound namespace prefix '{pfx}'"),
                    );
                } else {
                    return Err(self
                        .input
                        .fatal(format!("unbound namespace prefix '{pfx}'")));
                }
            }
        }

        // Resolve namespace URIs for non-xmlns prefixed attributes.
        // Unprefixed attributes do NOT inherit the default namespace (per spec).
        // Skip entirely when there are no prefixed non-xmlns attributes.
        let has_prefixed_attrs = !attributes.is_empty()
            && attributes
                .iter()
                .any(|a| a.prefix.is_some() && a.prefix.as_deref() != Some("xmlns"));
        if has_prefixed_attrs {
            for attr in &mut attributes {
                if let Some(pfx) = &attr.prefix {
                    if pfx == "xmlns" {
                        continue; // namespace declaration, not a real attribute prefix
                    }
                    let resolved = self.ns.resolve(Some(pfx.as_str())).map(String::from);
                    if pfx != "xml" && resolved.is_none() {
                        if self.options.recover {
                            self.input.push_diagnostic(
                                ErrorSeverity::Error,
                                format!("unbound namespace prefix '{pfx}' on attribute"),
                            );
                        } else {
                            return Err(self
                                .input
                                .fatal(format!("unbound namespace prefix '{pfx}' on attribute")));
                        }
                    }
                    attr.namespace = resolved;
                }
            }
        }

        // Namespace-aware attribute uniqueness: two attributes with the same
        // namespace URI and local name are duplicates, even if they use different
        // prefixes (Namespaces in XML 1.0 §6.3).
        // Only meaningful when there are 2+ namespaced (non-xmlns) attributes.
        // Skip entirely when no prefixed attributes exist (no namespaces were
        // resolved, so ns_attr_count is guaranteed to be 0).
        if has_prefixed_attrs {
            let ns_attr_count = attributes.iter().filter(|a| a.namespace.is_some()).count();
            if ns_attr_count >= 2 {
                // O(n²) comparison avoids HashSet allocation
                'ns_outer: for i in 1..attributes.len() {
                    if attributes[i].namespace.is_none() {
                        continue;
                    }
                    for j in 0..i {
                        if attributes[j].namespace.is_none() {
                            continue;
                        }
                        if attributes[i].namespace == attributes[j].namespace
                            && attributes[i].name == attributes[j].name
                        {
                            let display = if let Some(ns) = &attributes[i].namespace {
                                format!("{{{}}}:{}", ns, attributes[i].name)
                            } else {
                                attributes[i].name.clone()
                            };
                            if self.options.recover {
                                self.input.push_diagnostic(
                                    ErrorSeverity::Error,
                                    format!("namespace-aware duplicate attribute: '{display}'"),
                                );
                            } else {
                                return Err(self.input.fatal(format!(
                                    "namespace-aware duplicate attribute: '{display}'"
                                )));
                            }
                            break 'ns_outer;
                        }
                    }
                }
            }
        }

        // Remove stripped attributes (e.g., invalid xmlns:xml rebindings).
        if !strip_attr_indices.is_empty() {
            // Remove in reverse order to preserve indices.
            for &idx in strip_attr_indices.iter().rev() {
                attributes.remove(idx);
            }
        }

        // Consume the original name String via split_owned_name, avoiding a
        // re-allocation for unprefixed names (the common case). For unprefixed
        // names, split_owned_name returns (None, name) — just a move, zero copy.
        let (elem_prefix_owned, elem_local_owned) = split_owned_name(name);
        // Auto-populate id_map for "id" attributes (enables element_by_id
        // and fast CSS #id selectors without requiring DTD validation).
        let id_value = attributes.iter().find_map(|a| {
            if a.prefix.is_none() && a.name == "id" {
                Some(a.value.clone())
            } else {
                None
            }
        });

        let elem_id = self.doc.create_node(NodeKind::Element {
            name: elem_local_owned,
            prefix: elem_prefix_owned,
            namespace: elem_ns,
            attributes,
        });
        self.doc.append_child(parent, elem_id);

        if let Some(id_val) = id_value {
            self.doc.set_id(&id_val, elem_id);
        }

        // Empty element tag <foo/>
        if self.input.looking_at(b"/>") {
            self.input.advance(2);
            if has_ns_decls {
                self.ns.pop_scope();
            }
            self.input.decrement_depth();
            return Ok(elem_id);
        }

        // Start tag close >
        self.input.expect_byte(b'>')?;

        // Parse element content
        self.parse_content(elem_id)?;

        // Parse end tag — read back the stored name from the tree node
        // for matching, since the original name was consumed by split_owned_name.
        self.input.expect_str(b"</")?;
        let (match_prefix, match_local) = {
            let node = self.doc.node(elem_id);
            match &node.kind {
                NodeKind::Element { name, prefix, .. } => (prefix.as_deref(), name.as_str()),
                _ => unreachable!(),
            }
        };
        if let Some(end_name) = self.input.parse_name_eq_parts(match_prefix, match_local)? {
            let expected = match match_prefix {
                Some(pfx) => format!("{pfx}:{match_local}"),
                None => match_local.to_string(),
            };
            if self.options.recover {
                self.input.push_diagnostic(
                    ErrorSeverity::Error,
                    format!("mismatched end tag: expected </{expected}>, found </{end_name}>"),
                );
            } else {
                return Err(self.input.fatal(format!(
                    "mismatched end tag: expected </{expected}>, found </{end_name}>"
                )));
            }
        }
        self.input.skip_whitespace();
        self.input.expect_byte(b'>')?;

        // Pop the namespace scope when leaving this element (only if we pushed).
        if has_ns_decls {
            self.ns.pop_scope();
        }
        self.input.decrement_depth();

        Ok(elem_id)
    }

    /// Normalizes an attribute value based on its DTD-declared type.
    ///
    /// For non-CDATA types (e.g., NMTOKEN, ID, IDREF), collapses whitespace:
    /// trim leading/trailing whitespace, reduce internal whitespace sequences
    /// to single spaces (XML 1.0 §3.3.3).
    fn normalize_attr_value_by_type(
        &self,
        element_name: &str,
        attr_name: &str,
        value: &str,
    ) -> String {
        // Fast path: skip lookup when no DTD attribute types are declared
        // (the common case for documents without a DTD).
        if !self.attr_types.is_empty() {
            let key = (element_name.to_string(), attr_name.to_string());
            if let Some(attr_type) = self.attr_types.get(&key) {
                if !matches!(attr_type, AttributeType::CData) {
                    return value.split_whitespace().collect::<Vec<_>>().join(" ");
                }
            }
        }
        value.to_string()
    }

    // --- Content ---
    // See XML 1.0 §3.1: [43] content

    fn parse_content(&mut self, parent: NodeId) -> Result<(), ParseError> {
        loop {
            if self.input.at_end() {
                if self.options.recover {
                    break;
                }
                return Err(self
                    .input
                    .fatal("unexpected end of input in element content"));
            }

            // End tag starts
            if self.input.looking_at(b"</") {
                break;
            }

            if self.input.looking_at(b"<![CDATA[") {
                self.parse_cdata(parent)?;
            } else if self.input.looking_at(b"<!--") {
                self.parse_comment(parent)?;
            } else if self.input.looking_at(b"<?") {
                self.parse_processing_instruction(parent)?;
            } else if self.input.peek() == Some(b'<') {
                self.parse_element(parent)?;
            } else {
                self.parse_char_data(parent)?;
            }
        }
        Ok(())
    }

    // --- Character Data ---
    // See XML 1.0 §2.4: [14] CharData

    #[allow(clippy::too_many_lines)]
    fn parse_char_data(&mut self, parent: NodeId) -> Result<(), ParseError> {
        let mut text = String::new();

        while !self.input.at_end() {
            // Bulk scan: find the next `<`, `&`, or `]]>` boundary and
            // consume all safe bytes in one go.
            let safe_len = self.input.scan_char_data();
            if safe_len > 0 {
                let start = self.input.pos();
                let chunk = std::str::from_utf8(self.input.slice(start, start + safe_len))
                    .map_err(|_| self.input.fatal("invalid UTF-8 in character data"))?;
                // Fast byte-level pre-check for invalid XML chars (0x7F,
                // U+FFFE, U+FFFF). Skips the expensive char-by-char
                // validation for the 99.9% of chunks that are clean.
                let bad_char = if may_contain_invalid_xml_chars(chunk.as_bytes()) {
                    find_invalid_xml_char(chunk)
                } else {
                    None
                };
                // Append text with CR normalization if needed (XML 1.0 §2.11)
                if chunk.as_bytes().contains(&b'\r') {
                    let mut chars = chunk.chars().peekable();
                    while let Some(ch) = chars.next() {
                        if ch == '\r' {
                            if chars.peek() == Some(&'\n') {
                                chars.next();
                            }
                            text.push('\n');
                        } else {
                            text.push(ch);
                        }
                    }
                } else {
                    text.push_str(chunk);
                }
                // chunk borrow released — safe to mutably borrow self.input
                self.input.advance_counting_lines(safe_len);
                if let Some(bad) = bad_char {
                    if self.options.recover {
                        self.input.push_diagnostic(
                            ErrorSeverity::Error,
                            format!("invalid XML character: U+{:04X}", bad as u32),
                        );
                    } else {
                        return Err(self
                            .input
                            .fatal(format!("invalid XML character: U+{:04X}", bad as u32)));
                    }
                }
                continue;
            }

            if self.input.peek() == Some(b'<') {
                break;
            }

            // XML 1.0 §2.4: "]]>" is forbidden in character data
            if self.input.looking_at(b"]]>") {
                if self.options.recover {
                    self.input.push_diagnostic(
                        ErrorSeverity::Error,
                        "']]>' not allowed in character data".to_string(),
                    );
                    text.push_str("]]>");
                    self.input.advance(3);
                    continue;
                }
                return Err(self.input.fatal("']]>' not allowed in character data"));
            }

            if self.input.peek() == Some(b'&') {
                // Check if this is a named entity reference (not char ref, not builtin)
                // whose replacement text is plain text (no '<'). If so, preserve
                // it as an EntityRef node rather than expanding. Entities that
                // contain XML markup ('<') must still be expanded so that the
                // markup is validated (e.g., namespace prefix checking).
                // Skip the peek for builtins at byte level to avoid String allocation.
                if self.input.peek_at(1) != Some(b'#') && !self.is_looking_at_builtin_entity_ref() {
                    if let Some(entity_name) = self.peek_entity_ref_name() {
                        // entity_name is guaranteed non-builtin at this point.
                        // Preserve as EntityRef node if:
                        // 1. The entity is internally declared and text-only (no '<'), OR
                        // 2. The entity is undeclared but we're in tolerant mode
                        //    (external DTD or PE refs make undeclared entities non-fatal)
                        let is_text_only =
                            self.input.entity_map.get(&entity_name).is_some_and(|v| {
                                // Check the replacement text after expanding character
                                // references, not the raw value. An entity like
                                // "&#60;foo>" contains no literal '<' but expands to
                                // "<foo>" which is markup and must be re-parsed.
                                let replacement = crate::validation::dtd::expand_char_refs_only(v);
                                !replacement.contains('<')
                            });
                        let is_undeclared_tolerant =
                            !self.input.entity_map.contains_key(&entity_name)
                                && !self.input.entity_external.contains_key(&entity_name)
                                && (self.input.has_pe_references || self.input.has_external_dtd);
                        let should_preserve = is_text_only || is_undeclared_tolerant;

                        if should_preserve {
                            // Flush accumulated text before the entity ref
                            if !text.is_empty() {
                                let text_id = self.doc.create_node(NodeKind::Text {
                                    content: std::mem::take(&mut text),
                                });
                                self.doc.append_child(parent, text_id);
                            }
                            // Consume &name;
                            self.input.advance(1); // &
                            let name = self.input.parse_name()?;
                            self.input.expect_byte(b';')?;
                            // Count entity expansion for limit tracking
                            self.input.entity_expansions += 1;
                            let entity_value = self.input.entity_map.get(&name).cloned();
                            let ref_id = self.doc.create_node(NodeKind::EntityRef {
                                name,
                                value: entity_value,
                            });
                            self.doc.append_child(parent, ref_id);
                            continue;
                        }
                    }
                }
                self.input.parse_reference_into(&mut text)?;
            } else {
                let ch = self.input.next_char()?;
                text.push(ch);
            }
        }

        if !text.is_empty() {
            // Strip blank text nodes if configured
            if self.options.no_blanks && text.chars().all(char::is_whitespace) {
                return Ok(());
            }
            let text_id = self.doc.create_node(NodeKind::Text { content: text });
            self.doc.append_child(parent, text_id);
        }

        Ok(())
    }

    /// Checks if the input is positioned at a builtin entity reference
    /// (`&amp;`, `&lt;`, `&gt;`, `&apos;`, `&quot;`) using byte-level
    /// checks. This avoids the `String` allocation of `peek_entity_ref_name`.
    #[inline]
    fn is_looking_at_builtin_entity_ref(&self) -> bool {
        let remaining = self.input.remaining();
        if remaining.len() < 4 || remaining[0] != b'&' {
            return false;
        }
        let after_amp = &remaining[1..];
        after_amp.starts_with(b"lt;")
            || after_amp.starts_with(b"gt;")
            || after_amp.starts_with(b"amp;")
            || after_amp.starts_with(b"apos;")
            || after_amp.starts_with(b"quot;")
    }

    /// Peeks ahead to extract the entity name from `&name;` without consuming
    /// any input. Returns `None` if the next bytes don't form a valid entity
    /// reference pattern.
    fn peek_entity_ref_name(&self) -> Option<String> {
        // We're at `&` — look ahead past it to find the name and `;`
        let remaining = self.input.remaining();
        if remaining.len() < 2 || remaining[0] != b'&' {
            return None;
        }
        let mut i = 1;
        // Collect name bytes
        let name_start = i;
        while i < remaining.len()
            && (remaining[i].is_ascii_alphanumeric()
                || remaining[i] == b'_'
                || remaining[i] == b':'
                || remaining[i] == b'-'
                || remaining[i] == b'.')
        {
            i += 1;
        }
        if i == name_start || i >= remaining.len() || remaining[i] != b';' {
            return None;
        }
        std::str::from_utf8(&remaining[name_start..i])
            .ok()
            .map(String::from)
    }

    /// Checks if all entity references (`&name;`) in a raw attribute value
    /// text are declared in the entity map. Returns false if any undeclared
    /// entity ref is found (these should not be preserved via `raw_value`).
    fn all_entity_refs_declared(&self, raw: &str) -> bool {
        let bytes = raw.as_bytes();
        let mut i = 0;
        while i < bytes.len() {
            if bytes[i] == b'&' && i + 1 < bytes.len() && bytes[i + 1] != b'#' {
                // Named entity reference — extract the name
                let mut j = i + 1;
                while j < bytes.len()
                    && (bytes[j].is_ascii_alphanumeric()
                        || bytes[j] == b'_'
                        || bytes[j] == b':'
                        || bytes[j] == b'-'
                        || bytes[j] == b'.')
                {
                    j += 1;
                }
                if j < bytes.len() && bytes[j] == b';' {
                    let name = std::str::from_utf8(&bytes[i + 1..j]).unwrap_or("");
                    if !is_builtin_entity(name) && !self.input.entity_map.contains_key(name) {
                        return false;
                    }
                    i = j + 1;
                } else {
                    i += 1;
                }
            } else {
                i += 1;
            }
        }
        true
    }

    // --- Attributes ---
    // See XML 1.0 §3.1: [41] Attribute

    fn parse_attribute(&mut self) -> Result<Attribute, ParseError> {
        let name = self.input.parse_name()?;
        self.input.skip_whitespace();
        self.input.expect_byte(b'=')?;
        self.input.skip_whitespace();

        // Capture raw attribute value text (before entity expansion) so we
        // can preserve entity references during serialization.
        let raw_start = self.input.pos();
        let value = self.input.parse_attribute_value()?;
        let raw_end = self.input.pos();

        // Extract raw value (between quotes) — the raw slice includes the
        // outer quote chars, so trim them. Skip entirely when the raw bytes
        // contain no '&' (most attributes have no entity references).
        let raw_value = if raw_end > raw_start + 2 {
            let raw_bytes = self.input.slice(raw_start + 1, raw_end - 1);
            if raw_bytes.contains(&b'&') {
                let raw_str = std::str::from_utf8(raw_bytes).ok().map(str::to_string);
                // Only store raw_value if it differs from the expanded value
                // (i.e., it contained entity references that got expanded) AND
                // all entity references in the raw value are declared (not
                // undeclared entities that expanded to empty string).
                raw_str.filter(|raw| *raw != value && self.all_entity_refs_declared(raw))
            } else {
                None
            }
        } else {
            None
        };

        let (prefix, local_name) = split_owned_name(name);

        Ok(Attribute {
            name: local_name,
            value,
            prefix,
            namespace: None,
            raw_value,
        })
    }

    // --- Comments ---
    // See XML 1.0 §2.5: [15] Comment

    fn parse_comment(&mut self, parent: NodeId) -> Result<(), ParseError> {
        let content = parse_comment_content(&mut self.input)?;
        let comment_id = self.doc.create_node(NodeKind::Comment { content });
        self.doc.append_child(parent, comment_id);
        Ok(())
    }

    // --- CDATA Sections ---
    // See XML 1.0 §2.7: [18] CDSect

    fn parse_cdata(&mut self, parent: NodeId) -> Result<(), ParseError> {
        let content = parse_cdata_content(&mut self.input)?;
        let cdata_id = self.doc.create_node(NodeKind::CData { content });
        self.doc.append_child(parent, cdata_id);
        Ok(())
    }

    // --- Processing Instructions ---
    // See XML 1.0 §2.6: [16] PI

    fn parse_processing_instruction(&mut self, parent: NodeId) -> Result<(), ParseError> {
        let (target, data) = parse_pi_content(&mut self.input)?;
        let pi_id = self
            .doc
            .create_node(NodeKind::ProcessingInstruction { target, data });
        self.doc.append_child(parent, pi_id);
        Ok(())
    }
}

/// Returns true if the entity name is one of the five XML builtin entities.
fn is_builtin_entity(name: &str) -> bool {
    matches!(name, "amp" | "lt" | "gt" | "apos" | "quot")
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::tree::Document;
    use pretty_assertions::assert_eq;

    fn parse(input: &str) -> Document {
        Document::parse_str(input).unwrap_or_else(|e| panic!("parse failed: {e}"))
    }

    #[test]
    fn test_parse_empty_element() {
        let doc = parse("<root/>");
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_name(root), Some("root"));
        assert_eq!(doc.first_child(root), None);
    }

    #[test]
    fn test_parse_element_with_text() {
        let doc = parse("<greeting>Hello, world!</greeting>");
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_name(root), Some("greeting"));
        assert_eq!(doc.text_content(root), "Hello, world!");
    }

    #[test]
    fn test_parse_nested_elements() {
        let doc = parse("<a><b><c/></b></a>");
        let a = doc.root_element().unwrap();
        assert_eq!(doc.node_name(a), Some("a"));

        let b = doc.first_child(a).unwrap();
        assert_eq!(doc.node_name(b), Some("b"));

        let c = doc.first_child(b).unwrap();
        assert_eq!(doc.node_name(c), Some("c"));
    }

    #[test]
    fn test_parse_attributes() {
        let doc = parse("<div id=\"main\" class=\"big\"/>");
        let root = doc.root_element().unwrap();
        assert_eq!(doc.attribute(root, "id"), Some("main"));
        assert_eq!(doc.attribute(root, "class"), Some("big"));
    }

    #[test]
    fn test_parse_single_quoted_attributes() {
        let doc = parse("<div id='main'/>");
        let root = doc.root_element().unwrap();
        assert_eq!(doc.attribute(root, "id"), Some("main"));
    }

    #[test]
    fn test_parse_xml_declaration() {
        let doc = parse("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root/>");
        assert_eq!(doc.version.as_deref(), Some("1.0"));
        assert_eq!(doc.encoding.as_deref(), Some("UTF-8"));
    }

    #[test]
    fn test_parse_xml_declaration_standalone() {
        let doc = parse("<?xml version=\"1.0\" standalone=\"yes\"?><root/>");
        assert_eq!(doc.standalone, Some(true));
    }

    #[test]
    fn test_parse_comment() {
        let doc = parse("<root><!-- hello --></root>");
        let root = doc.root_element().unwrap();
        let child = doc.first_child(root).unwrap();
        assert_eq!(doc.node_text(child), Some(" hello "));
    }

    #[test]
    fn test_parse_cdata() {
        let doc = parse("<root><![CDATA[x < 1 && y > 2]]></root>");
        let root = doc.root_element().unwrap();
        let child = doc.first_child(root).unwrap();
        assert_eq!(doc.node_text(child), Some("x < 1 && y > 2"));
    }

    #[test]
    fn test_parse_processing_instruction() {
        let doc = parse("<?my-pi some data?><root/>");
        let pi = doc.first_child(doc.root()).unwrap();
        assert_eq!(doc.node_name(pi), Some("my-pi"));
        assert_eq!(doc.node_text(pi), Some("some data"));
    }

    #[test]
    fn test_parse_entity_references() {
        let doc = parse("<root>&amp; &lt; &gt; &apos; &quot;</root>");
        let root = doc.root_element().unwrap();
        assert_eq!(doc.text_content(root), "& < > ' \"");
    }

    #[test]
    fn test_parse_char_reference_decimal() {
        let doc = parse("<root>&#65;</root>");
        let root = doc.root_element().unwrap();
        assert_eq!(doc.text_content(root), "A");
    }

    #[test]
    fn test_parse_char_reference_hex() {
        let doc = parse("<root>&#x41;</root>");
        let root = doc.root_element().unwrap();
        assert_eq!(doc.text_content(root), "A");
    }

    #[test]
    fn test_parse_mixed_content() {
        let doc = parse("<p>Hello <b>world</b>!</p>");
        let p = doc.root_element().unwrap();
        let children: Vec<_> = doc.children(p).collect();
        assert_eq!(children.len(), 3); // "Hello ", <b>, "!"

        assert_eq!(doc.node_text(children[0]), Some("Hello "));
        assert_eq!(doc.node_name(children[1]), Some("b"));
        assert_eq!(doc.text_content(children[1]), "world");
        assert_eq!(doc.node_text(children[2]), Some("!"));
    }

    #[test]
    fn test_parse_prefixed_element() {
        let doc = parse("<svg:rect xmlns:svg=\"http://www.w3.org/2000/svg\"/>");
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_name(root), Some("rect"));
        match &doc.node(root).kind {
            NodeKind::Element { prefix, .. } => {
                assert_eq!(prefix.as_deref(), Some("svg"));
            }
            _ => panic!("expected element"),
        }
    }

    #[test]
    fn test_parse_prefixed_attribute() {
        let doc = parse("<root xml:lang=\"en\"/>");
        let root = doc.root_element().unwrap();
        let attrs = doc.attributes(root);
        assert_eq!(attrs.len(), 1);
        assert_eq!(attrs[0].name, "lang");
        assert_eq!(attrs[0].prefix.as_deref(), Some("xml"));
        assert_eq!(attrs[0].value, "en");
    }

    #[test]
    fn test_parse_error_mismatched_tags() {
        let result = Document::parse_str("<a></b>");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_error_unexpected_eof() {
        let result = Document::parse_str("<a>");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_error_no_root() {
        let result = Document::parse_str("");
        // XML 1.0 §2.1 requires a root element
        assert!(result.is_err());
    }

    /// The XML declaration prefix that `serialize()` always emits.
    const DECL: &str = "<?xml version=\"1.0\"?>\n";

    #[test]
    fn test_roundtrip_simple() {
        let input = "<root><child>text</child></root>";
        let doc = parse(input);
        let output = crate::serial::serialize(&doc);
        assert_eq!(output, format!("{DECL}{input}\n"));
    }

    #[test]
    fn test_roundtrip_attributes() {
        let input = "<root attr=\"value\"><child id=\"1\"/></root>";
        let doc = parse(input);
        let output = crate::serial::serialize(&doc);
        assert_eq!(output, format!("{DECL}{input}\n"));
    }

    #[test]
    fn test_roundtrip_entities() {
        let input = "<root>&amp; &lt; &gt;</root>";
        let doc = parse(input);
        let output = crate::serial::serialize(&doc);
        // After parsing, entities are resolved to characters.
        // Serialization re-escapes them.
        assert_eq!(output, format!("{DECL}<root>&amp; &lt; &gt;</root>\n"));
    }

    #[test]
    fn test_roundtrip_comment() {
        let input = "<root><!-- comment --></root>";
        let doc = parse(input);
        let output = crate::serial::serialize(&doc);
        assert_eq!(output, format!("{DECL}{input}\n"));
    }

    #[test]
    fn test_roundtrip_cdata() {
        let input = "<root><![CDATA[data & stuff]]></root>";
        let doc = parse(input);
        let output = crate::serial::serialize(&doc);
        assert_eq!(output, format!("{DECL}{input}\n"));
    }

    #[test]
    fn test_roundtrip_pi() {
        let input = "<?target data?><root/>";
        let doc = parse(input);
        let output = crate::serial::serialize(&doc);
        assert_eq!(output, format!("{DECL}{input}\n"));
    }

    #[test]
    fn test_roundtrip_xml_declaration() {
        let input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root/>";
        let doc = parse(input);
        let output = crate::serial::serialize(&doc);
        assert_eq!(
            output,
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root/>\n"
        );
    }

    #[test]
    fn test_whitespace_in_attribute_value() {
        let doc = parse("<root attr=\"a\tb\nc\"/>");
        let root = doc.root_element().unwrap();
        // Tabs and newlines in attribute values are normalized to spaces
        assert_eq!(doc.attribute(root, "attr"), Some("a b c"));
    }

    #[test]
    fn test_name_chars() {
        use super::super::input::{is_name_char, is_name_start_char};

        assert!(is_name_start_char('A'));
        assert!(is_name_start_char('z'));
        assert!(is_name_start_char('_'));
        assert!(is_name_start_char(':'));
        assert!(!is_name_start_char('0'));
        assert!(!is_name_start_char('-'));

        assert!(is_name_char('A'));
        assert!(is_name_char('0'));
        assert!(is_name_char('-'));
        assert!(is_name_char('.'));
        assert!(!is_name_char(' '));
    }

    #[test]
    fn test_parse_doctype_simple() {
        let doc = parse("<!DOCTYPE html><html/>");
        let root = doc.root();
        let children: Vec<_> = doc.children(root).collect();
        assert_eq!(children.len(), 2);

        match &doc.node(children[0]).kind {
            NodeKind::DocumentType {
                name,
                system_id,
                public_id,
                ..
            } => {
                assert_eq!(name, "html");
                assert_eq!(*system_id, None);
                assert_eq!(*public_id, None);
            }
            other => panic!("expected DocumentType, got {other:?}"),
        }

        assert_eq!(doc.node_name(children[1]), Some("html"));
    }

    #[test]
    fn test_parse_doctype_system() {
        let doc = parse("<!DOCTYPE root SYSTEM \"root.dtd\"><root/>");
        let root = doc.root();
        let children: Vec<_> = doc.children(root).collect();
        assert_eq!(children.len(), 2);

        match &doc.node(children[0]).kind {
            NodeKind::DocumentType {
                name,
                system_id,
                public_id,
                ..
            } => {
                assert_eq!(name, "root");
                assert_eq!(system_id.as_deref(), Some("root.dtd"));
                assert_eq!(*public_id, None);
            }
            other => panic!("expected DocumentType, got {other:?}"),
        }
    }

    #[test]
    fn test_parse_doctype_public() {
        let doc = parse(
            "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0//EN\" \
             \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html/>",
        );
        let root = doc.root();
        let children: Vec<_> = doc.children(root).collect();
        assert_eq!(children.len(), 2);

        match &doc.node(children[0]).kind {
            NodeKind::DocumentType {
                name,
                system_id,
                public_id,
                ..
            } => {
                assert_eq!(name, "html");
                assert_eq!(public_id.as_deref(), Some("-//W3C//DTD XHTML 1.0//EN"));
                assert_eq!(
                    system_id.as_deref(),
                    Some("http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd")
                );
            }
            other => panic!("expected DocumentType, got {other:?}"),
        }
    }

    #[test]
    fn test_parse_doctype_internal_subset() {
        let doc = parse("<!DOCTYPE root [<!ELEMENT root (#PCDATA)>]><root/>");
        let root = doc.root();
        let children: Vec<_> = doc.children(root).collect();
        assert_eq!(children.len(), 2);

        match &doc.node(children[0]).kind {
            NodeKind::DocumentType {
                name,
                system_id,
                public_id,
                ..
            } => {
                assert_eq!(name, "root");
                assert_eq!(*system_id, None);
                assert_eq!(*public_id, None);
            }
            other => panic!("expected DocumentType, got {other:?}"),
        }

        assert_eq!(doc.node_name(children[1]), Some("root"));
    }

    #[test]
    fn test_parse_doctype_multiline_internal_subset() {
        let input =
            "<!DOCTYPE root [\n<!ELEMENT y (#PCDATA|x|x)*>\n<!ELEMENT root ANY>\n]>\n\n<root/>";
        let doc = parse(input);
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_name(root), Some("root"));
    }

    #[test]
    fn test_parse_doctype_with_entity() {
        let input = "<!DOCTYPE doc [\n<!ELEMENT doc (#PCDATA)>\n<!ENTITY rsqb \"]\">\n]>\n<doc>&rsqb;</doc>";
        let doc = parse(input);
        let root = doc.root_element().unwrap();
        assert_eq!(doc.text_content(root), "]");
    }

    #[test]
    fn test_parse_doctype_content_model() {
        let input = "<!DOCTYPE violation [\n<!ELEMENT violation (a,a,a,b)>\n<!ELEMENT a EMPTY>\n<!ELEMENT b EMPTY>\n]>\n<violation>\n    <a/>\n    <a/>\n    <b/>\n</violation>";
        let doc = parse(input);
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_name(root), Some("violation"));
    }

    #[test]
    fn test_parse_doctype_with_crlf() {
        // Test with CRLF line endings (like the OASIS conformance tests)
        let input =
            "<!DOCTYPE doc\r\n[\r\n<!ELEMENT doc ANY>\r\n<!ELEMENT a (doc?)>\r\n]>\r\n<doc/>";
        let doc = parse(input);
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_name(root), Some("doc"));
    }

    #[test]
    fn test_parse_doctype_attlist() {
        let input = "<!DOCTYPE root [\n<!ELEMENT root EMPTY>\n<!ATTLIST root\n    token\tNMTOKEN\t\t#REQUIRED\n    >\n\n    <!-- comment -->\n]>\n<root token=\"dev@null\"/>";
        let doc = parse(input);
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_name(root), Some("root"));
    }

    #[test]
    fn test_parse_doctype_comment_with_apostrophe() {
        // Apostrophes in DTD comments must not confuse the bracket scanner
        let input = "<!DOCTYPE root [\n<!ELEMENT root ANY>\n<!-- can't break -->\n]>\n<root/>";
        let doc = parse(input);
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_name(root), Some("root"));
    }

    #[test]
    fn test_roundtrip_doctype() {
        // Simple DOCTYPE (no whitespace between DOCTYPE and root in input)
        let input = "<!DOCTYPE html><html/>";
        let doc = parse(input);
        let output = crate::serial::serialize(&doc);
        assert_eq!(output, format!("{DECL}<!DOCTYPE html><html/>\n"));

        // DOCTYPE with SYSTEM
        let input = "<!DOCTYPE root SYSTEM \"root.dtd\"><root/>";
        let doc = parse(input);
        let output = crate::serial::serialize(&doc);
        assert_eq!(
            output,
            format!("{DECL}<!DOCTYPE root SYSTEM \"root.dtd\"><root/>\n")
        );

        // DOCTYPE with PUBLIC
        let input = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0//EN\" \
                      \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html/>";
        let doc = parse(input);
        let output = crate::serial::serialize(&doc);
        assert_eq!(
            output,
            format!(
                "{DECL}<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0//EN\" \
                 \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html/>\n"
            )
        );
    }

    // --- Namespace resolution tests ---

    #[test]
    fn test_parse_default_namespace() {
        let doc = parse("<root xmlns=\"http://example.com\"/>");
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_namespace(root), Some("http://example.com"));
    }

    #[test]
    fn test_parse_prefixed_namespace() {
        let doc = parse("<ns:root xmlns:ns=\"http://example.com\"/>");
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_name(root), Some("root"));
        assert_eq!(doc.node_namespace(root), Some("http://example.com"));
        match &doc.node(root).kind {
            NodeKind::Element { prefix, .. } => {
                assert_eq!(prefix.as_deref(), Some("ns"));
            }
            _ => panic!("expected element"),
        }
    }

    #[test]
    fn test_parse_nested_namespace() {
        // Child elements inherit the default namespace from the parent.
        let doc = parse("<root xmlns=\"http://example.com\"><child/></root>");
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_namespace(root), Some("http://example.com"));

        let child = doc.first_child(root).unwrap();
        assert_eq!(doc.node_name(child), Some("child"));
        assert_eq!(doc.node_namespace(child), Some("http://example.com"));
    }

    #[test]
    fn test_parse_namespace_override() {
        // A child element can override the parent's default namespace.
        let doc = parse(
            "<root xmlns=\"http://example.com\">\
             <child xmlns=\"http://other.com\"/>\
             </root>",
        );
        let root = doc.root_element().unwrap();
        assert_eq!(doc.node_namespace(root), Some("http://example.com"));

        let child = doc.first_child(root).unwrap();
        assert_eq!(doc.node_namespace(child), Some("http://other.com"));
    }

    #[test]
    fn test_parse_xml_namespace() {
        // The xml: prefix is always bound to the XML namespace URI.
        let doc = parse("<root xml:lang=\"en\"/>");
        let root = doc.root_element().unwrap();
        let attrs = doc.attributes(root);
        assert_eq!(attrs.len(), 1);
        assert_eq!(attrs[0].name, "lang");
        assert_eq!(attrs[0].prefix.as_deref(), Some("xml"));
        assert_eq!(
            attrs[0].namespace.as_deref(),
            Some("http://www.w3.org/XML/1998/namespace")
        );
    }

    #[test]
    fn test_parse_attribute_namespace() {
        // Prefixed attributes get their namespace resolved.
        let doc = parse("<root xmlns:app=\"http://example.com/app\" app:version=\"2.0\"/>");
        let root = doc.root_element().unwrap();
        let attrs = doc.attributes(root);

        // Find the app:version attribute
        let version_attr = attrs.iter().find(|a| a.name == "version").unwrap();
        assert_eq!(version_attr.prefix.as_deref(), Some("app"));
        assert_eq!(
            version_attr.namespace.as_deref(),
            Some("http://example.com/app")
        );

        // The xmlns:app attribute should not have a resolved namespace itself.
        let xmlns_attr = attrs.iter().find(|a| a.name == "app").unwrap();
        assert_eq!(xmlns_attr.prefix.as_deref(), Some("xmlns"));
        assert_eq!(xmlns_attr.namespace, None);
    }
}