-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDOMCore.pas
1908 lines (1655 loc) · 53.4 KB
/
DOMCore.pas
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
unit DomCore;
{
This unit represents in implementation of the core DOM classes.
https://www.w3.org/TR/DOM-Level-3-Core/core.html
History
=======
1/7/2021
- Removed TEntityReference (per HTML5)
12/22/2021
- Removed TDocumentType.Entities, Notations, and InternalSubset (per HTML5)
- Removed TNode.IsSupported (per HTML5)
12/20/2021
- fixed TNode.NodeValue was not calling GetNodeValue getter
- fixed attribute lookups to be case insensitive (like everything else in html)
- fixed tag name normalization to UPPERCASE, the canonical form in the spec
(https://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-104682815)
(https://dom.spec.whatwg.org/#dom-element-tagname)
- added doctype node to the tree. DocType is now the convenience getter that returns that node
- fixed InsertNode to allow DOCTYPE nodes to be inserted to the document
- node.InsertBefore should only fail the WRONG_DOCUMENT_ERR check if the node has an ownerDocument assigned.
- added TNodeType type
- InsertBefore will no longer reject a node if its OwnerDocument is nil
- AcceptNode is now case insensitive to node local names
- NamedNodeMap.GetNamedItem and GetNamedItemNS are no longer case sensitive
- TElement constructor now canonically UPPERCASEs the tag name
- Documents no longer take a doctype in their constructor. You add doctype node as a child of the document node
- Added CreateHtmlDocument to DOMImplementation, following the html5 spec of what it should contain
- DOMImplemenation.createDocument doctype parameter is now optional
- Changed DOMImplementation.HasFeature to always return true, per HTML5 spec
}
interface
uses
Classes, SysUtils;
const
TAB = 9;
LF = 10;
CR = 13;
SP = 32;
WhiteSpace = [TAB, LF, CR, SP];
type
TNodeType = type Word;
const
NONE = 0; //extension
ELEMENT_NODE = 1; //An Element node like <p> or <div>.
ATTRIBUTE_NODE = 2; //An Attribute of an Element. Attributes no longer implement the Node interface as of DOM4.
TEXT_NODE = 3; //The actual Text inside an Element or Attr.
CDATA_SECTION_NODE = 4; //A CDATASection, such as <!CDATA[[ … ]]>.
// ENTITY_REFERENCE_NODE = 5; //An XML Entity Reference node, such as &foo;. Removed in DOM4.
// ENTITY_NODE = 6; //An XML <!ENTITY …> node. Removed in DOM4.
PROCESSING_INSTRUCTION_NODE = 7; //A ProcessingInstruction of an XML document, such as <?xml-stylesheet … ?>.
COMMENT_NODE = 8; //A Comment node, such as <!-- … -->.
DOCUMENT_NODE = 9; //A Document node.
DOCUMENT_TYPE_NODE = 10; //A DocumentType node, such as <!DOCTYPE html>.
DOCUMENT_FRAGMENT_NODE = 11; //A DocumentFragment node.
// NOTATION_NODE = 12; //An XML <!NOTATION ...> node. Removed in DOM4.
END_ELEMENT_NODE = 255; // extension
//DomException error codes
INDEX_SIZE_ERR = 1;
DOMSTRING_SIZE_ERR = 2;
HIERARCHY_REQUEST_ERR = 3;
WRONG_DOCUMENT_ERR = 4;
INVALID_CHARACTER_ERR = 5;
NO_DATA_ALLOWED_ERR = 6;
NO_MODIFICATION_ALLOWED_ERR = 7;
NOT_FOUND_ERR = 8;
NOT_SUPPORTED_ERR = 9;
INUSE_ATTRIBUTE_ERR = 10;
INVALID_STATE_ERR = 11; //DOM Level 2
SYNTAX_ERR = 12; //DOM Level 2
INVALID_MODIFICATION_ERR = 13; //DOM Level 2
NAMESPACE_ERR = 14; //DOM Level 2
INVALID_ACCESS_ERR = 15; //DOM Level 2
VALIDATION_ERR = 16; //DOM Level 3
TYPE_MISMATCH_ERR = 17; //DOM Level 3
{HTML DTDs}
DTD_HTML_STRICT = 1;
DTD_HTML_LOOSE = 2;
DTD_HTML_FRAMESET = 3;
DTD_XHTML_STRICT = 4;
DTD_XHTML_LOOSE = 5;
DTD_XHTML_FRAMESET = 6;
type
//TODO: Change this to just UnicodeString. Yes old DOM interfaces created a
//type called DomString. But that was meant to define a set of concepts of what
//their strings require. Those requirements are met by UnicodeString (and WideString).
//So we just define UnicodeString as a modernizer for Delphi 5.
TDomString = {$IFDEF UNICODE}UnicodeString{$ELSE}WideString{$ENDIF};
DomException = class(Exception)
private
FCode: Integer;
public
constructor Create(ErrorCode: Integer); overload;
constructor Create(ErrorCode: Integer; AdditionalMessage: string); overload;
property code: Integer read FCode;
end;
{
The DOMStringList interface provides the abstraction of an ordered collection of DOMString values,
without defining or constraining how this collection is implemented.
The items in the DOMStringList are accessible via an integral index, starting from 0.
Added in DOM level 3
}
IDOMStringList = interface
['{789034BD-ABC7-451B-AB27-8C976874364A}']
function getLength: Integer;
function Item(Index: Integer): TDomString;
function Contains(const str: TDomString): Boolean;
property Length: Integer read getLength;
end;
TNamespaceURIList = class
private
FList: array of TDomString; //removed need for WStrings unit
function GetItem(I: Integer): TDomString;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function Add(const NamespaceURI: TDomString): Integer;
property Item[I: Integer]: TDomString read GetItem; default;
end;
TDocument = class;
TNodeList = class;
TNamedNodeMap = class;
TElement = class;
TNode = class
private
FOwnerDocument: TDocument;
FParentNode: TNode;
FNamespaceURI: Integer;
FPrefix: TDomString;
FNodeName: TDomString;
FNodeValue: TDomString;
FAttributes: TNamedNodeMap;
function GetFirstChild: TNode;
function GetLastChild: TNode;
function GetPreviousSibling: TNode;
function GetNextSibling: TNode;
function GetLocalName: TDomString;
function GetNamespaceURI: TDomString;
function InsertSingleNode(newChild, refChild: TNode): TNode;
procedure SetOwnerDocument(const Value: TDocument);
function GetTextContent: TDomString; virtual;
protected
FChildNodes: TNodeList;
function GetNodeName: TDomString; virtual;
function GetNodeValue: TDomString; virtual;
function GetNodeType: TNodeType; virtual; abstract;
function GetParentNode: TNode; virtual;
function CanInsert(Node: TNode): Boolean; virtual;
function ExportNode(otherDocument: TDocument; deep: Boolean): TNode; virtual;
procedure SetNodeValue(const value: TDomString); virtual;
procedure SetNamespaceURI(const value: TDomString);
procedure SetPrefix(const value: TDomString);
procedure SetLocalName(const value: TDomString);
procedure CloneChildNodesFrom(Node: TNode);
constructor Create(ownerDocument: TDocument; const namespaceURI, qualifiedName: TDomString; withNS: Boolean);
public
destructor Destroy; override;
function InsertBefore(newChild, refChild: TNode): TNode;
function ReplaceChild(newChild, oldChild: TNode): TNode;
function RemoveChild(oldChild: TNode): TNode;
function AppendChild(newChild: TNode): TNode;
function hasChildNodes: Boolean;
function cloneNode(deep: Boolean): TNode; virtual; abstract;
//function isSupported(const feature, version: TDomString): Boolean; deprecated 'Removed in HTML5';
function HasAttributes: Boolean; //DOM Level 2
function ancestorOf(node: TNode): Boolean; //extension
function getElementsByTagName(const name: TDomString): TNodeList;
function getElementsByTagNameNS(const namespaceURI, localName: TDomString): TNodeList;
function GetElementByID(const ElementID: TDomString): TElement;
procedure normalize;
property NodeType: TNodeType read GetNodeType;
property NodeName: TDomString read GetNodeName;
property NodeValue: TDomString read GetNodeValue write SetNodeValue;
property TextContent: TDomString read GetTextContent; //TODO: SetTextContent (https://dom.spec.whatwg.org/#dom-node-textcontent)
property ParentNode: TNode read GetParentNode;
property ChildNodes: TNodeList read FChildNodes;
property FirstChild: TNode read GetFirstChild;
property LastChild: TNode read GetLastChild;
property PreviousSibling: TNode read GetPreviousSibling;
property NextSibling: TNode read GetNextSibling;
property Attributes: TNamedNodeMap read FAttributes;
property OwnerDocument: TDocument read FOwnerDocument write SetOwnerDocument;
property NamespaceURI: TDomString read GetNamespaceURI; //DOM Level 2
property Prefix: TDomString read FPrefix write SetPrefix; //DOM Level 2
property LocalName: TDomString read GetLocalName; //DOM Level 2
end;
TNodeList = class
private
FOwnerNode: TNode;
FList: TList;
protected
function GetLength: Integer; virtual;
function IndexOf(node: TNode): Integer;
procedure Add(node: TNode);
procedure Delete(I: Integer);
procedure Insert(I: Integer; node: TNode);
procedure Remove(node: TNode);
procedure Clear(WithItems: Boolean);
property ownerNode: TNode read FOwnerNode;
constructor Create(AOwnerNode: TNode);
public
destructor Destroy; override;
function Item(Index: Integer): TNode; virtual;
property Items[Index: Integer]: TNode read Item; default; //extension
property Length: Integer read GetLength;
end;
TNamedNodeMap = class(TNodeList)
public
function getNamedItem(const name: TDomString): TNode;
function setNamedItem(arg: TNode): TNode;
function removeNamedItem(const name: TDomString): TNode;
function getNamedItemNS(const namespaceURI, localName: TDomString): TNode;
function setNamedItemNS(arg: TNode): TNode;
function removeNamedItemNS(const namespaceURI, localName: TDomString): TNode;
end;
TCharacterData = class(TNode)
private
function GetLength: Integer;
protected
procedure SetNodeValue(const value: TDomString); override;
function GetTextContent: TDomString; override;
constructor Create(ownerDocument: TDocument; const data: TDomString);
public
function substringData(offset, count: Integer): TDomString;
procedure appendData(const arg: TDomString);
procedure deleteData(offset, count: Integer);
procedure insertData(offset: Integer; arg: TDomString);
procedure replaceData(offset, count: Integer; const arg: TDomString);
property data: TDomString read GetNodeValue write SetNodeValue;
property length: Integer read GetLength;
end;
TComment = class(TCharacterData)
protected
function GetNodeName: TDomString; override;
function GetNodeType: TNodeType; override;
function ExportNode(otherDocument: TDocument; deep: Boolean): TNode; override;
public
function cloneNode(deep: Boolean): TNode; override;
end;
TTextNode = class(TCharacterData)
protected
function GetNodeName: TDomString; override;
function GetNodeType: TNodeType; override;
function ExportNode(otherDocument: TDocument; deep: Boolean): TNode; override;
public
function cloneNode(deep: Boolean): TNode; override;
function splitText(offset: Integer): TTextNode;
end;
TCDATASection = class(TTextNode)
protected
function GetNodeName: TDomString; override;
function GetNodeType: TNodeType; override;
function ExportNode(otherDocument: TDocument; deep: Boolean): TNode; override;
public
function cloneNode(deep: Boolean): TNode; override;
end;
TAttr = class(TNode)
private
function GetOwnerElement: TElement;
function GetSpecified: Boolean;
protected
function GetNodeValue: TDomString; override;
function GetNodeType: TNodeType; override;
function GetParentNode: TNode; override;
function CanInsert(node: TNode): Boolean; override;
function ExportNode(ownerDocument: TDocument; deep: Boolean): TNode; override;
procedure SetNodeValue(const Value: TDomString); override;
function GetTextContent: TDomString; override;
public
function cloneNode(deep: Boolean): TNode; override;
property name: TDomString read GetNodeName;
property specified: Boolean read GetSpecified;
property Value: TDomString read GetNodeValue write SetNodeValue;
property ownerElement: TElement read GetOwnerElement; //DOM Level2
end;
TElement = class(TNode)
private
FIsEmpty: Boolean;
protected
function GetNodeType: TNodeType; override;
function CanInsert(node: TNode): Boolean; override;
function ExportNode(otherDocument: TDocument; deep: Boolean): TNode; override;
function GetTextContent: TDomString; override;
constructor Create(ownerDocument: TDocument; const namespaceURI, qualifiedName: TDomString; withNS: Boolean);
public
function cloneNode(deep: Boolean): TNode; override;
function getAttribute(const name: TDomString): TDomString;
function getAttributeNode(const name: TDomString): TAttr;
function setAttributeNode(newAttr: TAttr): TAttr;
function removeAttributeNode(oldAttr: TAttr): TAttr;
function getAttributeNS(const namespaceURI, localName: TDomString): TDomString;
function getAttributeNodeNS(const namespaceURI, localName: TDomString): TAttr;
function setAttributeNodeNS(newAttr: TAttr): TAttr;
function hasAttribute(const name: TDomString): Boolean;
function hasAttributeNS(const namespaceURI, localName: TDomString): Boolean;
procedure setAttribute(const name, value: TDomString);
procedure removeAttribute(const name: TDomString);
procedure setAttributeNS(const namespaceURI, qualifiedName, value: TDomString);
procedure removeAttributeNS(const namespaceURI, localName: TDomString);
property tagName: TDomString read GetNodeName;
property isEmpty: Boolean read FIsEmpty write FIsEmpty;
end;
TProcessingInstruction = class(TNode)
private
function GetTarget: TDomString;
function GetData: TDomString;
procedure SetData(const value: TDomString);
protected
function GetNodeType: TNodeType; override;
function ExportNode(otherDocument: TDocument; deep: Boolean): TNode; override;
constructor Create(ownerDocument: TDocument; const target, data: TDomString);
public
function cloneNode(deep: Boolean): TNode; override;
property target: TDomString read GetTarget;
property data: TDomString read GetData write SetData;
end;
TDocumentFragment = class(TNode)
protected
function CanInsert(node: TNode): Boolean; override;
function GetNodeType: TNodeType; override;
function GetNodeName: TDomString; override;
function ExportNode(otherDocument: TDocument; deep: Boolean): TNode; override;
function GetTextContent: TDomString; override;
constructor Create(ownerDocument: TDocument);
public
function cloneNode(deep: Boolean): TNode; override;
end;
TDocumentType = class(TNode)
private
FPublicID: TDomString;
FSystemID: TDomString;
//FEntities: TNamedNodeMap; deprecated 'Removed in HTML5';
//FNotations: TNamedNodeMap; deprecated 'Removed in HTML5';
//FInternalSubset: TDomString; deprecated 'Removed in HTML5';
protected
function GetNodeType: TNodeType; override;
constructor Create(ownerDocument: TDocument; const name, publicId, systemId: TDomString);
public
function cloneNode(deep: Boolean): TNode; override;
property name: TDomString read GetNodeName;
property publicId: TDomString read FPublicID;
property systemId: TDomString read FSystemID;
//property entities: TNamedNodeMap read FEntities; deprecated 'Removed in HTML5';
//property notations: TNamedNodeMap read FNotations; deprecated 'Removed in HTML5';
//property internalSubset: TDomString read FInternalSubset; deprecated 'Removed in HTML5';
end;
TDocument = class(TNode)
private
//FDocType: TDocumentType; is now a child of the document node as the DOM intended
FNamespaceURIList: TNamespaceURIList;
FSearchNodeLists: TList;
FQuirksMode: Boolean;
function GetDocumentElement: TElement;
function GetDocType: TDocumentType;
function GetHead: TElement;
function GetBody: TElement;
protected
function GetNodeName: TDomString; override;
function GetNodeType: TNodeType; override;
function CanInsert(Node: TNode): Boolean; override;
function createDocType(const name, publicId, systemId: TDomString): TDocumentType;
procedure AddSearchNodeList(NodeList: TNodeList);
procedure RemoveSearchNodeList(NodeList: TNodeList);
procedure InvalidateSearchNodeLists;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function cloneNode(deep: Boolean): TNode; override;
function createElement(const tagName: TDomString): TElement;
function createDocumentFragment: TDocumentFragment;
function createTextNode(const data: TDomString): TTextNode;
function CreateComment(const data: TDomString): TComment;
function createCDATASection(const data: TDomString): TCDATASection;
function createProcessingInstruction(const target, data: TDomString): TProcessingInstruction;
function createAttribute(const name: TDomString): TAttr;
//function createEntityReference(const name: TDomString): TEntityReference; removed in HTML 5
function importNode(importedNode: TNode; deep: Boolean): TNode;
function createElementNS(const namespaceURI, qualifiedName: TDomString): TElement;
function createAttributeNS(const namespaceURI, qualifiedName: TDomString): TAttr;
property Doctype: TDocumentType read GetDocType; //DONE: BUGBUG Should be readonly
property NamespaceURIList: TNamespaceURIList read FNamespaceURIList;
property DocumentElement: TElement read GetDocumentElement;
property QuirksMode: Boolean read FQuirksMode write FQuirksMode;
// DOM Tree Accessors - https://html.spec.whatwg.org/#dom-tree-accessors
property Head: TElement read GetHead; //Returns the head element.
property Body: TElement read GetBody; //Returns the body element.
end;
{
The DOMImplementation interface provides a number of methods for performing operations
that are independent of any particular instance of the document object model.
}
DomImplementation = class
public
class function createDocumentType(const QualifiedName, PublicId, SystemId: TDomString): TDocumentType;
class function createDocument(const NamespaceURI, QualifiedName: TDomString; Doctype: TDocumentType=nil): TDocument;
class function createHtmlDocument(const Title: TDomString=''): TDocument;
class function hasFeature(const feature, version: TDomString): Boolean; deprecated 'Useless; always returns true';
class function createEmptyDocument(doctype: TDocumentType): TDocument; // extension
class function createHtmlDocumentType(htmlDocType: Integer): TDocumentType; //extension
end;
implementation
uses
Entities, System.UITypes, System.Types;
const
ExceptionMsg: array[INDEX_SIZE_ERR..TYPE_MISMATCH_ERR] of string = (
'Index or size is negative, or greater than the allowed value',
'The specified range of text does not fit into a DOMString',
'Node is inserted somewhere it doesn''t belong ',
'Node is used in a different document than the one that created it',
'Invalid or illegal character is specified, such as in a name',
'Data is specified for a node which does not support data',
'An attempt is made to modify an object where modifications are not allowed',
'An attempt is made to reference a node in a context where it does not exist',
'Implementation does not support the requested type of object or operation',
'An attempt is made to add an attribute that is already in use elsewhere',
'An attempt is made to use an object that is not, or is no longer, usable',
'An invalid or illegal string is specified',
'An attempt is made to modify the type of the underlying object',
'An attempt is made to create or change an object in a way which is incorrect with regard to namespaces',
'A parameter or an operation is not supported by the underlying object',
'The method would make the Node invalid with respect to "partial validity', //VALIDATION_ERR
'The type of the object is incompatible with the expected type of the parameter associated to the object' //TYPE_MISMATCH_ERR
);
ID_NAME = 'id';
type
TDTDParams = record
PublicId: TDomString;
SystemId: TDomString;
end;
TDTDList = array[DTD_HTML_STRICT..DTD_XHTML_FRAMESET] of TDTDParams;
const
DTDList: TDTDList = (
(publicId: '-//W3C//DTD HTML 4.01//EN'; systemId: 'http://www.w3.org/TR/html4/strict.dtd'),
(publicId: '-//W3C//DTD HTML 4.01 Transitional//EN'; systemId: 'http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd'),
(publicId: '-//W3C//DTD HTML 4.01 Frameset//EN'; systemId: 'http://www.w3.org/TR/1999/REC-html401-19991224/frameset.dtd'),
(publicId: '-//W3C//DTD XHTML 1.0 Strict//EN'; systemId: 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'),
(publicId: '-//W3C//DTD XHTML 1.0 Transitional//EN'; systemId: 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'),
(publicId: '-//W3C//DTD XHTML 1.0 Frameset//EN'; systemId: 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd')
);
HTML_TAG_NAME = 'html';
type
TSearchNodeList = class(TNodeList)
private
FNamespaceParam : TDomString;
FNameParam : TDomString;
FSynchronized: Boolean;
function GetLength: Integer; override;
function acceptNode(node: TNode): Boolean;
procedure TraverseTree(rootNode: TNode);
procedure Rebuild;
public
constructor Create(AOwnerNode: TNode; const namespaceURI, name: TDomString);
destructor Destroy; override;
procedure Invalidate;
function Item(index: Integer): TNode; override;
end;
{
function Concat(const S1, S2: TDomString): TDomString;
begin
SetLength(Result, Length(S1) + Length(S2));
Move(S1[1], Result[1], 2 * Length(S1));
Move(S2[1], Result[Length(S1) + 1], 2 * Length(S2))
end;
}
function IsNCName(const Value: TDomString): Boolean;
begin
//TODO
Result := true
end;
constructor TNamespaceURIList.Create;
begin
inherited Create;
end;
destructor TNamespaceURIList.Destroy;
begin
inherited Destroy
end;
procedure TNamespaceURIList.Clear;
begin
SetLength(FList, 0);
end;
function TNamespaceURIList.GetItem(I: Integer): TDomString;
begin
Result := FList[I]
end;
function TNamespaceURIList.Add(const NamespaceURI: TDomString): Integer;
var
I: Integer;
begin
for I := 0 to High(FList) do
begin
if FList[I] = NamespaceURI then
begin
Result := I;
Exit
end;
end;
I := Length(FList);
SetLength(FList, I+1);
FList[I] := NamespaceURI;
Result := I;
end;
constructor DomException.Create(ErrorCode: Integer);
begin
inherited Create(ExceptionMsg[ErrorCode]);
FCode := ErrorCode
end;
constructor TNode.Create(ownerDocument: TDocument; const namespaceURI, qualifiedName: TDomString; withNS: Boolean);
var
I: Integer;
begin
inherited Create;
FOwnerDocument := ownerDocument;
SetNamespaceURI(namespaceURI);
if withNS then
begin
I := Pos(':', qualifiedName);
if I <> 0 then
begin
SetPrefix(Copy(qualifiedName, 1, I - 1));
SetLocalName(Copy(qualifiedName, I + 1, Length(qualifiedName) - I))
end
else
SetLocalName(qualifiedName)
end
else
SetLocalName(qualifiedName);
FChildNodes := TNodeList.Create(Self)
end;
destructor TNode.Destroy;
begin
if Assigned(FChildNodes) then
begin
FChildNodes.Clear(true);
FChildNodes.Free
end;
if Assigned(FAttributes) then
begin
FAttributes.Clear(true);
FAttributes.Free
end;
inherited Destroy
end;
function TNode.GetFirstChild: TNode;
begin
if ChildNodes.length <> 0 then
Result := ChildNodes.item(0)
else
Result := nil
end;
function TNode.GetLastChild: TNode;
begin
if ChildNodes.length <> 0 then
Result := ChildNodes.item(ChildNodes.length - 1)
else
Result := nil
end;
function TNode.GetPreviousSibling: TNode;
var
I: Integer;
begin
Result := nil;
if Assigned(ParentNode) then
begin
I := ParentNode.ChildNodes.IndexOf(Self);
if I > 0 then
Result := ParentNode.ChildNodes.item(I - 1)
end
end;
function TNode.GetTextContent: TDomString;
begin
Result := '';
end;
function TNode.GetNextSibling: TNode;
var
I: Integer;
begin
Result := nil;
if Assigned(ParentNode) then
begin
I := ParentNode.ChildNodes.IndexOf(Self);
if (I >= 0) and (I < ParentNode.ChildNodes.length - 1) then
Result := ParentNode.ChildNodes.item(I + 1)
end
end;
function TNode.GetNodeName: TDomString;
begin
{
The tagName getter steps are to return this’s HTML-uppercased qualified name.
But this isn't a tag name; this could be an attribute name - which should be lowercase
}
if FPrefix <> '' then
Result := FPrefix + ':' + FNodeName
else
Result := FNodeName;
end;
function TNode.GetNodeValue: TDomString;
begin
Result := FNodeValue
end;
function TNode.GetParentNode: TNode;
begin
Result := FParentNode
end;
function TNode.GetLocalName: TDomString;
begin
Result := FNodeName
end;
function TNode.CanInsert(Node: TNode): Boolean;
begin
Result := false;
end;
function TNode.ExportNode(otherDocument: TDocument; deep: Boolean): TNode;
begin
raise DomException.Create(NOT_SUPPORTED_ERR)
end;
function TNode.getElementsByTagName(const name: TDomString): TNodeList;
begin
Result := TSearchNodeList.Create(Self, '*', name)
end;
function TNode.getElementsByTagNameNS(const namespaceURI, localName: TDomString): TNodeList;
begin
Result := TSearchNodeList.Create(Self, namespaceURI, localName)
end;
function TNode.GetElementByID(const ElementID: TDomString): TElement;
var
element: TElement;
i: Integer;
attr: TAttr;
begin
{
Returns the Element that has an ID attribute with the given value.
If no such element exists, this returns null.
If more than one element has an ID attribute with that value,
what is returned is undefined.
The DOM implementation is expected to use the attribute Attr.isId to
determine if an attribute is of type ID.
Note: Attributes with the name "ID" or "id" are not of type ID unless so defined.
Parameters
- elementId of type DOMString
The unique id value for an element.
Return Value
- Element
The matching element or null if there is none.
}
Result := nil;
if NodeType = ELEMENT_NODE then
begin
element := Self as TElement;
attr := element.getAttributeNode('id');
if attr <> nil then
begin
Result := element;
Exit;
end;
end;
for i := 0 to ChildNodes.Length-1 do
begin
Result := ChildNodes.Item(i).GetElementByID(ElementID);
if Result <> nil then
Exit;
end;
end;
procedure TNode.SetNodeValue(const value: TDomString);
begin
raise DomException.Create(NO_MODIFICATION_ALLOWED_ERR)
end;
procedure TNode.SetOwnerDocument(const Value: TDocument);
begin
FOwnerDocument := Value;
end;
procedure TNode.SetNamespaceURI(const value: TDomString);
begin
if value <> '' then
//TODO validate
FNamespaceURI := OwnerDocument.namespaceURIList.Add(value)
end;
function TNode.GetNamespaceURI: TDomString;
begin
Result := OwnerDocument.namespaceURIList[FNamespaceURI]
end;
procedure TNode.SetPrefix(const value: TDomString);
begin
if not IsNCName(value) then
raise DomException.Create(INVALID_CHARACTER_ERR);
FPrefix := value
end;
procedure TNode.SetLocalName(const value: TDomString);
begin
if not IsNCName(value) then
raise DomException.Create(INVALID_CHARACTER_ERR);
FNodeName := value
end;
procedure TNode.CloneChildNodesFrom(Node: TNode);
var
childNode: TNode;
I: Integer;
begin
for I := 0 to Node.ChildNodes.length - 1 do
begin
childNode := Node.ChildNodes.item(I);
AppendChild(childNode.cloneNode(true))
end
end;
function TNode.InsertSingleNode(newChild, refChild: TNode): TNode;
var
I: Integer;
begin
if not CanInsert(newChild) or newChild.ancestorOf(Self) then
raise DomException.Create(HIERARCHY_REQUEST_ERR,
'NewChild: '+newChild.NodeName+'='+newChild.NodeValue);
if newChild <> refChild then
begin
if Assigned(refChild) then
begin
I := FChildNodes.IndexOf(refChild);
if I < 0 then
raise DomException.Create(NOT_FOUND_ERR);
FChildNodes.Insert(I, newChild)
end
else
FChildNodes.Add(newChild);
if Assigned(newChild.ParentNode) then
newChild.ParentNode.RemoveChild(newChild);
newChild.FParentNode := Self
end;
Result := newChild
end;
function TNode.InsertBefore(newChild, refChild: TNode): TNode;
begin
if (newChild.OwnerDocument <> nil) and (newChild.OwnerDocument <> OwnerDocument) then
raise DomException.Create(WRONG_DOCUMENT_ERR);
if newChild.NodeType = DOCUMENT_FRAGMENT_NODE then
begin
while Assigned(newChild.FirstChild) do
InsertSingleNode(newChild.FirstChild, refChild);
Result := newChild;
end
else
Result := InsertSingleNode(newChild, refChild);
if Assigned(OwnerDocument) then
OwnerDocument.InvalidateSearchNodeLists
end;
function TNode.ReplaceChild(newChild, oldChild: TNode): TNode;
begin
//1. If parent is not a Document, DocumentFragment, or Element node, then throw a "HierarchyRequestError" DOMException.
if not (Self.NodeType in [DOCUMENT_NODE, DOCUMENT_FRAGMENT_NODE, ELEMENT_NODE]) then
raise DomException.Create(HIERARCHY_REQUEST_ERR);
//3. If child’s parent is not parent, then throw a "NotFoundError" DOMException.
if oldChild.ParentNode <> Self then
raise DomException.Create(NOT_FOUND_ERR);
//4. If node is not a DocumentFragment, DocumentType, Element, or CharacterData node, then throw a "HierarchyRequestError" DOMException.
if not (newChild is TDocumentFragment) and not (newChild is TDocumentType) and not (newChild is TElement) and not (newChild is TCharacterData) then
raise DomException.Create(HIERARCHY_REQUEST_ERR);
//5. If either node is a Text node and parent is a document, or node is a doctype and parent is not a document, then throw a "HierarchyRequestError" DOMException.
if ((newChild is TTextNode) and (Self is TDocument)) or ((newChild is TDocumentType) and (not (Self is TDocument))) then
raise DomException.Create(HIERARCHY_REQUEST_ERR);
if newChild <> oldChild then
begin
insertBefore(newChild, oldChild);
RemoveChild(oldChild)
end;
Result := oldChild;
if Assigned(OwnerDocument) then
OwnerDocument.InvalidateSearchNodeLists
end;
function TNode.AppendChild(newChild: TNode): TNode;
begin
Result := insertBefore(newChild, nil);
if Assigned(OwnerDocument) then
OwnerDocument.InvalidateSearchNodeLists
end;
function TNode.RemoveChild(oldChild: TNode): TNode;
var
I: Integer;
begin
I := FChildNodes.IndexOf(oldChild);
if I < 0 then
raise DomException.Create(NOT_FOUND_ERR);
FChildNodes.Delete(I);
oldChild.FParentNode := nil;
Result := oldChild;
if Assigned(OwnerDocument) then
OwnerDocument.InvalidateSearchNodeLists
end;
function TNode.hasChildNodes: Boolean;
begin
Result := FChildNodes.length <> 0
end;
//function TNode.isSupported(const feature, version: TDomString): Boolean;
//begin
// Result := DOMImplementation.hasFeature(feature, version)
//end;
function TNode.HasAttributes: Boolean;
begin
Result := Assigned(FAttributes) and (FAttributes.length <> 0)
end;
function TNode.ancestorOf(node: TNode): Boolean;
begin
while Assigned(node) do
begin
if node = self then
begin
Result := true;
Exit
end;
node := node.ParentNode
end;
Result := false
end;
procedure TNode.normalize;
var
childNode: TNode;
textNode: TTextNode;
I: Integer;
begin
I := 0;
while I < ChildNodes.length do
begin
childNode := ChildNodes.item(I);
if childNode.NodeType = ELEMENT_NODE then
begin
(childNode as TElement).normalize;
Inc(I)
end
else
if childNode.NodeType = TEXT_NODE then
begin
textNode := childNode as TTextNode;
Inc(I);
childNode := ChildNodes.item(I);
while childNode.NodeType = TEXT_NODE do
begin
textNode.appendData((childNode as TTextNode).Data);
Inc(I);
childNode := ChildNodes.item(I)
end
end
else
Inc(I)
end
end;
constructor TNodeList.Create(AOwnerNode: TNode);
begin
inherited Create;
FOwnerNode := AOwnerNode;
FList := TList.Create
end;
destructor TNodeList.Destroy;
begin
FList.Free;
inherited Destroy
end;
function TNodeList.IndexOf(node: TNode): Integer;
begin
Result := FList.IndexOf(node)
end;
function TNodeList.GetLength: Integer;
begin
Result := FList.Count
end;
procedure TNodeList.Insert(I: Integer; Node: TNode);
begin
FList.Insert(I, Node)
end;
procedure TNodeList.Delete(I: Integer);
begin
FList.Delete(I)
end;
procedure TNodeList.Add(node: TNode);
begin
FList.Add(node)
end;
procedure TNodeList.Remove(node: TNode);
begin
FList.Remove(node)
end;
function TNodeList.item(index: Integer): TNode;
begin
if (index >= 0) and (index < length) then
Result := FList[index]
else
Result := nil
end;
procedure TNodeList.Clear(WithItems: Boolean);
var