exml 0.7.2

Pure Rust XML library based on libxml2
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
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
use std::{cell::RefCell, collections::HashMap, mem::replace, rc::Rc, sync::Arc};

use crate::{
    chvalid::XmlCharValid,
    dom::{
        node::NodeStrongRef,
        user_data::{DOMUserData, OperationType, UserDataHandler},
    },
    error::XmlParserErrors,
    parser::split_qname2,
    tree::{validate_name, validate_qname},
};

use super::{
    DOMException, NodeType, XML_NS_NAMESPACE, XML_XML_NAMESPACE,
    attlistdecl::DefaultDecl,
    attr::AttrRef,
    character_data::{CDATASectionRef, CommentRef, TextRef},
    check_owner_document_sameness, check_vertical_hierarchy,
    document_fragment::DocumentFragmentRef,
    document_type::DocumentTypeRef,
    dom_implementation::{DEFAULT_DOM_IMPLEMENTATION, DOMImplementation},
    element::ElementRef,
    entity::{EntityRef, EntityType, xml_entities_err},
    entity_reference::EntityReferenceRef,
    named_node_map::NamedNodeMap,
    node::{Node, NodeConnection, NodeRef},
    node_list::FilteredSubtreeElementsList,
    pi::ProcessingInstructionRef,
};

/// Implementation of [Document](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-i-Document)
/// interface on [1.4 Fundamental Interfaces: Core Module](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-BBACDC08)
pub struct Document {
    // /// [1.1.1 The DOM Structure Model](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1590626202)
    // /// - no parent
    // parent_node: Option<NodeWeakRef>,
    /// [1.1.1 The DOM Structure Model](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1590626202)  
    /// - `Element` (maximum of one)
    /// - `ProcessingInstruction`
    /// - `Comment`
    /// - `DocumentType` (maximum of one)
    first_child: Option<NodeStrongRef>,
    last_child: Option<NodeStrongRef>,
    // /// [1.1.1 The DOM Structure Model](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1590626202)
    // /// - no sibling
    // previous_sibling: Option<NodeWeakRef>,
    // next_sibling: Option<NodeRef>,
    /// Implementation of `doctype` attribute.  
    doctype: Option<DocumentTypeRef>,
    /// Implementation of `implementation` attribute.
    implementation: Arc<dyn DOMImplementation>,
    /// Implementation of `documentElement` attribute.
    document_element: Option<ElementRef>,
    /// Implementation of `documentURI` attribute.
    document_uri: Option<Rc<str>>,
    /// Implementation of `inputEncoding` attribute.
    input_encoding: Option<Rc<str>>,
    /// Implementation of `xmlEncoding` attribute.
    xml_encoding: Option<Rc<str>>,
    /// Implementation of `xmlStandalone` attribute.  
    /// In libxml2, this field us used as follows.
    ///
    /// | value | meaning                                                                |
    /// | ----- |:---------------------------------------------------------------------- |
    /// | `1`   | standalone="yes"                                                       |
    /// | `0`   | standalone="no"                                                        |
    /// | `-1`  | there is no XML declaration                                            |
    /// | `-2`  | there is an XML declaration, but no standalone attribute was specified |
    xml_standalone: i8,
    /// Implementation of `xmlVersion` attribute.
    xml_version: Option<Rc<str>>,

    user_data: Option<HashMap<String, (DOMUserData, Option<Arc<dyn UserDataHandler>>)>>,

    /// HTML document or not
    html: bool,
    /// 0      : enable modification check if set
    /// 1 - 31 : unused
    flag: u32,

    // Predefined entities.
    //  0: lt
    //  1: gt
    //  2: amp
    //  3: apos
    //  4: quot
    predefined_entities: [EntityRef; 5],
}

impl Document {
    /// Enable check for read-only node.\
    /// As a result, editing of nodes specified as read-only in the DOM specification
    /// becomes impossible.
    pub fn enable_read_only_check(&mut self) {
        self.flag |= 0b01;
    }

    /// Disable check for read-only node.\
    /// It allows editing of nodes that are not editable in the DOM specification
    /// (e.g., DTD nodes).
    pub fn disable_read_only_check(&mut self) {
        self.flag &= !0b01;
    }

    /// Check if read-only check is enabled.
    pub fn is_enabled_read_only_check(&self) -> bool {
        self.flag & 0b01 != 0
    }

    /// Do an entity lookup in the document entity hash table and
    /// returns the corresponding entity, otherwise a lookup is done
    /// in the predefined entities too.
    ///
    /// If found, return found entity wrapped `Some`.  
    /// Otherwise, return `None`.
    #[doc(alias = "xmlGetDocEntity")]
    pub fn get_entity(&self, name: &str) -> Option<EntityRef> {
        if let Some(ent) = self
            .doctype
            .as_ref()
            .and_then(|doctype| doctype.get_entity(name))
        {
            return Some(ent);
        }

        match name {
            "lt" => Some(self.predefined_entities[0].clone()),
            "gt" => Some(self.predefined_entities[1].clone()),
            "amp" => Some(self.predefined_entities[2].clone()),
            "apos" => Some(self.predefined_entities[3].clone()),
            "quot" => Some(self.predefined_entities[4].clone()),
            _ => None,
        }
    }

    /// Do a global encoding of a string, replacing the predefined entities
    /// and non ASCII values with their entities and CharRef counterparts.
    /// Contrary to xmlEncodeEntities, this routine is reentrant, and result
    /// must be deallocated.
    ///
    /// Returns a newly allocated string with the substitution done.
    #[doc(alias = "xmlEncodeEntitiesInternal")]
    fn encode_entities_internal<const ATTR: bool>(&mut self, input: &str) -> String {
        // allocate an translation buffer.
        let mut cur = input;
        let mut out = String::new();
        while !cur.is_empty() {
            // By default one have to encode at least '<', '>', '"' and '&' !
            if cur.starts_with('<') {
                // Special handling of server side include in HTML attributes
                if self.html && ATTR && cur.starts_with("<!--") {
                    if let Some(pos) = cur.find("-->") {
                        out.push_str(&cur[..pos + 3]);
                        cur = &cur[pos + 3..];
                        continue;
                    }
                }
                out.push_str("&lt;");
            } else if cur.starts_with('>') {
                out.push_str("&gt;");
            } else if cur.starts_with('&') {
                // Special handling of &{...} construct from HTML 4, see
                // http://www.w3.org/TR/html401/appendix/notes.html#h-B.7.1
                if self.html && ATTR && cur[1..].starts_with('{') {
                    if let Some(pos) = cur.find('}') {
                        out.push_str(&cur[..pos + 1]);
                        cur = &cur[pos + 1..];
                        continue;
                    }
                }
                out.push_str("&amp;");
            } else if matches!(cur.as_bytes()[0], 0x20..0x80 | b'\n' | b'\t')
                || (self.html && cur.starts_with('\r'))
            {
                // default case, just copy !
                out.push(cur.as_bytes()[0] as char);
            } else if matches!(cur.as_bytes()[0], 0x80..) {
                if self.input_encoding.is_some() || self.html {
                    let c = cur.chars().next().unwrap();
                    out.push(c);
                    cur = &cur[c.len_utf8()..];
                    continue;
                } else {
                    let val = cur.chars().next().unwrap();
                    if !val.is_xml_char() {
                        xml_entities_err(
                            XmlParserErrors::XmlErrInvalidChar,
                            "xmlEncodeEntities: char out of range\n",
                        );
                        self.input_encoding = Some("ISO-8859-1".into());
                        out.push_str(format!("&#{}", cur.as_bytes()[0]).as_str());
                        cur = &cur[1..];
                        continue;
                    }
                    // We could do multiple things here. Just save as a c_char ref
                    out.push_str(format!("&#x{:X}", val as u32).as_str());
                    cur = &cur[val.len_utf8()..];
                    continue;
                }
            } else if cur.as_bytes()[0].is_xml_char() {
                out.push_str(format!("&#{};", cur.as_bytes()[0]).as_str());
            }
            cur = &cur[1..];
        }
        out
    }
}

/// Wrapper of `Rc<RefCell<Document>>`.
#[derive(Clone)]
pub struct DocumentRef(pub(super) Rc<RefCell<Document>>);

impl DocumentRef {
    /// Implementation of [`createDocument`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Level-2-Core-DOM-createDocument) method.
    ///
    /// In the specification, this is implemented in [`DOMImplementation`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-102161490).
    ///
    /// ```text
    /// Creates a DOM Document object of the specified type with its document element.
    /// Note that based on the DocumentType given to create the document, the implementation
    /// may instantiate specialized Document objects that support additional features than
    /// the "Core", such as "HTML" [DOM Level 2 HTML]. On the other hand, setting the
    /// DocumentType after the document was created makes this very unlikely to happen.
    /// Alternatively, specialized Document creation methods, such as createHTMLDocument
    /// [DOM Level 2 HTML], can be used to obtain specific types of Document objects.
    ///
    /// Parameters
    ///     namespaceURI of type DOMString
    ///         The namespace URI of the document element to create or null.
    ///     qualifiedName of type DOMString
    ///         The qualified name of the document element to be created or null.
    ///     doctype of type DocumentType
    ///         The type of document to be created or null.
    ///         When doctype is not null, its Node.ownerDocument attribute is set to the
    ///         document being created.
    ///
    /// Return Value
    ///     Document A new Document object with its document element. If the NamespaceURI,
    ///              qualifiedName, and doctype are null, the returned Document is empty with
    ///              no document element.
    ///
    /// Exceptions
    ///     DOMException
    ///     INVALID_CHARACTER_ERR: Raised if the specified qualified name is not an XML name
    ///                            according to [XML 1.0].
    ///     NAMESPACE_ERR:         Raised if the qualifiedName is malformed, if the
    ///                            qualifiedName has a prefix and the namespaceURI is null,
    ///                            or if the qualifiedName is null and the namespaceURI is
    ///                            different from null, or if the qualifiedName has a prefix
    ///                            that is "xml" and the namespaceURI is different from
    ///                            "http://www.w3.org/XML/1998/namespace" [XML Namespaces],
    ///                            or if the DOM implementation does not support the "XML"
    ///                            feature but a non-null namespace URI was provided, since
    ///                            namespaces were defined by XML.
    ///     WRONG_DOCUMENT_ERR:    Raised if doctype has already been used with a different
    ///                            document or was created from a different implementation.
    ///     NOT_SUPPORTED_ERR:     May be raised if the implementation does not support the
    ///                            feature "XML" and the language exposed through the Document
    ///                            does not support XML Namespaces (such as [HTML 4.01]).
    /// ```
    pub fn new(
        namespace_uri: Option<&str>,
        qualified_name: Option<&str>,
        doctype: Option<DocumentTypeRef>,
    ) -> Result<Self, DOMException> {
        if doctype
            .as_ref()
            .is_some_and(|doctype| doctype.owner_document().is_some())
        {
            return Err(DOMException::WrongDocumentErr);
        }

        let mut new = DocumentRef(Rc::new(RefCell::new(Document {
            first_child: None,
            last_child: None,
            doctype: None,
            implementation: (*DEFAULT_DOM_IMPLEMENTATION).clone(),
            document_element: None,
            document_uri: None,
            input_encoding: None,
            xml_encoding: None,
            xml_standalone: 0,
            xml_version: None,
            user_data: None,
            html: false,
            flag: 0,
            predefined_entities: [
                EntityRef::new(None, "lt".into(), EntityType::InternalPredefinedEntity),
                EntityRef::new(None, "gt".into(), EntityType::InternalPredefinedEntity),
                EntityRef::new(None, "amp".into(), EntityType::InternalPredefinedEntity),
                EntityRef::new(None, "apos".into(), EntityType::InternalPredefinedEntity),
                EntityRef::new(None, "quot".into(), EntityType::InternalPredefinedEntity),
            ],
        })));

        // TODO: check if the DTD specifies this document is HTML or not.
        if let Some(doctype) = doctype {
            new.append_child(doctype.into())?;
        }

        if let Some(qname) = qualified_name {
            if validate_name::<false>(qname).is_err() {
                return Err(DOMException::InvalidCharacterErr);
            }
            if validate_qname::<false>(qname).is_err() {
                return Err(DOMException::NamespaceErr);
            }

            let elem = if let Some((prefix, _)) = split_qname2(qname) {
                let Some(ns_uri) = namespace_uri else {
                    // ... if the qualifiedName has a prefix and the namespaceURI is null,
                    return Err(DOMException::NamespaceErr);
                };

                // ... or if the qualifiedName has a prefix that is "xml" and the namespaceURI
                // is different from "http://www.w3.org/XML/1998/namespace" [XML Namespaces],
                if prefix == "xml" && ns_uri != "http://www.w3.org/XML/1998/namespace" {
                    return Err(DOMException::NamespaceErr);
                }
                ElementRef::with_namespace(new.clone(), qname.into(), Some(ns_uri.into()))
            } else {
                ElementRef::new(new.clone(), qname.into())
            };

            new.append_child(elem.into())?;
        } else {
            // ... or if the qualifiedName is null and the namespaceURI is different from null
            if namespace_uri.is_some() {
                return Err(DOMException::NamespaceErr);
            }
        }

        // Setup predefined entities
        let predefined_entities = new.0.borrow().predefined_entities.clone();
        for (mut ent, text) in predefined_entities.iter().cloned().zip([
            new.create_text_node("<"),
            new.create_text_node(">"),
            new.create_text_node("&"),
            new.create_text_node("'"),
            new.create_text_node("\""),
        ]) {
            ent.set_owner_document(new.clone());
            ent.append_child(text.into())?;
        }

        Ok(new)
    }

    /// Implementation of [`createElement`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-2141741547) method.
    ///
    /// # Specification
    /// ```text
    /// Creates an element of the type specified. Note that the instance returned implements
    /// the Element interface, so attributes can be specified directly on the returned object.
    /// In addition, if there are known attributes with default values, Attr nodes
    /// representing them are automatically created and attached to the element.
    /// To create an element with a qualified name and namespace URI, use the createElementNS method.
    ///
    /// Parameters
    ///     tagName of type DOMString
    ///         The name of the element type to instantiate. For XML, this is case-sensitive,
    ///         otherwise it depends on the case-sensitivity of the markup language in use.
    ///         In that case, the name is mapped to the canonical form of that markup
    ///         by the DOM implementation.
    ///
    /// Return Value
    ///     Element A new Element object with the nodeName attribute set to tagName,
    ///             and localName, prefix, and namespaceURI set to null.
    ///
    /// Exceptions
    ///     DOMException
    ///     INVALID_CHARACTER_ERR: Raised if the specified name is not an XML name
    ///                            according to the XML version in use specified in
    ///                            the Document.xmlVersion attribute.
    /// ```
    pub fn create_element(&self, tag_name: impl Into<Rc<str>>) -> Result<ElementRef, DOMException> {
        let tag_name: Rc<str> = tag_name.into();
        if validate_name::<false>(&tag_name).is_err() {
            return Err(DOMException::InvalidCharacterErr);
        }

        Ok(ElementRef::new(self.clone(), tag_name))
    }

    /// Implementation of [`createDocumentFragment`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-35CB04B5) method.
    ///
    /// # Specification
    /// ```text
    /// Creates an empty DocumentFragment object.
    ///
    /// Return Value
    ///     DocumentFragment A new DocumentFragment.
    ///
    /// No Parameters
    /// No Exceptions
    /// ```
    pub fn create_document_fragment(&self) -> DocumentFragmentRef {
        DocumentFragmentRef::from_doc(self.clone())
    }

    /// Implementation of [`createTextNode`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1975348127) method.
    ///
    /// # Specification
    /// ```text
    /// Creates a Text node given the specified string.
    ///
    /// Parameters
    ///     data of type DOMString
    ///         The data for the node.
    ///
    /// Return Value
    ///     Text The new Text object.
    ///
    /// No Exceptions
    /// ```
    pub fn create_text_node(&self, data: impl Into<String>) -> TextRef {
        TextRef::from_doc(self.clone(), data.into())
    }

    /// Implementation of [`createComment`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1334481328) method.
    ///
    /// # Specification
    /// ```text
    /// Creates a Comment node given the specified string.
    ///
    /// Parameters
    ///     data of type DOMString
    ///         The data for the node.
    ///
    /// Return Value
    ///     Comment The new Comment object.
    ///
    /// No Exceptions
    /// ```
    pub fn create_comment(&self, data: impl Into<String>) -> CommentRef {
        CommentRef::from_doc(self.clone(), data.into())
    }

    /// Implementation of [`createCDATASection`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-D26C0AF8) method.
    ///
    /// According to the specification, [`Err`] is returned if the document is an HTML document.
    ///
    /// # Specification
    /// ```text
    /// Creates a CDATASection node whose value is the specified string.
    ///
    /// Parameters
    ///     data of type DOMString
    ///         The data for the CDATASection contents.
    ///
    /// Return Value
    ///     CDATASection The new CDATASection object.
    ///
    /// Exceptions
    ///     DOMException
    ///     NOT_SUPPORTED_ERR: Raised if this document is an HTML document.
    /// ```
    pub fn create_cdata_section(
        &self,
        data: impl Into<String>,
    ) -> Result<CDATASectionRef, DOMException> {
        if self.is_html() {
            Err(DOMException::NotSupportedErr)
        } else {
            Ok(CDATASectionRef::from_doc(self.clone(), data.into()))
        }
    }

    /// Implementation of [`createProcessingInstruction`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-135944439) method.
    ///
    /// # Specification
    /// ```text
    /// Creates a ProcessingInstruction node given the specified name and data strings.
    ///
    /// Parameters
    ///     target of type DOMString
    ///         The target part of the processing instruction.
    ///         Unlike Document.createElementNS or Document.createAttributeNS,
    ///         no namespace well-formed checking is done on the target name.
    ///         Applications should invoke Document.normalizeDocument()
    ///         with the parameter "namespaces" set to true in order to ensure
    ///         that the target name is namespace well-formed.
    ///     data of type DOMString
    ///         The data for the node.
    ///
    /// Return Value
    ///     ProcessingInstruction The new ProcessingInstruction object.
    ///
    /// Exceptions
    ///     DOMException
    ///     INVALID_CHARACTER_ERR: Raised if the specified target is not
    ///                            an XML name according to the XML version in use
    ///                            specified in the Document.xmlVersion attribute.
    ///     NOT_SUPPORTED_ERR:     Raised if this document is an HTML document.
    /// ```
    pub fn create_processing_instruction(
        &self,
        target: impl Into<Rc<str>>,
        data: Option<impl Into<Rc<str>>>,
    ) -> Result<ProcessingInstructionRef, DOMException> {
        if self.is_html() {
            return Err(DOMException::NotSupportedErr);
        }

        let target: Rc<str> = target.into();

        // [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
        if target.eq_ignore_ascii_case("xml") || validate_name::<false>(&target).is_err() {
            return Err(DOMException::InvalidCharacterErr);
        }

        Ok(ProcessingInstructionRef::from_doc(
            self.clone(),
            target,
            data.map(|data| data.into()),
        ))
    }

    /// Implementation of [`createAttribute`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1084891198) method.
    ///
    /// # Specification
    /// ```text
    /// Creates an Attr of the given name. Note that the Attr instance can then be set
    /// on an Element using the setAttributeNode method.
    /// To create an attribute with a qualified name and namespace URI, use the
    /// createAttributeNS method.
    ///
    /// Parameters
    ///     name of type DOMString
    ///         The name of the attribute.
    ///
    /// Return Value
    ///     Attr A new Attr object with the nodeName attribute set to name, and localName,
    ///          prefix, and namespaceURI set to null. The value of the attribute
    ///          is the empty string.
    ///
    /// Exceptions
    ///     DOMException
    ///     INVALID_CHARACTER_ERR: Raised if the specified name is not an XML name
    ///                            according to the XML version in use specified in
    ///                            the Document.xmlVersion attribute.
    /// ```
    pub fn create_attribute(&self, name: impl Into<Rc<str>>) -> Result<AttrRef, DOMException> {
        let name: Rc<str> = name.into();
        if validate_name::<false>(&name).is_err() {
            return Err(DOMException::InvalidCharacterErr);
        }

        Ok(AttrRef::new(self.clone(), name))
    }

    /// Implementation of [`createEntityReference`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-392B75AE) method.
    ///
    /// # Specification
    /// ```text
    /// Creates an EntityReference object. In addition, if the referenced entity is known,
    /// the child list of the EntityReference node is made the same as that of
    /// the corresponding Entity node.
    /// Note: If any descendant of the Entity node has an unbound namespace prefix,
    /// the corresponding descendant of the created EntityReference node is also unbound;
    /// (its namespaceURI is null). The DOM Level 2 and 3 do not support any mechanism
    /// to resolve namespace prefixes in this case.
    ///
    /// Parameters
    ///     name of type DOMString
    ///         The name of the entity to reference.
    ///         Unlike Document.createElementNS or Document.createAttributeNS,
    ///         no namespace well-formed checking is done on the entity name.
    ///         Applications should invoke Document.normalizeDocument() with the parameter
    ///         "namespaces" set to true in order to ensure that the entity name is namespace
    ///         well-formed.
    ///
    /// Return Value
    ///     EntityReference The new EntityReference object.
    ///
    /// Exceptions
    ///     DOMException
    ///     INVALID_CHARACTER_ERR: Raised if the specified name is not an XML name according
    ///                            to the XML version in use specified in the
    ///                            Document.xmlVersion attribute.
    ///     NOT_SUPPORTED_ERR:     Raised if this document is an HTML document.
    /// ```
    pub fn create_entity_reference(
        &self,
        name: impl Into<Rc<str>>,
    ) -> Result<EntityReferenceRef, DOMException> {
        if self.is_html() {
            return Err(DOMException::NotSupportedErr);
        }
        let name: Rc<str> = name.into();
        if validate_name::<false>(&name).is_err() {
            return Err(DOMException::InvalidCharacterErr);
        }
        let mut entref = EntityReferenceRef::from_doc(self.clone(), name);

        if let Some(entity) = self.get_entity(&entref.node_name()) {
            let read_only_check = self.is_enabled_read_only_check();
            self.0.borrow_mut().disable_read_only_check();
            let mut children = entity.first_child();
            while let Some(child) = children {
                children = child.next_sibling();
                entref.append_child(child.clone_node(true))?;
            }
            if read_only_check {
                self.0.borrow_mut().enable_read_only_check();
            }
        }
        Ok(entref)
    }

    /// Implementation of [`getElementsByTagName`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-A6C9094) method.
    ///
    /// # Specification
    /// ```text
    /// Returns a NodeList of all the Elements in document order with a given tag name
    /// and are contained in the document.
    ///
    /// Parameters
    ///     tagname of type DOMString
    ///         The name of the tag to match on. The special value "*" matches all tags.
    ///         For XML, the tagname parameter is case-sensitive, otherwise it depends
    ///         on the case-sensitivity of the markup language in use.
    ///
    /// Return Value
    ///     NodeList A new NodeList object containing all the matched Elements.
    ///
    /// No Exceptions
    /// ```
    pub fn get_elements_by_tag_name(&self, tag_name: &str) -> FilteredSubtreeElementsList {
        // If this document is HTML document, the tagname is case-insensitive.
        if self.is_html() {
            FilteredSubtreeElementsList::new(
                self.clone().into(),
                None,
                tag_name.to_owned(),
                |elem, _, name| name == "*" || elem.node_name().eq_ignore_ascii_case(name),
            )
        } else {
            FilteredSubtreeElementsList::new(
                self.clone().into(),
                None,
                tag_name.to_owned(),
                |elem, _, name| name == "*" || elem.node_name().as_ref() == name,
            )
        }
    }

    /// Implementation of [`importNode`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Core-Document-importNode) method.
    ///
    /// # Specification
    /// ```text
    /// Imports a node from another document to this document, without altering
    /// or removing the source node from the original document; this method creates
    /// a new copy of the source node. The returned node has no parent; (parentNode is null).
    /// For all nodes, importing a node creates a node object owned by the importing document,
    /// with attribute values identical to the source node's nodeName and nodeType,
    /// plus the attributes related to namespaces (prefix, localName, and namespaceURI).
    /// As in the cloneNode operation, the source node is not altered. User data associated
    /// to the imported node is not carried over. However, if any UserDataHandlers has been
    /// specified along with the associated data these handlers will be called with
    /// the appropriate parameters before this method returns.
    /// Additional information is copied as appropriate to the nodeType, attempting to mirror
    /// the behavior expected if a fragment of XML or HTML source was copied from one document
    /// to another, recognizing that the two documents may have different DTDs in the XML case.
    /// The following list describes the specifics for each type of node.
    ///
    /// <too long...please refer to the specification>
    ///
    /// Parameters
    ///     importedNode of type Node
    ///         The node to import.
    ///     deep of type boolean
    ///         If true, recursively import the subtree under the specified node; if false,
    ///         import only the node itself, as explained above. This has no effect on nodes
    ///         that cannot have any children, and on Attr, and EntityReference nodes.
    ///
    /// Return Value
    ///     Node The imported node that belongs to this Document.
    ///
    /// Exceptions
    ///     DOMException
    ///     NOT_SUPPORTED_ERR:     Raised if the type of node being imported is not supported.
    ///     INVALID_CHARACTER_ERR: Raised if one of the imported names is not an XML name
    ///                            according to the XML version in use specified in the
    ///                            Document.xmlVersion attribute. This may happen when
    ///                            importing  an XML 1.1 [XML 1.1] element into an XML 1.0
    ///                            document, for instance.
    /// ```
    pub fn import_node(
        &mut self,
        imported_node: NodeRef,
        deep: bool,
    ) -> Result<NodeRef, DOMException> {
        match &imported_node {
            NodeRef::Attribute(attr) => {
                let mut new_attr = if attr.local_name().is_some() {
                    self.create_attribute_ns(
                        attr.namespace_uri().as_deref(),
                        attr.node_name().as_ref(),
                    )
                } else {
                    self.create_attribute(attr.node_name().as_ref())
                }?;
                let mut children = attr.first_child();
                while let Some(child) = children {
                    children = child.next_sibling();
                    new_attr.append_child(self.import_node(child, true)?)?;
                }

                attr.handle_user_data(OperationType::NodeImported, Some(new_attr.clone().into()));
                Ok(new_attr.into())
            }
            NodeRef::DocumentFragment(frag) => {
                let mut new = self.create_document_fragment();
                if deep {
                    let mut children = frag.first_child();
                    while let Some(child) = children {
                        children = child.next_sibling();
                        new.append_child(self.import_node(child, true)?)?;
                    }
                }
                frag.handle_user_data(OperationType::NodeImported, Some(new.clone().into()));
                Ok(new.into())
            }
            NodeRef::Document(_) | NodeRef::DocumentType(_) => Err(DOMException::NotSupportedErr),
            NodeRef::Element(elem) => {
                let mut new = if elem.local_name().is_some() {
                    self.create_element_ns(
                        elem.namespace_uri().as_deref(),
                        elem.node_name().as_ref(),
                    )
                } else {
                    self.create_element(elem.node_name())
                }?;

                let attrs = elem.attributes();
                for i in 0..attrs.length() {
                    let attr = attrs.item(i).unwrap();
                    if attr.specified() {
                        let attr = self.import_node(attr.into(), deep)?.as_attribute().unwrap();
                        new.set_attribute_node_ns(attr.clone())
                            .or_else(|_| new.set_attribute_node(attr))?;
                    }
                }

                if deep {
                    let mut children = elem.first_child();
                    while let Some(child) = children {
                        children = child.next_sibling();
                        new.append_child(self.import_node(child, true)?)?;
                    }
                }
                elem.handle_user_data(OperationType::NodeImported, Some(new.clone().into()));
                Ok(new.into())
            }
            NodeRef::EntityReference(entref) => {
                let new = self.create_entity_reference(entref.node_name().as_ref())?;
                entref.handle_user_data(OperationType::NodeImported, Some(new.clone().into()));
                Ok(new.into())
            }
            node @ (NodeRef::Notation(_)
            | NodeRef::CDATASection(_)
            | NodeRef::Comment(_)
            | NodeRef::ProcessingInstruction(_)
            | NodeRef::Text(_)
            | NodeRef::Entity(_)) => {
                let mut new = node.clone_node(false);
                new.set_owner_document(self.clone());

                let read_only_check = self.is_enabled_read_only_check();
                self.disable_read_only_check();
                let mut children = node.first_child();
                while let Some(child) = children {
                    let new_child = self.import_node(child.clone(), deep)?;
                    new.append_child(new_child)?;
                    children = child.next_sibling();
                }
                if read_only_check {
                    self.enable_read_only_check();
                }
                node.handle_user_data(OperationType::NodeImported, Some(new.clone()));
                Ok(new)
            }
        }
    }

    /// Implementation of [`createElementNS`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-DocCrElNS) method.
    ///
    /// # Specification
    /// ```text
    /// Creates an element of the given qualified name and namespace URI.
    /// Per [XML Namespaces], applications must use the value null as the namespaceURI
    /// parameter for methods if they wish to have no namespace.
    ///
    /// Parameters
    ///     namespaceURI of type DOMString
    ///         The namespace URI of the element to create.
    ///     qualifiedName of type DOMString
    ///         The qualified name of the element type to instantiate.
    ///
    /// Return Value
    ///     Element A new Element object with the following attributes:
    ///
    /// Exceptions
    ///     DOMException
    ///     INVALID_CHARACTER_ERR: Raised if the specified qualifiedName is not an XML name
    ///                            according to the XML version in use specified in the
    ///                            Document.xmlVersion attribute.
    ///     NAMESPACE_ERR:         Raised if the qualifiedName is a malformed qualified name,
    ///                            if the qualifiedName has a prefix and the namespaceURI
    ///                            is null, or if the qualifiedName has a prefix that is "xml"
    ///                            and the namespaceURI is different from
    ///                            "http://www.w3.org/XML/1998/namespace" [XML Namespaces],
    ///                            or if the qualifiedName or its prefix is "xmlns" and the
    ///                            namespaceURI is different from
    ///                            "http://www.w3.org/2000/xmlns/", or if the namespaceURI
    ///                            is "http://www.w3.org/2000/xmlns/" and neither
    ///                            the qualifiedName nor its prefix is "xmlns".
    ///     NOT_SUPPORTED_ERR:     Always thrown if the current document does not support
    ///                            the "XML" feature, since namespaces were defined by XML.
    /// ```
    pub fn create_element_ns(
        &mut self,
        namespace_uri: Option<&str>,
        qualified_name: &str,
    ) -> Result<ElementRef, DOMException> {
        if validate_name::<false>(qualified_name).is_err() {
            return Err(DOMException::InvalidCharacterErr);
        }
        if validate_qname::<false>(qualified_name).is_err() {
            return Err(DOMException::NamespaceErr);
        }

        match split_qname2(qualified_name) {
            Some((prefix, _)) => {
                // ... if the qualifiedName has a prefix and the namespaceURI is null, ...
                let Some(ns_uri) = namespace_uri else {
                    return Err(DOMException::NamespaceErr);
                };

                // ... if the qualifiedName has a prefix that is "xml"
                // and the namespaceURI is different from "http://www.w3.org/XML/1998/namespace" ...
                if prefix == "xml" && ns_uri != XML_XML_NAMESPACE {
                    return Err(DOMException::NamespaceErr);
                }

                // ... if the qualifiedName or its prefix is "xmlns"
                // and the namespaceURI is different from "http://www.w3.org/2000/xmlns/" ...
                if (prefix == "xmlns" || qualified_name == "xmlns") && ns_uri != XML_NS_NAMESPACE {
                    return Err(DOMException::NamespaceErr);
                }

                // ... if the namespaceURI is "http://www.w3.org/2000/xmlns/"
                // and neither the qualifiedName nor its prefix is "xmlns".
                if ns_uri == XML_NS_NAMESPACE && prefix != "xmlns" && qualified_name != "xmlns" {
                    return Err(DOMException::NamespaceErr);
                }

                Ok(ElementRef::with_namespace(
                    self.clone(),
                    qualified_name.into(),
                    Some(ns_uri.into()),
                ))
            }
            None => Ok(ElementRef::with_namespace(
                self.clone(),
                qualified_name.into(),
                namespace_uri.map(|uri| uri.into()),
            )),
        }
    }

    /// Implementation of [`createAttributeNS`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-DocCrAttrNS) method.
    ///
    /// # Specification
    /// ```text
    /// Creates an attribute of the given qualified name and namespace URI.
    /// Per [XML Namespaces], applications must use the value null as the namespaceURI
    /// parameter for methods if they wish to have no namespace.
    ///
    /// Parameters
    ///     namespaceURI of type DOMString
    ///         The namespace URI of the attribute to create.
    ///     qualifiedName of type DOMString
    ///         The qualified name of the attribute to instantiate.
    ///
    /// Return Value
    ///     Attr A new Attr object with the following attributes:
    ///          (ommited)
    ///
    /// Exceptions
    ///     DOMException
    ///     INVALID_CHARACTER_ERR: Raised if the specified qualifiedName is not an XML name
    ///                            according to the XML version in use specified in the
    ///                            Document.xmlVersion attribute.
    ///     NAMESPACE_ERR:         Raised if the qualifiedName is a malformed qualified name,
    ///                            if the qualifiedName has a prefix and the namespaceURI is
    ///                            null, if the qualifiedName has a prefix that is "xml" and
    ///                            the namespaceURI is different from
    ///                            "http://www.w3.org/XML/1998/namespace", if the qualifiedName
    ///                            or its prefix is "xmlns" and the namespaceURI is different
    ///                            from "http://www.w3.org/2000/xmlns/", or if the namespaceURI
    ///                            is "http://www.w3.org/2000/xmlns/" and neither the
    ///                            qualifiedName nor its prefix is "xmlns".
    ///     NOT_SUPPORTED_ERR:     Always thrown if the current document does not support the
    ///                            "XML" feature, since namespaces were defined by XML.
    /// ```
    pub fn create_attribute_ns(
        &self,
        namespace_uri: Option<&str>,
        qualified_name: &str,
    ) -> Result<AttrRef, DOMException> {
        if validate_name::<false>(qualified_name).is_err() {
            return Err(DOMException::InvalidCharacterErr);
        }
        if validate_qname::<false>(qualified_name).is_err() {
            return Err(DOMException::NamespaceErr);
        }

        match split_qname2(qualified_name) {
            Some((prefix, _)) => {
                // ... if the qualifiedName has a prefix and the namespaceURI is null, ...
                let Some(ns_uri) = namespace_uri else {
                    return Err(DOMException::NamespaceErr);
                };

                // ... if the qualifiedName has a prefix that is "xml"
                // and the namespaceURI is different from "http://www.w3.org/XML/1998/namespace" ...
                if prefix == "xml" && ns_uri != XML_XML_NAMESPACE {
                    return Err(DOMException::NamespaceErr);
                }

                // ... if the qualifiedName or its prefix is "xmlns"
                // and the namespaceURI is different from "http://www.w3.org/2000/xmlns/" ...
                if prefix == "xmlns" && ns_uri != XML_NS_NAMESPACE {
                    return Err(DOMException::NamespaceErr);
                }

                // ... if the namespaceURI is "http://www.w3.org/2000/xmlns/"
                // and neither the qualifiedName nor its prefix is "xmlns".
                if ns_uri == XML_NS_NAMESPACE && prefix != "xmlns" {
                    return Err(DOMException::NamespaceErr);
                }

                Ok(AttrRef::with_namespace(
                    self.clone(),
                    qualified_name.into(),
                    ns_uri.into(),
                ))
            }
            None => {
                // ... if the qualifiedName or its prefix is "xmlns"
                // and the namespaceURI is different from "http://www.w3.org/2000/xmlns/" ...
                if qualified_name == "xmlns" && namespace_uri != Some(XML_NS_NAMESPACE) {
                    return Err(DOMException::NamespaceErr);
                }

                // ... if the namespaceURI is "http://www.w3.org/2000/xmlns/"
                // and neither the qualifiedName nor its prefix is "xmlns".
                if namespace_uri == Some(XML_NS_NAMESPACE) && qualified_name != "xmlns" {
                    return Err(DOMException::NamespaceErr);
                }

                if let Some(ns_uri) = namespace_uri {
                    Ok(AttrRef::with_namespace(
                        self.clone(),
                        qualified_name.into(),
                        ns_uri.into(),
                    ))
                } else {
                    Ok(AttrRef::new(self.clone(), qualified_name.into()))
                }
            }
        }
    }

    /// Implementation of [`getElementsByTagNameNS`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-getElBTNNS) method.
    ///
    /// # Specification
    /// ```text
    /// Returns a NodeList of all the Elements with a given local name and namespace URI
    /// in document order.
    ///
    /// Parameters
    ///     namespaceURI of type DOMString
    ///         The namespace URI of the elements to match on. The special value "*" matches
    ///         all namespaces.
    ///     localName of type DOMString
    ///         The local name of the elements to match on. The special value "*" matches
    ///         all local names.
    ///
    /// Return Value
    ///     NodeList A new NodeList object containing all the matched Elements.
    ///
    /// No Exceptions
    /// ```
    pub fn get_elements_by_tag_name_ns(
        &self,
        namespace_uri: Option<&str>,
        local_name: &str,
    ) -> FilteredSubtreeElementsList {
        // If this document is HTML document, the tagname is case-insensitive.
        if self.is_html() {
            FilteredSubtreeElementsList::new(
                self.clone().into(),
                namespace_uri.map(|uri| uri.to_owned()),
                local_name.to_owned(),
                |elem, uri, local_name| {
                    (local_name == "*"
                        || match elem.local_name() {
                            Some(ln) => ln.eq_ignore_ascii_case(local_name),
                            None => elem.node_name().eq_ignore_ascii_case(local_name),
                        })
                        && (uri == Some("*")
                            || match (elem.namespace_uri(), uri) {
                                (Some(elem_ns), Some(uri)) if elem_ns.eq_ignore_ascii_case(uri) => {
                                    true
                                }
                                (None, None) => true,
                                _ => false,
                            })
                },
            )
        } else {
            FilteredSubtreeElementsList::new(
                self.clone().into(),
                namespace_uri.map(|uri| uri.to_owned()),
                local_name.to_owned(),
                |elem, uri, local_name| {
                    (local_name == "*"
                        || match elem.local_name() {
                            Some(ln) => ln.as_ref() == local_name,
                            None => elem.node_name().as_ref() == local_name,
                        })
                        && (uri == Some("*")
                            || match (elem.namespace_uri(), uri) {
                                (Some(elem_ns), Some(uri)) if elem_ns.as_ref() == uri => true,
                                (None, None) => true,
                                _ => false,
                            })
                },
            )
        }
    }

    /// Implementation of [`getElementById`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-getElBId) method.
    ///
    /// # Specification
    /// ```text
    /// Returns the Element that has an ID attribute with the given value.
    /// If no such element exists, this returns null. If more than one element has
    /// an ID attribute with that value, what is returned is undefined.
    /// The DOM implementation is expected to use the attribute Attr.isId to determine
    /// if an attribute is of type ID.
    ///
    /// Note: Attributes with the name "ID" or "id" are not of type ID unless so defined.
    ///
    /// Parameters
    ///     elementId of type DOMString
    ///         The unique id value for an element.
    ///
    /// Return Value
    ///     Element The matching element or null if there is none.
    ///
    /// No Exceptions
    /// ```
    pub fn get_element_by_id(&self, element_id: impl Into<Rc<str>>) -> Option<ElementRef> {
        let element_id: Rc<str> = element_id.into();
        let mut children = self.first_child();
        while let Some(child) = children {
            if let NodeRef::Element(elem) = &child {
                let attrs = elem.attributes();
                for i in 0..attrs.length() {
                    let attr = attrs.item(i).unwrap();
                    if attr.is_id() && attr.node_value().as_deref() == Some(&element_id) {
                        return Some(elem.clone());
                    }
                }
            }

            if let Some(first) = child.first_child() {
                children = Some(first);
            } else if let Some(next) = child.next_sibling() {
                children = Some(next);
            } else {
                children = child.parent_node();
                while let Some(par) = children {
                    if let Some(next) = par.next_sibling() {
                        children = Some(next);
                        break;
                    }
                    children = par.parent_node();
                }
            }
        }
        None
    }

    /// Implementation of [`adoptNode`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Document3-adoptNode) method.
    ///
    /// # Specification
    /// ```text
    /// Attempts to adopt a node from another document to this document. If supported,
    /// it changes the ownerDocument of the source node, its children, as well as the
    /// attached attribute nodes if there are any. If the source node has a parent it
    /// is first removed from the child list of its parent. This effectively allows moving
    /// a subtree from one document to another (unlike importNode() which create a copy
    /// of the source node instead of moving it). When it fails, applications should
    /// use Document.importNode() instead. Note that if the adopted node is already part
    /// of this document (i.e. the source and target document are the same), this method
    /// still has the effect of removing the source node from the child list of its parent,
    /// if any. The following list describes the specifics for each type of node.
    ///
    /// <too long...please refer to the specification>
    ///
    /// Note: Since it does not create new nodes unlike the Document.importNode() method,
    ///       this method does not raise an INVALID_CHARACTER_ERR exception, and applications
    ///       should use the Document.normalizeDocument() method to check if an imported
    ///       name is not an XML name according to the XML version in use.
    ///
    /// Parameters
    ///     source of type Node
    ///         The node to move into this document.
    ///
    /// Return Value
    ///     Node The adopted node, or null if this operation fails, such as when the source
    ///          node comes from a different implementation.
    ///
    /// Exceptions
    ///     DOMException
    ///     NOT_SUPPORTED_ERR:           Raised if the source node is of type DOCUMENT,
    ///                                  DOCUMENT_TYPE.
    ///     NO_MODIFICATION_ALLOWED_ERR: Raised when the source node is readonly.
    /// ```
    pub fn adopt_node(&mut self, mut source: NodeRef) -> Result<NodeRef, DOMException> {
        if matches!(
            source.node_type(),
            NodeType::Document | NodeType::DocumentType
        ) {
            return Err(DOMException::NotSupportedErr);
        }

        if let Some(mut parent) = source.parent_node() {
            parent.remove_child(source.clone())?;
        }

        if !check_owner_document_sameness(self, &source) {
            return Ok(source);
        }

        source.adopted_to(self.clone());

        Ok(source)
    }

    /// Implementation of [`normalizeDocument`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Document3-normalizeDocument) method.
    ///
    /// # Specification
    /// ```text
    /// This method acts as if the document was going through a save and load cycle,
    /// putting the document in a "normal" form. As a consequence, this method updates the
    /// replacement tree of EntityReference nodes and normalizes Text nodes, as defined in
    /// the method Node.normalize().
    /// Otherwise, the actual result depends on the features being set on the
    /// Document.domConfig object and governing what operations actually take place.
    /// Noticeably this method could also make the document namespace well-formed according
    /// to the algorithm described in Namespace Normalization, check the character
    /// normalization, remove the CDATASection nodes, etc. See DOMConfiguration for details.
    ///
    /// <..snip..>
    ///
    /// Mutation events, when supported, are generated to reflect the changes occurring
    /// onthe document.
    /// If errors occur during the invocation of this method, such as an attempt to update
    /// a read-only node or a Node.nodeName contains an invalid character according to
    /// the XML version in use, errors or warnings (DOMError.SEVERITY_ERROR or
    /// DOMError.SEVERITY_WARNING) will be reported using the DOMErrorHandler object
    /// associated with the "error-handler" parameter. Note this method might also report
    /// fatal errors (DOMError.SEVERITY_FATAL_ERROR) if an implementation cannot recover
    /// from an error.
    ///
    /// No Parameters
    /// No Return Value
    /// No Exceptions
    /// ```
    pub fn normalize_document(&mut self) {
        todo!()
    }

    /// Implementation of [`renameNode`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Document3-renameNode) method.
    ///
    /// # Specification
    /// ```text
    /// Rename an existing node of type ELEMENT_NODE or ATTRIBUTE_NODE.
    /// When possible this simply changes the name of the given node, otherwise this creates
    /// a new node with the specified name and replaces the existing node with the new node
    /// as described below.
    /// If simply changing the name of the given node is not possible, the following operations
    /// are performed: a new node is created, any registered event listener is registered on
    /// the new node, any user data attached to the old node is removed from that node, the old
    /// node is removed from its parent if it has one, the children are moved to the new node,
    /// if the renamed node is an Element its attributes are moved to the new node, the new
    /// node is inserted at the position the old node used to have in its parent's child nodes
    /// list if it has one, the user data that was attached to the old node is attached to
    /// the new node.
    /// When the node being renamed is an Element only the specified attributes are moved,
    /// default attributes originated from the DTD are updated according to the new element
    /// name.
    /// In addition, the implementation may update default attributes from other schemas.
    /// Applications should use Document.normalizeDocument() to guarantee these attributes are
    /// up-to-date.
    /// When the node being renamed is an Attr that is attached to an Element, the node is
    /// first removed from the Element attributes map. Then, once renamed, either by modifying
    /// the existing node or creating a new one as described above, it is put back.
    /// In addition,
    ///
    /// - a user data event NODE_RENAMED is fired,
    /// - when the implementation supports the feature "MutationNameEvents", each mutation
    ///   operation involved in this method fires the appropriate event, and in the end the
    ///   event {http://www.w3.org/2001/xml-events, DOMElementNameChanged}
    ///   or {http://www.w3.org/2001/xml-events, DOMAttributeNameChanged} is fired.
    ///
    /// Parameters
    ///     n of type Node
    ///         The node to rename.
    ///     namespaceURI of type DOMString
    ///         The new namespace URI.
    ///     qualifiedName of type DOMString
    ///         The new qualified name.
    ///
    /// Return Value
    ///     Node The renamed node. This is either the specified node or the new node that was
    ///          created to replace the specified node.
    ///
    /// Exceptions
    ///     DOMException
    ///     NOT_SUPPORTED_ERR:     Raised when the type of the specified node is neither
    ///                            ELEMENT_NODE nor ATTRIBUTE_NODE, or if the implementation
    ///                            does not support the renaming of the document element.
    ///     INVALID_CHARACTER_ERR: Raised if the new qualified name is not an XML name
    ///                            according to the XML version in use specified in the
    ///                            Document.xmlVersion attribute.
    ///     WRONG_DOCUMENT_ERR:    Raised when the specified node was created from a different
    ///                            document than this document.
    ///     NAMESPACE_ERR:         Raised if the qualifiedName is a malformed qualified name,
    ///                            if the qualifiedName has a prefix and the namespaceURI is
    ///                            null, or if the qualifiedName has a prefix that is "xml" and
    ///                            the namespaceURI is different from
    ///                            "http://www.w3.org/XML/1998/namespace" [XML Namespaces].
    ///                            Also raised, when the node being renamed is an attribute,
    ///                            if the qualifiedName, or its prefix, is "xmlns" and the
    ///                            namespaceURI is different from
    ///                            "http://www.w3.org/2000/xmlns/".
    /// ```
    pub fn rename_node(
        &mut self,
        mut n: NodeRef,
        namespace_uri: Option<impl Into<Rc<str>>>,
        qualified_name: impl Into<Rc<str>>,
    ) -> Result<NodeRef, DOMException> {
        // TODO: handle HTML element and attribute
        if self.is_html() {
            return Err(DOMException::NotSupportedErr);
        }

        if !check_owner_document_sameness(self, &n) {
            return Err(DOMException::WrongDocumentErr);
        }

        let qname: Rc<str> = qualified_name.into();
        if validate_qname::<false>(&qname).is_err() {
            return Err(DOMException::InvalidCharacterErr);
        }

        let ns_uri: Option<Rc<str>> = namespace_uri.map(|uri| uri.into());
        match &mut n {
            NodeRef::Element(elem) => {
                match (split_qname2(&qname), ns_uri.as_deref()) {
                    (Some((pre, _)), Some(ns_uri)) => {
                        if pre == "xml" && ns_uri != XML_XML_NAMESPACE {
                            return Err(DOMException::NamespaceErr);
                        }
                    }
                    (Some(_), None) => {
                        return Err(DOMException::NamespaceErr);
                    }
                    _ => {}
                }
                let mut res = elem.clone();
                res.rename(qname, ns_uri);
                elem.handle_user_data(OperationType::NodeRenamed, None);
                Ok(n)
            }
            NodeRef::Attribute(attr) => {
                let mut keep = None;
                if let Some(mut elem) = attr.owner_element() {
                    elem.remove_attribute_node(attr.clone())?;
                    keep = Some(elem);
                }

                match (split_qname2(&qname), ns_uri.as_deref()) {
                    (Some((pre, _)), Some(ns_uri)) => {
                        if pre == "xml" && ns_uri != XML_XML_NAMESPACE {
                            return Err(DOMException::NamespaceErr);
                        }
                        if pre == "xmlns" && ns_uri != XML_NS_NAMESPACE {
                            return Err(DOMException::NamespaceErr);
                        }
                    }
                    (Some(_), None) => {
                        return Err(DOMException::NamespaceErr);
                    }
                    (None, Some(ns_uri)) => {
                        if qname.as_ref() == "xmlns" && ns_uri != XML_NS_NAMESPACE {
                            return Err(DOMException::NamespaceErr);
                        }
                    }
                    (None, None) => {}
                }

                attr.rename(qname, ns_uri);
                if let Some(mut elem) = keep {
                    elem.set_attribute_node(attr.clone())?;
                }
                attr.handle_user_data(OperationType::NodeRenamed, None);
                Ok(n)
            }
            _ => Err(DOMException::NotSupportedErr),
        }
    }

    /// Implementation of [`doctype`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-B63ED1A31) attribute.
    ///
    /// # Specification
    /// ```text
    /// doctype of type DocumentType, readonly, modified in DOM Level 3
    ///     The Document Type Declaration (see DocumentType) associated with this document.
    ///     For XML documents without a document type declaration this returns null.
    ///     For HTML documents, a DocumentType object may be returned, independently of the
    ///     presence or absence of document type declaration in the HTML document.
    ///     This provides direct access to the DocumentType node, child node of this Document.
    ///     This node can be set at document creation time and later changed through the use
    ///     of child nodes manipulation methods, such as Node.insertBefore,
    ///     or Node.replaceChild. Note, however, that while some implementations may
    ///     instantiate different types of Document objects supporting additional features
    ///     than the "Core", such as "HTML" [DOM Level 2 HTML], based on the DocumentType
    ///     specified at creation time, changing it afterwards is very unlikely to result
    ///     in a change of the features supported.
    /// ```
    pub fn doctype(&self) -> Option<DocumentTypeRef> {
        self.0.borrow().doctype.clone()
    }

    /// Implementation of [`implementation`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1B793EBA) attribute.
    ///
    /// # Specification
    /// ```text
    /// implementation of type DOMImplementation, readonly
    ///     The DOMImplementation object that handles this document. A DOM application may
    ///     use objects from multiple implementations.
    /// ```
    pub fn implementation(&self) -> Arc<dyn DOMImplementation> {
        self.0.borrow().implementation.clone()
    }

    /// Implementation of [`documentElement`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-87CD092) attribute.
    ///
    /// # Specification
    /// ```text
    /// documentElement of type Element, readonly
    ///     This is a convenience attribute that allows direct access to the child node
    ///     that is the document element of the document.
    /// ```
    pub fn document_element(&self) -> Option<ElementRef> {
        self.0.borrow().document_element.clone()
    }

    /// Implementation of [`inputEncoding`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Document3-inputEncoding) attribute.
    ///
    /// # Specification
    /// ```text
    /// inputEncoding of type DOMString, readonly, introduced in DOM Level 3
    ///     An attribute specifying the encoding used for this document at the time of
    ///     the parsing. This is null when it is not known, such as when the Document was
    ///     created in memory.
    /// ```
    pub fn input_encoding(&self) -> Option<Rc<str>> {
        self.0.borrow().input_encoding.clone()
    }

    /// Implementation of [`xmlEncoding`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Document3-encoding) attribute.
    ///
    /// # Specification
    /// ```text
    /// xmlEncoding of type DOMString, readonly, introduced in DOM Level 3
    ///     An attribute specifying, as part of the XML declaration, the encoding of
    ///     this document. This is null when unspecified or when it is not known, such as
    ///     when the Document was created in memory.
    /// ```
    pub fn xml_encoding(&self) -> Option<Rc<str>> {
        self.0.borrow().xml_encoding.clone()
    }

    /// Implementation of [`xmlStandalone`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Document3-standalone) attribute.
    ///
    /// # Specification
    /// ```text
    /// xmlStandalone of type boolean, introduced in DOM Level 3
    ///     An attribute specifying, as part of the XML declaration, whether this document
    ///     is standalone. This is false when unspecified.
    ///
    ///     Note: No verification is done on the value when setting this attribute.
    ///           Applications should use Document.normalizeDocument() with the "validate"
    ///           parameter to verify if the value matches the validity constraint for
    ///           standalone document declaration as defined in [XML 1.0].
    /// ```
    pub fn xml_standalone(&self) -> bool {
        self.0.borrow().xml_standalone == 1
    }

    /// Implementation of [`xmlStandalone`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Document3-standalone) attribute.
    ///
    /// # Specification
    /// ```text
    /// xmlStandalone of type boolean, introduced in DOM Level 3
    ///     An attribute specifying, as part of the XML declaration, whether this document
    ///     is standalone. This is false when unspecified.
    ///
    ///     Note: No verification is done on the value when setting this attribute.
    ///           Applications should use Document.normalizeDocument() with the "validate"
    ///           parameter to verify if the value matches the validity constraint for
    ///           standalone document declaration as defined in [XML 1.0].
    ///
    /// Exceptions on setting
    ///     DOMException
    ///     NOT_SUPPORTED_ERR: Raised if this document does not support the "XML" feature.
    /// ```
    pub fn set_xml_standalone(&mut self, standalone: bool) -> Result<(), DOMException> {
        if !self.implementation().has_feature("XML", None) {
            return Err(DOMException::NotSupportedErr);
        }
        self.0.borrow_mut().xml_standalone = standalone as i8;
        Ok(())
    }

    /// Implementation of [`xmlVersion`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Document3-version) attribute.
    ///
    /// # Specification
    /// ```text
    /// An attribute specifying, as part of the XML declaration, the version numberof
    /// this document. If there is no declaration and if this document supports the
    /// "XML" feature, the value is "1.0". If this document does not support the "XML" feature,
    /// the value is always null. Changing this attribute will affect methods that check
    /// for invalid characters in XML names. Application should invoke
    /// Document.normalizeDocument() in order to check for invalid characters in the Nodes
    /// that are already part of this Document.
    /// DOM applications may use the DOMImplementation.hasFeature(feature, version) method
    /// with parameter values "XMLVersion" and "1.0" (respectively) to determine if
    /// an implementation supports [XML 1.0]. DOM applications may use the same method
    /// with parameter values "XMLVersion" and "1.1" (respectively) to determine if
    /// an implementation supports [XML 1.1]. In both cases, in order to support XML,
    /// an implementation must also support the "XML" feature defined in this specification.
    /// Document objects supporting a version of the "XMLVersion" feature must not raise
    /// a NOT_SUPPORTED_ERR exception for the same version number when using Document.xmlVersion.
    /// ```
    pub fn xml_version(&self) -> Option<Rc<str>> {
        if let Some(version) = self.0.borrow().xml_version.clone() {
            Some(version)
        } else if self.implementation().has_feature("XML", None) {
            Some("1.0".into())
        } else {
            None
        }
    }

    /// Implementation of [`xmlVersion`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Document3-version) attribute.
    ///
    /// # Specification
    /// ```text
    /// An attribute specifying, as part of the XML declaration, the version numberof
    /// this document. If there is no declaration and if this document supports the
    /// "XML" feature, the value is "1.0". If this document does not support the "XML"
    /// feature, the value is always null. Changing this attribute will affect methods
    /// that check for invalid characters in XML names. Application should invoke
    /// Document.normalizeDocument() in order to check for invalid characters in the Nodes
    /// that are already part of this Document.
    /// DOM applications may use the DOMImplementation.hasFeature(feature, version) method
    /// with parameter values "XMLVersion" and "1.0" (respectively) to determine if
    /// an implementation supports [XML 1.0]. DOM applications may use the same method
    /// with parameter values "XMLVersion" and "1.1" (respectively) to determine if
    /// an implementation supports [XML 1.1]. In both cases, in order to support XML,
    /// an implementation must also support the "XML" feature defined in this specification.
    /// Document objects supporting a version of the "XMLVersion" feature must not raise
    /// a NOT_SUPPORTED_ERR exception for the same version number when using Document.xmlVersion.
    ///
    /// Exceptions on setting
    ///     DOMException
    ///     NOT_SUPPORTED_ERR: Raised if the version is set to a value that is not supported
    ///                        by this Document or if this document does not support the
    ///                        "XML" feature.
    /// ```
    pub fn set_xml_version(&mut self, xml_version: &str) -> Result<(), DOMException> {
        if !self.implementation().has_feature("XML", None)
            || !self
                .implementation()
                .has_feature("XMLVersion", Some(xml_version))
        {
            return Err(DOMException::NotSupportedErr);
        }
        self.0.borrow_mut().xml_version = Some(xml_version.into());
        Ok(())
    }

    /// Implementation of [`documentURI`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Document3-documentURI) attribute.
    ///
    /// # Specification
    /// ```text
    /// documentURI of type DOMString, introduced in DOM Level 3
    ///     The location of the document or null if undefined or if the Document was created
    ///     using DOMImplementation.createDocument. No lexical checking is performed when
    ///     setting this attribute; this could result in a null value returned when using
    ///     Node.baseURI.
    ///     Beware that when the Document supports the feature "HTML" [DOM Level 2 HTML],
    ///     the href attribute of the HTML BASE element takes precedence over this attribute
    ///     when computing Node.baseURI.
    /// ```
    pub fn document_uri(&self) -> Option<Rc<str>> {
        self.0.borrow().document_uri.clone()
    }

    /// Do an entity lookup in the document entity hash table and
    /// returns the corresponding entity, otherwise a lookup is done
    /// in the predefined entities too.
    ///
    /// If found, return found entity wrapped `Some`.  
    /// Otherwise, return `None`.
    #[doc(alias = "xmlGetDocEntity")]
    pub fn get_entity(&self, name: &str) -> Option<EntityRef> {
        self.0.borrow().get_entity(name)
    }

    pub(super) fn get_default_attribute(
        &self,
        elem_name: &str,
        attr_name: &str,
    ) -> Option<AttrRef> {
        let elemdecl = self.doctype()?.get_element_decl(elem_name)?;
        let attlistdecl = elemdecl.get_attribute_decl(attr_name)?;
        match attlistdecl.default_decl() {
            DefaultDecl::None(def) | DefaultDecl::Fixed(def) => {
                let mut attr = self.create_attribute(attr_name).ok()?;
                attr.set_value(def.as_ref()).ok()?;
                attr.set_specified(false);
                Some(attr)
            }
            _ => None,
        }
    }

    pub(super) fn get_default_attribute_ns(
        &self,
        context_node: ElementRef,
        attr_name: &str,
    ) -> Option<AttrRef> {
        let elemdecl = self
            .doctype()?
            .get_element_decl(&context_node.node_name())?;
        let attlistdecl = elemdecl.get_attribute_decl(attr_name)?;
        match attlistdecl.default_decl() {
            DefaultDecl::None(def) | DefaultDecl::Fixed(def) => {
                let mut attr = if let Some(namespace_uri) = split_qname2(attr_name)
                    .and_then(|(pre, _)| context_node.lookup_namespace_uri(Some(pre)))
                {
                    self.create_attribute_ns(Some(&namespace_uri), attr_name)
                        .ok()?
                } else {
                    self.create_attribute_ns(None, attr_name).ok()?
                };
                attr.set_value(def.as_ref()).ok()?;
                attr.set_specified(false);
                Some(attr)
            }
            _ => None,
        }
    }

    pub(super) fn get_default_attributes(&self, elem_name: &str) -> Option<Vec<AttrRef>> {
        let elemdecl = self.doctype()?.get_element_decl(elem_name)?;
        let mut res = vec![];
        for decl in elemdecl.get_attribute_decls() {
            match decl.default_decl() {
                DefaultDecl::None(def) | DefaultDecl::Fixed(def) => {
                    let mut attr = self.create_attribute(decl.name()).ok()?;
                    attr.set_value(def.as_ref()).ok()?;
                    attr.set_specified(false);

                    // Due to the requirements of the XML specification, attributes
                    // with default values are not ID attributes, so it is not necessary
                    // to check whether they should be set as ID attributes.
                    //
                    // 3.3.1 Attribute Types
                    // [Validity constraint: ID Attribute Default]
                    // An ID attribute MUST have a declared default of #IMPLIED or #REQUIRED.

                    res.push(attr);
                }
                _ => {}
            }
        }
        Some(res)
    }

    /// Check if this is HTML document.
    pub fn is_html(&self) -> bool {
        self.0.borrow().html
    }

    /// Enable check for read-only node.\
    /// As a result, editing of nodes specified as read-only in the DOM specification
    /// becomes impossible.
    pub fn enable_read_only_check(&mut self) {
        self.0.borrow_mut().enable_read_only_check();
    }

    /// Disable check for read-only node.\
    /// It allows editing of nodes that are not editable in the DOM specification
    /// (e.g., DTD nodes).
    pub fn disable_read_only_check(&mut self) {
        self.0.borrow_mut().disable_read_only_check();
    }

    /// Check if read-only check is enabled.
    pub fn is_enabled_read_only_check(&self) -> bool {
        self.0.borrow().is_enabled_read_only_check()
    }
}

impl Node for DocumentRef {
    fn node_name(&self) -> Rc<str> {
        "#document".into()
    }

    fn node_value(&self) -> Option<Rc<str>> {
        None
    }

    fn set_node_value(&mut self, _: impl Into<String>) -> Result<(), DOMException> {
        Ok(())
    }

    fn node_type(&self) -> NodeType {
        NodeType::Document
    }

    fn first_child(&self) -> Option<NodeRef> {
        self.0.borrow().first_child.clone().map(From::from)
    }

    fn last_child(&self) -> Option<NodeRef> {
        self.0.borrow().last_child.clone().map(From::from)
    }

    fn insert_before(
        &mut self,
        mut new_child: NodeRef,
        ref_child: Option<NodeRef>,
    ) -> Result<NodeRef, DOMException> {
        // In this implementation, if `new_child` and `ref_child` are same node,
        // do nothing and return `new_child`.
        if ref_child
            .as_ref()
            .is_some_and(|ref_child| new_child.is_same_node(ref_child))
        {
            return Ok(new_child);
        }

        // HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children
        // of the type of the newChild node (..snip)
        if new_child.node_type() == NodeType::DocumentFragment {
            let mut children = new_child.first_child();
            while let Some(child) = children {
                children = child.next_sibling();
                if !check_vertical_hierarchy(self.node_type(), child.node_type()) {
                    return Err(DOMException::HierarchyRequestErr);
                }
                match child.node_type() {
                    NodeType::Element if self.document_element().is_some() => {
                        return Err(DOMException::HierarchyRequestErr);
                    }
                    NodeType::DocumentType if self.doctype().is_some() => {
                        return Err(DOMException::HierarchyRequestErr);
                    }
                    _ => {}
                }
            }
        } else if !check_vertical_hierarchy(self.node_type(), new_child.node_type()) {
            return Err(DOMException::HierarchyRequestErr);
        }
        // NOT_FOUND_ERR: Raised if refChild is not a child of this node.
        if ref_child.as_ref().is_some_and(|ref_child| {
            ref_child
                .parent_node()
                .is_none_or(|par| !self.is_same_node(&par))
        }) {
            return Err(DOMException::NotFoundErr);
        }
        // WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document
        // than the one that created this node.
        if !check_owner_document_sameness(self, &new_child) {
            return Err(DOMException::WrongDocumentErr);
        }

        // HIERARCHY_REQUEST_ERR: Raised if (..snip..) this node is of type Document
        // and the DOM application attempts to insert a second DocumentType or Element node.
        match new_child.node_type() {
            NodeType::Element => {
                if self.document_element().is_some() {
                    return Err(DOMException::HierarchyRequestErr);
                }
                // If `new_child` is an Element that passes all checks, it is a document element.
                self.0.borrow_mut().document_element = new_child.as_element();
            }
            NodeType::DocumentType => {
                if self.doctype().is_some() {
                    return Err(DOMException::HierarchyRequestErr);
                }
                // The DocumentType that passed the check should have a dummy Document set,
                // so set the owner Document here to `self`.
                new_child.set_owner_document(self.clone());
                // and set `new_child` as the `doctype` of this document.
                self.0.borrow_mut().doctype = new_child.as_document_type();
            }
            _ => {}
        }

        if let Some(ref_child) = ref_child {
            match &mut new_child {
                // If newChild is a DocumentFragment object, all of its children are inserted,
                // in the same order, before refChild.
                NodeRef::DocumentFragment(frag) => {
                    let mut child = frag.first_child();
                    while let Some(mut now) = child {
                        child = now.next_sibling();
                        now.connect_as_previous_sibling(ref_child.clone());
                    }
                }
                other => {
                    other.connect_as_previous_sibling(ref_child);
                }
            }
        } else {
            let mut children = match new_child.clone() {
                NodeRef::DocumentFragment(frag) => frag.first_child(),
                mut other => {
                    other.disconnect_parent_and_sibling();
                    Some(other)
                }
            };
            while let Some(mut child) = children {
                children = child.next_sibling();
                child.set_parent_node(Some(self.clone().into()));
                let last = self.set_last_child(Some(child.clone()));
                if let Some(mut last) = last {
                    // If old last child is found,
                    // it is set as the previous sibling of `new_child`.
                    last.set_next_sibling(Some(child.clone()));
                    child.set_previous_sibling(Some(last));
                } else {
                    // Otherwise, `self` has no children.
                    // Therefore, I need to set `new_child` as also the first child of `self`.
                    self.set_first_child(Some(child.clone()));
                }
            }
        }

        Ok(new_child)
    }

    fn clone_node(&self, deep: bool) -> NodeRef {
        let doctype = self.doctype().map(|doctype| {
            let NodeRef::DocumentType(doctype) = doctype.clone_node(deep) else {
                unreachable!()
            };
            doctype
        });
        let document_element = self.document_element().map(|elem| {
            let NodeRef::Element(elem) = elem.clone_node(deep) else {
                unreachable!()
            };
            elem
        });
        let mut doc = DocumentRef(Rc::new(RefCell::new(Document {
            first_child: None,
            last_child: None,
            doctype,
            document_element,
            implementation: self.0.borrow().implementation.clone(),
            document_uri: self.0.borrow().document_uri.clone(),
            input_encoding: self.0.borrow().input_encoding.clone(),
            xml_encoding: self.0.borrow().xml_encoding.clone(),
            xml_standalone: self.0.borrow().xml_standalone,
            xml_version: self.0.borrow().xml_version.clone(),
            user_data: None,
            html: self.0.borrow().html,
            flag: self.0.borrow().flag,
            predefined_entities: self.0.borrow().predefined_entities.clone(),
        })));
        for (i, ent) in self
            .0
            .borrow()
            .predefined_entities
            .iter()
            .cloned()
            .enumerate()
        {
            let new = doc.import_node(ent.into(), true).expect("Internal Error");
            doc.0.borrow_mut().predefined_entities[i] = new.as_entity().unwrap();
        }

        if deep {
            let mut children = self.first_child();
            while let Some(child) = children {
                children = child.next_sibling();
                let new = doc.import_node(child, deep).expect("Internal Error");
                doc.append_child(new).expect("Internal Error");
            }
        }

        self.handle_user_data(OperationType::NodeCloned, Some(doc.clone().into()));
        doc.into()
    }

    fn text_content(&self) -> Option<String> {
        None
    }

    fn set_text_content(&mut self, _text: impl Into<String>) -> Result<(), DOMException> {
        Ok(())
    }

    fn is_same_node(&self, other: &NodeRef) -> bool {
        let NodeRef::Document(other) = other else {
            return false;
        };
        Rc::ptr_eq(&self.0, &other.0)
    }

    fn lookup_prefix(&self, ns_uri: &str) -> Option<Rc<str>> {
        self.document_element()
            .and_then(|elem| elem.lookup_prefix(ns_uri))
    }

    fn is_default_namespace(&self, ns_uri: &str) -> bool {
        self.document_element()
            .map(|elem| elem.is_default_namespace(ns_uri))
            .unwrap_or(false)
    }

    fn lookup_namespace_uri(&self, prefix: Option<&str>) -> Option<Rc<str>> {
        self.document_element()?.lookup_namespace_uri(prefix)
    }

    fn set_user_data(
        &mut self,
        key: impl Into<String>,
        data: DOMUserData,
        handler: Option<Arc<dyn UserDataHandler>>,
    ) -> Option<DOMUserData> {
        self.0
            .borrow_mut()
            .user_data
            .get_or_insert_default()
            .insert(key.into(), (data, handler))
            .map(|v| v.0)
    }

    fn get_user_data(&self, key: &str) -> Option<DOMUserData> {
        self.0
            .borrow()
            .user_data
            .as_ref()
            .and_then(|user_data| user_data.get(key))
            .map(|v| v.0.clone())
    }

    fn is_read_only(&self) -> bool {
        false
    }
}

impl NodeConnection for DocumentRef {
    fn set_parent_node(&mut self, _: Option<NodeRef>) -> Option<NodeRef> {
        None
    }

    fn set_first_child(&mut self, new_child: Option<NodeRef>) -> Option<NodeRef> {
        replace(
            &mut self.0.borrow_mut().first_child,
            new_child.map(From::from),
        )
        .map(From::from)
    }

    fn set_last_child(&mut self, new_child: Option<NodeRef>) -> Option<NodeRef> {
        replace(
            &mut self.0.borrow_mut().last_child,
            new_child.map(From::from),
        )
        .map(From::from)
    }

    fn set_previous_sibling(&mut self, _: Option<NodeRef>) -> Option<NodeRef> {
        None
    }

    fn set_next_sibling(&mut self, _: Option<NodeRef>) -> Option<NodeRef> {
        None
    }

    fn set_owner_document(&mut self, _: DocumentRef) -> Option<DocumentRef> {
        None
    }

    fn adopted_to(&mut self, _new_doc: DocumentRef) {
        // `Document` nodes cannot be adopted.
    }

    fn handle_user_data(&self, operation: OperationType, dst: Option<NodeRef>) {
        if let Some(user_data) = self.0.borrow().user_data.as_ref() {
            for (key, value) in user_data.iter() {
                if let Some(handler) = value.1.clone() {
                    handler.handle(
                        operation,
                        key.as_str(),
                        value.0.clone(),
                        self.clone().into(),
                        dst.clone(),
                    );
                }
            }
        }
    }
}

impl From<DocumentRef> for NodeRef {
    fn from(value: DocumentRef) -> Self {
        NodeRef::Document(value)
    }
}

impl From<Rc<RefCell<Document>>> for DocumentRef {
    fn from(value: Rc<RefCell<Document>>) -> Self {
        Self(value)
    }
}