dom.pp 41 KB

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