dom.pp 40 KB

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