extras2.pp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. {**********************************************************************
  2. This file is part of the Free Component Library (FCL)
  3. Some DOM test cases adapted by hand (because automatic conversion
  4. is not yet possible for them).
  5. Copyright (c) 2001-2004 World Wide Web Consortium,
  6. (Massachusetts Institute of Technology, Institut National de
  7. Recherche en Informatique et en Automatique, Keio University). All
  8. Rights Reserved.
  9. Copyright (c) 2009 by Sergei Gorelkin, [email protected]
  10. See the file COPYING.FPC, included in this distribution,
  11. for details about the copyright.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. **********************************************************************}
  16. unit extras2;
  17. {$mode objfpc}{$H+}
  18. interface
  19. uses
  20. SysUtils, Classes, DOM, xmlread, xmlwrite, domunit, testregistry;
  21. implementation
  22. type
  23. TDOMTestExtra2 = class(TDOMTestBase)
  24. published
  25. procedure ls3_canonicform08;
  26. procedure ls3_canonicform09;
  27. procedure ls3_canonicform10;
  28. procedure ls3_canonicform11;
  29. procedure ls3_DomWriterTest5;
  30. procedure ls3_DomWriterTest6;
  31. end;
  32. const
  33. // This is example #1 from c14n specs, but modified to comply with HTML grammar
  34. canonicform01 =
  35. '<?xml version="1.0"?>'^M^J+
  36. ^M^J+
  37. '<?xml-stylesheet href="doc.xsl"'^M^J+
  38. ' type="text/xsl" ?>'^M^J+
  39. ^M^J+
  40. '<!DOCTYPE html SYSTEM "xhtml1-strict.dtd">'^M^J+
  41. '<html xmlns="http://www.w3.org/1999/xhtml"><head><title>canonicalform01</title></head><body onload="parent.loadComplete()">'^M^J+
  42. '<p>Hello, world!<!-- Comment 1 --></p></body></html>'^M^J+
  43. ^M^J+
  44. '<?pi-without-data ?>'^M^J+
  45. ^M^J+
  46. '<!-- Comment 2 -->'^M^J+
  47. ^M^J+
  48. '<!-- Comment 3 -->'^M^J;
  49. canonicform03 =
  50. '<!DOCTYPE html [<!ATTLIST acronym title CDATA "default">]>'^M^J+
  51. '<html xmlns="http://www.w3.org/1999/xhtml"><head><title>canonicalform03</title></head><body onload="parent.loadComplete()">'^M^J+
  52. ' <br />'^M^J+
  53. ' <br ></br>'^M^J+
  54. ' <div name = "elem3" id="elem3" />'^M^J+
  55. ' <div name="elem4" id="elem4" ></div>'^M^J+
  56. ' <div a:attr="out" b:attr="sorted" name="all" class="I''m"'^M^J+
  57. ' xmlns:b="http://www.ietf.org"'^M^J+
  58. ' xmlns:a="http://www.w3.org"'^M^J+
  59. ' xmlns="http://example.org"/>'^M^J+
  60. ' <div xmlns="" xmlns:a="http://www.w3.org">'^M^J+
  61. ' <div xmlns="http://www.ietf.org">'^M^J+
  62. ' <div xmlns="" xmlns:a="http://www.w3.org">'^M^J+
  63. ' <acronym xmlns="" xmlns:a="http://www.ietf.org"/>'^M^J+
  64. ' </div>'^M^J+
  65. ' </div>'^M^J+
  66. ' </div>'^M^J+
  67. '</body></html>'^M^J;
  68. { TDOMTestExtra }
  69. { test canonical form with comments }
  70. procedure TDOMTestExtra2.ls3_canonicform08;
  71. var
  72. doc: TDOMDocument;
  73. node: TDOMNode;
  74. nodeType: Integer;
  75. nodeValue: DOMString;
  76. length: Integer;
  77. begin
  78. FParser.Options.CanonicalForm := True;
  79. LoadStringData(doc, canonicform01);
  80. begin
  81. node := TDOMNode(doc).firstChild;
  82. nodeType := node.nodeType;
  83. assertEquals('PIisFirstChild', 7, nodeType);
  84. nodeValue := TDOMProcessingInstruction(node).data;
  85. length := system.length(nodeValue);
  86. assertEquals('piDataLength', 36, length);
  87. node := node.nextSibling;
  88. nodeType := node.nodeType;
  89. assertEquals('TextisSecondChild', 3, nodeType);
  90. nodeValue := node.nodeValue;
  91. length := system.length(nodeValue);
  92. assertEquals('secondChildLength', 1, length);
  93. node := node.nextSibling;
  94. nodeType := node.nodeType;
  95. assertEquals('ElementisThirdChild', 1, nodeType);
  96. node := node.nextSibling;
  97. nodeType := node.nodeType;
  98. assertEquals('TextisFourthChild', 3, nodeType);
  99. nodeValue := node.nodeValue;
  100. length := system.length(nodeValue);
  101. assertEquals('fourthChildLength', 1, length);
  102. node := node.nextSibling;
  103. nodeType := node.nodeType;
  104. assertEquals('PIisFifthChild', 7, nodeType);
  105. nodeValue := TDOMProcessingInstruction(node).data;
  106. assertEqualsW('trailingPIData', '', nodeValue);
  107. node := node.nextSibling;
  108. nodeType := node.nodeType;
  109. assertEquals('TextisSixthChild', 3, nodeType);
  110. nodeValue := node.nodeValue;
  111. length := system.length(nodeValue);
  112. assertEquals('sixthChildLength', 1, length);
  113. node := node.nextSibling;
  114. nodeType := node.nodeType;
  115. assertEquals('CommentisSeventhChild', 8, nodeType);
  116. node := node.nextSibling;
  117. nodeType := node.nodeType;
  118. assertEquals('TextisEighthChild', 3, nodeType);
  119. nodeValue := node.nodeValue;
  120. length := system.length(nodeValue);
  121. assertEquals('eighthChildLength', 1, length);
  122. node := node.nextSibling;
  123. nodeType := node.nodeType;
  124. assertEquals('CommentisNinthChild', 8, nodeType);
  125. node := node.nextSibling;
  126. assertNull('TenthIsNull', node);
  127. end;
  128. end;
  129. { test canonical form without comments }
  130. procedure TDOMTestExtra2.ls3_canonicform09;
  131. var
  132. doc: TDOMDocument;
  133. node: TDOMNode;
  134. nodeType: Integer;
  135. nodeValue: DOMString;
  136. length: Integer;
  137. begin
  138. FParser.Options.CanonicalForm := True;
  139. FParser.Options.IgnoreComments := True;
  140. LoadStringData(doc, canonicform01);
  141. begin
  142. node := TDOMNode(doc).firstChild;
  143. nodeType := node.nodeType;
  144. assertEquals('PIisFirstChild', 7, nodeType);
  145. nodeValue := TDOMProcessingInstruction(node).data;
  146. length := system.length(nodeValue);
  147. assertEquals('piDataLength', 36, length);
  148. node := node.nextSibling;
  149. nodeType := node.nodeType;
  150. assertEquals('TextisSecondChild', 3, nodeType);
  151. nodeValue := node.nodeValue;
  152. length := system.length(nodeValue);
  153. assertEquals('secondChildLength', 1, length);
  154. node := node.nextSibling;
  155. nodeType := node.nodeType;
  156. assertEquals('ElementisThirdChild', 1, nodeType);
  157. node := node.nextSibling;
  158. nodeType := node.nodeType;
  159. assertEquals('TextisFourthChild', 3, nodeType);
  160. nodeValue := node.nodeValue;
  161. length := system.length(nodeValue);
  162. assertEquals('fourthChildLength', 1, length);
  163. node := node.nextSibling;
  164. nodeType := node.nodeType;
  165. assertEquals('PIisFifthChild', 7, nodeType);
  166. nodeValue := TDOMProcessingInstruction(node).data;
  167. assertEqualsW('trailingPIData', '', nodeValue);
  168. node := node.nextSibling;
  169. assertNull('SixthIsNull', node);
  170. end;
  171. end;
  172. { test removal of superfluous namespace declarations }
  173. procedure TDOMTestExtra2.ls3_canonicform10;
  174. var
  175. doc: TDOMDocument;
  176. divList: TDOMNodeList;
  177. divEl: TDOMElement;
  178. node: TDOMNode;
  179. begin
  180. FParser.Options.CanonicalForm := True;
  181. LoadStringData(doc, canonicform03);
  182. divList := doc.getElementsByTagName('div');
  183. TDOMNode(divEl) := divList[5];
  184. node := divEl.getAttributeNode('xmlns');
  185. assertNotNull('xmlnsPresent', node);
  186. node := divEl.getAttributeNode('xmlns:a');
  187. assertNull('xmlnsANotPresent', node);
  188. end;
  189. { test that defaulted attributes are being replaced by 'normal' ones }
  190. procedure TDOMTestExtra2.ls3_canonicform11;
  191. var
  192. doc: TDOMDocument;
  193. elemList: TDOMNodeList;
  194. elem: TDOMElement;
  195. attr: TDOMAttr;
  196. attrSpecified: Boolean;
  197. attrValue: DOMString;
  198. begin
  199. FParser.Options.CanonicalForm := True;
  200. LoadStringData(doc, canonicform03);
  201. elemList := doc.getElementsByTagName('acronym');
  202. TDOMNode(elem) := elemList[0];
  203. attr := elem.getAttributeNode('title');
  204. assertNotNull('titlePresent', attr);
  205. attrSpecified := attr.specified;
  206. assertTrue('titleSpecified', attrSpecified);
  207. attrValue := attr.nodeValue;
  208. assertEqualsW('titleValue', 'default', attrValue);
  209. end;
  210. { tests that namespace fixup is done while serializing }
  211. { attribute has no prefix }
  212. procedure TDOMTestExtra2.ls3_DomWriterTest5;
  213. var
  214. domImpl: TDOMImplementation;
  215. origDoc: TDOMDocument;
  216. parsedDoc: TDOMDocument;
  217. docElem: TDOMElement;
  218. stream: TStringStream;
  219. docElemLocalName: DOMString;
  220. docElemNS: DOMString;
  221. attrValue: DOMString;
  222. const
  223. namespaceURI = 'http://www.example.com/DOMWriterTest5';
  224. begin
  225. FParser.Options.Namespaces := True;
  226. domImpl := GetImplementation;
  227. origDoc := domImpl.createDocument(namespaceURI, 'test', nil);
  228. GC(origDoc);
  229. docElem := origDoc.documentElement;
  230. docElem.setAttributeNS(namespaceURI, 'attr', 'test value');
  231. stream := TStringStream.Create('');
  232. GC(stream);
  233. writeXML(origDoc, stream);
  234. LoadStringData(parsedDoc, stream.DataString);
  235. docElem := parsedDoc.documentElement;
  236. docElemLocalName := docElem.localName;
  237. assertEqualsW('docElemLocalName', 'test', docElemLocalName);
  238. docElemNS := TDOMNode(docElem).namespaceURI;
  239. assertEqualsW('docElemNS', namespaceURI, docElemNS);
  240. attrValue := docElem.getAttributeNS(namespaceURI, 'attr');
  241. assertEqualsW('properNSAttrValue', 'test value', attrValue);
  242. end;
  243. { tests that namespace fixup is done while serializing }
  244. { same as above, but using an attribute that has a prefix }
  245. procedure TDOMTestExtra2.ls3_DomWriterTest6;
  246. var
  247. domImpl: TDOMImplementation;
  248. origDoc: TDOMDocument;
  249. parsedDoc: TDOMDocument;
  250. docElem: TDOMElement;
  251. stream: TStringStream;
  252. docElemLocalName: DOMString;
  253. docElemNS: DOMString;
  254. attrValue: DOMString;
  255. const
  256. namespaceURI = 'http://www.example.com/DOMWriterTest5';
  257. begin
  258. FParser.Options.Namespaces := True;
  259. domImpl := GetImplementation;
  260. origDoc := domImpl.createDocument(namespaceURI, 'test', nil);
  261. GC(origDoc);
  262. docElem := origDoc.documentElement;
  263. docElem.setAttributeNS(namespaceURI, 'test:attr', 'test value');
  264. stream := TStringStream.Create('');
  265. GC(stream);
  266. writeXML(origDoc, stream);
  267. LoadStringData(parsedDoc, stream.DataString);
  268. docElem := parsedDoc.documentElement;
  269. docElemLocalName := docElem.localName;
  270. assertEqualsW('docElemLocalName', 'test', docElemLocalName);
  271. docElemNS := TDOMNode(docElem).namespaceURI;
  272. assertEqualsW('docElemNS', namespaceURI, docElemNS);
  273. attrValue := docElem.getAttributeNS(namespaceURI, 'attr');
  274. assertEqualsW('properNSAttrValue', 'test value', attrValue);
  275. end;
  276. initialization
  277. RegisterTest(TDOMTestExtra2);
  278. end.