extras2.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. // canonical form: PreserveWhitespace, Namespaces, NamespaceDeclarations = True;
  79. // Entities, CDSections = False;
  80. FParser.Options.PreserveWhitespace := True;
  81. FParser.Options.Namespaces := True;
  82. LoadStringData(doc, canonicform01);
  83. begin
  84. node := TDOMNode(doc).firstChild;
  85. nodeType := node.nodeType;
  86. assertEquals('PIisFirstChild', 7, nodeType);
  87. nodeValue := TDOMProcessingInstruction(node).data;
  88. length := system.length(nodeValue);
  89. assertEquals('piDataLength', 36, length);
  90. node := node.nextSibling;
  91. nodeType := node.nodeType;
  92. assertEquals('TextisSecondChild', 3, nodeType);
  93. nodeValue := node.nodeValue;
  94. length := system.length(nodeValue);
  95. assertEquals('secondChildLength', 1, length);
  96. node := node.nextSibling;
  97. nodeType := node.nodeType;
  98. assertEquals('ElementisThirdChild', 1, nodeType);
  99. node := node.nextSibling;
  100. nodeType := node.nodeType;
  101. assertEquals('TextisFourthChild', 3, nodeType);
  102. nodeValue := node.nodeValue;
  103. length := system.length(nodeValue);
  104. assertEquals('fourthChildLength', 1, length);
  105. node := node.nextSibling;
  106. nodeType := node.nodeType;
  107. assertEquals('PIisFifthChild', 7, nodeType);
  108. nodeValue := TDOMProcessingInstruction(node).data;
  109. assertEquals('trailingPIData', '', nodeValue);
  110. node := node.nextSibling;
  111. nodeType := node.nodeType;
  112. assertEquals('TextisSixthChild', 3, nodeType);
  113. nodeValue := node.nodeValue;
  114. length := system.length(nodeValue);
  115. assertEquals('sixthChildLength', 1, length);
  116. node := node.nextSibling;
  117. nodeType := node.nodeType;
  118. assertEquals('CommentisSeventhChild', 8, nodeType);
  119. node := node.nextSibling;
  120. nodeType := node.nodeType;
  121. assertEquals('TextisEighthChild', 3, nodeType);
  122. nodeValue := node.nodeValue;
  123. length := system.length(nodeValue);
  124. assertEquals('eighthChildLength', 1, length);
  125. node := node.nextSibling;
  126. nodeType := node.nodeType;
  127. assertEquals('CommentisNinthChild', 8, nodeType);
  128. node := node.nextSibling;
  129. assertNull('TenthIsNull', node);
  130. end;
  131. end;
  132. { test canonical form without comments }
  133. procedure TDOMTestExtra2.ls3_canonicform09;
  134. var
  135. doc: TDOMDocument;
  136. node: TDOMNode;
  137. nodeType: Integer;
  138. nodeValue: DOMString;
  139. length: Integer;
  140. begin
  141. // canonical form: PreserveWhitespace, Namespaces, NamespaceDeclarations = True;
  142. // Entities, CDSections = False;
  143. FParser.Options.PreserveWhitespace := True;
  144. FParser.Options.Namespaces := True;
  145. FParser.Options.IgnoreComments := True;
  146. LoadStringData(doc, canonicform01);
  147. begin
  148. node := TDOMNode(doc).firstChild;
  149. nodeType := node.nodeType;
  150. assertEquals('PIisFirstChild', 7, nodeType);
  151. nodeValue := TDOMProcessingInstruction(node).data;
  152. length := system.length(nodeValue);
  153. assertEquals('piDataLength', 36, length);
  154. node := node.nextSibling;
  155. nodeType := node.nodeType;
  156. assertEquals('TextisSecondChild', 3, nodeType);
  157. nodeValue := node.nodeValue;
  158. length := system.length(nodeValue);
  159. assertEquals('secondChildLength', 1, length);
  160. node := node.nextSibling;
  161. nodeType := node.nodeType;
  162. assertEquals('ElementisThirdChild', 1, nodeType);
  163. node := node.nextSibling;
  164. nodeType := node.nodeType;
  165. assertEquals('TextisFourthChild', 3, nodeType);
  166. nodeValue := node.nodeValue;
  167. length := system.length(nodeValue);
  168. assertEquals('fourthChildLength', 1, length);
  169. node := node.nextSibling;
  170. nodeType := node.nodeType;
  171. assertEquals('PIisFifthChild', 7, nodeType);
  172. nodeValue := TDOMProcessingInstruction(node).data;
  173. assertEquals('trailingPIData', '', nodeValue);
  174. node := node.nextSibling;
  175. assertNull('SixthIsNull', node);
  176. end;
  177. end;
  178. { test removal of superfluous namespace declarations }
  179. procedure TDOMTestExtra2.ls3_canonicform10;
  180. var
  181. doc: TDOMDocument;
  182. divList: TDOMNodeList;
  183. divEl: TDOMElement;
  184. node: TDOMNode;
  185. begin
  186. FParser.Options.PreserveWhitespace := True;
  187. FParser.Options.Namespaces := True;
  188. LoadStringData(doc, canonicform03);
  189. divList := doc.getElementsByTagName('div');
  190. TDOMNode(divEl) := divList[5];
  191. node := divEl.getAttributeNode('xmlns');
  192. assertNotNull('xmlnsPresent', node);
  193. node := divEl.getAttributeNode('xmlns:a');
  194. assertNull('xmlnsANotPresent', node);
  195. end;
  196. { test that defaulted attributes are being replaced by 'normal' ones }
  197. procedure TDOMTestExtra2.ls3_canonicform11;
  198. var
  199. doc: TDOMDocument;
  200. elemList: TDOMNodeList;
  201. elem: TDOMElement;
  202. attr: TDOMAttr;
  203. attrSpecified: Boolean;
  204. attrValue: DOMString;
  205. begin
  206. FParser.Options.PreserveWhitespace := True;
  207. FParser.Options.Namespaces := True;
  208. LoadStringData(doc, canonicform03);
  209. elemList := doc.getElementsByTagName('acronym');
  210. TDOMNode(elem) := elemList[0];
  211. attr := elem.getAttributeNode('title');
  212. assertNotNull('titlePresent', attr);
  213. attrSpecified := attr.specified;
  214. assertTrue('titleSpecified', attrSpecified);
  215. attrValue := attr.nodeValue;
  216. assertEquals('titleValue', 'default', attrValue);
  217. end;
  218. { tests that namespace fixup is done while serializing }
  219. { attribute has no prefix }
  220. procedure TDOMTestExtra2.ls3_DomWriterTest5;
  221. var
  222. domImpl: TDOMImplementation;
  223. origDoc: TDOMDocument;
  224. parsedDoc: TDOMDocument;
  225. docElem: TDOMElement;
  226. stream: TStringStream;
  227. docElemLocalName: DOMString;
  228. docElemNS: DOMString;
  229. attrValue: DOMString;
  230. const
  231. namespaceURI = 'http://www.example.com/DOMWriterTest5';
  232. begin
  233. FParser.Options.Namespaces := True;
  234. domImpl := GetImplementation;
  235. origDoc := domImpl.createDocument(namespaceURI, 'test', nil);
  236. docElem := origDoc.documentElement;
  237. docElem.setAttributeNS(namespaceURI, 'attr', 'test value');
  238. stream := TStringStream.Create('');
  239. GC(stream);
  240. writeXML(origDoc, stream);
  241. LoadStringData(parsedDoc, stream.DataString);
  242. docElem := parsedDoc.documentElement;
  243. docElemLocalName := docElem.localName;
  244. assertEquals('docElemLocalName', 'test', docElemLocalName);
  245. docElemNS := TDOMNode(docElem).namespaceURI;
  246. assertEquals('docElemNS', namespaceURI, docElemNS);
  247. attrValue := docElem.getAttributeNS(namespaceURI, 'attr');
  248. assertEquals('properNSAttrValue', 'test value', attrValue);
  249. end;
  250. { tests that namespace fixup is done while serializing }
  251. { same as above, but using an attribute that has a prefix }
  252. procedure TDOMTestExtra2.ls3_DomWriterTest6;
  253. var
  254. domImpl: TDOMImplementation;
  255. origDoc: TDOMDocument;
  256. parsedDoc: TDOMDocument;
  257. docElem: TDOMElement;
  258. stream: TStringStream;
  259. docElemLocalName: DOMString;
  260. docElemNS: DOMString;
  261. attrValue: DOMString;
  262. const
  263. namespaceURI = 'http://www.example.com/DOMWriterTest5';
  264. begin
  265. FParser.Options.Namespaces := True;
  266. domImpl := GetImplementation;
  267. origDoc := domImpl.createDocument(namespaceURI, 'test', nil);
  268. docElem := origDoc.documentElement;
  269. docElem.setAttributeNS(namespaceURI, 'test:attr', 'test value');
  270. stream := TStringStream.Create('');
  271. GC(stream);
  272. writeXML(origDoc, stream);
  273. LoadStringData(parsedDoc, stream.DataString);
  274. docElem := parsedDoc.documentElement;
  275. docElemLocalName := docElem.localName;
  276. assertEquals('docElemLocalName', 'test', docElemLocalName);
  277. docElemNS := TDOMNode(docElem).namespaceURI;
  278. assertEquals('docElemNS', namespaceURI, docElemNS);
  279. attrValue := docElem.getAttributeNS(namespaceURI, 'attr');
  280. assertEquals('properNSAttrValue', 'test value', attrValue);
  281. end;
  282. initialization
  283. RegisterTest(TDOMTestExtra2);
  284. end.