DOM.pas 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  1. {
  2. This file is part of the Free Component Library
  3. Implementation of DOM interfaces
  4. Copyright (c) 1999-2003 by Sebastian Guenther, [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. This unit provides classes which implement the interfaces defined in the
  13. DOM (Document Object Model) specification.
  14. The current state is:
  15. DOM Level 1 - Almost completely implemented
  16. DOM Level 2 - Partially implemented
  17. Specification used for this implementation:
  18. "Document Object Model (DOM) Level 2 Specification Version 1.0
  19. W3C Candidate Recommendation 07 March, 2000"
  20. http://www.w3.org/TR/2000/CR-DOM-Level-2-20000307
  21. }
  22. unit DOM;
  23. interface
  24. uses SysUtils, Classes;
  25. type
  26. // -------------------------------------------------------
  27. // DOMString
  28. // -------------------------------------------------------
  29. {$IFDEF ver1_0}
  30. DOMString = String;
  31. {$ELSE}
  32. DOMString = WideString;
  33. {$ENDIF}
  34. // -------------------------------------------------------
  35. // DOMException
  36. // -------------------------------------------------------
  37. const
  38. // DOM Level 1 exception codes:
  39. INDEX_SIZE_ERR = 1; // index or size is negative, or greater than the allowed value
  40. DOMSTRING_SIZE_ERR = 2; // Specified range of text does not fit into a DOMString
  41. HIERARCHY_REQUEST_ERR = 3; // node is inserted somewhere it does not belong
  42. WRONG_DOCUMENT_ERR = 4; // node is used in a different document than the one that created it (that does not support it)
  43. INVALID_CHARACTER_ERR = 5; // invalid or illegal character is specified, such as in a name
  44. NO_DATA_ALLOWED_ERR = 6; // data is specified for a node which does not support data
  45. NO_MODIFICATION_ALLOWED_ERR = 7; // an attempt is made to modify an object where modifications are not allowed
  46. NOT_FOUND_ERR = 8; // an attempt is made to reference a node in a context where it does not exist
  47. NOT_SUPPORTED_ERR = 9; // implementation does not support the type of object requested
  48. INUSE_ATTRIBUTE_ERR = 10; // an attempt is made to add an attribute that is already in use elsewhere
  49. // DOM Level 2 exception codes:
  50. INVALID_STATE_ERR = 11; // an attempt is made to use an object that is not, or is no longer, usable
  51. SYNTAX_ERR = 12; // invalid or illegal string specified
  52. INVALID_MODIFICATION_ERR = 13; // an attempt is made to modify the type of the underlying object
  53. NAMESPACE_ERR = 14; // an attempt is made to create or change an object in a way which is incorrect with regard to namespaces
  54. INVALID_ACCESS_ERR = 15; // parameter or operation is not supported by the underlying object
  55. type
  56. EDOMError = class(Exception)
  57. protected
  58. constructor Create(ACode: Integer; const ASituation: String);
  59. public
  60. Code: Integer;
  61. end;
  62. EDOMIndexSize = class(EDOMError)
  63. public
  64. constructor Create(const ASituation: String);
  65. end;
  66. EDOMHierarchyRequest = class(EDOMError)
  67. public
  68. constructor Create(const ASituation: String);
  69. end;
  70. EDOMWrongDocument = class(EDOMError)
  71. public
  72. constructor Create(const ASituation: String);
  73. end;
  74. EDOMNotFound = class(EDOMError)
  75. public
  76. constructor Create(const ASituation: String);
  77. end;
  78. EDOMNotSupported = class(EDOMError)
  79. public
  80. constructor Create(const ASituation: String);
  81. end;
  82. EDOMInUseAttribute = class(EDOMError)
  83. public
  84. constructor Create(const ASituation: String);
  85. end;
  86. EDOMInvalidState = class(EDOMError)
  87. public
  88. constructor Create(const ASituation: String);
  89. end;
  90. EDOMSyntax = class(EDOMError)
  91. public
  92. constructor Create(const ASituation: String);
  93. end;
  94. EDOMInvalidModification = class(EDOMError)
  95. public
  96. constructor Create(const ASituation: String);
  97. end;
  98. EDOMNamespace = class(EDOMError)
  99. public
  100. constructor Create(const ASituation: String);
  101. end;
  102. EDOMInvalidAccess = class(EDOMError)
  103. public
  104. constructor Create(const ASituation: String);
  105. end;
  106. // -------------------------------------------------------
  107. // Node
  108. // -------------------------------------------------------
  109. const
  110. ELEMENT_NODE = 1;
  111. ATTRIBUTE_NODE = 2;
  112. TEXT_NODE = 3;
  113. CDATA_SECTION_NODE = 4;
  114. ENTITY_REFERENCE_NODE = 5;
  115. ENTITY_NODE = 6;
  116. PROCESSING_INSTRUCTION_NODE = 7;
  117. COMMENT_NODE = 8;
  118. DOCUMENT_NODE = 9;
  119. DOCUMENT_TYPE_NODE = 10;
  120. DOCUMENT_FRAGMENT_NODE = 11;
  121. NOTATION_NODE = 12;
  122. type
  123. TDOMImplementation = class;
  124. TDOMDocumentFragment = class;
  125. TDOMDocument = class;
  126. TDOMNode = class;
  127. TDOMNodeList = class;
  128. TDOMNamedNodeMap = class;
  129. TDOMCharacterData = class;
  130. TDOMAttr = class;
  131. TDOMElement = class;
  132. TDOMText = class;
  133. TDOMComment = class;
  134. TDOMCDATASection = class;
  135. TDOMDocumentType = class;
  136. TDOMNotation = class;
  137. TDOMEntity = class;
  138. TDOMEntityReference = class;
  139. TDOMProcessingInstruction = class;
  140. TRefClass = class
  141. protected
  142. RefCounter: LongInt;
  143. public
  144. constructor Create;
  145. function AddRef: LongInt; virtual;
  146. function Release: LongInt; virtual;
  147. end;
  148. TDOMNode = class
  149. protected
  150. FNodeName, FNodeValue: DOMString;
  151. FNodeType: Integer;
  152. FParentNode: TDOMNode;
  153. FPreviousSibling, FNextSibling: TDOMNode;
  154. FOwnerDocument: TDOMDocument;
  155. function GetNodeValue: DOMString; virtual;
  156. procedure SetNodeValue(AValue: DOMString); virtual;
  157. function GetFirstChild: TDOMNode; virtual;
  158. function GetLastChild: TDOMNode; virtual;
  159. function GetAttributes: TDOMNamedNodeMap; virtual;
  160. constructor Create(AOwner: TDOMDocument);
  161. public
  162. // Free NodeList with TDOMNodeList.Release!
  163. function GetChildNodes: TDOMNodeList; virtual;
  164. property NodeName: DOMString read FNodeName;
  165. property NodeValue: DOMString read GetNodeValue write SetNodeValue;
  166. property NodeType: Integer read FNodeType;
  167. property ParentNode: TDOMNode read FParentNode;
  168. property FirstChild: TDOMNode read GetFirstChild;
  169. property LastChild: TDOMNode read GetLastChild;
  170. property ChildNodes: TDOMNodeList read GetChildNodes;
  171. property PreviousSibling: TDOMNode read FPreviousSibling;
  172. property NextSibling: TDOMNode read FNextSibling;
  173. property Attributes: TDOMNamedNodeMap read GetAttributes;
  174. property OwnerDocument: TDOMDocument read FOwnerDocument;
  175. function InsertBefore(NewChild, RefChild: TDOMNode): TDOMNode; virtual;
  176. function ReplaceChild(NewChild, OldChild: TDOMNode): TDOMNode; virtual;
  177. function RemoveChild(OldChild: TDOMNode): TDOMNode; virtual;
  178. function AppendChild(NewChild: TDOMNode): TDOMNode; virtual;
  179. function HasChildNodes: Boolean; virtual;
  180. function CloneNode(deep: Boolean): TDOMNode; overload;
  181. // Extensions to DOM interface:
  182. function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  183. overload; virtual;
  184. function FindNode(const ANodeName: DOMString): TDOMNode;
  185. end;
  186. { The following class is an implementation specific extension, it is just an
  187. extended implementation of TDOMNode, the generic DOM::Node interface
  188. implementation. (Its main purpose is to save memory in a big node tree) }
  189. TDOMNode_WithChildren = class(TDOMNode)
  190. protected
  191. FFirstChild, FLastChild: TDOMNode;
  192. function GetFirstChild: TDOMNode; override;
  193. function GetLastChild: TDOMNode; override;
  194. procedure CloneChildren(ACopy: TDOMNode; ACloneOwner: TDOMDocument);
  195. public
  196. destructor Destroy; override;
  197. function InsertBefore(NewChild, RefChild: TDOMNode): TDOMNode; override;
  198. function ReplaceChild(NewChild, OldChild: TDOMNode): TDOMNode; override;
  199. function RemoveChild(OldChild: TDOMNode): TDOMNode; override;
  200. function AppendChild(NewChild: TDOMNode): TDOMNode; override;
  201. function HasChildNodes: Boolean; override;
  202. end;
  203. // -------------------------------------------------------
  204. // NodeList
  205. // -------------------------------------------------------
  206. TDOMNodeList = class(TRefClass)
  207. protected
  208. node: TDOMNode;
  209. filter: DOMString;
  210. UseFilter: Boolean;
  211. constructor Create(ANode: TDOMNode; AFilter: DOMString);
  212. function GetCount: LongInt;
  213. function GetItem(index: LongWord): TDOMNode;
  214. public
  215. property Item[index: LongWord]: TDOMNode read GetItem;
  216. property Count: LongInt read GetCount;
  217. end;
  218. // -------------------------------------------------------
  219. // NamedNodeMap
  220. // -------------------------------------------------------
  221. TDOMNamedNodeMap = class(TList)
  222. protected
  223. OwnerDocument: TDOMDocument;
  224. function GetItem(index: LongWord): TDOMNode;
  225. procedure SetItem(index: LongWord; AItem: TDOMNode);
  226. function GetLength: LongInt;
  227. constructor Create(AOwner: TDOMDocument);
  228. public
  229. function GetNamedItem(const name: DOMString): TDOMNode;
  230. function SetNamedItem(arg: TDOMNode): TDOMNode;
  231. function RemoveNamedItem(const name: DOMString): TDOMNode;
  232. property Item[index: LongWord]: TDOMNode read GetItem write SetItem; default;
  233. property Length: LongInt read GetLength;
  234. end;
  235. // -------------------------------------------------------
  236. // CharacterData
  237. // -------------------------------------------------------
  238. TDOMCharacterData = class(TDOMNode)
  239. protected
  240. function GetLength: LongInt;
  241. public
  242. property Data: DOMString read FNodeValue;
  243. property Length: LongInt read GetLength;
  244. function SubstringData(offset, count: LongWord): DOMString;
  245. procedure AppendData(const arg: DOMString);
  246. procedure InsertData(offset: LongWord; const arg: DOMString);
  247. procedure DeleteData(offset, count: LongWord);
  248. procedure ReplaceData(offset, count: LongWord; const arg: DOMString);
  249. end;
  250. // -------------------------------------------------------
  251. // DOMImplementation
  252. // -------------------------------------------------------
  253. TDOMImplementation = class
  254. public
  255. function HasFeature(const feature, version: DOMString): Boolean;
  256. // Introduced in DOM Level 2:
  257. function CreateDocumentType(const QualifiedName, PublicID,
  258. SystemID: DOMString): TDOMDocumentType;
  259. function CreateDocument(const NamespaceURI, QualifiedName: DOMString;
  260. doctype: TDOMDocumentType): TDOMDocument;
  261. end;
  262. // -------------------------------------------------------
  263. // DocumentFragment
  264. // -------------------------------------------------------
  265. TDOMDocumentFragment = class(TDOMNode_WithChildren)
  266. protected
  267. constructor Create(AOwner: TDOMDocument);
  268. end;
  269. // -------------------------------------------------------
  270. // Document
  271. // -------------------------------------------------------
  272. TDOMDocument = class(TDOMNode_WithChildren)
  273. protected
  274. FDocType: TDOMDocumentType;
  275. FImplementation: TDOMImplementation;
  276. function GetDocumentElement: TDOMElement;
  277. public
  278. property DocType: TDOMDocumentType read FDocType;
  279. property Impl: TDOMImplementation read FImplementation;
  280. property DocumentElement: TDOMElement read GetDocumentElement;
  281. function CreateElement(const tagName: DOMString): TDOMElement; virtual;
  282. function CreateDocumentFragment: TDOMDocumentFragment;
  283. function CreateTextNode(const data: DOMString): TDOMText;
  284. function CreateComment(const data: DOMString): TDOMComment;
  285. function CreateCDATASection(const data: DOMString): TDOMCDATASection;
  286. virtual;
  287. function CreateProcessingInstruction(const target, data: DOMString):
  288. TDOMProcessingInstruction; virtual;
  289. function CreateAttribute(const name: DOMString): TDOMAttr; virtual;
  290. function CreateEntityReference(const name: DOMString): TDOMEntityReference;
  291. virtual;
  292. // Free NodeList with TDOMNodeList.Release!
  293. function GetElementsByTagName(const tagname: DOMString): TDOMNodeList;
  294. // Extensions to DOM interface:
  295. constructor Create; virtual;
  296. function CreateEntity(const data: DOMString): TDOMEntity;
  297. end;
  298. TXMLDocument = class(TDOMDocument)
  299. public
  300. // These fields are extensions to the DOM interface:
  301. XMLVersion, Encoding, StylesheetType, StylesheetHRef: DOMString;
  302. function CreateCDATASection(const data: DOMString): TDOMCDATASection; override;
  303. function CreateProcessingInstruction(const target, data: DOMString):
  304. TDOMProcessingInstruction; override;
  305. function CreateEntityReference(const name: DOMString): TDOMEntityReference; override;
  306. end;
  307. // -------------------------------------------------------
  308. // Attr
  309. // -------------------------------------------------------
  310. TDOMAttr = class(TDOMNode_WithChildren)
  311. protected
  312. FSpecified: Boolean;
  313. AttrOwner: TDOMNamedNodeMap;
  314. function GetNodeValue: DOMString; override;
  315. procedure SetNodeValue(AValue: DOMString); override;
  316. constructor Create(AOwner: TDOMDocument);
  317. public
  318. function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  319. overload; override;
  320. property Name: DOMString read FNodeName;
  321. property Specified: Boolean read FSpecified;
  322. property Value: DOMString read GetNodeValue write SetNodeValue;
  323. end;
  324. // -------------------------------------------------------
  325. // Element
  326. // -------------------------------------------------------
  327. TDOMElement = class(TDOMNode_WithChildren)
  328. protected
  329. FAttributes: TDOMNamedNodeMap;
  330. function GetAttributes: TDOMNamedNodeMap; override;
  331. constructor Create(AOwner: TDOMDocument); virtual;
  332. public
  333. destructor Destroy; override;
  334. function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  335. overload; override;
  336. property TagName: DOMString read FNodeName;
  337. function GetAttribute(const name: DOMString): DOMString;
  338. procedure SetAttribute(const name, value: DOMString);
  339. procedure RemoveAttribute(const name: DOMString);
  340. function GetAttributeNode(const name: DOMString): TDOMAttr;
  341. procedure SetAttributeNode(NewAttr: TDOMAttr);
  342. function RemoveAttributeNode(OldAttr: TDOMAttr): TDOMAttr;
  343. // Free NodeList with TDOMNodeList.Release!
  344. function GetElementsByTagName(const name: DOMString): TDOMNodeList;
  345. procedure Normalize;
  346. property AttribStrings[const Name: DOMString]: DOMString
  347. read GetAttribute write SetAttribute; default;
  348. end;
  349. // -------------------------------------------------------
  350. // Text
  351. // -------------------------------------------------------
  352. TDOMText = class(TDOMCharacterData)
  353. protected
  354. constructor Create(AOwner: TDOMDocument);
  355. public
  356. function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  357. overload; override;
  358. function SplitText(offset: LongWord): TDOMText;
  359. end;
  360. // -------------------------------------------------------
  361. // Comment
  362. // -------------------------------------------------------
  363. TDOMComment = class(TDOMCharacterData)
  364. protected
  365. constructor Create(AOwner: TDOMDocument);
  366. public
  367. function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  368. overload; override;
  369. end;
  370. // -------------------------------------------------------
  371. // CDATASection
  372. // -------------------------------------------------------
  373. TDOMCDATASection = class(TDOMText)
  374. protected
  375. constructor Create(AOwner: TDOMDocument);
  376. public
  377. function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  378. overload; override;
  379. end;
  380. // -------------------------------------------------------
  381. // DocumentType
  382. // -------------------------------------------------------
  383. TDOMDocumentType = class(TDOMNode)
  384. protected
  385. FEntities, FNotations: TDOMNamedNodeMap;
  386. constructor Create(AOwner: TDOMDocument);
  387. public
  388. function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  389. overload; override;
  390. property Name: DOMString read FNodeName;
  391. property Entities: TDOMNamedNodeMap read FEntities;
  392. property Notations: TDOMNamedNodeMap read FEntities;
  393. end;
  394. // -------------------------------------------------------
  395. // Notation
  396. // -------------------------------------------------------
  397. TDOMNotation = class(TDOMNode)
  398. protected
  399. FPublicID, FSystemID: DOMString;
  400. constructor Create(AOwner: TDOMDocument);
  401. public
  402. function CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  403. overload; override;
  404. property PublicID: DOMString read FPublicID;
  405. property SystemID: DOMString read FSystemID;
  406. end;
  407. // -------------------------------------------------------
  408. // Entity
  409. // -------------------------------------------------------
  410. TDOMEntity = class(TDOMNode_WithChildren)
  411. protected
  412. FPublicID, FSystemID, FNotationName: DOMString;
  413. constructor Create(AOwner: TDOMDocument);
  414. public
  415. property PublicID: DOMString read FPublicID;
  416. property SystemID: DOMString read FSystemID;
  417. property NotationName: DOMString read FNotationName;
  418. end;
  419. // -------------------------------------------------------
  420. // EntityReference
  421. // -------------------------------------------------------
  422. TDOMEntityReference = class(TDOMNode_WithChildren)
  423. protected
  424. constructor Create(AOwner: TDOMDocument);
  425. end;
  426. // -------------------------------------------------------
  427. // ProcessingInstruction
  428. // -------------------------------------------------------
  429. TDOMProcessingInstruction = class(TDOMNode)
  430. protected
  431. constructor Create(AOwner: TDOMDocument);
  432. public
  433. property Target: DOMString read FNodeName;
  434. property Data: DOMString read FNodeValue;
  435. end;
  436. // =======================================================
  437. // =======================================================
  438. implementation
  439. constructor TRefClass.Create;
  440. begin
  441. inherited Create;
  442. RefCounter := 1;
  443. end;
  444. function TRefClass.AddRef: LongInt;
  445. begin
  446. Inc(RefCounter);
  447. Result := RefCounter;
  448. end;
  449. function TRefClass.Release: LongInt;
  450. begin
  451. Dec(RefCounter);
  452. Result := RefCounter;
  453. if RefCounter <= 0 then Free;
  454. end;
  455. // -------------------------------------------------------
  456. // DOM Exception
  457. // -------------------------------------------------------
  458. constructor EDOMError.Create(ACode: Integer; const ASituation: String);
  459. begin
  460. Code := ACode;
  461. inherited Create(Self.ClassName + ' in ' + ASituation);
  462. end;
  463. constructor EDOMIndexSize.Create(const ASituation: String); // 1
  464. begin
  465. inherited Create(INDEX_SIZE_ERR, ASituation);
  466. end;
  467. constructor EDOMHierarchyRequest.Create(const ASituation: String); // 3
  468. begin
  469. inherited Create(HIERARCHY_REQUEST_ERR, ASituation);
  470. end;
  471. constructor EDOMWrongDocument.Create(const ASituation: String); // 4
  472. begin
  473. inherited Create(WRONG_DOCUMENT_ERR, ASituation);
  474. end;
  475. constructor EDOMNotFound.Create(const ASituation: String); // 8
  476. begin
  477. inherited Create(NOT_FOUND_ERR, ASituation);
  478. end;
  479. constructor EDOMNotSupported.Create(const ASituation: String); // 9
  480. begin
  481. inherited Create(NOT_SUPPORTED_ERR, ASituation);
  482. end;
  483. constructor EDOMInUseAttribute.Create(const ASituation: String); // 10
  484. begin
  485. inherited Create(INUSE_ATTRIBUTE_ERR, ASituation);
  486. end;
  487. constructor EDOMInvalidState.Create(const ASituation: String); // 11
  488. begin
  489. inherited Create(INVALID_STATE_ERR, ASituation);
  490. end;
  491. constructor EDOMSyntax.Create(const ASituation: String); // 12
  492. begin
  493. inherited Create(SYNTAX_ERR, ASituation);
  494. end;
  495. constructor EDOMInvalidModification.Create(const ASituation: String); // 13
  496. begin
  497. inherited Create(INVALID_MODIFICATION_ERR, ASituation);
  498. end;
  499. constructor EDOMNamespace.Create(const ASituation: String); // 14
  500. begin
  501. inherited Create(NAMESPACE_ERR, ASituation);
  502. end;
  503. constructor EDOMInvalidAccess.Create(const ASituation: String); // 15
  504. begin
  505. inherited Create(INVALID_ACCESS_ERR, ASituation);
  506. end;
  507. // -------------------------------------------------------
  508. // Node
  509. // -------------------------------------------------------
  510. constructor TDOMNode.Create(AOwner: TDOMDocument);
  511. begin
  512. FOwnerDocument := AOwner;
  513. inherited Create;
  514. end;
  515. function TDOMNode.GetNodeValue: DOMString;
  516. begin
  517. Result := FNodeValue;
  518. end;
  519. procedure TDOMNode.SetNodeValue(AValue: DOMString);
  520. begin
  521. FNodeValue := AValue;
  522. end;
  523. function TDOMNode.GetChildNodes: TDOMNodeList;
  524. begin
  525. Result := TDOMNodeList.Create(Self, '*');
  526. end;
  527. function TDOMNode.GetFirstChild: TDOMNode; begin Result := nil end;
  528. function TDOMNode.GetLastChild: TDOMNode; begin Result := nil end;
  529. function TDOMNode.GetAttributes: TDOMNamedNodeMap; begin Result := nil end;
  530. function TDOMNode.InsertBefore(NewChild, RefChild: TDOMNode): TDOMNode;
  531. begin
  532. raise EDOMHierarchyRequest.Create('Node.InsertBefore');
  533. end;
  534. function TDOMNode.ReplaceChild(NewChild, OldChild: TDOMNode): TDOMNode;
  535. begin
  536. raise EDOMHierarchyRequest.Create('Node.ReplaceChild');
  537. end;
  538. function TDOMNode.RemoveChild(OldChild: TDOMNode): TDOMNode;
  539. begin
  540. raise EDOMHierarchyRequest.Create('Node.RemoveChild');
  541. end;
  542. function TDOMNode.AppendChild(NewChild: TDOMNode): TDOMNode;
  543. begin
  544. raise EDOMHierarchyRequest.Create('Node.AppendChild');
  545. end;
  546. function TDOMNode.HasChildNodes: Boolean;
  547. begin
  548. Result := False;
  549. end;
  550. function TDOMNode.CloneNode(deep: Boolean): TDOMNode;
  551. begin
  552. Result:=CloneNode(deep, FOwnerDocument);
  553. end;
  554. function TDOMNode.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  555. begin
  556. raise EDOMNotSupported.Create('CloneNode not implemented for ' + ClassName);
  557. end;
  558. function TDOMNode.FindNode(const ANodeName: DOMString): TDOMNode;
  559. var
  560. child: TDOMNode;
  561. begin
  562. child := FirstChild;
  563. while Assigned(child) do
  564. begin
  565. if child.NodeName = ANodeName then
  566. begin
  567. Result := child;
  568. exit;
  569. end;
  570. child := child.NextSibling;
  571. end;
  572. Result := nil;
  573. end;
  574. function TDOMNode_WithChildren.GetFirstChild: TDOMNode;
  575. begin
  576. Result := FFirstChild;
  577. end;
  578. function TDOMNode_WithChildren.GetLastChild: TDOMNode;
  579. begin
  580. Result := FLastChild;
  581. end;
  582. destructor TDOMNode_WithChildren.Destroy;
  583. var
  584. child, next: TDOMNode;
  585. begin
  586. child := FirstChild;
  587. while Assigned(child) do
  588. begin
  589. next := child.NextSibling;
  590. child.Free;
  591. child := next;
  592. end;
  593. inherited Destroy;
  594. end;
  595. function TDOMNode_WithChildren.InsertBefore(NewChild, RefChild: TDOMNode):
  596. TDOMNode;
  597. var
  598. i: Integer;
  599. begin
  600. Result := NewChild;
  601. if not Assigned(RefChild) then
  602. begin
  603. AppendChild(NewChild);
  604. exit;
  605. end;
  606. if NewChild.FOwnerDocument <> FOwnerDocument then
  607. raise EDOMWrongDocument.Create('NodeWC.InsertBefore');
  608. if RefChild.ParentNode <> Self then
  609. raise EDOMHierarchyRequest.Create('NodeWC.InsertBefore');
  610. if NewChild.NodeType = DOCUMENT_FRAGMENT_NODE then
  611. raise EDOMNotSupported.Create('NodeWC.InsertBefore for DocumentFragment');
  612. NewChild.FNextSibling := RefChild;
  613. if RefChild = FFirstChild then
  614. FFirstChild := NewChild
  615. else
  616. begin
  617. RefChild.FPreviousSibling.FNextSibling := NewChild;
  618. NewChild.FPreviousSibling := RefChild.FPreviousSibling;
  619. end;
  620. RefChild.FPreviousSibling := NewChild;
  621. NewChild.FParentNode := Self;
  622. end;
  623. function TDOMNode_WithChildren.ReplaceChild(NewChild, OldChild: TDOMNode):
  624. TDOMNode;
  625. begin
  626. InsertBefore(NewChild, OldChild);
  627. if Assigned(OldChild) then
  628. RemoveChild(OldChild);
  629. Result := NewChild;
  630. end;
  631. function TDOMNode_WithChildren.RemoveChild(OldChild: TDOMNode):
  632. TDOMNode;
  633. begin
  634. if OldChild.ParentNode <> Self then
  635. raise EDOMHierarchyRequest.Create('NodeWC.RemoveChild');
  636. if OldChild = FFirstChild then
  637. FFirstChild := FFirstChild.NextSibling
  638. else
  639. OldChild.FPreviousSibling.FNextSibling := OldChild.FNextSibling;
  640. if OldChild = FLastChild then
  641. FLastChild := FLastChild.FPreviousSibling
  642. else
  643. OldChild.FNextSibling.FPreviousSibling := OldChild.FPreviousSibling;
  644. OldChild.Free;
  645. end;
  646. function TDOMNode_WithChildren.AppendChild(NewChild: TDOMNode): TDOMNode;
  647. var
  648. Parent: TDOMNode;
  649. begin
  650. if NewChild.FOwnerDocument <> FOwnerDocument then
  651. raise EDOMWrongDocument.Create('NodeWC.AppendChild');
  652. Parent := Self;
  653. while Assigned(Parent) do
  654. begin
  655. if Parent = NewChild then
  656. raise EDOMHierarchyRequest.Create('NodeWC.AppendChild (cycle in tree)');
  657. Parent := Parent.ParentNode;
  658. end;
  659. if NewChild.FParentNode = Self then
  660. RemoveChild(NewChild);
  661. if NewChild.NodeType = DOCUMENT_FRAGMENT_NODE then
  662. raise EDOMNotSupported.Create('NodeWC.AppendChild for DocumentFragments')
  663. else begin
  664. if Assigned(FFirstChild) then
  665. begin
  666. FLastChild.FNextSibling := NewChild;
  667. NewChild.FPreviousSibling := FLastChild;
  668. end else
  669. FFirstChild := NewChild;
  670. FLastChild := NewChild;
  671. NewChild.FParentNode := Self;
  672. end;
  673. Result := NewChild;
  674. end;
  675. function TDOMNode_WithChildren.HasChildNodes: Boolean;
  676. begin
  677. Result := Assigned(FFirstChild);
  678. end;
  679. procedure TDOMNode_WithChildren.CloneChildren(ACopy: TDOMNode; ACloneOwner: TDOMDocument);
  680. var
  681. node: TDOMNode;
  682. begin
  683. node := FirstChild;
  684. while Assigned(node) do
  685. begin
  686. ACopy.AppendChild(node.CloneNode(True, ACloneOwner));
  687. node := node.NextSibling;
  688. end;
  689. end;
  690. // -------------------------------------------------------
  691. // NodeList
  692. // -------------------------------------------------------
  693. constructor TDOMNodeList.Create(ANode: TDOMNode; AFilter: DOMString);
  694. begin
  695. inherited Create;
  696. node := ANode;
  697. filter := AFilter;
  698. UseFilter := filter <> '*';
  699. end;
  700. function TDOMNodeList.GetCount: LongInt;
  701. var
  702. child: TDOMNode;
  703. begin
  704. Result := 0;
  705. child := node.FirstChild;
  706. while Assigned(child) do
  707. begin
  708. if (not UseFilter) or (child.NodeName = filter) then
  709. Inc(Result);
  710. child := child.NextSibling;
  711. end;
  712. end;
  713. function TDOMNodeList.GetItem(index: LongWord): TDOMNode;
  714. var
  715. child: TDOMNode;
  716. begin
  717. Result := nil;
  718. if index < 0 then
  719. exit;
  720. child := node.FirstChild;
  721. while Assigned(child) do
  722. begin
  723. if index = 0 then
  724. begin
  725. Result := child;
  726. break;
  727. end;
  728. if (not UseFilter) or (child.NodeName = filter) then
  729. Dec(index);
  730. child := child.NextSibling;
  731. end;
  732. end;
  733. // -------------------------------------------------------
  734. // NamedNodeMap
  735. // -------------------------------------------------------
  736. constructor TDOMNamedNodeMap.Create(AOwner: TDOMDocument);
  737. begin
  738. inherited Create;
  739. OwnerDocument := AOwner;
  740. end;
  741. function TDOMNamedNodeMap.GetItem(index: LongWord): TDOMNode;
  742. begin
  743. Result := TDOMNode(Items[index]);
  744. end;
  745. procedure TDOMNamedNodeMap.SetItem(index: LongWord; AItem: TDOMNode);
  746. begin
  747. Items[index] := AItem;
  748. end;
  749. function TDOMNamedNodeMap.GetLength: LongInt;
  750. begin
  751. Result := Count;
  752. end;
  753. function TDOMNamedNodeMap.GetNamedItem(const name: DOMString): TDOMNode;
  754. var
  755. i: Integer;
  756. begin
  757. for i := 0 to Count - 1 do
  758. begin
  759. Result := Item[i];
  760. if Result.NodeName = name then
  761. exit;
  762. end;
  763. Result := nil;
  764. end;
  765. function TDOMNamedNodeMap.SetNamedItem(arg: TDOMNode): TDOMNode;
  766. var
  767. i: Integer;
  768. begin
  769. if arg.FOwnerDocument <> OwnerDocument then
  770. raise EDOMWrongDocument.Create('NamedNodeMap.SetNamedItem');
  771. if arg.NodeType = ATTRIBUTE_NODE then
  772. begin
  773. if Assigned(TDOMAttr(arg).AttrOwner) then
  774. raise EDOMInUseAttribute.Create('NamedNodeMap.SetNamedItem');
  775. TDOMAttr(arg).AttrOwner := Self;
  776. end;
  777. for i := 0 to Count - 1 do
  778. if Item[i].NodeName = arg.NodeName then
  779. begin
  780. Result := Item[i];
  781. Item[i] := arg;
  782. exit;
  783. end;
  784. Add(arg);
  785. Result := nil;
  786. end;
  787. function TDOMNamedNodeMap.RemoveNamedItem(const name: DOMString): TDOMNode;
  788. var
  789. i: Integer;
  790. begin
  791. for i := 0 to Count - 1 do
  792. if Item[i].NodeName = name then
  793. begin
  794. Result := Item[i];
  795. Result.FParentNode := nil;
  796. exit;
  797. end;
  798. raise EDOMNotFound.Create('NamedNodeMap.RemoveNamedItem');
  799. end;
  800. // -------------------------------------------------------
  801. // CharacterData
  802. // -------------------------------------------------------
  803. function TDOMCharacterData.GetLength: LongInt;
  804. begin
  805. Result := system.Length(FNodeValue);
  806. end;
  807. function TDOMCharacterData.SubstringData(offset, count: LongWord): DOMString;
  808. begin
  809. if (offset < 0) or (offset > Length) or (count < 0) then
  810. raise EDOMIndexSize.Create('CharacterData.SubstringData');
  811. Result := Copy(FNodeValue, offset + 1, count);
  812. end;
  813. procedure TDOMCharacterData.AppendData(const arg: DOMString);
  814. begin
  815. FNodeValue := FNodeValue + arg;
  816. end;
  817. procedure TDOMCharacterData.InsertData(offset: LongWord; const arg: DOMString);
  818. begin
  819. if (offset < 0) or (offset > Length) then
  820. raise EDOMIndexSize.Create('CharacterData.InsertData');
  821. FNodeValue := Copy(FNodeValue, 1, offset) + arg +
  822. Copy(FNodeValue, offset + 1, Length);
  823. end;
  824. procedure TDOMCharacterData.DeleteData(offset, count: LongWord);
  825. begin
  826. if (offset < 0) or (offset > Length) or (count < 0) then
  827. raise EDOMIndexSize.Create('CharacterData.DeleteData');
  828. FNodeValue := Copy(FNodeValue, 1, offset) +
  829. Copy(FNodeValue, offset + count + 1, Length);
  830. end;
  831. procedure TDOMCharacterData.ReplaceData(offset, count: LongWord; const arg: DOMString);
  832. begin
  833. DeleteData(offset, count);
  834. InsertData(offset, arg);
  835. end;
  836. // -------------------------------------------------------
  837. // DocumentFragmet
  838. // -------------------------------------------------------
  839. constructor TDOMDocumentFragment.Create(AOwner: TDOMDocument);
  840. begin
  841. FNodeType := DOCUMENT_FRAGMENT_NODE;
  842. FNodeName := '#document-fragment';
  843. inherited Create(AOwner);
  844. end;
  845. // -------------------------------------------------------
  846. // DOMImplementation
  847. // -------------------------------------------------------
  848. function TDOMImplementation.HasFeature(const feature, version: DOMString):
  849. Boolean;
  850. begin
  851. Result := False;
  852. end;
  853. function TDOMImplementation.CreateDocumentType(const QualifiedName, PublicID,
  854. SystemID: DOMString): TDOMDocumentType;
  855. begin
  856. // !!!: Implement this method (easy to do)
  857. raise EDOMNotSupported.Create('DOMImplementation.CreateDocumentType');
  858. end;
  859. function TDOMImplementation.CreateDocument(const NamespaceURI,
  860. QualifiedName: DOMString; doctype: TDOMDocumentType): TDOMDocument;
  861. begin
  862. // !!!: Implement this method (easy to do)
  863. raise EDOMNotSupported.Create('DOMImplementation.CreateDocument');
  864. end;
  865. // -------------------------------------------------------
  866. // Document
  867. // -------------------------------------------------------
  868. constructor TDOMDocument.Create;
  869. begin
  870. FNodeType := DOCUMENT_NODE;
  871. FNodeName := '#document';
  872. inherited Create(nil);
  873. FOwnerDocument := Self;
  874. end;
  875. function TDOMDocument.GetDocumentElement: TDOMElement;
  876. var
  877. node: TDOMNode;
  878. begin
  879. node := FFirstChild;
  880. while Assigned(node) do
  881. begin
  882. if node.FNodeType = ELEMENT_NODE then
  883. begin
  884. Result := TDOMElement(node);
  885. exit;
  886. end;
  887. node := node.NextSibling;
  888. end;
  889. Result := nil;
  890. end;
  891. function TDOMDocument.CreateElement(const tagName: DOMString): TDOMElement;
  892. begin
  893. Result := TDOMElement.Create(Self);
  894. Result.FNodeName := tagName;
  895. end;
  896. function TDOMDocument.CreateDocumentFragment: TDOMDocumentFragment;
  897. begin
  898. Result := TDOMDocumentFragment.Create(Self);
  899. end;
  900. function TDOMDocument.CreateTextNode(const data: DOMString): TDOMText;
  901. begin
  902. Result := TDOMText.Create(Self);
  903. Result.FNodeValue := data;
  904. end;
  905. function TDOMDocument.CreateComment(const data: DOMString): TDOMComment;
  906. begin
  907. Result := TDOMComment.Create(Self);
  908. Result.FNodeValue := data;
  909. end;
  910. function TDOMDocument.CreateCDATASection(const data: DOMString):
  911. TDOMCDATASection;
  912. begin
  913. raise EDOMNotSupported.Create('DOMDocument.CreateCDATASection');
  914. end;
  915. function TDOMDocument.CreateProcessingInstruction(const target,
  916. data: DOMString): TDOMProcessingInstruction;
  917. begin
  918. raise EDOMNotSupported.Create('DOMDocument.CreateProcessingInstruction');
  919. end;
  920. function TDOMDocument.CreateAttribute(const name: DOMString): TDOMAttr;
  921. begin
  922. Result := TDOMAttr.Create(Self);
  923. Result.FNodeName := name;
  924. end;
  925. function TDOMDocument.CreateEntityReference(const name: DOMString):
  926. TDOMEntityReference;
  927. begin
  928. raise EDOMNotSupported.Create('DOMDocument.CreateEntityReference');
  929. end;
  930. function TDOMDocument.CreateEntity(const data: DOMString): TDOMEntity;
  931. begin
  932. Result := TDOMEntity.Create(Self);
  933. Result.FNodeName := data;
  934. end;
  935. function TDOMDocument.GetElementsByTagName(const tagname: DOMString): TDOMNodeList;
  936. begin
  937. Result := TDOMNodeList.Create(Self, tagname);
  938. end;
  939. function TXMLDocument.CreateCDATASection(const data: DOMString):
  940. TDOMCDATASection;
  941. begin
  942. Result := TDOMCDATASection.Create(Self);
  943. Result.FNodeValue := data;
  944. end;
  945. function TXMLDocument.CreateProcessingInstruction(const target,
  946. data: DOMString): TDOMProcessingInstruction;
  947. begin
  948. Result := TDOMProcessingInstruction.Create(Self);
  949. Result.FNodeName := target;
  950. Result.FNodeValue := data;
  951. end;
  952. function TXMLDocument.CreateEntityReference(const name: DOMString):
  953. TDOMEntityReference;
  954. begin
  955. Result := TDOMEntityReference.Create(Self);
  956. Result.FNodeName := name;
  957. end;
  958. // -------------------------------------------------------
  959. // Attr
  960. // -------------------------------------------------------
  961. constructor TDOMAttr.Create(AOwner: TDOMDocument);
  962. begin
  963. FNodeType := ATTRIBUTE_NODE;
  964. inherited Create(AOwner);
  965. end;
  966. function TDOMAttr.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  967. begin
  968. Result := TDOMAttr.Create(ACloneOwner);
  969. Result.FNodeName := FNodeName;
  970. TDOMAttr(Result).FSpecified := FSpecified;
  971. if deep then
  972. CloneChildren(Result, ACloneOwner);
  973. end;
  974. function TDOMAttr.GetNodeValue: DOMString;
  975. var
  976. child: TDOMNode;
  977. begin
  978. SetLength(Result, 0);
  979. if Assigned(FFirstChild) then
  980. begin
  981. child := FFirstChild;
  982. while Assigned(child) do
  983. begin
  984. if child.NodeType = ENTITY_REFERENCE_NODE then
  985. Result := Result + '&' + child.NodeName + ';'
  986. else
  987. Result := Result + child.NodeValue;
  988. child := child.NextSibling;
  989. end;
  990. end;
  991. end;
  992. procedure TDOMAttr.SetNodeValue(AValue: DOMString);
  993. var
  994. tn: TDOMText;
  995. begin
  996. FSpecified := True;
  997. tn := TDOMText.Create(FOwnerDocument);
  998. tn.FNodeValue := AValue;
  999. if Assigned(FFirstChild) then
  1000. ReplaceChild(tn, FFirstChild)
  1001. else
  1002. AppendChild(tn);
  1003. end;
  1004. // -------------------------------------------------------
  1005. // Element
  1006. // -------------------------------------------------------
  1007. constructor TDOMElement.Create(AOwner: TDOMDocument);
  1008. begin
  1009. FNodeType := ELEMENT_NODE;
  1010. inherited Create(AOwner);
  1011. FAttributes := TDOMNamedNodeMap.Create(AOwner);
  1012. end;
  1013. destructor TDOMElement.Destroy;
  1014. var
  1015. i: Integer;
  1016. begin
  1017. {As the attributes are _not_ childs of the element node, we have to free
  1018. them manually here:}
  1019. for i := 0 to FAttributes.Count - 1 do
  1020. FAttributes[i].Free;
  1021. FAttributes.Free;
  1022. inherited Destroy;
  1023. end;
  1024. function TDOMElement.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  1025. var
  1026. i: Integer;
  1027. begin
  1028. Result := TDOMElement.Create(ACloneOwner);
  1029. Result.FNodeName := FNodeName;
  1030. for i := 0 to FAttributes.Count - 1 do
  1031. TDOMElement(Result).FAttributes.Add(FAttributes[i].CloneNode(True, ACloneOwner));
  1032. if deep then
  1033. CloneChildren(Result, ACloneOwner);
  1034. end;
  1035. function TDOMElement.GetAttributes: TDOMNamedNodeMap;
  1036. begin
  1037. Result := FAttributes;
  1038. end;
  1039. function TDOMElement.GetAttribute(const name: DOMString): DOMString;
  1040. var
  1041. i: Integer;
  1042. begin
  1043. for i := 0 to FAttributes.Count - 1 do
  1044. if FAttributes[i].NodeName = name then
  1045. begin
  1046. Result := FAttributes[i].NodeValue;
  1047. exit;
  1048. end;
  1049. SetLength(Result, 0);
  1050. end;
  1051. procedure TDOMElement.SetAttribute(const name, value: DOMString);
  1052. var
  1053. i: Integer;
  1054. attr: TDOMAttr;
  1055. begin
  1056. for i := 0 to FAttributes.Count - 1 do
  1057. if FAttributes[i].NodeName = name then
  1058. begin
  1059. FAttributes[i].NodeValue := value;
  1060. exit;
  1061. end;
  1062. attr := TDOMAttr.Create(FOwnerDocument);
  1063. attr.FNodeName := name;
  1064. attr.NodeValue := value;
  1065. FAttributes.Add(attr);
  1066. end;
  1067. procedure TDOMElement.RemoveAttribute(const name: DOMString);
  1068. var
  1069. i: Integer;
  1070. begin
  1071. for i := 0 to FAttributes.Count - 1 do
  1072. if FAttributes[i].NodeName = name then
  1073. begin
  1074. FAttributes[i].Free;
  1075. FAttributes.Delete(i);
  1076. exit;
  1077. end;
  1078. end;
  1079. function TDOMElement.GetAttributeNode(const name: DOMString): TDOMAttr;
  1080. var
  1081. i: Integer;
  1082. begin
  1083. for i := 0 to FAttributes.Count - 1 do
  1084. if FAttributes[i].NodeName = name then
  1085. begin
  1086. Result := TDOMAttr(FAttributes[i]);
  1087. exit;
  1088. end;
  1089. Result := nil;
  1090. end;
  1091. procedure TDOMElement.SetAttributeNode(NewAttr: TDOMAttr);
  1092. var
  1093. i: Integer;
  1094. begin
  1095. for i := 0 to FAttributes.Count - 1 do
  1096. if FAttributes[i].NodeName = NewAttr.NodeName then
  1097. begin
  1098. FAttributes[i].Free;
  1099. FAttributes[i] := NewAttr;
  1100. exit;
  1101. end;
  1102. end;
  1103. function TDOMElement.RemoveAttributeNode(OldAttr: TDOMAttr): TDOMAttr;
  1104. var
  1105. i: Integer;
  1106. node: TDOMNode;
  1107. begin
  1108. for i := 0 to FAttributes.Count - 1 do
  1109. begin
  1110. node := FAttributes[i];
  1111. if node = OldAttr then
  1112. begin
  1113. FAttributes.Delete(i);
  1114. Result := TDOMAttr(node);
  1115. exit;
  1116. end;
  1117. end;
  1118. end;
  1119. function TDOMElement.GetElementsByTagName(const name: DOMString): TDOMNodeList;
  1120. begin
  1121. Result := TDOMNodeList.Create(Self, name);
  1122. end;
  1123. procedure TDOMElement.Normalize;
  1124. begin
  1125. // !!!: Not implemented
  1126. end;
  1127. // -------------------------------------------------------
  1128. // Text
  1129. // -------------------------------------------------------
  1130. constructor TDOMText.Create(AOwner: TDOMDocument);
  1131. begin
  1132. FNodeType := TEXT_NODE;
  1133. FNodeName := '#text';
  1134. inherited Create(AOwner);
  1135. end;
  1136. function TDOMText.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  1137. begin
  1138. Result := TDOMText.Create(ACloneOwner);
  1139. Result.FNodeValue := FNodeValue;
  1140. end;
  1141. function TDOMText.SplitText(offset: LongWord): TDOMText;
  1142. begin
  1143. if offset > Length then
  1144. raise EDOMIndexSize.Create('Text.SplitText');
  1145. Result := TDOMText.Create(FOwnerDocument);
  1146. Result.FNodeValue := Copy(FNodeValue, offset + 1, Length);
  1147. FNodeValue := Copy(FNodeValue, 1, offset);
  1148. FParentNode.InsertBefore(Result, FNextSibling);
  1149. end;
  1150. // -------------------------------------------------------
  1151. // Comment
  1152. // -------------------------------------------------------
  1153. constructor TDOMComment.Create(AOwner: TDOMDocument);
  1154. begin
  1155. FNodeType := COMMENT_NODE;
  1156. FNodeName := '#comment';
  1157. inherited Create(AOwner);
  1158. end;
  1159. function TDOMComment.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  1160. begin
  1161. Result := TDOMComment.Create(ACloneOwner);
  1162. Result.FNodeValue := FNodeValue;
  1163. end;
  1164. // -------------------------------------------------------
  1165. // CDATASection
  1166. // -------------------------------------------------------
  1167. constructor TDOMCDATASection.Create(AOwner: TDOMDocument);
  1168. begin
  1169. inherited Create(AOwner);
  1170. FNodeType := CDATA_SECTION_NODE;
  1171. FNodeName := '#cdata-section';
  1172. end;
  1173. function TDOMCDATASection.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  1174. begin
  1175. Result := TDOMCDATASection.Create(ACloneOwner);
  1176. Result.FNodeValue := FNodeValue;
  1177. end;
  1178. // -------------------------------------------------------
  1179. // DocumentType
  1180. // -------------------------------------------------------
  1181. constructor TDOMDocumentType.Create(AOwner: TDOMDocument);
  1182. begin
  1183. FNodeType := DOCUMENT_TYPE_NODE;
  1184. inherited Create(AOwner);
  1185. end;
  1186. function TDOMDocumentType.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  1187. begin
  1188. Result := TDOMDocumentType.Create(ACloneOwner);
  1189. Result.FNodeName := FNodeName;
  1190. end;
  1191. // -------------------------------------------------------
  1192. // Notation
  1193. // -------------------------------------------------------
  1194. constructor TDOMNotation.Create(AOwner: TDOMDocument);
  1195. begin
  1196. FNodeType := NOTATION_NODE;
  1197. inherited Create(AOwner);
  1198. end;
  1199. function TDOMNotation.CloneNode(deep: Boolean; ACloneOwner: TDOMDocument): TDOMNode;
  1200. begin
  1201. Result := TDOMNotation.Create(ACloneOwner);
  1202. Result.FNodeName := FNodeName;
  1203. end;
  1204. // -------------------------------------------------------
  1205. // Entity
  1206. // -------------------------------------------------------
  1207. constructor TDOMEntity.Create(AOwner: TDOMDocument);
  1208. begin
  1209. FNodeType := ENTITY_NODE;
  1210. inherited Create(AOwner);
  1211. end;
  1212. // -------------------------------------------------------
  1213. // EntityReference
  1214. // -------------------------------------------------------
  1215. constructor TDOMEntityReference.Create(AOwner: TDOMDocument);
  1216. begin
  1217. FNodeType := ENTITY_REFERENCE_NODE;
  1218. inherited Create(AOwner);
  1219. end;
  1220. // -------------------------------------------------------
  1221. // ProcessingInstruction
  1222. // -------------------------------------------------------
  1223. constructor TDOMProcessingInstruction.Create(AOwner: TDOMDocument);
  1224. begin
  1225. FNodeType := PROCESSING_INSTRUCTION_NODE;
  1226. inherited Create(AOwner);
  1227. end;
  1228. end.
  1229. {
  1230. Revision 1.13 2003/11/15 10:31:50 michael
  1231. + Fixed CloneNode overloaded call (from Andreas Hausladen)
  1232. Revision 1.12 2003/01/15 21:59:55 sg
  1233. * the units DOM, XMLRead and XMLWrite now compile with Delphi without
  1234. modifications as well
  1235. Revision 1.11 2002/12/11 21:06:07 sg
  1236. * Small cleanups
  1237. * Replaced htmldoc unit with dom_html unit
  1238. * Added SAX parser framework and SAX HTML parser
  1239. Revision 1.10 2002/09/07 15:15:29 peter
  1240. * old logs removed and tabs fixed
  1241. Revision 1.9 2002/03/01 10:02:38 sg
  1242. * Fixed read access method for TDOMAttr.Value
  1243. }