exml 0.7.3-deprecated

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
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
use std::{
    cell::RefCell,
    rc::{Rc, Weak},
    sync::Arc,
};

use crate::{
    tree::XML_XML_NAMESPACE,
    uri::{XmlURI, build_uri},
};

use super::{
    DOCUMENT_POSITION_CONTAINED_BY, DOCUMENT_POSITION_CONTAINS, DOCUMENT_POSITION_DISCONNECTED,
    DOCUMENT_POSITION_FOLLOWING, DOCUMENT_POSITION_PRECEDING, DOMException, DocumentPosition,
    NodeType,
    attr::{Attr, AttrRef},
    character_data::{
        CDATASection, CDATASectionRef, CharacterData, Comment, CommentRef, Text, TextRef,
    },
    check_no_modification_allowed_err, check_owner_document_sameness, check_vertical_hierarchy,
    document::{Document, DocumentRef},
    document_fragment::{DocumentFragment, DocumentFragmentRef},
    document_type::{DocumentType, DocumentTypeRef},
    element::{Element, ElementRef},
    entity::{Entity, EntityRef},
    entity_reference::{EntityReference, EntityReferenceRef},
    named_node_map::{AttributeMap, NamedNodeMap},
    node_list::ChildNodesList,
    notation::{Notation, NotationRef},
    pi::{ProcessingInstruction, ProcessingInstructionRef},
    user_data::{DOMUserData, OperationType, UserDataHandler},
};

/// Implementation of [Node](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1950641247)
/// 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)
///
/// Actual node representations are implemented as [`NodeRef`].
#[allow(private_bounds)]
pub trait Node: NodeConnection {
    /// Implementation of [`nodeName`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-F68D095) attribute.
    ///
    /// # Specification
    /// ```text
    /// nodeName of type DOMString, readonly
    ///     The name of this node, depending on its type; see the table above.
    /// ```
    fn node_name(&self) -> Rc<str>;
    /// Implementation of [`nodeValue`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-F68D080) attribute.
    ///
    /// # Specification
    /// ```text
    /// nodeValue of type DOMString
    ///     The value of this node, depending on its type; see the table above.
    ///     When it is defined to be null, setting it has no effect, including
    ///     if the node is read-only.
    ///
    ///     Exceptions on retrieval
    ///         DOMException
    ///         DOMSTRING_SIZE_ERR: Raised when it would return more characters than fit
    ///                             in a DOMString variable on the implementation platform.
    /// ```
    fn node_value(&self) -> Option<Rc<str>>;
    /// Implementation of [`nodeValue`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-F68D080) attribute.
    ///
    /// # Specification
    /// ```text
    /// nodeValue of type DOMString
    ///     The value of this node, depending on its type; see the table above.
    ///     When it is defined to be null, setting it has no effect, including
    ///     if the node is read-only.
    ///
    ///     Exceptions on setting
    ///         DOMException
    ///         NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly and
    ///                                      if it is not defined to be null.
    /// ```
    fn set_node_value(&mut self, value: impl Into<String>) -> Result<(), DOMException>;

    /// Implementation of [`nodeType`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-111237558) attribute.
    ///
    /// # Specification
    /// ```text
    /// nodeType of type unsigned short, readonly
    ///     A code representing the type of the underlying object, as defined above.
    /// ```
    fn node_type(&self) -> NodeType;
    /// Implementation of [`parentNode`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1060184317) attribute.
    ///
    /// # Specification
    /// ```text
    /// parentNode of type Node, readonly
    ///     The parent of this node. All nodes, except Attr, Document, DocumentFragment,
    ///     Entity, and Notation may have a parent. However, if a node has just been
    ///     created and not yet added to the tree, or if it has been removed from the tree,
    ///     this is null.
    /// ```
    fn parent_node(&self) -> Option<NodeRef> {
        None
    }
    /// Implementation of [`childNodes`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1451460987) attribute.
    ///
    /// # Specification
    /// ```text
    /// childNodes of type NodeList, readonly
    ///     A NodeList that contains all children of this node. If there are no children,
    ///     this is a NodeList containing no nodes.
    /// ```
    fn child_nodes(&self) -> ChildNodesList<NodeRef> {
        ChildNodesList::new(self.clone().into())
    }
    /// Implementation of [`firstChild`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-169727388) attribute.
    ///
    /// # Specification
    /// ```text
    /// firstChild of type Node, readonly
    ///     The first child of this node. If there is no such node, this returns null.
    /// ```
    fn first_child(&self) -> Option<NodeRef> {
        None
    }
    /// Implementation of [`lastChild`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-61AD09FB) attribute.
    ///
    /// # Specification
    /// ```text
    /// lastChild of type Node, readonly
    ///     The last child of this node. If there is no such node, this returns null.
    /// ```
    fn last_child(&self) -> Option<NodeRef> {
        None
    }
    /// Implementation of [`previousSibling`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-640FB3C8) attribute.
    ///
    /// # Specification
    /// ```text
    /// previousSibling of type Node, readonly
    ///     The node immediately preceding this node. If there is no such node,
    ///     this returns null.
    /// ```
    fn previous_sibling(&self) -> Option<NodeRef> {
        None
    }
    /// Implementation of [`nextSibling`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-6AC54C2F) attribute.
    ///
    /// # Specification
    /// ```text
    /// nextSibling of type Node, readonly
    ///     The node immediately following this node. If there is no such node,
    ///     this returns null.
    /// ```
    fn next_sibling(&self) -> Option<NodeRef> {
        None
    }
    /// Implementation of [`attributes`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-84CF096) attribute.
    ///
    /// # Specification
    /// ```text
    /// attributes of type NamedNodeMap, readonly
    ///     A NamedNodeMap containing the attributes of this node (if it is an Element)
    ///     or null otherwise.
    /// ```
    fn attributes(&self) -> Option<AttributeMap> {
        None
    }
    /// Implementation of [`ownerDocument`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-node-ownerDoc) attribute.
    ///
    /// # Specification
    /// ```text
    /// ownerDocument of type Document, readonly, modified in DOM Level 2
    ///     The Document object associated with this node. This is also the Document object
    ///     used to create new nodes. When this node is a Document or a DocumentType which
    ///     is not used with any Document yet, this is null.
    /// ```
    fn owner_document(&self) -> Option<DocumentRef> {
        None
    }

    /// Implementation of [`insertBefore`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-952280727) method.
    ///
    /// If `new_child` and `ref_child` are same nodes,
    /// this method does nothing and return `Ok(new_child)`.
    ///
    /// # Specification
    /// ```text
    /// Inserts the node newChild before the existing child node refChild.
    /// If refChild is null, insert newChild at the end of the list of children.
    /// If newChild is a DocumentFragment object, all of its children are inserted,
    /// in the same order, before refChild.
    /// If the newChild is already in the tree, it is first removed.
    ///
    /// Note: Inserting a node before itself is implementation dependent.
    ///
    /// Parameters
    ///     newChild of type Node
    ///         The node to insert.
    ///     refChild of type Node
    ///         The reference node, i.e., the node before which the new node must be inserted.
    ///
    /// Return Value
    ///     Node The node being inserted.
    ///
    /// Exceptions
    ///     DOMException
    ///     HIERARCHY_REQUEST_ERR:       Raised if this node is of a type that does not allow
    ///                                  children of the type of the newChild node, or if the
    ///                                  node to insert is one of this node's ancestors or
    ///                                  this node itself, or if this node is of type Document
    ///                                  and the DOM application attempts to insert a second
    ///                                  DocumentType or Element node.
    ///     WRONG_DOCUMENT_ERR:          Raised if newChild was created from a different
    ///                                  document than the one that created this node.
    ///     NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or if the parent of
    ///                                  the node being inserted is readonly.
    ///     NOT_FOUND_ERR:               Raised if refChild is not a child of this node.
    ///     NOT_SUPPORTED_ERR:           if this node is of type Document, this exception
    ///                                  might be raised if the DOM implementation doesn't
    ///                                  support the insertion of a DocumentType or Element
    ///                                  node.
    /// ```
    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);
        }

        check_no_modification_allowed_err(self)?;

        // 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);
                }
            }
        } 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..) the node to insert is
        // one of this node's ancestors or this node itself, (..snip..)
        if self.is_same_node(&new_child) {
            return Err(DOMException::HierarchyRequestErr);
        }
        let mut par = self.parent_node();
        while let Some(cur) = par {
            par = cur.parent_node();
            if new_child.is_same_node(&cur) {
                return Err(DOMException::HierarchyRequestErr);
            }
        }

        // 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.
        if self.node_type() == NodeType::Document
            && matches!(
                new_child.node_type(),
                NodeType::Element | NodeType::DocumentType
            )
        {
            let mut chilren = self.first_child();
            while let Some(child) = chilren {
                chilren = child.next_sibling();
                if child.node_type() == new_child.node_type() {
                    return Err(DOMException::HierarchyRequestErr);
                }
            }
        }

        let read_only = self.is_read_only();
        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());
                        if read_only {
                            now.set_read_only();
                        }
                    }
                }
                other => {
                    other.connect_as_previous_sibling(ref_child);
                    if read_only {
                        other.set_read_only();
                    }
                }
            }
        } 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()));
                }
                if read_only {
                    child.set_read_only();
                }
            }
        }

        Ok(new_child)
    }

    /// Implementation of [`replaceChild`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-785887307) method.
    ///
    /// # Specification
    /// ```text
    /// Replaces the child node oldChild with newChild in the list of children,
    /// and returns the oldChild node.  
    /// If newChild is a DocumentFragment object, oldChild is replaced by all of
    /// the DocumentFragment children, which are inserted in the same order.  
    /// If the newChild is already in the tree, it is first removed.
    ///
    /// Note: Replacing a node with itself is implementation dependent.
    ///
    /// Parameters
    ///     newChild of type Node
    ///         The new node to put in the child list.
    ///     oldChild of type Node
    ///         The node being replaced in the list.
    ///
    /// Return Value
    ///     Node The node replaced.
    ///
    /// Exceptions
    ///     DOMException
    ///     HIERARCHY_REQUEST_ERR:       Raised if this node is of a type that does not allow
    ///                                  children of the type of the newChild node, or if the
    ///                                  node to put in is one of this node's ancestors or
    ///                                  this node itself, or if this node is of type Document
    ///                                  and the result of the replacement operation would add
    ///                                  a second DocumentType or Element on the Document node.
    ///     WRONG_DOCUMENT_ERR:          Raised if newChild was created from a different
    ///                                  document than the one that created this node.
    ///     NO_MODIFICATION_ALLOWED_ERR: Raised if this node or the parent of the new node is
    ///                                  readonly.
    ///     NOT_FOUND_ERR:               Raised if oldChild is not a child of this node.
    ///     NOT_SUPPORTED_ERR:           if this node is of type Document, this exception
    ///                                  might be raised if the DOM implementation doesn't
    ///                                  support the replacement of the DocumentType child or
    ///                                  Element child.
    /// ```
    fn replace_child(
        &mut self,
        new_child: NodeRef,
        mut old_child: NodeRef,
    ) -> Result<NodeRef, DOMException> {
        // In this implementation, if `new_child` and `ref_child` are same node,
        // do nothing and return `new_child`.
        if new_child.is_same_node(&old_child) {
            return Ok(new_child);
        }

        check_no_modification_allowed_err(self)?;

        // 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);
                }
            }
        } else if !check_vertical_hierarchy(self.node_type(), new_child.node_type()) {
            return Err(DOMException::HierarchyRequestErr);
        }
        // NOT_FOUND_ERR: Raised if oldChild is not a child of this node.
        if old_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..) the node to put in is
        // one of this node's ancestors or this node itself, (..snip..)
        if self.is_same_node(&new_child) {
            return Err(DOMException::HierarchyRequestErr);
        }
        let mut par = self.parent_node();
        while let Some(cur) = par {
            par = cur.parent_node();
            if new_child.is_same_node(&cur) {
                return Err(DOMException::HierarchyRequestErr);
            }
        }

        // HIERARCHY_REQUEST_ERR: Raised if (..snip..) this node is of type Document
        // and the result of the replacement operation would add
        // a second DocumentType or Element on the Document node.
        if self.node_type() == NodeType::Document
            && matches!(
                new_child.node_type(),
                NodeType::Element | NodeType::DocumentType
            )
        {
            let mut chilren = self.first_child();
            while let Some(child) = chilren {
                chilren = child.next_sibling();
                if child.node_type() == new_child.node_type() && !child.is_same_node(&old_child) {
                    return Err(DOMException::HierarchyRequestErr);
                }
            }
        }

        let mut children = match new_child {
            NodeRef::DocumentFragment(frag) => frag.first_child(),
            mut other => {
                other.disconnect_parent_and_sibling();
                Some(other)
            }
        };
        let read_only = self.is_read_only();
        let par = old_child.set_parent_node(None);
        let prev = old_child.set_previous_sibling(None);
        let next = old_child.set_next_sibling(None);
        old_child.unset_read_only();
        let prev = prev.or_else(|| {
            let mut child = children.clone()?;
            children = child.next_sibling();
            child.set_parent_node(par.clone());
            if read_only {
                child.set_read_only();
            }
            self.set_first_child(Some(child.clone()));
            Some(child)
        });
        if let Some(mut prev) = prev {
            while let Some(mut child) = children {
                children = child.next_sibling();
                child.set_parent_node(par.clone());
                child.set_previous_sibling(Some(prev.clone()));
                if read_only {
                    child.set_read_only();
                }
                prev.set_next_sibling(Some(child.clone()));
                prev = child;
            }
            prev.set_next_sibling(next.clone());
            if let Some(mut next) = next {
                next.set_previous_sibling(Some(prev));
            }
        }
        Ok(old_child)
    }

    /// Implementation of [`removeChild`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1734834066) method.
    ///
    /// # Specification
    /// ```text
    /// Removes the child node indicated by oldChild from the list of children, and returns it.
    ///
    /// Parameters
    ///     oldChild of type Node
    ///         The node being removed.
    ///
    /// Return Value
    ///     Node The node removed.
    ///
    /// Exceptions
    ///     DOMException
    ///     NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
    ///     NOT_FOUND_ERR:               Raised if oldChild is not a child of this node.
    ///     NOT_SUPPORTED_ERR:           if this node is of type Document, this exception
    ///                                  might be raised if the DOM implementation doesn't
    ///                                  support the removal of the DocumentType child or the
    ///                                  Element child.
    /// ```
    fn remove_child(&mut self, mut old_child: NodeRef) -> Result<NodeRef, DOMException> {
        check_no_modification_allowed_err(self)?;

        // NOT_FOUND_ERR: Raised if oldChild is not a child of this node.
        if old_child
            .parent_node()
            .is_none_or(|par| !self.is_same_node(&par))
        {
            return Err(DOMException::NotFoundErr);
        }

        old_child.disconnect_parent_and_sibling();
        old_child.unset_read_only();
        Ok(old_child)
    }

    /// Implementation of [`appendChild`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-184E7107) method.
    ///
    /// # Specification
    /// ```text
    /// Adds the node newChild to the end of the list of children of this node.
    /// If the newChild is already in the tree, it is first removed.
    ///
    /// Parameters
    ///     newChild of type Node
    ///         The node to add.
    ///         If it is a DocumentFragment object, the entire contents of
    ///         the document fragment are moved into the child list of this node
    ///
    /// Return Value
    ///     Node The node added.
    ///
    /// Exceptions
    ///     DOMException
    ///     HIERARCHY_REQUEST_ERR:       Raised if this node is of a type that does not allow
    ///                                  children of the type of the newChild node, or if the
    ///                                  node to append is one of this node's ancestors or
    ///                                  this node itself, or if this node is of type Document
    ///                                  and the DOM application attempts to append a second
    ///                                  DocumentType or Element node.
    ///     WRONG_DOCUMENT_ERR:          Raised if newChild was created from a different
    ///                                  document than the one that created this node.
    ///     NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or if the previous
    ///                                  parent of the node being inserted is readonly.
    ///     NOT_SUPPORTED_ERR:           if the newChild node is a child of the Document node,
    ///                                  this exception might be raised if the DOM
    ///                                  implementation doesn't support the removal of the
    ///                                  DocumentType child or Element child.
    /// ```
    fn append_child(&mut self, new_child: NodeRef) -> Result<NodeRef, DOMException> {
        self.insert_before(new_child, None)
    }

    /// Implementation of [`hasChildNodes`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-810594187) method.
    ///
    /// # Specification
    /// ```text
    /// Returns whether this node has any children.
    ///
    /// Return Value
    ///     boolean Returns true if this node has any children, false otherwise.
    ///
    /// No Parameters
    /// No Exceptions
    /// ```
    fn has_child_nodes(&self) -> bool {
        self.first_child().is_some()
    }

    /// Implementation of [`cloneNode`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-3A0ED0A4) method.
    ///
    /// # Specification
    /// ```text
    /// Returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes.
    /// The duplicate node has no parent (parentNode is null) and no user data.
    /// 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.
    /// Cloning an Element copies all attributes and their values,
    /// including those generated by the XML processor to represent defaulted attributes,
    /// but this method does not copy any children it contains unless it is a deep clone.
    /// This includes text contained in an the Element since the text is contained
    /// in a child Text node. Cloning an Attr directly, as opposed to be cloned as part of
    /// an Element cloning operation, returns a specified attribute (specified is true).
    /// Cloning an Attr always clones its children, since they represent its value,
    /// no matter whether this is a deep clone or not.
    /// Cloning an EntityReference automatically constructs its subtree
    /// if a corresponding Entity is available, no matter whether this is a deep clone or not.
    /// Cloning any other type of node simply returns a copy of this node.
    /// Note that cloning an immutable subtree results in a mutable copy,
    /// but the children of an EntityReference clone are readonly.
    /// In addition, clones of unspecified Attr nodes are specified.
    /// And, cloning Document, DocumentType, Entity, and Notation nodes is implementation dependent.
    ///
    /// Parameters
    ///     deep of type boolean
    ///         If true, recursively clone the subtree under the specified node;
    ///         if false, clone only the node itself (and its attributes, if it is an Element).
    ///
    /// Return Value
    ///     Node The duplicate node.
    ///
    /// No Exceptions
    /// ```
    fn clone_node(&self, deep: bool) -> NodeRef;

    /// Implementation of [`normalize`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-normalize) method.
    ///
    /// # Specification
    /// ```text
    /// Puts all Text nodes in the full depth of the sub-tree underneath this Node, including
    /// attribute nodes, into a "normal" form where only structure (e.g., elements, comments,
    /// processing instructions, CDATA sections, and entity references) separates Text nodes,
    /// i.e., there are neither adjacent Text nodes nor empty Text nodes. This can be used to
    /// ensure that the DOM view of a document is the same as if it were saved and re-loaded,
    /// and is useful when operations (such as XPointer [XPointer] lookups) that depend on a
    /// particular document tree structure are to be used. If the parameter
    /// "normalize-characters" of the DOMConfiguration object attached to the
    /// Node.ownerDocument is true, this method will also fully normalize the characters of
    /// the Text nodes.
    ///
    /// Note: In cases where the document contains CDATASections, the normalize operation
    /// alone may not be sufficient, since XPointers do not differentiate between Text nodes
    /// and CDATASection nodes.
    ///
    /// No Parameters
    /// No Return Value
    /// No Exceptions
    /// ```
    fn normalize(&mut self) {
        let mut children = self.first_child();
        while let Some(child) = children {
            match child {
                NodeRef::Text(mut text) => {
                    children = text.next_sibling();
                    while let Some(next) = children {
                        match next {
                            NodeRef::Text(next) => {
                                text.append_data(next.data().as_str()).ok();
                                self.remove_child(next.into()).ok();
                                children = text.next_sibling();
                            }
                            next => {
                                children = Some(next);
                                break;
                            }
                        }
                    }
                    if text.length() == 0 {
                        self.remove_child(text.into()).ok();
                    }
                }
                NodeRef::Element(mut elem) => {
                    elem.normalize();
                    children = elem.next_sibling();
                }
                child => children = child.next_sibling(),
            }
        }

        if let Some(attrs) = self.attributes() {
            for i in 0..attrs.length() {
                let mut attr = attrs.item(i).unwrap();
                attr.normalize();
            }
        }
    }

    /// Implementation of [`isSupported`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Level-2-Core-Node-supports) attribute.
    ///
    /// # Specification
    /// ```text
    /// Tests whether the DOM implementation implements a specific feature and that feature
    /// is supported by this node, as specified in DOM Features.
    ///
    /// Parameters
    ///     feature of type DOMString
    ///         The name of the feature to test.
    ///     version of type DOMString
    ///         This is the version number of the feature to test.
    ///
    /// Return Value
    ///     boolean Returns true if the specified feature is supported on this node,
    ///             false otherwise.
    ///
    /// No Exceptions
    /// ```
    fn is_supported(&self, feature: &str, version: Option<&str>) -> bool {
        self.owner_document()
            .is_some_and(|doc| doc.implementation().has_feature(feature, version))
    }

    /// Implementation of [`namespaceURI`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-NodeNSname) attribute.
    ///
    /// # Specification
    /// ```text
    /// namespaceURI of type DOMString, readonly, introduced in DOM Level 2
    ///     The namespace URI of this node, or null if it is unspecified (see XML Namespaces).
    ///     This is not a computed value that is the result of a namespace lookup
    ///     based on an examination of the namespace declarations in scope.
    ///     It is merely the namespace URI given at creation time.
    ///     For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and
    ///     nodes created with a DOM Level 1 method, such as Document.createElement(),
    ///     this is always null.
    ///
    ///     Note: Per the Namespaces in XML Specification [XML Namespaces] an attribute
    ///     does not inherit its namespace from the element it is attached to.
    ///     If an attribute is not explicitly given a namespace, it simply has no namespace.
    /// ```
    fn namespace_uri(&self) -> Option<Rc<str>> {
        None
    }

    /// Implementation of [`prefix`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-NodeNSPrefix) attribute.
    ///
    /// # Specification
    /// ```text
    /// prefix of type DOMString, introduced in DOM Level 2
    ///     The namespace prefix of this node, or null if it is unspecified. When it is
    ///     defined to be null, setting it has no effect, including if the node is read-only.
    ///     Note that setting this attribute, when permitted, changes the nodeName
    ///     attribute, which holds the qualified name, as well as the tagName and name
    ///     attributes of the Element and Attr interfaces, when applicable.
    ///     Setting the prefix to null makes it unspecified, setting it to an empty string
    ///     is implementation dependent.
    ///     Note also that changing the prefix of an attribute that is known to have
    ///     a default value, does not make a new attribute with the default value and the
    ///     original prefix appear, since the namespaceURI and localName do not change.
    ///     For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes
    ///     created with a DOM Level 1 method, such as createElement from the Document
    ///     interface, this is always null.
    /// ```
    fn prefix(&self) -> Option<Rc<str>> {
        None
    }
    /// Implementation of [`prefix`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-NodeNSPrefix) attribute.
    ///
    /// # Specification
    /// ```text
    /// prefix of type DOMString, introduced in DOM Level 2
    ///     The namespace prefix of this node, or null if it is unspecified. When it is
    ///     defined to be null, setting it has no effect, including if the node is read-only.
    ///     Note that setting this attribute, when permitted, changes the nodeName
    ///     attribute, which holds the qualified name, as well as the tagName and name
    ///     attributes of the Element and Attr interfaces, when applicable.
    ///     Setting the prefix to null makes it unspecified, setting it to an empty string
    ///     is implementation dependent.
    ///     Note also that changing the prefix of an attribute that is known to have
    ///     a default value, does not make a new attribute with the default value and the
    ///     original prefix appear, since the namespaceURI and localName do not change.
    ///     For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes
    ///     created with a DOM Level 1 method, such as createElement from the Document
    ///     interface, this is always null.
    ///
    ///     Exceptions on setting
    ///         DOMException
    ///         INVALID_CHARACTER_ERR:       Raised if the specified prefix contains an
    ///                                      illegal character according to the XML version
    ///                                      in use specified in the Document.xmlVersion
    ///                                      attribute.
    ///         NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
    ///         NAMESPACE_ERR:               Raised if the specified prefix is malformed
    ///                                      per the Namespaces in XML specification, if
    ///                                      the namespaceURI of this node is null, if the
    ///                                      specified prefix is "xml" and the namespaceURI
    ///                                      of this node is different from
    ///                                      "http://www.w3.org/XML/1998/namespace", if this
    ///                                      node is an attribute and the specified prefix
    ///                                      is "xmlns" and the namespaceURI of this node is
    ///                                      different from "http://www.w3.org/2000/xmlns/",
    ///                                      or if this node is an attribute and the
    ///                                      qualifiedName of this node is "xmlns"
    ///                                      [XML Namespaces].
    /// ```
    fn set_prefix(&mut self, _prefix: Option<impl Into<Rc<str>>>) -> Result<(), DOMException> {
        Ok(())
    }

    /// Implementation of [`localName`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-NodeNSLocalN) attribute.
    ///
    /// # Specification
    /// ```text
    /// localName of type DOMString, readonly, introduced in DOM Level 2
    ///     Returns the local part of the qualified name of this node.
    ///     For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes
    ///     created with a DOM Level 1 method, such as Document.createElement(), this is
    ///     always null.
    /// ```
    fn local_name(&self) -> Option<Rc<str>> {
        None
    }

    /// Implementation of [`hasAttributes`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-NodeHasAttrs) method.
    ///
    /// # Specification
    /// ```text
    /// Returns whether this node (if it is an element) has any attributes.
    ///
    /// Return Value
    ///     boolean Returns true if this node has any attributes, false otherwise.
    /// ```
    fn has_attributes(&self) -> bool {
        self.attributes().is_some_and(|attr| !attr.is_empty())
    }

    /// Implementation of [`baseURI`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-baseURI) attribute.
    ///
    /// # Specification
    /// ```text
    /// baseURI of type DOMString, readonly, introduced in DOM Level 3
    ///     The absolute base URI of this node or null if the implementation wasn't able
    ///     to obtain an absolute URI. This value is computed as described in Base URIs.
    ///     However, when the Document supports the feature "HTML" [DOM Level 2 HTML],
    ///     the base URI is computed using first the value of the href attribute
    ///     of the HTML BASE element if any, and the value of the documentURI attribute
    ///     from the Document interface otherwise.
    /// ```
    fn base_uri(&self) -> Option<String> {
        if let Some(doc) = self.owner_document().filter(|doc| doc.is_html()) {
            // when the Document supports the feature "HTML" [DOM Level 2 HTML],
            // the base URI is computed using first the value of the href attribute
            // of the HTML BASE element if any, and the value of the documentURI attribute
            // from the Document interface otherwise.
            let mut children = doc.first_child();
            while let Some(child) = children {
                children = child.next_sibling();
                if let NodeRef::Element(elem) = child {
                    let name = elem.node_name();
                    if name.eq_ignore_ascii_case("html") || name.eq_ignore_ascii_case("head") {
                        children = elem.first_child();
                        continue;
                    } else if name.eq_ignore_ascii_case("base") {
                        return elem
                            .get_attribute_node("href")
                            .and_then(|attr| attr.text_content());
                    }
                }
            }
            return None;
        }

        let mut bases = vec![];
        if let Some(base) = self
            .attributes()
            .and_then(|attrs| {
                attrs
                    .get_named_item_ns(Some(XML_XML_NAMESPACE), "base")
                    .ok()
                    .flatten()
            })
            .and_then(|attr| attr.node_value())
        {
            let base = base.to_string();
            if XmlURI::parse(&base).is_some_and(|base| base.scheme.is_some()) {
                return Some(base);
            }
            bases.push(base);
        }
        let mut parents = self.parent_node();
        while let Some(parent) = parents {
            parents = parent.parent_node();

            if let NodeRef::Element(elem) = parent {
                if let Some(attr) = elem
                    .get_attribute_ns(Some(XML_XML_NAMESPACE), "base")
                    .ok()
                    .filter(|base| !base.is_empty())
                {
                    if XmlURI::parse(&attr).is_some_and(|base| base.scheme.is_some()) {
                        return bases
                            .into_iter()
                            .rev()
                            .try_fold(attr, |base, uri| build_uri(&uri, &base));
                    }
                    bases.push(attr);
                }
            }
        }

        if let Some(url) = self.owner_document().and_then(|doc| doc.document_uri()) {
            bases.push(url.to_string());
        }

        let base = bases.pop()?;
        bases
            .into_iter()
            .rev()
            .try_fold(base, |base, uri| build_uri(&uri, &base))
    }

    /// Implementation of [`compareDocumentPosition`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-compareDocumentPosition) method.
    ///
    /// # Specification
    /// ```text
    /// Compares the reference node, i.e. the node on which this method is being called,
    /// with a node, i.e. the one passed as a parameter, with regard to their position
    /// in the document and according to the document order.
    ///
    /// Parameters
    ///     other of type Node
    ///         The node to compare against the reference node.
    ///
    /// Return Value
    ///     unsigned short Returns how the node is positioned relatively to the reference
    ///                    node.
    ///
    /// Exceptions
    ///     DOMException
    ///     NOT_SUPPORTED_ERR: when the compared nodes are from different DOM implementations
    ///                        that do not coordinate to return consistent
    ///                        implementation-specific results.
    /// ```
    fn compare_document_position(&self, other: &NodeRef) -> DocumentPosition {
        // reference:
        // https://github.com/apache/xerces2-j/blob/trunk/src/org/apache/xerces/dom/NodeImpl.java

        // If the nodes are the same, no flags should be set
        if self.is_same_node(other) {
            return DocumentPosition::new();
        }

        // If from different documents, we know they are disconnected.
        if !check_owner_document_sameness(self, other) {
            return DocumentPosition::new().set_flag(DOCUMENT_POSITION_DISCONNECTED);
        }

        // get the respective Document owners.
        let this_owner_doc = if let NodeRef::Document(doc) = self.clone().into() {
            doc
        } else {
            self.owner_document().expect("Internal Error")
        };
        let other_owner_doc = if let NodeRef::Document(doc) = other {
            doc.clone()
        } else {
            other.owner_document().expect("Internal Erorr")
        };

        // Find the ancestor of each node, and the distance each node is from
        // its ancestor.
        // During this traversal, look for ancestor/descendent relationships
        // between the 2 nodes in question.
        // We do this now, so that we get this info correct for attribute nodes
        // and their children.

        let mut this_ancestor: NodeRef = self.clone().into();
        let mut this_depth = 0;
        while let Some(node) = this_ancestor.parent_node() {
            this_depth += 1;
            if this_ancestor.is_same_node(other) {
                // The other node is an ancestor of this one.
                return DocumentPosition::new()
                    .set_flag(DOCUMENT_POSITION_CONTAINS)
                    .set_flag(DOCUMENT_POSITION_PRECEDING);
            }
            this_ancestor = node;
        }

        let mut other_ancestor = other.clone();
        let mut other_depth = 0;
        while let Some(node) = other_ancestor.parent_node() {
            other_depth += 1;
            if self.is_same_node(&node) {
                // The other node is a descendent of the reference node.
                return DocumentPosition::new()
                    .set_flag(DOCUMENT_POSITION_CONTAINED_BY)
                    .set_flag(DOCUMENT_POSITION_FOLLOWING);
            }
            other_ancestor = node;
        }

        let mut this_node: NodeRef = self.clone().into();
        let mut other_node = other.clone();
        if let Some(doctype) = this_owner_doc.doctype() {
            match doctype.compare_decl_position(&this_node, &other_node) {
                Ok(cmp) => {
                    return match cmp {
                        std::cmp::Ordering::Less => {
                            DocumentPosition::new().set_flag(DOCUMENT_POSITION_FOLLOWING)
                        }
                        std::cmp::Ordering::Equal => DocumentPosition::new(),
                        std::cmp::Ordering::Greater => {
                            DocumentPosition::new().set_flag(DOCUMENT_POSITION_PRECEDING)
                        }
                    };
                }
                Err((Some(_), None)) => {
                    this_depth = doctype.parent_node().is_some() as i32;
                    this_node = doctype.into();
                    this_ancestor = this_owner_doc.clone().into();
                }
                Err((None, Some(_))) => {
                    other_depth = doctype.parent_node().is_some() as i32;
                    other_node = doctype.into();
                    other_ancestor = other_owner_doc.clone().into();
                }
                _ => {}
            }
        }
        let this_ancestor_type = this_ancestor.node_type();
        let other_ancestor_type = other_ancestor.node_type();

        // Special casing for ENTITY, NOTATION, DOCTYPE and ATTRIBUTES
        // LM:  should rewrite this.
        match this_ancestor_type {
            NodeType::Notation | NodeType::Entity => {
                let container = this_owner_doc.doctype();
                if container
                    .as_ref()
                    .is_some_and(|doctype| doctype.is_same_node(&other_ancestor))
                {
                    return DocumentPosition::new()
                        .set_flag(DOCUMENT_POSITION_CONTAINS)
                        .set_flag(DOCUMENT_POSITION_PRECEDING);
                }
                match other_ancestor_type {
                    NodeType::Notation | NodeType::Entity => {
                        // If neither `self` nor `other` is a Notation or Entity
                        // but the positional relationship cannot be determined,
                        // they should not belong to the same tree.
                        return DocumentPosition::new().set_flag(DOCUMENT_POSITION_DISCONNECTED);
                    }
                    _ => {}
                }
                this_node = this_owner_doc.clone().into();
                this_ancestor = this_owner_doc.clone().into();
            }
            NodeType::DocumentType => {
                return if this_owner_doc.is_same_node(&other_node) {
                    DocumentPosition::new()
                        .set_flag(DOCUMENT_POSITION_PRECEDING)
                        .set_flag(DOCUMENT_POSITION_CONTAINS)
                } else {
                    DocumentPosition::new().set_flag(DOCUMENT_POSITION_FOLLOWING)
                };
            }
            NodeType::Attribute => {
                let attr = this_ancestor.as_attribute().unwrap();
                let Some(elem) = attr.owner_element() else {
                    let mut ancestor = other.parent_node();
                    while let Some(par) = ancestor {
                        ancestor = par.parent_node();
                        if attr.is_same_node(&par) {
                            return DocumentPosition::new()
                                .set_flag(DOCUMENT_POSITION_FOLLOWING)
                                .set_flag(DOCUMENT_POSITION_CONTAINED_BY);
                        }
                    }
                    return DocumentPosition::new().set_flag(DOCUMENT_POSITION_DISCONNECTED);
                };
                if let NodeRef::Attribute(oattr) = &other_ancestor {
                    let Some(oelem) = oattr.owner_element() else {
                        return DocumentPosition::new().set_flag(DOCUMENT_POSITION_DISCONNECTED);
                    };
                    if oelem.is_same_node(&NodeRef::Element(elem.clone())) {
                        let attrs = elem.attributes();
                        return if attrs.index_of(&attr) < attrs.index_of(oattr) {
                            DocumentPosition::new().set_flag(DOCUMENT_POSITION_FOLLOWING)
                        } else {
                            DocumentPosition::new().set_flag(DOCUMENT_POSITION_PRECEDING)
                        };
                    }
                }

                // Now, find the ancestor of the element
                this_depth = 0;
                this_node = NodeRef::Element(elem);
                this_ancestor = this_node.clone();
                while let Some(node) = this_ancestor.parent_node() {
                    this_depth += 1;
                    if this_node.is_same_node(&other_node) {
                        // The other node is an ancestor of the owning element
                        return DocumentPosition::new()
                            .set_flag(DOCUMENT_POSITION_CONTAINS)
                            .set_flag(DOCUMENT_POSITION_PRECEDING);
                    }
                    this_ancestor = node;
                }
            }
            _ => {}
        }
        match other_ancestor_type {
            NodeType::Notation | NodeType::Entity => {
                let container = this_owner_doc.doctype();
                if container.is_some_and(|doctype| self.is_same_node(&doctype.into())) {
                    return DocumentPosition::new()
                        .set_flag(DOCUMENT_POSITION_CONTAINED_BY)
                        .set_flag(DOCUMENT_POSITION_FOLLOWING);
                }
                other_node = this_owner_doc.clone().into();
                other_ancestor = this_owner_doc.clone().into();
            }
            NodeType::DocumentType => {
                return if other_owner_doc.is_same_node(&this_node) {
                    DocumentPosition::new()
                        .set_flag(DOCUMENT_POSITION_FOLLOWING)
                        .set_flag(DOCUMENT_POSITION_CONTAINED_BY)
                } else {
                    DocumentPosition::new().set_flag(DOCUMENT_POSITION_PRECEDING)
                };
            }
            NodeType::Attribute => {
                other_depth = 0;
                let NodeRef::Attribute(oattr) = &other_ancestor else {
                    unimplemented!()
                };
                let Some(oelem) = oattr.owner_element() else {
                    return DocumentPosition::new().set_flag(DOCUMENT_POSITION_DISCONNECTED);
                };
                other_node = NodeRef::Element(oelem);
                other_ancestor = other_node.clone();
                while let Some(node) = other_ancestor.parent_node() {
                    other_depth += 1;
                    if other_ancestor.is_same_node(&this_node) {
                        // The other node is a descendent of the reference
                        // node's element
                        return DocumentPosition::new()
                            .set_flag(DOCUMENT_POSITION_FOLLOWING)
                            .set_flag(DOCUMENT_POSITION_CONTAINED_BY);
                    }
                    other_ancestor = node;
                }
            }
            _ => {}
        }

        // thisAncestor and otherAncestor must be the same at this point,
        // otherwise, the original nodes are disconnected
        if !this_ancestor.is_same_node(&other_ancestor) {
            return DocumentPosition::new().set_flag(DOCUMENT_POSITION_DISCONNECTED);
        }

        // Go up the parent chain of the deeper node, until we find a node
        // with the same depth as the shallower node

        if this_depth > other_depth {
            for _ in 0..this_depth - other_depth {
                this_node = this_node.parent_node().unwrap();
            }
            // Check if the node we have reached is in fact "otherNode". This can
            // happen in the case of attributes.  In this case, otherNode
            // "precedes" this.
            if this_node.is_same_node(&other_node) {
                return DocumentPosition::new().set_flag(DOCUMENT_POSITION_PRECEDING);
            }
        } else {
            for _ in 0..other_depth - this_depth {
                other_node = other_node.parent_node().unwrap();
            }
            // Check if the node we have reached is in fact "thisNode".  This can
            // happen in the case of attributes.  In this case, otherNode
            // "follows" this.
            if other_node.is_same_node(&this_node) {
                return DocumentPosition::new().set_flag(DOCUMENT_POSITION_FOLLOWING);
            }
        }

        // We now have nodes at the same depth in the tree.  Find a common
        // ancestor.
        while let Some((tp, op)) = this_node
            .parent_node()
            .zip(other_node.parent_node())
            .filter(|(t, p)| !t.is_same_node(p))
        {
            this_node = tp;
            other_node = op;
        }

        let parent = this_node.parent_node().unwrap();
        // At this point, thisNode and otherNode are direct children of
        // the common ancestor.
        // See whether thisNode or otherNode is the leftmost
        let mut children = parent.first_child();
        while let Some(child) = children {
            children = child.next_sibling();
            if child.is_same_node(&this_node) {
                return DocumentPosition::new().set_flag(DOCUMENT_POSITION_FOLLOWING);
            } else if child.is_same_node(&other_node) {
                return DocumentPosition::new().set_flag(DOCUMENT_POSITION_PRECEDING);
            }
        }
        // REVISIT:  shouldn't get here.   Should probably throw an  exception
        unreachable!()
    }

    /// Implementation of [`textContent`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-textContent) attribute.
    ///
    /// # Specification
    /// ```text
    /// textContent of type DOMString, introduced in DOM Level 3
    ///     This attribute returns the text content of this node and its descendants.
    ///     When it is defined to be null, setting it has no effect. On setting, any
    ///     possible children this node may have are removed and, if it the new string is
    ///     not empty or null, replaced by a single Text node containing the string this
    ///     attribute is set to.
    ///     On getting, no serialization is performed, the returned string does not contain
    ///     any markup. No whitespace normalization is performed and the returned string
    ///     does not contain the white spaces in element content (see the attribute
    ///     Text.isElementContentWhitespace). Similarly, on setting, no parsing is performed
    ///     either, the input string is taken as pure textual content.
    ///     The string returned is made of the text content of this node depending on its
    ///     type, as defined below:
    ///
    ///     <omitted>
    ///
    ///     Exceptions on retrieval
    ///         DOMException
    ///         DOMSTRING_SIZE_ERR: Raised when it would return more characters than fit
    ///                             in a DOMString variable on the implementation platform.
    /// ```
    fn text_content(&self) -> Option<String> {
        use NodeRef::*;

        let mut children = self.first_child();
        let mut res = String::new();
        while let Some(child) = children {
            children = child.next_sibling();

            match child {
                Comment(_) | ProcessingInstruction(_) => {}
                child => {
                    if let Some(value) = child.text_content().filter(|text| !text.is_empty()) {
                        res.push_str(&value);
                    }
                }
            }
        }
        Some(res)
    }

    /// Implementation of [`textContent`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-textContent) attribute.
    ///
    /// # Specification
    /// ```text
    /// textContent of type DOMString, introduced in DOM Level 3
    ///     This attribute returns the text content of this node and its descendants.
    ///     When it is defined to be null, setting it has no effect. On setting, any
    ///     possible children this node may have are removed and, if it the new string is
    ///     not empty or null, replaced by a single Text node containing the string this
    ///     attribute is set to.
    ///     On getting, no serialization is performed, the returned string does not contain
    ///     any markup. No whitespace normalization is performed and the returned string
    ///     does not contain the white spaces in element content (see the attribute
    ///     Text.isElementContentWhitespace). Similarly, on setting, no parsing is performed
    ///     either, the input string is taken as pure textual content.
    ///     The string returned is made of the text content of this node depending on its
    ///     type, as defined below:
    ///
    ///     <omitted>
    ///
    ///     Exceptions on setting
    ///         DOMException
    ///         NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.
    /// ```
    fn set_text_content(&mut self, text: impl Into<String>) -> Result<(), DOMException> {
        check_no_modification_allowed_err(self)?;

        let mut children = self.first_child();
        while let Some(child) = children {
            children = child.next_sibling();
            self.remove_child(child).ok();
        }

        let doc = self.owner_document().expect("Internal Error");
        let text = doc.create_text_node(text);
        self.append_child(text.into()).ok();
        Ok(())
    }

    /// Implementation of [`isSameNode`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-isSameNode) method.
    ///
    /// # Specification
    /// ```text
    /// Returns whether this node is the same node as the given one.
    /// This method provides a way to determine whether two Node references returned by the
    /// implementation reference the same object. When two Node references are references to
    /// the same object, even if through a proxy, the references may be used completely
    /// interchangeably, such that all attributes have the same values and calling the same
    /// DOM method on either reference always has exactly the same effect.
    ///
    /// Parameters
    ///     other of type Node
    ///         The node to test against.
    ///
    /// Return Value
    ///     boolean Returns true if the nodes are the same, false otherwise.
    ///
    /// No Exceptions
    /// ```
    fn is_same_node(&self, other: &NodeRef) -> bool;

    /// Implementation of [`lookupPrefix`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-lookupNamespacePrefix) method.
    ///
    /// The implementation was based on
    /// [Appendix B.2 Namespace Prefix Lookup](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#namespaces-algorithms-lookupNamespacePrefixAlgo).
    ///
    /// # Specification
    /// ```text
    /// Look up the prefix associated to the given namespace URI, starting from this node.
    /// The default namespace declarations are ignored by this method.
    /// See Namespace Prefix Lookup for details on the algorithm used by this method.
    ///
    /// Parameters
    ///     namespaceURI of type DOMString
    ///         The namespace URI to look for.
    ///
    /// Return Value
    ///     DOMString Returns an associated namespace prefix if found or null if none is
    ///               found. If more than one prefix are associated to the namespace prefix,
    ///               the returned namespace prefix is implementation dependent.
    ///
    /// No Exceptions
    /// ```
    fn lookup_prefix(&self, namespace_uri: &str) -> Option<Rc<str>> {
        let mut ancestor = self.parent_node();
        while let Some(par) = ancestor {
            ancestor = par.parent_node();
            if let NodeRef::Element(elem) = par {
                return elem.lookup_prefix(namespace_uri);
            }
        }
        None
    }

    /// Implementation of [`isDefaultNamespace`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-isDefaultNamespace) method.
    ///
    /// The implementation was based on
    /// [Appendix B.3 Default Namespace Lookup](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#namespaces-algorithms-isDefaultNamespaceAlgo).
    ///
    /// # Specification
    /// ```text
    /// This method checks if the specified namespaceURI is the default namespace or not.
    ///
    /// Parameters
    ///     namespaceURI of type DOMString
    ///         The namespace URI to look for.
    ///
    /// Return Value
    ///     boolean Returns true if the specified namespaceURI is the default namespace,
    ///             false otherwise.
    ///
    /// No Exceptions
    /// ```
    fn is_default_namespace(&self, namespace_uri: &str) -> bool {
        let mut ancestor = self.parent_node();
        while let Some(par) = ancestor {
            ancestor = par.parent_node();
            if let NodeRef::Element(elem) = par {
                return elem.is_default_namespace(namespace_uri);
            }
        }
        false
    }

    /// Implementation of [`lookupNamespaceURI`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-lookupNamespaceURI) method.
    ///
    /// The implementation was based on
    /// [Appendix B.4 Namespace URI Lookup](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#namespaces-algorithms-lookupNamespaceURIAlgo).
    ///
    /// # Specification
    /// ```text
    /// Look up the namespace URI associated to the given prefix, starting from this node.
    /// See Namespace URI Lookup for details on the algorithm used by this method.
    ///
    /// Parameters
    ///     prefix of type DOMString
    ///         The prefix to look for. If this parameter is null, the method will return
    ///         the default namespace URI if any.
    ///
    /// Return Value
    ///     DOMString Returns the associated namespace URI or null if none is found.
    ///
    /// No Exceptions
    /// ```
    fn lookup_namespace_uri(&self, prefix: Option<&str>) -> Option<Rc<str>> {
        self.parent_node()?.lookup_namespace_uri(prefix)
    }

    /// Implementation of [`isEqualNode`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-isEqualNode) method.
    ///
    /// # Note
    /// Normalization can affect equality, but this method does not normalize nodes.\
    /// If need, the caller should normalize nodes before calling this method.
    ///
    /// # Specification
    /// ```text
    /// Tests whether two nodes are equal.
    /// This method tests for equality of nodes, not sameness
    /// (i.e., whether the two nodes are references to the same object)
    /// which can be tested with Node.isSameNode().
    /// All nodes that are the same will also be equal, though the reverse may not be true.
    /// Two nodes are equal if and only if the following conditions are satisfied:
    ///
    /// - The two nodes are of the same type.
    /// - The following string attributes are equal: nodeName, localName, namespaceURI,
    ///   prefix, nodeValue. This is: they are both null, or they have the same length
    ///   and are character for character identical.
    /// - The attributes NamedNodeMaps are equal.
    ///   This is: they are both null, or they have the same length and for each node
    ///   that exists in one map there is a node that exists in the other map and is equal,
    ///   although not necessarily at the same index.
    /// - The childNodes NodeLists are equal.
    ///   This is: they are both null, or they have the same length and contain equal nodes
    ///   at the same index. Note that normalization can affect equality;
    ///   to avoid this, nodes should be normalized before being compared.
    ///
    /// For two DocumentType nodes to be equal, the following conditions must also be satisfied:
    ///
    /// - The following string attributes are equal: publicId, systemId, internalSubset.
    /// - The entities NamedNodeMaps are equal.
    /// - The notations NamedNodeMaps are equal.
    ///
    /// On the other hand, the following do not affect equality: the ownerDocument,
    /// baseURI, and parentNode attributes, the specified attribute for Attr nodes,
    /// the schemaTypeInfo attribute for Attr and Element nodes,
    /// the Text.isElementContentWhitespace attribute for Text nodes, as well as any user data
    /// or event listeners registered on the nodes.
    ///
    /// Note: As a general rule, anything not mentioned in the description above
    ///       is not significant in consideration of equality checking.
    ///       Note that future versions of this specification may take into account
    ///       more attributes and implementations conform to this specification are expected
    ///       to be updated accordingly.
    ///
    /// Parameters
    ///     arg of type Node
    ///         The node to compare equality with.
    ///
    /// Return Value
    ///     boolean Returns true if the nodes are equal, false otherwise.
    /// ```
    fn is_equal_node(&self, arg: &NodeRef) -> bool {
        if self.is_same_node(arg) {
            return true;
        }
        if self.node_type() != arg.node_type()
            || self.node_name() != arg.node_name()
            || self.local_name() != arg.local_name()
            || self.namespace_uri() != arg.namespace_uri()
            || self.prefix() != arg.prefix()
            || self.node_value() != arg.node_value()
        {
            return false;
        }

        match (self.attributes(), arg.attributes()) {
            (Some(lattrs), Some(rattrs)) => {
                if lattrs.length() != rattrs.length() {
                    return false;
                }

                let mut r = vec![];
                for i in 0..rattrs.length() {
                    r.push(rattrs.item(i).unwrap());
                }
                // Simply scanning and deleting.
                // Not efficient, but probably sufficient.
                for i in 0..lattrs.length() {
                    let attr = NodeRef::Attribute(lattrs.item(i).unwrap());
                    let Some(pos) = r.iter().position(|r| r.is_equal_node(&attr)) else {
                        return false;
                    };
                    r.swap_remove(pos);
                }
            }
            (None, None) => {}
            _ => return false,
        }

        let mut lch = self.first_child();
        let mut rch = arg.first_child();
        while let Some((l, r)) = lch.as_ref().zip(rch.as_ref()) {
            if !l.is_equal_node(r) {
                return false;
            }
            lch = l.next_sibling();
            rch = r.next_sibling();
        }
        lch.is_some() == rch.is_some()
    }

    /// Implementation of [`getFeature`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-getFeature) method.
    ///
    /// # Note
    /// In this crate, this method simply clones and returns `self` if
    /// [`DOMImplementation::has_feature`](crate::dom::dom_implementation::DOMImplementation::has_feature)
    /// returns `true`.\
    /// Since cast possibilities are uniquely determined by the type implementation,
    /// there is basically no need to use this method.
    ///
    /// # Specification
    /// ```text
    /// This method returns a specialized object which implements the specialized APIs of the specified feature and version, as specified in DOM Features. The specialized object may also be obtained by using binding-specific casting methods but is not necessarily expected to, as discussed in Mixed DOM Implementations. This method also allow the implementation to provide specialized objects which do not support the Node interface.
    ///
    /// Parameters
    ///     feature of type DOMString
    ///         The name of the feature requested. Note that any plus sign "+" prepended to the name of the feature will be ignored since it is not significant in the context of this method.
    ///     version of type DOMString
    ///         This is the version number of the feature to test.
    ///
    /// Return Value
    ///     DOMObject Returns an object which implements the specialized APIs of the
    ///               specified feature and version, if any, or null if there is no
    ///               object which implements interfaces associated with that feature.
    ///               If the DOMObject returned by this method implements the Node
    ///               interface, it must delegate to the primary core Node and not return
    ///               results inconsistent with the primary core Node such as attributes,
    ///               childNodes, etc.
    ///
    /// No Exceptions
    /// ```
    fn get_feature(&self, feature: &str, version: Option<&str>) -> Option<Self> {
        self.is_supported(feature, version).then(|| self.clone())
    }

    /// Implementation of [`setUserData`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-setUserData) method.
    ///
    /// # Specification
    /// ```text
    /// Associate an object to a key on this node. The object can later be retrieved
    /// from this node by calling getUserData with the same key.
    ///
    /// Parameters
    ///     key of type DOMString
    ///         The key to associate the object to.
    ///     data of type DOMUserData
    ///         The object to associate to the given key, or null to remove any
    ///         existing association to that key.
    ///     handler of type UserDataHandler
    ///         The handler to associate to that key, or null.
    ///
    /// Return Value
    ///     DOMUserData Returns the DOMUserData previously associated to the given key
    ///                 on this node, or null if there was none.
    ///
    /// No Exceptions
    /// ```
    fn set_user_data(
        &mut self,
        key: impl Into<String>,
        data: DOMUserData,
        handler: Option<Arc<dyn UserDataHandler>>,
    ) -> Option<DOMUserData>;

    /// Implementation of [`getUserData`](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-getUserData) method.
    ///
    /// # Specification
    /// ```text
    /// Retrieves the object associated to a key on a this node. The object must first
    /// have been set to this node by calling setUserData with the same key.
    ///
    /// Parameters
    ///     key of type DOMString
    ///         The key the object is associated to.
    ///
    /// Return Value
    ///     DOMUserData Returns the DOMUserData associated to the given key on this node,
    ///                 or null if there was none.
    ///
    /// No Exceptions
    /// ```
    fn get_user_data(&self, key: &str) -> Option<DOMUserData>;

    /// Check if this node is a read-only node.
    ///
    /// This method is independent of the Document setting and always returns
    /// whether this node is read-only based on the DOM specification.
    fn is_read_only(&self) -> bool;
}

/// A set of operations that change the adjacency of nodes.
///
/// None of these methods are exposed to the user
/// because they may cause inconsistencies in the tree constraints.
///
/// These method does not check the tree constraints.  
/// It is the responsibility of the user to maintain tree constraints.
pub(super) trait NodeConnection: Clone + Into<NodeRef> {
    // The setter method (set_xxx) simply sets the given node
    // and is not required to check the node's type constraints.

    /// Set new parent node.  
    /// Return old parent node if exists.
    fn set_parent_node(&mut self, new_parent: Option<NodeRef>) -> Option<NodeRef>;
    /// Set new first child node.  
    /// Return old first child node if exists.
    fn set_first_child(&mut self, new_child: Option<NodeRef>) -> Option<NodeRef>;
    /// Set new last child node.  
    /// Return old last child node if exists.
    fn set_last_child(&mut self, new_child: Option<NodeRef>) -> Option<NodeRef>;
    /// Set new previous sibling node.  
    /// Return old previous sibling node if exists.
    fn set_previous_sibling(&mut self, new_sibling: Option<NodeRef>) -> Option<NodeRef>;
    /// Set new next sibling node.  
    /// Return old next sibling node if exists.
    fn set_next_sibling(&mut self, new_sibling: Option<NodeRef>) -> Option<NodeRef>;
    /// Set new owner Document node.  
    /// Return old owner Document node if exists.
    fn set_owner_document(&mut self, new_doc: DocumentRef) -> Option<DocumentRef>;

    /// Set this node as the read-only node.
    fn set_read_only(&mut self) {}
    /// Unset this node as the read-only node.
    fn unset_read_only(&mut self) {}

    /// Replace ownerDocument of self and all elements of the subtree.
    ///
    /// The ownerDocument of siblings and ancestors must not be changed.
    fn adopted_to(&mut self, new_doc: DocumentRef);

    /// Connect `self` to the front of `next`.
    ///
    /// If `next` has a previous sibling, it is connected to the front of `self`.  
    /// Otherwise, `self` is set as a first child of the parent of `next`.
    ///
    /// If `self` already has a parent or siblings, these will be disconnected from `self`.
    ///
    /// # Note
    /// If `next` does not have a parent,
    /// the constraint that the root node is unique is broken.
    fn connect_as_previous_sibling(&mut self, mut next: NodeRef) {
        let prev = next.set_previous_sibling(None);
        let par = next.parent_node();
        let slf: NodeRef = self.clone().into();
        self.disconnect_parent_and_sibling();
        // new parent
        self.set_parent_node(par.clone());
        // self <-> next
        next.set_previous_sibling(Some(slf.clone()));
        self.set_next_sibling(Some(next));
        if let Some(mut prev) = prev {
            // prev <-> self
            prev.set_next_sibling(Some(slf));
            self.set_previous_sibling(Some(prev));
        } else if let Some(mut par) = par {
            // If a previous sibling does not exists,
            // `self` becomes the first child of its parent.
            par.set_first_child(Some(slf));
        }
    }
    /// Connect `self` to the back of `prev`.
    ///
    /// If `prev` has a next sibling, it is connected to the back of `self`.  
    /// Otherwise, `self` is set as a last child of the parent of `prev`.
    ///
    /// If `self` already has a parent or siblings, these will be disconnected from `self`.
    ///
    /// # Note
    /// If `prev` does not have a parent,
    /// the constraint that the root node is unique is broken.
    fn connect_as_next_sibling(&mut self, mut prev: NodeRef) {
        let next = prev.set_next_sibling(None);
        let par = prev.parent_node();
        let slf: NodeRef = self.clone().into();
        self.disconnect_parent_and_sibling();
        // new parent
        self.set_parent_node(par.clone());
        // prev <-> self
        prev.set_next_sibling(Some(slf.clone()));
        self.set_previous_sibling(Some(prev));
        if let Some(mut next) = next {
            // self <-> next
            next.set_previous_sibling(Some(slf));
            self.set_next_sibling(Some(next));
        } else if let Some(mut par) = par {
            // If a next sibling does not exists,
            // `self` becomes the last child of its parent.
            par.set_last_child(Some(slf));
        }
    }
    /// Disconnect the parent node of `self`.  
    /// If `self` has siblings, it is also disconnected from its siblings,
    /// and the siblings before and after it establish adjacency.
    ///
    /// As a result, `self` is the root node of the detached subtree.
    ///
    /// Return old parent node if exists.
    fn disconnect_parent_and_sibling(&mut self) -> Option<NodeRef> {
        let mut par = self.set_parent_node(None);
        let prev = self.set_previous_sibling(None);
        let next = self.set_next_sibling(None);
        match (prev, next) {
            (Some(mut prev), Some(mut next)) => {
                prev.set_next_sibling(Some(next.clone()));
                next.set_previous_sibling(Some(prev));
            }
            (Some(mut prev), None) => {
                prev.set_next_sibling(None);
                if let Some(par) = par.as_mut() {
                    par.set_last_child(Some(prev));
                }
            }
            (None, Some(mut next)) => {
                next.set_previous_sibling(None);
                if let Some(par) = par.as_mut() {
                    par.set_first_child(Some(next));
                }
            }
            (None, None) => {
                if let Some(par) = par.as_mut() {
                    par.set_first_child(None);
                    par.set_last_child(None);
                }
            }
        }
        par
    }

    fn handle_user_data(&self, operation: OperationType, dst: Option<NodeRef>);
}

/// Implementation of [Node](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1950641247)
/// 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)
#[derive(Clone)]
pub enum NodeRef {
    Element(ElementRef),
    Attribute(AttrRef),
    Text(TextRef),
    CDATASection(CDATASectionRef),
    EntityReference(EntityReferenceRef),
    Entity(EntityRef),
    ProcessingInstruction(ProcessingInstructionRef),
    Comment(CommentRef),
    Document(DocumentRef),
    DocumentType(DocumentTypeRef),
    DocumentFragment(DocumentFragmentRef),
    Notation(NotationRef),
}

impl NodeRef {
    /// Generate [`NodeWeakRef`] from `self`.
    pub(super) fn downgrade(&self) -> NodeWeakRef {
        use NodeRef::*;
        match self {
            Element(node) => NodeWeakRef::Element(Rc::downgrade(&node.0)),
            Attribute(node) => NodeWeakRef::Attribute(Rc::downgrade(&node.0)),
            Text(node) => NodeWeakRef::Text(Rc::downgrade(&node.0)),
            CDATASection(node) => NodeWeakRef::CDATASection(Rc::downgrade(&node.0)),
            EntityReference(node) => NodeWeakRef::EntityReference(Rc::downgrade(&node.0)),
            Entity(node) => NodeWeakRef::Entity(Rc::downgrade(&node.0)),
            ProcessingInstruction(node) => {
                NodeWeakRef::ProcessingInstruction(Rc::downgrade(&node.0))
            }
            Comment(node) => NodeWeakRef::Comment(Rc::downgrade(&node.0)),
            Document(node) => NodeWeakRef::Document(Rc::downgrade(&node.0)),
            DocumentType(node) => NodeWeakRef::DocumentType(Rc::downgrade(&node.0)),
            DocumentFragment(node) => NodeWeakRef::DocumentFragment(Rc::downgrade(&node.0)),
            Notation(node) => NodeWeakRef::Notation(Rc::downgrade(&node.0)),
        }
    }
}

macro_rules! impl_node_trait_to_noderef {
    (
        $(
            fn $( ($mut:tt) )? $fn:ident($( $arg_name:ident : $arg_type:ty ),*) -> $ret:ty
        ),*
    ) => {
        impl Node for NodeRef {
            $(
                fn $fn(& $( $mut )? self, $( $arg_name: $arg_type),* ) -> $ret {
                    match self {
                        NodeRef::Element(elem) => <ElementRef as Node>::$fn(elem, $( $arg_name ),* ),
                        NodeRef::Attribute(attr) => attr.$fn( $( $arg_name ),* ),
                        NodeRef::Text(text) => text.$fn( $( $arg_name ),* ),
                        NodeRef::CDATASection(cdata) => cdata.$fn( $( $arg_name ),* ),
                        NodeRef::EntityReference(entref) => entref.$fn( $( $arg_name ),* ),
                        NodeRef::Entity(ent) => ent.$fn( $( $arg_name ),* ),
                        NodeRef::ProcessingInstruction(pi) => pi.$fn( $( $arg_name ),* ),
                        NodeRef::Comment(comment) => comment.$fn( $( $arg_name ),* ),
                        NodeRef::Document(doc) => doc.$fn( $( $arg_name ),* ),
                        NodeRef::DocumentType(doctype) => doctype.$fn( $( $arg_name ),* ),
                        NodeRef::DocumentFragment(frag) => frag.$fn( $( $arg_name ),* ),
                        NodeRef::Notation(nota) => nota.$fn( $( $arg_name ),* ),
                    }
                }
            )*
        }
    };
}

impl_node_trait_to_noderef! {
    fn node_name() -> Rc<str>,
    fn node_value() -> Option<Rc<str>>,
    fn(mut) set_node_value(value: impl Into<String>) -> Result<(), DOMException>,
    fn node_type() -> NodeType,
    fn parent_node() -> Option<NodeRef>,
    fn child_nodes() -> ChildNodesList<NodeRef>,
    fn first_child() -> Option<NodeRef>,
    fn last_child() -> Option<NodeRef>,
    fn previous_sibling() -> Option<NodeRef>,
    fn next_sibling() -> Option<NodeRef>,
    fn attributes() -> Option<AttributeMap>,
    fn owner_document() -> Option<DocumentRef>,
    fn(mut) insert_before(new_child: NodeRef, ref_child: Option<NodeRef>) -> Result<NodeRef, DOMException>,
    fn(mut) replace_child(new_child: NodeRef, old_child: NodeRef) -> Result<NodeRef, DOMException>,
    fn(mut) remove_child(old_child: NodeRef) -> Result<NodeRef, DOMException>,
    fn(mut) append_child(new_child: NodeRef) -> Result<NodeRef, DOMException>,
    fn has_child_nodes() -> bool,
    fn clone_node(deep: bool) -> NodeRef,
    fn(mut) normalize() -> (),
    fn namespace_uri() -> Option<Rc<str>>,
    fn prefix() -> Option<Rc<str>>,
    fn(mut) set_prefix(prefix: Option<impl Into<Rc<str>>>) -> Result<(), DOMException>,
    fn local_name() -> Option<Rc<str>>,
    fn has_attributes() -> bool,
    fn base_uri() -> Option<String>,
    fn compare_document_position(other: &NodeRef) -> DocumentPosition,
    fn text_content() -> Option<String>,
    fn is_same_node(other: &NodeRef) -> bool,
    fn lookup_prefix(ns_uri: &str) -> Option<Rc<str>>,
    fn is_default_namespace(ns_uri: &str) -> bool,
    fn lookup_namespace_uri(prefix: Option<&str>) -> Option<Rc<str>>,
    fn is_equal_node(arg: &NodeRef) -> bool,
    fn(mut) set_user_data(key: impl Into<String>, data: DOMUserData, handler: Option<Arc<dyn UserDataHandler>>) -> Option<DOMUserData>,
    fn get_user_data(key: &str) -> Option<DOMUserData>,
    fn is_read_only() -> bool
}

macro_rules! impl_node_connection_to_noderef {
    (
        $(
            fn $( ($mut:tt) )? $fn:ident($( $arg_name:ident : $arg_type:ty ),*) -> $ret:ty
        ),*
    ) => {
        impl NodeConnection for NodeRef {
            $(
                fn $fn(& $( $mut )? self, $( $arg_name: $arg_type),* ) -> $ret {
                    match self {
                        NodeRef::Element(elem) => elem.$fn( $( $arg_name ),* ),
                        NodeRef::Attribute(attr) => attr.$fn( $( $arg_name ),* ),
                        NodeRef::Text(text) => text.$fn( $( $arg_name ),* ),
                        NodeRef::CDATASection(cdata) => cdata.$fn( $( $arg_name ),* ),
                        NodeRef::EntityReference(entref) => entref.$fn( $( $arg_name ),* ),
                        NodeRef::Entity(ent) => ent.$fn( $( $arg_name ),* ),
                        NodeRef::ProcessingInstruction(pi) => pi.$fn( $( $arg_name ),* ),
                        NodeRef::Comment(comment) => comment.$fn( $( $arg_name ),* ),
                        NodeRef::Document(doc) => doc.$fn( $( $arg_name ),* ),
                        NodeRef::DocumentType(doctype) => doctype.$fn( $( $arg_name ),* ),
                        NodeRef::DocumentFragment(frag) => frag.$fn( $( $arg_name ),* ),
                        NodeRef::Notation(nota) => nota.$fn( $( $arg_name ),* ),
                    }
                }
            )*
        }
    };
}

impl_node_connection_to_noderef! {
    fn(mut) set_parent_node(new_parent: Option<NodeRef>) -> Option<NodeRef>,
    fn(mut) set_first_child(new_child: Option<NodeRef>) -> Option<NodeRef>,
    fn(mut) set_last_child(new_child: Option<NodeRef>) -> Option<NodeRef>,
    fn(mut) set_previous_sibling(new_sibling: Option<NodeRef>) -> Option<NodeRef>,
    fn(mut) set_next_sibling(new_sibling: Option<NodeRef>) -> Option<NodeRef>,
    fn(mut) set_owner_document(new_doc: DocumentRef) -> Option<DocumentRef>,
    fn(mut) set_read_only() -> (),
    fn(mut) unset_read_only() -> (),
    fn(mut) adopted_to(new_doc: DocumentRef) -> (),
    fn(mut) connect_as_previous_sibling(next: NodeRef) -> (),
    fn(mut) connect_as_next_sibling(prev: NodeRef) -> (),
    fn(mut) disconnect_parent_and_sibling() -> Option<NodeRef>,
    fn handle_user_data(operation: OperationType, dst: Option<NodeRef>) -> ()
}

macro_rules! impl_node_conversion {
    ( $( ( $fn:ident, $var:ident, $t:ty ) ),* ) => {
        impl NodeRef {
            $(
                pub fn $fn (&self) -> Option<$t> {
                    match self {
                        NodeRef:: $var (node) => Some(node.clone()),
                        _ => None
                    }
                }
            )*
        }
    };
}

impl_node_conversion! {
    ( as_element, Element, ElementRef ),
    ( as_attribute, Attribute, AttrRef ),
    ( as_text_node, Text, TextRef ),
    ( as_cdata_section, CDATASection, CDATASectionRef ),
    ( as_entity, Entity, EntityRef ),
    ( as_entity_reference, EntityReference, EntityReferenceRef ),
    ( as_processing_instruction, ProcessingInstruction, ProcessingInstructionRef ),
    ( as_comment, Comment, CommentRef ),
    ( as_document, Document, DocumentRef ),
    ( as_document_type, DocumentType, DocumentTypeRef ),
    ( as_document_fragment, DocumentFragment, DocumentFragmentRef ),
    ( as_notation, Notation, NotationRef )
}

/// Implementation of [Node](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1950641247)
/// 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)
#[derive(Clone)]
pub(super) enum NodeStrongRef {
    Element(Rc<RefCell<Element>>),
    Attribute(Rc<RefCell<Attr>>),
    Text(Rc<RefCell<Text>>),
    CDATASection(Rc<RefCell<CDATASection>>),
    EntityReference(Rc<RefCell<EntityReference>>),
    Entity(Rc<RefCell<Entity>>),
    ProcessingInstruction(Rc<RefCell<ProcessingInstruction>>),
    Comment(Rc<RefCell<Comment>>),
    Document(Rc<RefCell<Document>>),
    DocumentType(Rc<RefCell<DocumentType>>),
    DocumentFragment(Rc<RefCell<DocumentFragment>>),
    Notation(Rc<RefCell<Notation>>),
}

impl From<NodeStrongRef> for NodeRef {
    fn from(value: NodeStrongRef) -> Self {
        match value {
            NodeStrongRef::Element(node) => From::from(ElementRef::from(node)),
            NodeStrongRef::Attribute(node) => From::from(AttrRef::from(node)),
            NodeStrongRef::Text(node) => From::from(TextRef::from(node)),
            NodeStrongRef::CDATASection(node) => From::from(CDATASectionRef::from(node)),
            NodeStrongRef::EntityReference(node) => From::from(EntityReferenceRef::from(node)),
            NodeStrongRef::Entity(node) => From::from(EntityRef::from(node)),
            NodeStrongRef::ProcessingInstruction(node) => {
                From::from(ProcessingInstructionRef::from(node))
            }
            NodeStrongRef::Comment(node) => From::from(CommentRef::from(node)),
            NodeStrongRef::Document(node) => From::from(DocumentRef::from(node)),
            NodeStrongRef::DocumentType(node) => From::from(DocumentTypeRef::from(node)),
            NodeStrongRef::DocumentFragment(node) => From::from(DocumentFragmentRef::from(node)),
            NodeStrongRef::Notation(node) => From::from(NotationRef::from(node)),
        }
    }
}

impl From<NodeRef> for NodeStrongRef {
    fn from(value: NodeRef) -> Self {
        match value {
            NodeRef::Element(node) => NodeStrongRef::Element(node.0),
            NodeRef::Attribute(node) => NodeStrongRef::Attribute(node.0),
            NodeRef::Text(node) => NodeStrongRef::Text(node.0),
            NodeRef::CDATASection(node) => NodeStrongRef::CDATASection(node.0),
            NodeRef::EntityReference(node) => NodeStrongRef::EntityReference(node.0),
            NodeRef::Entity(node) => NodeStrongRef::Entity(node.0),
            NodeRef::ProcessingInstruction(node) => NodeStrongRef::ProcessingInstruction(node.0),
            NodeRef::Comment(node) => NodeStrongRef::Comment(node.0),
            NodeRef::Document(node) => NodeStrongRef::Document(node.0),
            NodeRef::DocumentType(node) => NodeStrongRef::DocumentType(node.0),
            NodeRef::DocumentFragment(node) => NodeStrongRef::DocumentFragment(node.0),
            NodeRef::Notation(node) => NodeStrongRef::Notation(node.0),
        }
    }
}

/// Implementation of [Node](https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-ID-1950641247)
/// 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)
#[derive(Clone)]
pub(super) enum NodeWeakRef {
    Element(Weak<RefCell<Element>>),
    Attribute(Weak<RefCell<Attr>>),
    Text(Weak<RefCell<Text>>),
    CDATASection(Weak<RefCell<CDATASection>>),
    EntityReference(Weak<RefCell<EntityReference>>),
    Entity(Weak<RefCell<Entity>>),
    ProcessingInstruction(Weak<RefCell<ProcessingInstruction>>),
    Comment(Weak<RefCell<Comment>>),
    Document(Weak<RefCell<Document>>),
    DocumentType(Weak<RefCell<DocumentType>>),
    DocumentFragment(Weak<RefCell<DocumentFragment>>),
    Notation(Weak<RefCell<Notation>>),
}

impl NodeWeakRef {
    /// Generate [`NodeRef`] from `self`.  
    /// Success conditions are the same as for [`std::rc::Weak::upgrade`].
    pub fn upgrade(&self) -> Option<NodeRef> {
        match self {
            NodeWeakRef::Element(node) => {
                let elem = node.upgrade()?;
                let doc = elem.borrow().owner_document()?;
                Some(NodeRef::Element(ElementRef(elem, doc)))
            }
            NodeWeakRef::Attribute(node) => {
                let attr = node.upgrade()?;
                let doc = attr.borrow().owner_document()?;
                Some(NodeRef::Attribute(AttrRef(attr, doc)))
            }
            NodeWeakRef::Text(node) => {
                let text = node.upgrade()?;
                let doc = text.borrow().owner_document()?;
                Some(NodeRef::Text(TextRef(text, doc)))
            }
            NodeWeakRef::CDATASection(node) => {
                let cdata = node.upgrade()?;
                let doc = cdata.borrow().owner_document()?;
                Some(NodeRef::CDATASection(CDATASectionRef(cdata, doc)))
            }
            NodeWeakRef::EntityReference(node) => {
                let entref = node.upgrade()?;
                let doc = entref.borrow().owner_document()?;
                Some(NodeRef::EntityReference(EntityReferenceRef(entref, doc)))
            }
            NodeWeakRef::Entity(node) => {
                let ent = node.upgrade()?;
                let doc = ent.borrow().owner_document();
                Some(NodeRef::Entity(EntityRef(ent, doc)))
            }
            NodeWeakRef::ProcessingInstruction(node) => {
                let pi = node.upgrade()?;
                let doc = pi.borrow().owner_document()?;
                Some(NodeRef::ProcessingInstruction(ProcessingInstructionRef(
                    pi, doc,
                )))
            }
            NodeWeakRef::Comment(node) => {
                let comment = node.upgrade()?;
                let doc = comment.borrow().owner_document()?;
                Some(NodeRef::Comment(CommentRef(comment, doc)))
            }
            NodeWeakRef::Document(node) => {
                node.upgrade().map(DocumentRef::from).map(NodeRef::Document)
            }
            NodeWeakRef::DocumentType(node) => {
                let doctype = node.upgrade()?;
                let doc = doctype.borrow().owner_document();
                Some(NodeRef::DocumentType(DocumentTypeRef(doctype, doc)))
            }
            NodeWeakRef::DocumentFragment(node) => {
                let frag = node.upgrade()?;
                let doc = frag.borrow().owner_document()?;
                Some(NodeRef::DocumentFragment(DocumentFragmentRef(frag, doc)))
            }
            NodeWeakRef::Notation(node) => {
                let not = node.upgrade()?;
                let doc = not.borrow().owner_document();
                Some(NodeRef::Notation(NotationRef(not, doc)))
            }
        }
    }
}