extras.pp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. {**********************************************************************
  2. This file is part of the Free Component Library (FCL)
  3. DOM Test cases which are missing from w3.org test suite
  4. Copyright (c) 2008 by Sergei Gorelkin, [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit extras;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. SysUtils, Classes, DOM, xmlread, xmlwrite, domunit, testregistry;
  16. implementation
  17. type
  18. TDOMTestExtra = class(TDOMTestBase)
  19. published
  20. procedure attr_ownership01;
  21. procedure attr_ownership02;
  22. procedure attr_ownership03;
  23. procedure attr_ownership04;
  24. procedure attr_ownership05;
  25. procedure replacesamechild;
  26. procedure insertbeforefirst;
  27. procedure nsFixup1;
  28. procedure nsFixup2;
  29. procedure nsFixup3;
  30. end;
  31. { TDOMTestExtra }
  32. // verify that an attribute created by Element.SetAttribute()
  33. // has its OwnerElement assigned properly
  34. procedure TDOMTestExtra.attr_ownership01;
  35. var
  36. doc: TDOMDocument;
  37. el: TDOMElement;
  38. attr: TDOMAttr;
  39. attrOwner: TDOMElement;
  40. begin
  41. LoadStringData(doc, '<doc/>');
  42. el := doc.CreateElement('element1');
  43. el.SetAttribute('newAttr', 'newValue');
  44. attr := el.GetAttributeNode('newAttr');
  45. AssertNotNull('attribute', attr);
  46. attrOwner := attr.OwnerElement;
  47. AssertEquals('ownerElement', el, attrOwner);
  48. AssertTrue('specified', attr.Specified);
  49. end;
  50. // verify that an attribute created by Element.SetAttributeNS()
  51. // has its OwnerElement assigned properly
  52. procedure TDOMTestExtra.attr_ownership02;
  53. var
  54. doc: TDOMDocument;
  55. el: TDOMElement;
  56. attr: TDOMAttr;
  57. attrOwner: TDOMElement;
  58. begin
  59. LoadStringData(doc, '<doc/>');
  60. el := doc.CreateElement('element1');
  61. el.SetAttributeNS('http://www.freepascal.org', 'fpc:newAttr', 'newValue');
  62. attr := el.GetAttributeNodeNS('http://www.freepascal.org', 'newAttr');
  63. AssertNotNull('attribute', attr);
  64. attrOwner := attr.OwnerElement;
  65. AssertEquals('ownerElement', el, attrOwner);
  66. AssertTrue('specified', attr.Specified);
  67. end;
  68. // verify that NamedNodeMap.SetNamedItem() resets OwnerElement
  69. // of the attribute being replaced
  70. procedure TDOMTestExtra.attr_ownership03;
  71. var
  72. doc: TDOMDocument;
  73. el: TDOMElement;
  74. attr, attr2: TDOMAttr;
  75. retNode: TDOMNode;
  76. begin
  77. LoadStringData(doc, '<doc/>');
  78. el := doc.CreateElement('element1');
  79. attr := doc.CreateAttribute('newAttr');
  80. el.SetAttributeNode(attr);
  81. AssertEquals('ownerElement_before', el, attr.OwnerElement);
  82. attr2 := doc.CreateAttribute('newAttr');
  83. retNode := el.Attributes.SetNamedItem(attr2);
  84. AssertSame('retNode', attr, retNode);
  85. AssertNull('ownerElement_after', attr.OwnerElement);
  86. AssertEquals('ownerElement2', el, attr2.OwnerElement);
  87. end;
  88. // verify that NamedNodeMap.SetNamedItemNS() resets OwnerElement
  89. // of the attribute being replaced
  90. procedure TDOMTestExtra.attr_ownership04;
  91. var
  92. doc: TDOMDocument;
  93. el: TDOMElement;
  94. attr, attr2: TDOMAttr;
  95. retNode: TDOMNode;
  96. begin
  97. LoadStringData(doc, '<doc/>');
  98. el := doc.CreateElement('element1');
  99. attr := doc.CreateAttributeNS('http://www.freepascal.org', 'fpc:newAttr');
  100. el.SetAttributeNodeNS(attr);
  101. AssertEquals('ownerElement_before', el, attr.OwnerElement);
  102. attr2 := doc.CreateAttributeNS('http://www.freepascal.org', 'fpc:newAttr');
  103. retNode := el.Attributes.SetNamedItemNS(attr2);
  104. AssertSame('retNode', attr, retNode);
  105. AssertNull('ownerElement_after', attr.OwnerElement);
  106. AssertEquals('ownerElement2', el, attr2.OwnerElement);
  107. end;
  108. // verify that Element.removeAttributeNode() resets ownerElement
  109. // of the attribute being removed
  110. procedure TDOMTestExtra.attr_ownership05;
  111. var
  112. doc: TDOMDocument;
  113. el: TDOMElement;
  114. attr: TDOMAttr;
  115. begin
  116. LoadStringData(doc, '<doc/>');
  117. el := doc.CreateElement('element1');
  118. attr := doc.CreateAttributeNS('http://www.freepascal.org', 'fpc:newAttr');
  119. el.SetAttributeNodeNS(attr);
  120. AssertEquals('ownerElement_before', el, attr.OwnerElement);
  121. el.RemoveAttributeNode(attr);
  122. AssertNull('ownerElement_after', attr.ownerElement);
  123. end;
  124. // verify that replacing a node by itself does not remove it from the tree
  125. // (specs say this is implementation-dependent, but guess that means either
  126. // no-op or raising an exception, not removal).
  127. procedure TDOMTestExtra.replacesamechild;
  128. var
  129. doc: TDOMDocument;
  130. root, el, prev, next: TDOMNode;
  131. begin
  132. LoadStringData(doc, '<root><child1/><child2/><child3/></root>');
  133. root := doc.DocumentElement;
  134. el := root.ChildNodes[1];
  135. prev := el.PreviousSibling;
  136. next := el.NextSibling;
  137. AssertEqualsW('prev_name_before', 'child1', prev.NodeName);
  138. AssertEqualsW('next_name_before', 'child3', next.NodeName);
  139. root.replaceChild(el, el);
  140. prev := el.PreviousSibling;
  141. next := el.NextSibling;
  142. AssertNotNull('prev_after', prev);
  143. AssertNotNull('prev_after', next);
  144. AssertEqualsW('prev_name_after', 'child1', prev.NodeName);
  145. AssertEqualsW('next_name_after', 'child3', next.NodeName);
  146. end;
  147. // verify that inserting a node before the first child sets
  148. // both refnode.previoussibling and newnode.nextsibling properties
  149. procedure TDOMTestExtra.insertbeforefirst;
  150. var
  151. doc: TDOMDocument;
  152. root, refchild, newchild: TDOMNode;
  153. begin
  154. LoadStringData(doc, '<root><child1/><child2/><child3/></root>');
  155. root := doc.DocumentElement;
  156. refchild := root.FirstChild;
  157. newchild := doc.CreateElement('new');
  158. root.insertbefore(newchild, refchild);
  159. AssertEquals('prev', refchild.previoussibling, newchild);
  160. AssertEquals('next', newchild.nextsibling, refchild);
  161. AssertEquals('child', root.firstchild, newchild);
  162. end;
  163. const
  164. nsURI1 = 'http://www.example.com/ns1';
  165. nsURI2 = 'http://www.example.com/ns2';
  166. // verify the namespace fixup with two nested elements
  167. // (same localName, different nsURI, and no prefixes)
  168. procedure TDOMTestExtra.nsFixup1;
  169. var
  170. domImpl: TDOMImplementation;
  171. origDoc: TDOMDocument;
  172. parsedDoc: TDOMDocument;
  173. docElem: TDOMElement;
  174. el: TDOMElement;
  175. stream: TStringStream;
  176. list: TDOMNodeList;
  177. begin
  178. FParser.Options.Namespaces := True;
  179. domImpl := GetImplementation;
  180. origDoc := domImpl.createDocument(nsURI1, 'test', nil);
  181. GC(origDoc);
  182. docElem := origDoc.documentElement;
  183. el := origDoc.CreateElementNS(nsURI2, 'test');
  184. docElem.AppendChild(el);
  185. stream := TStringStream.Create('');
  186. GC(stream);
  187. writeXML(origDoc, stream);
  188. LoadStringData(parsedDoc, stream.DataString);
  189. docElem := parsedDoc.documentElement;
  190. assertEqualsW('docElemLocalName', 'test', docElem.localName);
  191. assertEqualsW('docElemNS', nsURI1, docElem.namespaceURI);
  192. list := docElem.GetElementsByTagNameNS(nsURI2, '*');
  193. assertEquals('ns2_elementCount', 1, list.Length);
  194. el := TDOMElement(list[0]);
  195. assertEqualsW('ns2_nodeName', 'test', el.nodeName);
  196. end;
  197. // verify the namespace fixup with two nested elements
  198. // (same localName, different nsURI, different prefixes)
  199. procedure TDOMTestExtra.nsFixup2;
  200. var
  201. domImpl: TDOMImplementation;
  202. origDoc: TDOMDocument;
  203. parsedDoc: TDOMDocument;
  204. docElem: TDOMElement;
  205. el: TDOMElement;
  206. stream: TStringStream;
  207. list: TDOMNodeList;
  208. begin
  209. FParser.Options.Namespaces := True;
  210. domImpl := GetImplementation;
  211. origDoc := domImpl.createDocument(nsURI1, 'a:test', nil);
  212. GC(origDoc);
  213. docElem := origDoc.documentElement;
  214. el := origDoc.CreateElementNS(nsURI2, 'b:test');
  215. docElem.AppendChild(el);
  216. stream := TStringStream.Create('');
  217. GC(stream);
  218. writeXML(origDoc, stream);
  219. LoadStringData(parsedDoc, stream.DataString);
  220. docElem := parsedDoc.documentElement;
  221. assertEqualsW('docElemLocalName', 'test', docElem.localName);
  222. assertEqualsW('docElemNS', nsURI1, docElem.namespaceURI);
  223. list := docElem.GetElementsByTagNameNS(nsURI2, '*');
  224. assertEquals('ns2_elementCount', 1, list.Length);
  225. el := TDOMElement(list[0]);
  226. assertEqualsW('ns2_nodeName', 'b:test', el.nodeName);
  227. end;
  228. // verify the namespace fixup with two nested elements and an attribute
  229. // attribute's prefix must change to that of document element
  230. procedure TDOMTestExtra.nsFixup3;
  231. var
  232. domImpl: TDOMImplementation;
  233. origDoc: TDOMDocument;
  234. parsedDoc: TDOMDocument;
  235. docElem: TDOMElement;
  236. el: TDOMElement;
  237. stream: TStringStream;
  238. list: TDOMNodeList;
  239. attr: TDOMAttr;
  240. begin
  241. FParser.Options.Namespaces := True;
  242. domImpl := GetImplementation;
  243. origDoc := domImpl.createDocument(nsURI1, 'a:test', nil);
  244. GC(origDoc);
  245. docElem := origDoc.documentElement;
  246. el := origDoc.CreateElementNS(nsURI2, 'b:test');
  247. docElem.AppendChild(el);
  248. el.SetAttributeNS(nsURI1, 'test:attr', 'test value');
  249. stream := TStringStream.Create('');
  250. GC(stream);
  251. writeXML(origDoc, stream);
  252. LoadStringData(parsedDoc, stream.DataString);
  253. docElem := parsedDoc.documentElement;
  254. assertEqualsW('docElemLocalName', 'test', docElem.localName);
  255. assertEqualsW('docElemNS', nsURI1, docElem.namespaceURI);
  256. list := docElem.GetElementsByTagNameNS(nsURI2, '*');
  257. assertEquals('ns2_elementCount', 1, list.Length);
  258. el := TDOMElement(list[0]);
  259. attr := el.GetAttributeNodeNS(nsURI1, 'attr');
  260. assertEqualsW('attr_nodeName', 'a:attr', attr.nodeName);
  261. end;
  262. initialization
  263. RegisterTest(TDOMTestExtra);
  264. end.