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
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
|
{
This file is part of the Free Component Library
Implementation of DOM interfaces
Copyright (c) 1999-2000 by Sebastian Guenther, sg@freepascal.org
Modified in 2006 by Sergei Gorelkin, sergei_gorelkin@mail.ru
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{
This unit provides classes which implement the interfaces defined in the
DOM (Document Object Model) specification.
The current state is:
DOM Level 1 - Almost completely implemented
DOM Level 2 - Partially implemented
Specification used for this implementation:
"Document Object Model (DOM) Level 2 Specification Version 1.0
W3C Recommendation 11 November, 2000"
http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113
}
unit DOM;
{$ifdef fpc}
{$MODE objfpc}{$H+}
{$endif}
interface
uses
SysUtils, Classes, AVL_Tree;
// -------------------------------------------------------
// DOMException
// -------------------------------------------------------
const
// DOM Level 1 exception codes:
INDEX_SIZE_ERR = 1; // index or size is negative, or greater than the allowed value
DOMSTRING_SIZE_ERR = 2; // Specified range of text does not fit into a DOMString
HIERARCHY_REQUEST_ERR = 3; // node is inserted somewhere it does not belong
WRONG_DOCUMENT_ERR = 4; // node is used in a different document than the one that created it (that does not support it)
INVALID_CHARACTER_ERR = 5; // invalid or illegal character is specified, such as in a name
NO_DATA_ALLOWED_ERR = 6; // data is specified for a node which does not support data
NO_MODIFICATION_ALLOWED_ERR = 7; // an attempt is made to modify an object where modifications are not allowed
NOT_FOUND_ERR = 8; // an attempt is made to reference a node in a context where it does not exist
NOT_SUPPORTED_ERR = 9; // implementation does not support the type of object requested
INUSE_ATTRIBUTE_ERR = 10; // an attempt is made to add an attribute that is already in use elsewhere
// DOM Level 2 exception codes:
INVALID_STATE_ERR = 11; // an attempt is made to use an object that is not, or is no longer, usable
SYNTAX_ERR = 12; // invalid or illegal string specified
INVALID_MODIFICATION_ERR = 13; // an attempt is made to modify the type of the underlying object
NAMESPACE_ERR = 14; // an attempt is made to create or change an object in a way which is incorrect with regard to namespaces
INVALID_ACCESS_ERR = 15; // parameter or operation is not supported by the underlying object
// -------------------------------------------------------
// Node
// -------------------------------------------------------
const
ELEMENT_NODE = 1;
ATTRIBUTE_NODE = 2;
TEXT_NODE = 3;
CDATA_SECTION_NODE = 4;
ENTITY_REFERENCE_NODE = 5;
ENTITY_NODE = 6;
PROCESSING_INSTRUCTION_NODE = 7;
COMMENT_NODE = 8;
DOCUMENT_NODE = 9;
DOCUMENT_TYPE_NODE = 10;
DOCUMENT_FRAGMENT_NODE = 11;
NOTATION_NODE = 12;
type
TDOMImplementation = class;
TDOMDocumentFragment = class;
TDOMDocument = class;
TDOMNode = class;
TDOMNodeList = class;
TDOMNamedNodeMap = class;
TDOMCharacterData = class;
TDOMAttr = class;
TDOMElement = class;
TDOMText = class;
TDOMComment = class;
TDOMCDATASection = class;
TDOMDocumentType = class;
TDOMNotation = class;
TDOMEntity = class;
TDOMEntityReference = class;
TDOMProcessingInstruction = class;
// -------------------------------------------------------
// DOMString
// -------------------------------------------------------
TSetOfChar = set of Char;
DOMString = WideString;
DOMPChar = PWideChar;
PDOMString = ^DOMString;
EDOMError = class(Exception)
public
Code: Integer;
constructor Create(ACode: Integer; const ASituation: String);
end;
EDOMIndexSize = class(EDOMError)
public
constructor Create(const ASituation: String);
end;
EDOMHierarchyRequest = class(EDOMError)
public
constructor Create(const ASituation: String);
end;
EDOMWrongDocument = class(EDOMError)
public
constructor Create(const ASituation: String);
end;
EDOMNotFound = class(EDOMError)
public
constructor Create(const ASituation: String);
end;
EDOMNotSupported = class(EDOMError)
public
constructor Create(const ASituation: String);
end;
EDOMInUseAttribute = class(EDOMError)
public
constructor Create(const ASituation: String);
end;
EDOMInvalidState = class(EDOMError)
public
constructor Create(const ASituation: String);
end;
EDOMSyntax = class(EDOMError)
public
constructor Create(const ASituation: String);
end;
EDOMInvalidModification = class(EDOMError)
public
constructor Create(const ASituation: String);
end;
EDOMNamespace = class(EDOMError)
public
constructor Create(const ASituation: String);
end;
EDOMInvalidAccess = class(EDOMError)
public
constructor Create(const ASituation: String);
end;
TRefClass = class
protected
RefCounter: LongInt;
public
constructor Create;
function AddRef: LongInt; virtual;
function Release: LongInt; virtual;
end;
{ NodeType, NodeName and NodeValue had been moved from fields to functions.
This lowers memory usage and also obsoletes most constructors,
at a slight performance penalty. However, NodeName and NodeValue are
accessible via fields using specialized properties of descendant classes,
e.g. TDOMElement.TagName, TDOMCharacterData.Data etc.}
TDOMNode = class
protected
FParentNode: TDOMNode;
FPreviousSibling, FNextSibling: TDOMNode;
FOwnerDocument: TDOMDocument;
function GetNodeName: DOMString; virtual;
function GetNodeValue: DOMString; virtual;
procedure SetNodeValue(const AValue: DOMString); virtual;
function GetFirstChild: TDOMNode; virtual;
function GetLastChild: TDOMNode; virtual;
function GetAttributes: TDOMNamedNodeMap; virtual;
function GetRevision: Integer;
function GetNodeType: Integer; virtual; abstract;
function GetTextContent: DOMString; virtual;
procedure SetTextContent(const AValue: DOMString); virtual;
function GetLocalName: DOMString; virtual;
function GetNamespaceURI: DOMString; virtual;
public
constructor Create(AOwner: TDOMDocument);
destructor Destroy; override;
// Free NodeList with TDOMNodeList.Release!
function GetChildNodes: TDOMNodeList;
property NodeName: DOMString read GetNodeName;
property NodeValue: DOMString read GetNodeValue write SetNodeValue;
property NodeType: Integer read GetNodeType;
property ParentNode: TDOMNode read FParentNode;
property FirstChild: TDOMNode read GetFirstChild;
property LastChild: TDOMNode read GetLastChild;
property ChildNodes: TDOMNodeList read GetChildNodes;
property PreviousSibling: TDOMNode read FPreviousSibling;
property NextSibling: TDOMNode read FNextSibling;
property Attributes: TDOMNamedNodeMap read GetAttributes;
// DOM 2: is now nil for documents and unused DocTypes
property OwnerDocument: TDOMDocument read FOwnerDocument;
function InsertBefore(NewChild, RefChild: TDOMNode): TDOMNode; virtual;
function ReplaceChild(NewChild, OldChild: TDOMNode): TDOMNode; virtual;
function RemoveChild(OldChild: TDOMNode): TDOMNode; virtual;
function AppendChild(NewChild: TDOMNode): TDOMNode; virtual;
function HasChildNodes: Boolean; virtual;
function CloneNode(deep: Boolean): TDOMNode; overload;
// DOM level 2
(*
function Supports(const Feature, Version: DOMString): Boolean;
*)
function HasAttributes: Boolean; virtual;
procedure Normalize;
// always '' for nodes other than ELEMENT and ATTRIBUTE
// as well as for nodes created with DOM 1 methods
//property NamespaceURI: DOMString read GetNamespaceURI;
//property LocalName: DOMString read GetLocalName;
(*
// Prefix may only be changed if it was specified at creation time.
property Prefix: DOMString read FPrefix (write SetPrefix?);
*)
// DOM level 3
property TextContent: DOMString read GetTextContent write SetTextContent;
// Extensions to DOM interface:
function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; virtual;
function FindNode(const ANodeName: DOMString): TDOMNode; virtual;
function CompareName(const name: DOMString): Integer; virtual;
end;
{ The following class is an implementation specific extension, it is just an
extended implementation of TDOMNode, the generic DOM::Node interface
implementation. (Its main purpose is to save memory in a big node tree) }
TDOMNode_WithChildren = class(TDOMNode)
protected
FFirstChild, FLastChild: TDOMNode;
FChildNodeTree: TAVLTree;
function GetFirstChild: TDOMNode; override;
function GetLastChild: TDOMNode; override;
procedure CloneChildren(ACopy: TDOMNode; ACloneOwner: TDOMDocument);
procedure AddToChildNodeTree(NewNode: TDOMNode);
procedure RemoveFromChildNodeTree(OldNode: TDOMNode);
procedure FreeChildren;
function GetTextContent: DOMString; override;
procedure SetTextContent(const AValue: DOMString); override;
function DoRemoveChild(OldChild: TDOMNode): TDOMNode;
public
destructor Destroy; override;
function InsertBefore(NewChild, RefChild: TDOMNode): TDOMNode; override;
function ReplaceChild(NewChild, OldChild: TDOMNode): TDOMNode; override;
function RemoveChild(OldChild: TDOMNode): TDOMNode; override;
function AppendChild(NewChild: TDOMNode): TDOMNode; override;
function HasChildNodes: Boolean; override;
function FindNode(const ANodeName: DOMString): TDOMNode; override;
end;
// -------------------------------------------------------
// NodeList
// -------------------------------------------------------
TDOMNodeList = class(TRefClass)
protected
FNode: TDOMNode;
FRevision: Integer;
FList: TList;
function GetCount: LongWord;
function GetItem(index: LongWord): TDOMNode;
procedure BuildList; virtual;
public
constructor Create(ANode: TDOMNode);
destructor Destroy; override;
property Item[index: LongWord]: TDOMNode read GetItem; default;
property Count: LongWord read GetCount;
end;
{ an extension to DOM interface, used to build recursive lists of elements }
TDOMElementList = class(TDOMNodeList)
protected
filter: DOMString;
FNamespaceFilter: DOMString;
UseFilter: Boolean;
procedure BuildList; override;
public
constructor Create(ANode: TDOMNode; const AFilter: DOMString); overload;
constructor Create(ANode: TDOMNode; const nsURI, localName: DOMString); overload;
end;
// -------------------------------------------------------
// NamedNodeMap
// -------------------------------------------------------
TDOMNamedNodeMap = class(TObject)
protected
FOwnerElement: TDOMNode;
FNodeType: Integer;
FList: TList;
function GetItem(index: LongWord): TDOMNode;
function GetLength: LongWord;
function Find(const name: DOMString; out Index: LongWord): Boolean;
function InternalRemove(const name: DOMString): TDOMNode;
public
constructor Create(AOwner: TDOMNode; ANodeType: Integer);
destructor Destroy; override;
function GetNamedItem(const name: DOMString): TDOMNode;
function SetNamedItem(arg: TDOMNode): TDOMNode;
function RemoveNamedItem(const name: DOMString): TDOMNode;
// Introduced in DOM Level 2:
function getNamedItemNS(const namespaceURI, localName: DOMString): TDOMNode;
function setNamedItemNS(arg: TDOMNode): TDOMNode;
function removeNamedItemNS(const namespaceURI,localName: DOMString): TDOMNode;
// FIX: made readonly. Reason: Anyone was allowed to insert any node without any checking.
property Item[index: LongWord]: TDOMNode read GetItem; default;
property Length: LongWord read GetLength;
end;
// -------------------------------------------------------
// CharacterData
// -------------------------------------------------------
TDOMCharacterData = class(TDOMNode)
private
FNodeValue: DOMString;
protected
function GetLength: LongWord;
function GetNodeValue: DOMString; override;
procedure SetNodeValue(const AValue: DOMString); override;
public
property Data: DOMString read FNodeValue write FNodeValue;
property Length: LongWord read GetLength;
function SubstringData(offset, count: LongWord): DOMString;
procedure AppendData(const arg: DOMString);
procedure InsertData(offset: LongWord; const arg: DOMString);
procedure DeleteData(offset, count: LongWord);
procedure ReplaceData(offset, count: LongWord; const arg: DOMString);
end;
// -------------------------------------------------------
// DOMImplementation
// -------------------------------------------------------
TDOMImplementation = class
public
function HasFeature(const feature, version: DOMString): Boolean;
// Introduced in DOM Level 2:
function CreateDocumentType(const QualifiedName, PublicID,
SystemID: DOMString): TDOMDocumentType;
function CreateDocument(const NamespaceURI, QualifiedName: DOMString;
doctype: TDOMDocumentType): TDOMDocument;
end;
// -------------------------------------------------------
// DocumentFragment
// -------------------------------------------------------
TDOMDocumentFragment = class(TDOMNode_WithChildren)
protected
function GetNodeType: Integer; override;
function GetNodeName: DOMString; override;
end;
// -------------------------------------------------------
// Document
// -------------------------------------------------------
TDOMDocument = class(TDOMNode_WithChildren)
protected
FIDList: TList;
FRevision: Integer;
FXML11: Boolean;
FImplementation: TDOMImplementation;
function GetDocumentElement: TDOMElement;
function GetDocType: TDOMDocumentType;
function GetNodeType: Integer; override;
function GetNodeName: DOMString; override;
function IndexOfNS(const nsURI: DOMString): Integer;
function FindID(const aID: DOMString; out Index: LongWord): Boolean;
procedure ClearIDList;
public
property DocType: TDOMDocumentType read GetDocType;
property Impl: TDOMImplementation read FImplementation;
property DocumentElement: TDOMElement read GetDocumentElement;
function CreateElement(const tagName: DOMString): TDOMElement; virtual;
function CreateElementBuf(Buf: DOMPChar; Length: Integer): TDOMElement;
function CreateDocumentFragment: TDOMDocumentFragment;
function CreateTextNode(const data: DOMString): TDOMText;
function CreateTextNodeBuf(Buf: DOMPChar; Length: Integer): TDOMText;
function CreateComment(const data: DOMString): TDOMComment;
function CreateCommentBuf(Buf: DOMPChar; Length: Integer): TDOMComment;
function CreateCDATASection(const data: DOMString): TDOMCDATASection;
virtual;
function CreateProcessingInstruction(const target, data: DOMString):
TDOMProcessingInstruction; virtual;
function CreateAttribute(const name: DOMString): TDOMAttr;
function CreateAttributeBuf(Buf: DOMPChar; Length: Integer): TDOMAttr;
function CreateEntityReference(const name: DOMString): TDOMEntityReference;
virtual;
// Free NodeList with TDOMNodeList.Release!
function GetElementsByTagName(const tagname: DOMString): TDOMNodeList;
// DOM level 2 methods
function ImportNode(ImportedNode: TDOMNode; Deep: Boolean): TDOMNode;
function CreateElementNS(const NamespaceURI, QualifiedName: DOMString): TDOMElement;
function CreateAttributeNS(const NamespaceURI, QualifiedName: DOMString): TDOMAttr;
function GetElementsByTagNameNS(const namespaceURI, localName: DOMString): TDOMNodeList;
function GetElementById(const ElementID: DOMString): TDOMElement;
// Extensions to DOM interface:
// TODO: obsolete now, but must check for usage dependencies
constructor Create;
destructor Destroy; override;
function AddID(Attr: TDOMAttr): Boolean;
procedure RemoveID(Attr: TDOMAttr);
end;
TXMLDocument = class(TDOMDocument)
private
FXMLVersion: DOMString;
procedure SetXMLVersion(const aValue: DOMString);
public
// These fields are extensions to the DOM interface:
Encoding, StylesheetType, StylesheetHRef: DOMString;
function CreateCDATASection(const data: DOMString): TDOMCDATASection; override;
function CreateProcessingInstruction(const target, data: DOMString):
TDOMProcessingInstruction; override;
function CreateEntityReference(const name: DOMString): TDOMEntityReference; override;
property XMLVersion: DOMString read FXMLVersion write SetXMLVersion;
end;
// -------------------------------------------------------
// Attr
// -------------------------------------------------------
TAttrDataType = (
dtCdata,
dtId,
dtIdRef,
dtIdRefs,
dtEntity,
dtEntities,
dtNmToken,
dtNmTokens,
dtNotation
);
TDOMAttr = class(TDOMNode_WithChildren)
protected
FName: DOMString;
FOwnerElement: TDOMElement;
// TODO: following 3 - replace with a link to AttDecl ??
// ('specified' isn't related...)
FSpecified: Boolean;
FDeclared: Boolean;
FDataType: TAttrDataType;
function GetNodeValue: DOMString; override;
function GetNodeType: Integer; override;
function GetNodeName: DOMString; override;
procedure SetNodeValue(const AValue: DOMString); override;
public
function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
property Name: DOMString read FName;
property Specified: Boolean read FSpecified;
property Value: DOMString read GetNodeValue write SetNodeValue;
property OwnerElement: TDOMElement read FOwnerElement;
// extensions
function CompareName(const AName: DOMString): Integer; override;
property DataType: TAttrDataType read FDataType;
end;
// -------------------------------------------------------
// Element
// -------------------------------------------------------
TDOMElement = class(TDOMNode_WithChildren)
protected
FNodeName: DOMString;
FAttributes: TDOMNamedNodeMap;
function GetNodeType: Integer; override;
function GetNodeName: DOMString; override;
function GetAttributes: TDOMNamedNodeMap; override;
public
destructor Destroy; override;
function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
property TagName: DOMString read FNodeName;
function GetAttribute(const name: DOMString): DOMString;
procedure SetAttribute(const name, value: DOMString);
procedure RemoveAttribute(const name: DOMString);
function GetAttributeNode(const name: DOMString): TDOMAttr;
function SetAttributeNode(NewAttr: TDOMAttr): TDOMAttr;
function RemoveAttributeNode(OldAttr: TDOMAttr): TDOMAttr;
// Free NodeList with TDOMNodeList.Release!
function GetElementsByTagName(const name: DOMString): TDOMNodeList;
// Introduced in DOM Level 2:
function GetAttributeNS(const namespaceURI, localName: DOMString): DOMString;
procedure SetAttributeNS(const namespaceURI, qualifiedName, value: DOMString);
procedure RemoveAttributeNS(const namespaceURI, localName: DOMString);
function GetAttributeNodeNS(const namespaceURI, localName: DOMString): TDOMAttr;
function SetAttributeNodeNS(newAttr: TDOMAttr): TDOMAttr;
function GetElementsByTagNameNS(const namespaceURI, localName: DOMString): TDOMNodeList;
function hasAttribute(const name: DOMString): Boolean;
function hasAttributeNS(const namespaceURI, localName: DOMString): Boolean;
function HasAttributes: Boolean; override;
// extension
function CompareName(const name: DOMString): Integer; override;
property AttribStrings[const Name: DOMString]: DOMString
read GetAttribute write SetAttribute; default;
end;
// -------------------------------------------------------
// Text
// -------------------------------------------------------
TDOMText = class(TDOMCharacterData)
protected
// set by parser if text contains only literal whitespace (i.e. not coming from CharRefs)
FMayBeIgnorable: Boolean;
function GetNodeType: Integer; override;
function GetNodeName: DOMString; override;
procedure SetNodeValue(const aValue: DOMString); override;
public
function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
function SplitText(offset: LongWord): TDOMText;
// Extension
property MayBeIgnorable: Boolean read FMayBeIgnorable write FMayBeIgnorable;
end;
// -------------------------------------------------------
// Comment
// -------------------------------------------------------
TDOMComment = class(TDOMCharacterData)
protected
function GetNodeType: Integer; override;
function GetNodeName: DOMString; override;
public
function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
end;
// -------------------------------------------------------
// CDATASection
// -------------------------------------------------------
TDOMCDATASection = class(TDOMText)
protected
function GetNodeType: Integer; override;
function GetNodeName: DOMString; override;
public
function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
end;
// -------------------------------------------------------
// DocumentType
// -------------------------------------------------------
TDOMDocumentType = class(TDOMNode)
protected
FName: DOMString;
FPublicID: DOMString;
FSystemID: DOMString;
FInternalSubset: DOMString;
FEntities, FNotations: TDOMNamedNodeMap;
FElementDefs: TDOMNamedNodeMap;
function GetEntities: TDOMNamedNodeMap;
function GetNotations: TDOMNamedNodeMap;
function GetNodeType: Integer; override;
function GetNodeName: DOMString; override;
function GetElementDefs: TDOMNamedNodeMap;
public
destructor Destroy; override;
function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
property Name: DOMString read FName;
property Entities: TDOMNamedNodeMap read GetEntities;
property Notations: TDOMNamedNodeMap read GetNotations;
// Introduced in DOM Level 2:
property PublicID: DOMString read FPublicID;
property SystemID: DOMString read FSystemID;
property InternalSubset: DOMString read FInternalSubset;
// extensions
property ElementDefs: TDOMNamedNodeMap read GetElementDefs;
end;
// -------------------------------------------------------
// Notation
// -------------------------------------------------------
TDOMNotation = class(TDOMNode)
protected
FName: DOMString;
FPublicID, FSystemID: DOMString;
function GetNodeType: Integer; override;
function GetNodeName: DOMString; override;
public
function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
property PublicID: DOMString read FPublicID;
property SystemID: DOMString read FSystemID;
end;
// -------------------------------------------------------
// Entity
// -------------------------------------------------------
TDOMEntity = class(TDOMNode_WithChildren)
protected
FName: DOMString;
FPublicID, FSystemID, FNotationName: DOMString;
function GetNodeType: Integer; override;
function GetNodeName: DOMString; override;
public
property PublicID: DOMString read FPublicID;
property SystemID: DOMString read FSystemID;
property NotationName: DOMString read FNotationName;
end;
// -------------------------------------------------------
// EntityReference
// -------------------------------------------------------
TDOMEntityReference = class(TDOMNode_WithChildren)
protected
FName: DOMString;
function GetNodeType: Integer; override;
function GetNodeName: DOMString; override;
public
function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
end;
// -------------------------------------------------------
// ProcessingInstruction
// -------------------------------------------------------
TDOMProcessingInstruction = class(TDOMNode)
private
FTarget: DOMString;
FNodeValue: DOMString;
protected
function GetNodeType: Integer; override;
function GetNodeName: DOMString; override;
function GetNodeValue: DOMString; override;
procedure SetNodeValue(const AValue: DOMString); override;
public
function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode; overload; override;
property Target: DOMString read FTarget;
property Data: DOMString read FNodeValue write FNodeValue;
end;
// =======================================================
// =======================================================
implementation
uses
xmlutils;
type
PIDItem = ^TIDItem;
TIDItem = record
ID: WideString;
Element: TDOMElement;
end;
constructor TRefClass.Create;
begin
inherited Create;
RefCounter := 1;
end;
function TRefClass.AddRef: LongInt;
begin
Inc(RefCounter);
Result := RefCounter;
end;
function TRefClass.Release: LongInt;
begin
Dec(RefCounter);
Result := RefCounter;
if RefCounter <= 0 then Free;
end;
// -------------------------------------------------------
// DOM Exception
// -------------------------------------------------------
constructor EDOMError.Create(ACode: Integer; const ASituation: String);
begin
Code := ACode;
inherited Create(Self.ClassName + ' in ' + ASituation);
end;
constructor EDOMIndexSize.Create(const ASituation: String); // 1
begin
inherited Create(INDEX_SIZE_ERR, ASituation);
end;
constructor EDOMHierarchyRequest.Create(const ASituation: String); // 3
begin
inherited Create(HIERARCHY_REQUEST_ERR, ASituation);
end;
constructor EDOMWrongDocument.Create(const ASituation: String); // 4
begin
inherited Create(WRONG_DOCUMENT_ERR, ASituation);
end;
constructor EDOMNotFound.Create(const ASituation: String); // 8
begin
inherited Create(NOT_FOUND_ERR, ASituation);
end;
constructor EDOMNotSupported.Create(const ASituation: String); // 9
begin
inherited Create(NOT_SUPPORTED_ERR, ASituation);
end;
constructor EDOMInUseAttribute.Create(const ASituation: String); // 10
begin
inherited Create(INUSE_ATTRIBUTE_ERR, ASituation);
end;
constructor EDOMInvalidState.Create(const ASituation: String); // 11
begin
inherited Create(INVALID_STATE_ERR, ASituation);
end;
constructor EDOMSyntax.Create(const ASituation: String); // 12
begin
inherited Create(SYNTAX_ERR, ASituation);
end;
constructor EDOMInvalidModification.Create(const ASituation: String); // 13
begin
inherited Create(INVALID_MODIFICATION_ERR, ASituation);
end;
constructor EDOMNamespace.Create(const ASituation: String); // 14
begin
inherited Create(NAMESPACE_ERR, ASituation);
end;
constructor EDOMInvalidAccess.Create(const ASituation: String); // 15
begin
inherited Create(INVALID_ACCESS_ERR, ASituation);
end;
// -------------------------------------------------------
// Node
// -------------------------------------------------------
constructor TDOMNode.Create(AOwner: TDOMDocument);
begin
FOwnerDocument := AOwner;
inherited Create;
end;
destructor TDOMNode.Destroy;
begin
if Assigned(FParentNode) and FParentNode.InheritsFrom(TDOMNode_WithChildren) then
TDOMNode_WithChildren(FParentNode).DoRemoveChild(Self);
inherited Destroy;
end;
function TDOMNode.GetNodeName: DOMString;
begin
Result := '';
end;
function TDOMNode.GetNodeValue: DOMString;
begin
Result := '';
end;
procedure TDOMNode.SetNodeValue(const AValue: DOMString);
begin
// do nothing
end;
function TDOMNode.GetChildNodes: TDOMNodeList;
begin
Result := TDOMNodeList.Create(Self);
end;
function TDOMNode.GetFirstChild: TDOMNode;
begin
Result := nil;
end;
function TDOMNode.GetLastChild: TDOMNode;
begin
Result := nil;
end;
function TDOMNode.GetAttributes: TDOMNamedNodeMap;
begin
Result := nil;
end;
function TDOMNode.InsertBefore(NewChild, RefChild: TDOMNode): TDOMNode;
begin
raise EDOMHierarchyRequest.Create('Node.InsertBefore');
Result:=nil;
end;
function TDOMNode.ReplaceChild(NewChild, OldChild: TDOMNode): TDOMNode;
begin
raise EDOMHierarchyRequest.Create('Node.ReplaceChild');
Result:=nil;
end;
function TDOMNode.RemoveChild(OldChild: TDOMNode): TDOMNode;
begin
// OldChild isn't in our child list
raise EDOMNotFound.Create('Node.RemoveChild');
Result:=nil;
end;
function TDOMNode.AppendChild(NewChild: TDOMNode): TDOMNode;
begin
raise EDOMHierarchyRequest.Create('Node.AppendChild');
Result:=nil;
end;
function TDOMNode.HasChildNodes: Boolean;
begin
Result := False;
end;
function TDOMNode.CloneNode(deep: Boolean): TDOMNode;
begin
Result := CloneNode(deep, FOwnerDocument);
end;
function TDOMNode.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
begin
raise EDOMNotSupported.CreateFmt('CloneNode not implemented for %s', [ClassName]);
Result:=nil;
end;
function TDOMNode.FindNode(const ANodeName: DOMString): TDOMNode;
begin
// FIX: we have no children, hence cannot find anything
Result := nil;
end;
function TDOMNode.GetRevision: Integer;
begin
Result := FOwnerDocument.FRevision;
end;
function TDOMNode.HasAttributes: Boolean;
begin
Result := False;
end;
// DONE: moved to TDOMNode and implemented
procedure TDOMNode.Normalize;
var
Child, tmp: TDOMNode;
Txt: TDOMText;
begin
Child := FirstChild;
Txt := nil;
while Assigned(Child) do
begin
if Child.NodeType = TEXT_NODE then
begin
if Assigned(Txt) then
begin
tmp := Child.NextSibling;
Txt.AppendData(TDOMText(Child).Data);
Txt.FMayBeIgnorable := Txt.FMayBeIgnorable and TDOMText(Child).FMayBeIgnorable;
RemoveChild(Child);
Child := tmp;
end
else
begin
Txt := TDOMText(Child);
Child := Child.NextSibling;
end
end
else
begin
Child.Normalize; // should be recursive!
Child := Child.NextSibling;
Txt := nil;
end;
end;
end;
function TDOMNode.GetTextContent: DOMString;
begin
Result := NodeValue;
end;
procedure TDOMNode.SetTextContent(const AValue: DOMString);
begin
NodeValue := AValue;
end;
function TDOMNode.GetNamespaceURI: DOMString;
begin
Result := '';
end;
function TDOMNode.GetLocalName: DOMString;
begin
Result := '';
end;
function CompareDOMStrings(const s1, s2: DOMPChar; l1, l2: integer): integer;
var i: integer;
begin
Result:=l1-l2;
i:=0;
while (i<l1) and (Result=0) do begin
Result:=ord(s1[i])-ord(s2[i]);
inc(i);
end;
end;
// generic version (slow)
function TDOMNode.CompareName(const name: DOMString): Integer;
var
SelfName: DOMString;
begin
SelfName := NodeName;
Result := CompareDOMStrings(DOMPChar(name), DOMPChar(SelfName), Length(name), Length(SelfName));
end;
//------------------------------------------------------------------------------
function CompareDOMNodeWithDOMNode(Node1, Node2: Pointer): integer;
begin
Result := TDOMNode(Node2).CompareName(TDOMNode(Node1).NodeName);
end;
function CompareDOMStringWithDOMNode(AKey, ANode: Pointer): integer;
begin
Result := TDOMNode(ANode).CompareName(PDOMString(AKey)^);
end;
function TDOMNode_WithChildren.GetFirstChild: TDOMNode;
begin
Result := FFirstChild;
end;
function TDOMNode_WithChildren.GetLastChild: TDOMNode;
begin
Result := FLastChild;
end;
destructor TDOMNode_WithChildren.Destroy;
begin
FreeChildren;
FreeAndNil(FChildNodeTree);
inherited Destroy;
end;
function TDOMNode_WithChildren.InsertBefore(NewChild, RefChild: TDOMNode):
TDOMNode;
var
Tmp: TDOMNode;
begin
Result := NewChild;
if not Assigned(RefChild) then
begin
AppendChild(NewChild);
exit;
end;
if NewChild.FOwnerDocument <> FOwnerDocument then
raise EDOMWrongDocument.Create('NodeWC.InsertBefore');
if RefChild.ParentNode <> Self then
raise EDOMHierarchyRequest.Create('NodeWC.InsertBefore');
Inc(FOwnerDocument.FRevision); // invalidate nodelists
// ugly workaround for RemoveChild issue...
if Assigned(NewChild.FParentNode) then
if NewChild.FParentNode.InheritsFrom(TDOMNode_WithChildren) then
TDOMNode_WithChildren(NewChild.FParentNode).DoRemoveChild(NewChild);
// DONE: Implemented InsertBefore for DocumentFragments (except ChildNodeTree)
if NewChild.NodeType = DOCUMENT_FRAGMENT_NODE then
begin
// Is fragment empty?
Tmp := NewChild.FirstChild;
if not Assigned(Tmp) then
Exit;
// reparent nodes
while Assigned(Tmp) do
begin
Tmp.FParentNode := Self;
Tmp := Tmp.NextSibling;
end;
// won't get here if RefChild = nil...
if (RefChild = nil) or (RefChild.FPreviousSibling = nil) then
begin // insert at the beginning <- AppendChild ??? ->
// no, AppendChild will insert after RefChild and we need it before
if Assigned(FirstChild) then
FirstChild.FPreviousSibling := NewChild.LastChild;
NewChild.LastChild.FNextSibling := FirstChild;
if not Assigned(FLastChild) then
FLastChild := NewChild.LastChild;
FFirstChild := NewChild.FirstChild;
end
else // insert to the middle
begin
NewChild.LastChild.FNextSibling := RefChild;
NewChild.FirstChild.FPreviousSibling := RefChild.FPreviousSibling;
RefChild.FPreviousSibling.FNextSibling := NewChild.FirstChild;
RefChild.FPreviousSibling := NewChild.LastChild;
end;
// finally, detach nodes from the fragment
TDOMDocumentFragment(NewChild).FFirstChild := nil;
TDOMDocumentFragment(NewChild).FLastChild := nil;
// TODO: ChildNodeTree...
Exit;
end;
NewChild.FNextSibling := RefChild;
if RefChild = FFirstChild then
FFirstChild := NewChild
else
begin
RefChild.FPreviousSibling.FNextSibling := NewChild;
NewChild.FPreviousSibling := RefChild.FPreviousSibling;
end;
RefChild.FPreviousSibling := NewChild;
NewChild.FParentNode := Self;
AddToChildNodeTree(NewChild);
end;
function TDOMNode_WithChildren.ReplaceChild(NewChild, OldChild: TDOMNode):
TDOMNode;
begin
RemoveFromChildNodeTree(OldChild);
InsertBefore(NewChild, OldChild);
if Assigned(OldChild) then
RemoveChild(OldChild);
// TODO: per DOM spec, must return OldChild, but OldChild is destroyed
Result := NewChild;
end;
function TDOMNode_WithChildren.DoRemoveChild(OldChild: TDOMNode): TDOMNode;
begin
if OldChild.ParentNode <> Self then
raise EDOMNotFound.Create('NodeWC.RemoveChild');
Inc(FOwnerDocument.FRevision); // invalidate nodelists
if OldChild = FFirstChild then
FFirstChild := FFirstChild.FNextSibling
else
OldChild.FPreviousSibling.FNextSibling := OldChild.FNextSibling;
if OldChild = FLastChild then
FLastChild := FLastChild.FPreviousSibling
else
OldChild.FNextSibling.FPreviousSibling := OldChild.FPreviousSibling;
RemoveFromChildNodeTree(OldChild);
// Make sure removed child does not contain references to nowhere
OldChild.FPreviousSibling := nil;
OldChild.FNextSibling := nil;
OldChild.FParentNode := nil;
Result := OldChild;
end;
function TDOMNode_WithChildren.RemoveChild(OldChild: TDOMNode):
TDOMNode;
begin
DoRemoveChild(OldChild);
// DOM level 2: Must return removed node
OldChild.Free;
Result:=nil;
end;
function TDOMNode_WithChildren.AppendChild(NewChild: TDOMNode): TDOMNode;
var
Tmp: TDOMNode;
begin
if NewChild.FOwnerDocument <> FOwnerDocument then
raise EDOMWrongDocument.Create('NodeWC.AppendChild');
Tmp := Self;
while Assigned(Tmp) do
begin
if Tmp = NewChild then
raise EDOMHierarchyRequest.Create('NodeWC.AppendChild (cycle in tree)');
Tmp := Tmp.ParentNode;
end;
Inc(FOwnerDocument.FRevision); // invalidate nodelists
// TODO: RemoveChild destroys removed node -> CRASH
// this is a very ugly workaround...
if Assigned(NewChild.FParentNode) then
if NewChild.FParentNode.InheritsFrom(TDOMNode_WithChildren) then
TDOMNode_WithChildren(NewChild.FParentNode).DoRemoveChild(NewChild);
// DONE: supported AppendChild for DocumentFragments (except ChildNodeTree)
if NewChild.NodeType = DOCUMENT_FRAGMENT_NODE then
begin
Tmp := NewChild.FirstChild;
// Is fragment empty?
if Assigned(Tmp) then
begin
// reparent nodes
while Assigned(Tmp) do
begin
Tmp.FParentNode := Self;
Tmp := Tmp.NextSibling;
end;
if Assigned(FLastChild) then
FLastChild.FNextSibling := NewChild.FirstChild;
NewChild.FirstChild.FPreviousSibling := LastChild;
if not Assigned(FFirstChild) then
FFirstChild := NewChild.FirstChild;
FLastChild := NewChild.LastChild;
// detach nodes from fragment
TDOMDocumentFragment(NewChild).FFirstChild := nil;
TDOMDocumentFragment(NewChild).FLastChild := nil;
// TODO: ChildNodeTree...
end;
end
else
begin
if Assigned(FFirstChild) then
begin
FLastChild.FNextSibling := NewChild;
NewChild.FPreviousSibling := FLastChild;
end else
FFirstChild := NewChild;
FLastChild := NewChild;
NewChild.FParentNode := Self;
AddToChildNodeTree(NewChild);
end;
Result := NewChild;
end;
function TDOMNode_WithChildren.HasChildNodes: Boolean;
begin
Result := Assigned(FFirstChild);
end;
function TDOMNode_WithChildren.FindNode(const ANodeName: DOMString): TDOMNode;
var AVLNode: TAVLTreeNode;
begin
Result:=nil;
if FChildNodeTree<>nil then begin
AVLNode:=FChildNodeTree.FindKey(Pointer(@ANodeName),
@CompareDOMStringWithDOMNode);
if AVLNode<>nil then
Result:=TDOMNode(AVLNode.Data);
end;
end;
procedure TDOMNode_WithChildren.CloneChildren(ACopy: TDOMNode;
ACloneOwner: TDOMDocument);
var
node: TDOMNode;
begin
node := FirstChild;
while Assigned(node) do
begin
ACopy.AppendChild(node.CloneNode(True, ACloneOwner));
node := node.NextSibling;
end;
end;
procedure TDOMNode_WithChildren.FreeChildren;
var
child, next: TDOMNode;
begin
if Assigned(FChildNodeTree) then
FChildNodeTree.Clear;
child := FFirstChild;
while Assigned(child) do
begin
next := child.NextSibling;
child.FParentNode := nil;
child.Free;
child := next;
end;
FFirstChild := nil;
FLastChild := nil;
end;
function TDOMNode_WithChildren.GetTextContent: DOMString;
var
child: TDOMNode;
begin
Result := '';
child := FFirstChild;
// TODO: probably very slow, optimization needed
// TODO: must ignore whitespace nodes
while Assigned(child) do
begin
if not (child.NodeType in [COMMENT_NODE, PROCESSING_INSTRUCTION_NODE]) then
Result := Result + child.TextContent;
child := child.NextSibling;
end;
end;
procedure TDOMNode_WithChildren.SetTextContent(const AValue: DOMString);
begin
FreeChildren;
if AValue <> '' then
AppendChild(FOwnerDocument.CreateTextNode(AValue));
end;
procedure TDOMNode_WithChildren.AddToChildNodeTree(NewNode: TDOMNode);
begin
if FChildNodeTree=nil then
FChildNodeTree:=TAVLTree.Create(@CompareDOMNodeWithDOMNode);
if FChildNodeTree.Find(NewNode)=nil then
FChildNodeTree.Add(NewNode);
end;
procedure TDOMNode_WithChildren.RemoveFromChildNodeTree(OldNode: TDOMNode);
begin
if FChildNodeTree<>nil then
FChildNodeTree.Remove(OldNode);
end;
// -------------------------------------------------------
// NodeList
// -------------------------------------------------------
constructor TDOMNodeList.Create(ANode: TDOMNode);
begin
inherited Create;
FNode := ANode;
FRevision := ANode.GetRevision-1; // force BuildList at first access
FList := TList.Create;
end;
destructor TDOMNodeList.Destroy;
begin
FList.Free;
inherited Destroy;
end;
procedure TDOMNodeList.BuildList;
var
Child: TDOMNode;
begin
FList.Clear;
FRevision := FNode.GetRevision; // refresh
Child := FNode.FirstChild;
while Assigned(Child) do
begin
FList.Add(Child);
Child := Child.NextSibling;
end;
end;
function TDOMNodeList.GetCount: LongWord;
begin
if FRevision <> FNode.GetRevision then
BuildList;
Result := FList.Count;
end;
function TDOMNodeList.GetItem(index: LongWord): TDOMNode;
begin
if FRevision <> FNode.GetRevision then
BuildList;
if index < LongWord(FList.Count) then
Result := TDOMNode(FList.List^[index])
else
Result := nil;
end;
{ TDOMElementList }
constructor TDOMElementList.Create(ANode: TDOMNode; const AFilter: DOMString);
begin
inherited Create(ANode);
filter := AFilter;
UseFilter := filter <> '*';
end;
constructor TDOMElementList.Create(ANode: TDOMNode; const nsURI, localName: DOMString);
begin
inherited Create(ANode);
filter := localName;
FNamespaceFilter := nsURI;
UseFilter := (filter <> '*') and (FNamespaceFilter <> '*');
end;
// TODO: namespace support here
procedure TDOMElementList.BuildList;
var
Child: TDOMNode;
begin
FList.Clear;
FRevision := FNode.GetRevision; // refresh
Child := FNode.FirstChild;
while Assigned(Child) and (Child <> FNode) do
begin
if (Child.NodeType = ELEMENT_NODE) and (not UseFilter or (TDOMElement(Child).TagName = filter)) then
FList.Add(Child);
// recursive track node hierarchy
if Assigned(Child.FirstChild) then
Child := Child.FirstChild
else
if Assigned(Child.NextSibling) then
Child := Child.NextSibling
else
begin
Child := Child.ParentNode;
while Assigned(Child) and (Child <> FNode) and not Assigned(Child.NextSibling) do
Child := Child.ParentNode;
if Assigned(Child) and (Child <> FNode) then
Child := Child.NextSibling;
end;
end;
end;
// -------------------------------------------------------
// NamedNodeMap
// -------------------------------------------------------
constructor TDOMNamedNodeMap.Create(AOwner: TDOMNode; ANodeType: Integer);
begin
inherited Create;
FOwnerElement := AOwner;
FNodeType := ANodeType;
FList := TList.Create;
end;
destructor TDOMNamedNodeMap.Destroy;
var
I: Integer;
begin
for I := FList.Count-1 downto 0 do
TDOMNode(FList[I]).Free;
FList.Free;
inherited Destroy;
end;
function TDOMNamedNodeMap.GetItem(index: LongWord): TDOMNode;
begin
if index < LongWord(FList.Count) then
Result := TDOMNode(FList.List^[index])
else
Result := nil;
end;
function TDOMNamedNodeMap.GetLength: LongWord;
begin
Result := FList.Count;
end;
function TDOMNamedNodeMap.Find(const name: DOMString; out Index: LongWord): Boolean;
var
L, H, I, C: Integer;
begin
Result := False;
L := 0;
H := FList.Count - 1;
while L <= H do
begin
I := (L + H) shr 1;
C := TDOMNode(FList.List^[I]).CompareName(name);
if C > 0 then L := I + 1 else
begin
H := I - 1;
if C = 0 then
begin
Result := True;
L := I;
end;
end;
end;
Index := L;
end;
function TDOMNamedNodeMap.GetNamedItem(const name: DOMString): TDOMNode;
var
i: Cardinal;
begin
if Find(name, i) then
Result := TDOMNode(FList.List^[i])
else
Result := nil;
end;
function TDOMNamedNodeMap.GetNamedItemNS(const namespaceURI, localName: DOMString): TDOMNode;
begin
// TODO: implement TDOMNamedNodeMap.GetNamedItemNS
raise EDOMNotSupported.Create('TDOMNamedNodeMap.GetNamedItemNS');
Result := nil;
end;
function TDOMNamedNodeMap.SetNamedItem(arg: TDOMNode): TDOMNode;
var
i: Cardinal;
AttrOwner: TDOMElement;
Exists: Boolean;
begin
if arg.FOwnerDocument <> FOwnerElement.FOwnerDocument then
raise EDOMWrongDocument.Create('NamedNodeMap.SetNamedItem');
if arg.NodeType <> FNodeType then
raise EDOMHierarchyRequest.Create('NamedNodeMap.SetNamedItem');
if FNodeType = ATTRIBUTE_NODE then
begin
AttrOwner := TDOMAttr(arg).ownerElement;
if Assigned(AttrOwner) and (AttrOwner <> FOwnerElement) then
raise EDOMInUseAttribute.Create('NamedNodeMap.SetNamedItem');
TDOMAttr(arg).FOwnerElement := TDOMElement(FOwnerElement);
Exists := Find(TDOMAttr(arg).FName, i); // optimization
end
else
Exists := Find(arg.NodeName, i);
if Exists then
begin
Result := TDOMNode(FList.List^[i]);
FList.List^[i] := arg;
exit;
end;
FList.Insert(i, arg);
Result := nil;
end;
function TDOMNamedNodeMap.SetNamedItemNS(arg: TDOMNode): TDOMNode;
begin
// TODO: implement TDOMNamedNodeMap.SetNamedItemNS
Result := nil;
end;
function TDOMNamedNodeMap.InternalRemove(const name: DOMString): TDOMNode;
var
i: Cardinal;
begin
Result := nil;
if Find(name, i) then
begin
Result := TDOMNode(FList.List^[i]);
FList.Delete(I);
if Result.NodeType = ATTRIBUTE_NODE then
TDOMAttr(Result).FOwnerElement := nil;
end;
end;
function TDOMNamedNodeMap.RemoveNamedItem(const name: DOMString): TDOMNode;
begin
Result := InternalRemove(name);
if Result = nil then
raise EDOMNotFound.Create('NamedNodeMap.RemoveNamedItem');
end;
function TDOMNamedNodeMap.RemoveNamedItemNS(const namespaceURI, localName: DOMString): TDOMNode;
begin
// TODO: Implement TDOMNamedNodeMap.RemoveNamedItemNS
Result := nil;
end;
// -------------------------------------------------------
// CharacterData
// -------------------------------------------------------
function TDOMCharacterData.GetLength: LongWord;
begin
Result := system.Length(FNodeValue);
end;
function TDOMCharacterData.GetNodeValue: DOMString;
begin
Result := FNodeValue;
end;
procedure TDOMCharacterData.SetNodeValue(const AValue: DOMString);
begin
FNodeValue := AValue;
end;
function TDOMCharacterData.SubstringData(offset, count: LongWord): DOMString;
begin
if (offset > Length) then
raise EDOMIndexSize.Create('CharacterData.SubstringData');
Result := Copy(FNodeValue, offset + 1, count);
end;
procedure TDOMCharacterData.AppendData(const arg: DOMString);
begin
FNodeValue := FNodeValue + arg;
end;
procedure TDOMCharacterData.InsertData(offset: LongWord; const arg: DOMString);
begin
if (offset > Length) then
raise EDOMIndexSize.Create('CharacterData.InsertData');
// TODO: use System.Insert?
FNodeValue := Copy(FNodeValue, 1, offset) + arg +
Copy(FNodeValue, offset + 1, Length);
end;
procedure TDOMCharacterData.DeleteData(offset, count: LongWord);
begin
if (offset > Length) then
raise EDOMIndexSize.Create('CharacterData.DeleteData');
// TODO: use System.Delete?
FNodeValue := Copy(FNodeValue, 1, offset) +
Copy(FNodeValue, offset + count + 1, Length);
end;
procedure TDOMCharacterData.ReplaceData(offset, count: LongWord; const arg: DOMString);
begin
DeleteData(offset, count);
InsertData(offset, arg);
end;
// -------------------------------------------------------
// DocumentFragmet
// -------------------------------------------------------
function TDOMDocumentFragment.GetNodeType: Integer;
begin
Result := DOCUMENT_FRAGMENT_NODE;
end;
function TDOMDocumentFragment.GetNodeName: DOMString;
begin
Result := '#document-fragment';
end;
// -------------------------------------------------------
// DOMImplementation
// -------------------------------------------------------
function TDOMImplementation.HasFeature(const feature, version: DOMString):
Boolean;
begin
// very basic
if (feature = 'XML') then
begin
if (version = '') or (version = '1.0') then
Result := True
else
Result := False;
end
else
Result := False;
end;
function TDOMImplementation.CreateDocumentType(const QualifiedName, PublicID,
SystemID: DOMString): TDOMDocumentType;
begin
// DONE: Implemented
Result := TDOMDocumentType.Create(nil);
Result.FName := QualifiedName;
// cannot have PublicID without SystemID
if SystemID <> '' then
begin
Result.FPublicID := PublicID;
Result.FSystemID := SystemID;
end;
end;
function TDOMImplementation.CreateDocument(const NamespaceURI,
QualifiedName: DOMString; doctype: TDOMDocumentType): TDOMDocument;
var
Root: TDOMNode;
begin
// TODO: This method is not usable yet due to CreateElementNS...
Result := TDOMDocument.Create;
Result.FImplementation := Self;
if Assigned(doctype) then
begin
if Assigned(doctype.OwnerDocument) then
raise EDOMWrongDocument.Create('DOMImplementation.CreateDocument');
Doctype.FOwnerDocument := Result;
Result.AppendChild(doctype);
end;
Root := Result.CreateElementNS(NamespaceURI, QualifiedName);
Result.AppendChild(Root);
end;
// -------------------------------------------------------
// Document
// -------------------------------------------------------
constructor TDOMDocument.Create;
begin
inherited Create(nil);
// TODO: DOM lvl 2 states that Document should be unowned. Any dependencies?
FOwnerDocument := Self;
FIDList := TList.Create;
end;
destructor TDOMDocument.Destroy;
begin
ClearIDList;
FIDList.Free;
inherited Destroy;
end;
function TDOMDocument.AddID(Attr: TDOMAttr): Boolean;
var
I: Cardinal;
Item: PIDItem;
begin
New(Item);
Item^.ID := Attr.Value;
Item^.Element := Attr.OwnerElement;
if not FindID(Item^.ID, I) then
begin
FIDList.Insert(I, Item);
Result := True;
end
else
begin
Dispose(Item);
Result := False;
end;
end;
procedure TDOMDocument.RemoveID(Attr: TDOMAttr);
begin
// TODO: Implement this
end;
function TDOMDocument.FindID(const aID: DOMString; out Index: LongWord): Boolean;
var
L, H, I, C: Integer;
P: PIDItem;
begin
Result := False;
L := 0;
H := FIDList.Count - 1;
while L <= H do
begin
I := (L + H) shr 1;
P := PIDItem(FIDList.List^[I]);
C := CompareDOMStrings(PWideChar(aID), PWideChar(P^.ID), Length(aID), Length(P^.ID));
if C > 0 then L := I + 1 else
begin
H := I - 1;
if C = 0 then
begin
Result := True;
L := I;
end;
end;
end;
Index := L;
end;
procedure TDOMDocument.ClearIDList;
var
I: Integer;
begin
if Assigned(FIDList) then
begin
for I := 0 to FIDList.Count-1 do
Dispose(PIDItem(FIDList.List^[I]));
FIDList.Clear;
end;
end;
function TDOMDocument.GetNodeType: Integer;
begin
Result := DOCUMENT_NODE;
end;
function TDOMDocument.GetNodeName: DOMString;
begin
Result := '#document';
end;
function TDOMDocument.GetDocumentElement: TDOMElement;
var
node: TDOMNode;
begin
node := FFirstChild;
while Assigned(node) and (node.NodeType <> ELEMENT_NODE) do
node := node.NextSibling;
Result := TDOMElement(node);
end;
function TDOMDocument.GetDocType: TDOMDocumentType;
var
node: TDOMNode;
begin
node := FFirstChild;
while Assigned(node) and (node.NodeType <> DOCUMENT_TYPE_NODE) do
node := node.NextSibling;
Result := TDOMDocumentType(node);
end;
function TDOMDocument.CreateElement(const tagName: DOMString): TDOMElement;
begin
if not IsXmlName(tagName, FXML11) then
raise EDOMError.Create(INVALID_CHARACTER_ERR, 'DOMDocument.CreateElement');
Result := TDOMElement.Create(Self);
Result.FNodeName := tagName;
// TODO: attach default attributes
end;
function TDOMDocument.CreateElementBuf(Buf: DOMPChar; Length: Integer): TDOMElement;
begin
Result := TDOMElement.Create(Self);
SetString(Result.FNodeName, Buf, Length);
// TODO: attach default attributes
end;
function TDOMDocument.CreateDocumentFragment: TDOMDocumentFragment;
begin
Result := TDOMDocumentFragment.Create(Self);
end;
function TDOMDocument.CreateTextNode(const data: DOMString): TDOMText;
begin
Result := TDOMText.Create(Self);
Result.FNodeValue := data;
end;
function TDOMDocument.CreateTextNodeBuf(Buf: DOMPChar; Length: Integer): TDOMText;
begin
Result := TDOMText.Create(Self);
SetString(Result.FNodeValue, Buf, Length);
end;
function TDOMDocument.CreateComment(const data: DOMString): TDOMComment;
begin
Result := TDOMComment.Create(Self);
Result.FNodeValue := data;
end;
function TDOMDocument.CreateCommentBuf(Buf: DOMPChar; Length: Integer): TDOMComment;
begin
Result := TDOMComment.Create(Self);
SetString(Result.FNodeValue, Buf, Length);
end;
function TDOMDocument.CreateCDATASection(const data: DOMString):
TDOMCDATASection;
begin
raise EDOMNotSupported.Create('DOMDocument.CreateCDATASection');
Result:=nil;
end;
function TDOMDocument.CreateProcessingInstruction(const target,
data: DOMString): TDOMProcessingInstruction;
begin
raise EDOMNotSupported.Create('DOMDocument.CreateProcessingInstruction');
Result:=nil;
end;
function TDOMDocument.CreateAttribute(const name: DOMString): TDOMAttr;
begin
if not IsXmlName(name, FXML11) then
raise EDOMError.Create(INVALID_CHARACTER_ERR, 'DOMDocument.CreateAttribute');
Result := TDOMAttr.Create(Self);
Result.FName := name;
end;
function TDOMDocument.CreateAttributeBuf(Buf: DOMPChar; Length: Integer): TDOMAttr;
begin
Result := TDOMAttr.Create(Self);
SetString(Result.FName, Buf, Length);
Result.FSpecified := True;
end;
function TDOMDocument.CreateEntityReference(const name: DOMString):
TDOMEntityReference;
begin
raise EDOMNotSupported.Create('DOMDocument.CreateEntityReference');
Result:=nil;
end;
function TDOMDocument.GetElementsByTagName(const tagname: DOMString): TDOMNodeList;
begin
Result := TDOMElementList.Create(Self, tagname);
end;
function TDOMDocument.GetElementsByTagNameNS(const namespaceURI, localName: DOMString): TDOMNodeList;
begin
Result := TDOMElementList.Create(Self, namespaceURI, localName);
end;
function TDOMDocument.CreateAttributeNS(const NamespaceURI,
QualifiedName: DOMString): TDOMAttr;
begin
// TODO: Implement TDOMDocument.CreateAttributeNS
raise EDOMNotSupported.Create('TDOMDocument.CreateAttributeNS');
Result := nil;
end;
function TDOMDocument.CreateElementNS(const NamespaceURI,
QualifiedName: DOMString): TDOMElement;
begin
// TODO: Implement TDOMDocument.CreateElementNS
raise EDOMNotSupported.Create('TDOMDocument.CreateElementNS');
Result := nil;
end;
function TDOMDocument.GetElementById(const ElementID: DOMString): TDOMElement;
var
I: Cardinal;
begin
// TODO: Implement TDOMDocument.GetElementById
if FindID(ElementID, I) then
Result := PIDItem(FIDList.List^[I])^.Element
else
Result := nil;
end;
function TDOMDocument.ImportNode(ImportedNode: TDOMNode;
Deep: Boolean): TDOMNode;
begin
// TODO: Implement TDOMDocument.ImportNode
raise EDOMNotSupported.Create('TDOMDocument.ImportNode');
Result := nil;
end;
function TDOMDocument.IndexOfNS(const nsURI: DOMString): Integer;
begin
// TODO: implement
Result := -1;
end;
function TXMLDocument.CreateCDATASection(const data: DOMString):
TDOMCDATASection;
begin
Result := TDOMCDATASection.Create(Self);
Result.FNodeValue := data;
end;
function TXMLDocument.CreateProcessingInstruction(const target,
data: DOMString): TDOMProcessingInstruction;
begin
if not IsXmlName(target, FXML11) then
raise EDOMError.Create(INVALID_CHARACTER_ERR, 'XMLDocument.CreateProcessingInstruction');
Result := TDOMProcessingInstruction.Create(Self);
Result.FTarget := target;
Result.FNodeValue := data;
end;
function TXMLDocument.CreateEntityReference(const name: DOMString):
TDOMEntityReference;
begin
if not IsXmlName(name, FXML11) then
raise EDOMError.Create(INVALID_CHARACTER_ERR, 'XMLDocument.CreateEntityReference');
Result := TDOMEntityReference.Create(Self);
Result.FName := name;
end;
procedure TXMLDocument.SetXMLVersion(const aValue: DOMString);
begin
FXMLVersion := aValue;
FXML11 := (aValue = '1.1');
end;
// -------------------------------------------------------
// Attr
// -------------------------------------------------------
function TDOMAttr.GetNodeType: Integer;
begin
Result := ATTRIBUTE_NODE;
end;
function TDOMAttr.GetNodeName: DOMString;
begin
Result := FName;
end;
function TDOMAttr.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
begin
// Cloned attribute is always specified and carries its children
Result := ACloneOwner.CreateAttribute(FName);
TDOMAttr(Result).FSpecified := True;
TDOMAttr(Result).FDataType := FDataType;
// Declared = ?
CloneChildren(Result, ACloneOwner);
end;
function TDOMAttr.GetNodeValue: DOMString;
begin
Result := GetTextContent;
if FDataType <> dtCdata then
NormalizeSpaces(Result);
end;
procedure TDOMAttr.SetNodeValue(const AValue: DOMString);
begin
FSpecified := True;
SetTextContent(AValue);
end;
function TDOMAttr.CompareName(const AName: DOMString): Integer;
begin
Result := CompareDOMStrings(DOMPChar(AName), DOMPChar(FName), Length(AName), Length(FName));
end;
// -------------------------------------------------------
// Element
// -------------------------------------------------------
function TDOMElement.GetNodeType: Integer;
begin
Result := ELEMENT_NODE;
end;
function TDOMElement.GetNodeName: DOMString;
begin
Result := FNodeName;
end;
destructor TDOMElement.Destroy;
begin
// FIX: Attribute nodes are now freed by TDOMNamedNodeMap.Destroy
FreeAndNil(FAttributes);
inherited Destroy;
end;
function TDOMElement.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
var
i: Integer;
begin
Result := ACloneOwner.CreateElement(FNodeName);
if Assigned(FAttributes) then
begin
for i := 0 to FAttributes.Length - 1 do
TDOMElement(Result).SetAttributeNode(TDOMAttr(FAttributes[i].CloneNode(True, ACloneOwner)));
end;
if deep then
CloneChildren(Result, ACloneOwner);
end;
function TDOMElement.GetAttributes: TDOMNamedNodeMap;
begin
if FAttributes=nil then
FAttributes := TDOMNamedNodeMap.Create(Self, ATTRIBUTE_NODE);
Result := FAttributes;
end;
function TDOMElement.GetAttribute(const name: DOMString): DOMString;
var
Attr: TDOMNode;
begin
SetLength(Result, 0);
if Assigned(FAttributes) then
begin
Attr := FAttributes.GetNamedItem(name);
if Assigned(Attr) then
Result := Attr.NodeValue;
end;
end;
function TDOMElement.GetAttributeNS(const namespaceURI, localName: DOMString): DOMString;
var
Attr: TDOMNode;
begin
SetLength(Result, 0);
if Assigned(FAttributes) then
begin
Attr := FAttributes.GetNamedItemNS(namespaceURI, localName);
if Assigned(Attr) then
Result := Attr.NodeValue;
end;
end;
procedure TDOMElement.SetAttribute(const name, value: DOMString);
var
I: Cardinal;
attr: TDOMAttr;
begin
if Attributes.Find(name, I) then
Attr := FAttributes[I] as TDOMAttr
else
begin
Attr := FOwnerDocument.CreateAttribute(name);
FAttributes.FList.Insert(I, Attr);
end;
attr.NodeValue := value;
end;
procedure TDOMElement.RemoveAttribute(const name: DOMString);
begin
// (note) NamedNodeMap.RemoveNamedItem can raise NOT_FOUND_ERR and we should not.
if Assigned(FAttributes) then
FAttributes.InternalRemove(name).Free;
end;
procedure TDOMElement.RemoveAttributeNS(const namespaceURI,
localName: DOMString);
begin
// TODO: Implement TDOMElement.RemoveAttributeNS
raise EDOMNotSupported.Create('TDOMElement.RemoveAttributeNS');
end;
procedure TDOMElement.SetAttributeNS(const namespaceURI, qualifiedName,
value: DOMString);
var
Attr: TDOMAttr;
begin
Attr := Attributes.GetNamedItemNS(namespaceURI, qualifiedName) as TDOMAttr;
if attr = nil then
begin
attr := FOwnerDocument.CreateAttributeNS(namespaceURI, qualifiedName);
// TODO 5: keep sorted!
FAttributes.FList.Add(attr);
end;
attr.NodeValue := value;
end;
function TDOMElement.GetAttributeNode(const name: DOMString): TDOMAttr;
begin
// DONE: delegated to TNamedNodeMap.GetNamedItem
if Assigned(FAttributes) then
Result := FAttributes.GetNamedItem(name) as TDOMAttr
else
Result := nil;
end;
function TDOMElement.GetAttributeNodeNS(const namespaceURI, localName: DOMString): TDOMAttr;
begin
if Assigned(FAttributes) then
Result := FAttributes.GetNamedItemNS(namespaceURI, localName) as TDOMAttr
else
Result := nil;
end;
function TDOMElement.SetAttributeNode(NewAttr: TDOMAttr): TDOMAttr;
begin
Result := Attributes.SetNamedItem(NewAttr) as TDOMAttr;
// TODO -cConformance: here goes inconsistency with DOM 2 - same as in TDOMNode.RemoveChild
Result.Free;
Result := nil;
end;
function TDOMElement.SetAttributeNodeNS(NewAttr: TDOMAttr): TDOMAttr;
begin
Result := Attributes.SetNamedItemNS(NewAttr) as TDOMAttr;
// TODO -cConformance: here goes inconsistency with DOM 2 - same as in TDOMNode.RemoveChild
Result.Free;
Result := nil;
end;
function TDOMElement.RemoveAttributeNode(OldAttr: TDOMAttr): TDOMAttr;
begin
Result:=nil;
if FAttributes=nil then exit;
// TODO: DOM 2: must raise NOT_FOUND_ERR if OldAttr is not ours.
// -- but what is the purpose of return value then?
// TODO: delegate to TNamedNodeMap? Nope, it does not have such method
// (note) one way around is to remove by name
if FAttributes.FList.Remove(OldAttr) > -1 then
Result := OldAttr;
end;
function TDOMElement.GetElementsByTagName(const name: DOMString): TDOMNodeList;
begin
Result := TDOMElementList.Create(Self, name);
end;
function TDOMElement.GetElementsByTagNameNS(const namespaceURI, localName: DOMString): TDOMNodeList;
begin
Result := TDOMElementList.Create(Self, namespaceURI, localName);
end;
function TDOMElement.hasAttribute(const name: DOMString): Boolean;
begin
Result := Assigned(FAttributes) and
Assigned(FAttributes.GetNamedItem(name));
end;
function TDOMElement.hasAttributeNS(const namespaceURI, localName: DOMString): Boolean;
begin
Result := Assigned(FAttributes) and
Assigned(FAttributes.getNamedItemNS(namespaceURI, localName));
end;
function TDOMElement.HasAttributes: Boolean;
begin
Result := Assigned(FAttributes) and (FAttributes.Length > 0);
end;
function TDOMElement.CompareName(const Name: DOMString): Integer;
begin
Result := CompareDOMStrings(DOMPChar(name), DOMPChar(FNodeName), Length(name), Length(FNodeName));
end;
// -------------------------------------------------------
// Text
// -------------------------------------------------------
function TDOMText.GetNodeType: Integer;
begin
Result := TEXT_NODE;
end;
function TDOMText.GetNodeName: DOMString;
begin
Result := '#text';
end;
procedure TDOMText.SetNodeValue(const aValue: DOMString);
begin
// TODO: may analyze aValue, but this will slow things down...
FMayBeIgnorable := False;
FNodeValue := aValue;
end;
function TDOMText.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
begin
Result := ACloneOwner.CreateTextNode(FNodeValue);
TDOMText(Result).FMayBeIgnorable := FMayBeIgnorable;
end;
function TDOMText.SplitText(offset: LongWord): TDOMText;
begin
if offset > Length then
raise EDOMIndexSize.Create('Text.SplitText');
Result := TDOMText.Create(FOwnerDocument);
Result.FNodeValue := Copy(FNodeValue, offset + 1, Length);
Result.FMayBeIgnorable := FMayBeIgnorable;
FNodeValue := Copy(FNodeValue, 1, offset);
FParentNode.InsertBefore(Result, FNextSibling);
end;
// -------------------------------------------------------
// Comment
// -------------------------------------------------------
function TDOMComment.GetNodeType: Integer;
begin
Result := COMMENT_NODE;
end;
function TDOMComment.GetNodeName: DOMString;
begin
Result := '#comment';
end;
function TDOMComment.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
begin
Result := ACloneOwner.CreateComment(FNodeValue);
end;
// -------------------------------------------------------
// CDATASection
// -------------------------------------------------------
function TDOMCDATASection.GetNodeType: Integer;
begin
Result := CDATA_SECTION_NODE;
end;
function TDOMCDATASection.GetNodeName: DOMString;
begin
Result := '#cdata-section';
end;
function TDOMCDATASection.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
begin
Result := ACloneOwner.CreateCDATASection(FNodeValue);
end;
// -------------------------------------------------------
// DocumentType
// -------------------------------------------------------
function TDOMDocumentType.GetNodeType: Integer;
begin
Result := DOCUMENT_TYPE_NODE;
end;
function TDOMDocumentType.GetNodeName: DOMString;
begin
Result := FName;
end;
destructor TDOMDocumentType.Destroy;
begin
FEntities.Free;
FNotations.Free;
FElementDefs.Free;
inherited Destroy;
end;
function TDOMDocumentType.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
begin
Result := TDOMDocumentType.Create(ACloneOwner);
TDOMDocumentType(Result).FName := FName;
TDOMDocumentType(Result).FSystemID := FSystemID;
TDOMDocumentType(Result).FPublicID := FPublicID;
// ignore deep - DocumentType cannot have children
// TODO: Clone Entities and Notations
end;
function TDOMDocumentType.GetEntities: TDOMNamedNodeMap;
begin
if FEntities = nil then
FEntities := TDOMNamedNodeMap.Create(Self, ENTITY_NODE);
Result := FEntities;
end;
function TDOMDocumentType.GetNotations: TDOMNamedNodeMap;
begin
if FNotations = nil then
FNotations := TDOMNamedNodeMap.Create(Self, NOTATION_NODE);
Result := FNotations;
end;
function TDOMDocumentType.GetElementDefs: TDOMNamedNodeMap;
begin
if FElementDefs = nil then
FElementDefs := TDOMNamedNodeMap.Create(Self, ELEMENT_NODE);
Result := FElementDefs;
end;
// -------------------------------------------------------
// Notation
// -------------------------------------------------------
function TDOMNotation.GetNodeType: Integer;
begin
Result := NOTATION_NODE;
end;
function TDOMNotation.GetNodeName: DOMString;
begin
Result := FName;
end;
function TDOMNotation.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
begin
Result := TDOMNotation.Create(ACloneOwner);
TDOMNotation(Result).FName := FName;
TDOMNotation(Result).FPublicID := PublicID;
TDOMNotation(Result).FSystemID := SystemID;
// notation cannot have children, ignore Deep
end;
// -------------------------------------------------------
// Entity
// -------------------------------------------------------
function TDOMEntity.GetNodeType: Integer;
begin
Result := ENTITY_NODE;
end;
function TDOMEntity.GetNodeName: DOMString;
begin
Result := FName;
end;
// -------------------------------------------------------
// EntityReference
// -------------------------------------------------------
function TDOMEntityReference.GetNodeType: Integer;
begin
Result := ENTITY_REFERENCE_NODE;
end;
function TDOMEntityReference.GetNodeName: DOMString;
begin
Result := FName;
end;
function TDOMEntityReference.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
begin
Result := ACloneOwner.CreateEntityReference(FName);
CloneChildren(Result, ACloneOwner);
end;
// -------------------------------------------------------
// ProcessingInstruction
// -------------------------------------------------------
function TDOMProcessingInstruction.CloneNode(deep: Boolean;
ACloneOwner: TDOMDocument): TDOMNode;
begin
Result := ACloneOwner.CreateProcessingInstruction(Target, Data);
end;
function TDOMProcessingInstruction.GetNodeType: Integer;
begin
Result := PROCESSING_INSTRUCTION_NODE;
end;
function TDOMProcessingInstruction.GetNodeName: DOMString;
begin
Result := FTarget;
end;
function TDOMProcessingInstruction.GetNodeValue: DOMString;
begin
Result := FNodeValue;
end;
procedure TDOMProcessingInstruction.SetNodeValue(const AValue: DOMString);
begin
FNodeValue := AValue;
end;
end.
|