dom.pp 41 KB

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