htmlelements.pp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. {
  2. $Id: header,v 1.1 2000/07/13 06:33:45 michael Exp $
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  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 htmlelements;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, DOM, HtmlDefs;
  16. type
  17. TURI = string;
  18. { THtmlCustomElement }
  19. THtmlCustomElement = class (TDOMElement)
  20. private
  21. FElementTag: THTMLElementTag;
  22. function GetAttributeName(index:integer): DOMString;
  23. function GetAttributeValue(index:integer): DOMString;
  24. function GetTagName: DOMString;
  25. procedure WriteAttributes (const aStream : TStream);
  26. procedure WriteSubNodes (const aStream : TStream);
  27. protected
  28. procedure StringToStream (const aStream : TStream; s : string);
  29. procedure StringToStream (const aStream : TStream; Fmt : string; Args : array of const);
  30. function EscapeString (s : string) : string;
  31. public
  32. constructor create (AOwner: TDOMDocument); virtual;
  33. function AsString : string;
  34. procedure WriteToStream (const aStream : TStream); virtual;
  35. function GetAttribute(const name: THTMLAttributeTag): DOMString;
  36. procedure SetAttribute(const name:THTMLAttributeTag; const value: DOMString);
  37. procedure RemoveAttribute(const name: THTMLAttributeTag);
  38. property ElementTag : THTMLElementTag read FElementTag write FElementTag;
  39. property TagName : DOMString read GetTagName;
  40. property NodeName : DOMstring read GetTagName;
  41. property AttrubuteNames [index:integer] : DOMString read GetAttributeName;
  42. property AttrubuteValues [index:integer] : DOMString read GetAttributeValue;
  43. end;
  44. THTMLElementClass = class of THTMLCustomELement;
  45. { THTMLDocument }
  46. THTMLDocument = class (TDOMDocument)
  47. public
  48. procedure SaveToStream (const aStream : TStream);
  49. procedure SaveToFile (const afilename : string);
  50. function Asstring : string;
  51. end;
  52. { THTMLIDElement }
  53. THTMLIDElement = class (THTMLCustomElement)
  54. public
  55. property ID : DOMString index atID read GetAttribute write SetAttribute;
  56. end;
  57. { THTMLs18nElement }
  58. THTMLs18nElement = class (THTMLCustomElement)
  59. private
  60. function GetDir: THTMLDir;
  61. procedure SetDir(const AValue: THTMLDir);
  62. public
  63. property Dir : THTMLDir read GetDir write SetDir;
  64. property Lang : DOMString index atLang read GetAttribute write SetAttribute;
  65. end;
  66. THTMLCoreAttrsElement = class (THTMLIDElement)
  67. public
  68. property elementclass : DOMString index atclass read GetAttribute write SetAttribute;
  69. property style : DOMString index atstyle read GetAttribute write SetAttribute;
  70. property title : DOMString index attitle read GetAttribute write SetAttribute;
  71. end;
  72. THTMLCores18nElement = class (THTMLCoreAttrsElement)
  73. private
  74. function GetDir: THTMLDir;
  75. procedure SetDir(const AValue: THTMLDir);
  76. public
  77. property Dir : THTMLDir read GetDir write SetDir;
  78. property Lang : DOMString index atLang read GetAttribute write SetAttribute;
  79. end;
  80. THTMLAttrsElement = class (THTMLCores18nElement)
  81. property onclick : DOMString index atonclick read GetAttribute write SetAttribute;
  82. property ondblclick : DOMString index atondblclick read GetAttribute write SetAttribute;
  83. property onmousedown : DOMString index atonmousedown read GetAttribute write SetAttribute;
  84. property onmouseup : DOMString index atonmouseup read GetAttribute write SetAttribute;
  85. property onmouseover : DOMString index atonmouseover read GetAttribute write SetAttribute;
  86. property onmousemove : DOMString index atonmousemove read GetAttribute write SetAttribute;
  87. property onmouseout : DOMString index atonmouseout read GetAttribute write SetAttribute;
  88. property onkeypress : DOMString index atonkeypress read GetAttribute write SetAttribute;
  89. property onkeydown : DOMString index atonkeydown read GetAttribute write SetAttribute;
  90. property onkeyup : DOMString index atonkeyup read GetAttribute write SetAttribute;
  91. end;
  92. // Descendants for all the elements, generated
  93. {$i tagsintf.inc}
  94. { THTML_text }
  95. THTML_text = class (THTMLCustomElement)
  96. public
  97. constructor create (AOwner: TDOMDocument); override;
  98. procedure WriteToStream (const aStream : TStream); override;
  99. end;
  100. implementation
  101. { THtmlCustomElement }
  102. function THtmlCustomElement.GetAttributeName(index:integer): DOMString;
  103. var d : TDOMNode;
  104. begin
  105. d := TDOMNode(Attributes[index]);
  106. result := d.NodeName;
  107. end;
  108. function THtmlCustomElement.GetAttributeValue(index:integer): DOMString;
  109. var d : TDOMNode;
  110. begin
  111. d := TDOMNode(Attributes[index]);
  112. result := d.NodeValue;
  113. end;
  114. function THtmlCustomElement.GetTagName: DOMString;
  115. begin
  116. result := HTMLElementProps[FElementTag].Name
  117. end;
  118. procedure THtmlCustomElement.WriteAttributes(const aStream: TStream);
  119. var a : THTMLAttributeTag;
  120. attrs : THTMLAttributeSet;
  121. s : DOMstring;
  122. begin
  123. attrs := HTMLElementProps[ElementTag].Attributes;
  124. for a := low(THTMLAttributeTag) to high(THTMLAttributeTag) do
  125. if a in attrs then
  126. begin
  127. s := GetAttribute (a);
  128. if s <> '' then
  129. if a in booleanAttributes then
  130. StringToStream (aStream, ' %s', [HTMLAttributeTag[a]])
  131. else
  132. StringToStream (aStream, ' %s="%s"', [HTMLAttributeTag[a], s]);
  133. end;
  134. end;
  135. procedure THtmlCustomElement.WriteSubNodes(const aStream: TStream);
  136. var d : TDomNode;
  137. begin
  138. d := GetFirstChild;
  139. while assigned (d) do
  140. begin
  141. if d is THtmlCustomElement then
  142. THtmlCustomElement(d).writetostream (aStream);
  143. d := d.NextSibling;
  144. end;
  145. end;
  146. procedure THtmlCustomElement.StringToStream(const aStream: TStream; s: string);
  147. begin
  148. if s <> '' then
  149. astream.WriteBuffer (s[1], length(s));
  150. end;
  151. procedure THtmlCustomElement.StringToStream(const aStream: TStream; Fmt: string;
  152. Args: array of const);
  153. begin
  154. StringToStream (aStream, format (Fmt, args));
  155. end;
  156. function THtmlCustomElement.EscapeString(s: string): string;
  157. begin
  158. result := s;
  159. //TODO: Needs to convert all the special signs to their html names ("<" has to be "&lt;" etc.)
  160. end;
  161. constructor THtmlCustomElement.create(AOwner: TDOMDocument);
  162. begin
  163. inherited create (AOwner);
  164. FElementTag := etUnknown;
  165. end;
  166. function THtmlCustomElement.AsString: string;
  167. var s : TStringStream;
  168. begin
  169. s := TStringStream.Create ('');
  170. try
  171. WriteToStream (s);
  172. result := s.datastring;
  173. finally
  174. s.free;
  175. end;
  176. end;
  177. procedure THtmlCustomElement.WriteToStream(const aStream: TStream);
  178. var f : THTMLElementFlags;
  179. begin
  180. StringToStream (aStream, '<%s', [TagName]);
  181. WriteAttributes (aStream);
  182. StringToStream (aStream, '>'#13#10);
  183. f := HTMLElementProps[FELementTag].flags;
  184. if (efSubelementContent in f) or (efPCDATAContent in f) then
  185. begin
  186. WriteSubNodes (aStream);
  187. StringToStream (aStream, '</%s>'#13#10, [TagName]);
  188. end;
  189. end;
  190. function THtmlCustomElement.GetAttribute(const name: THTMLAttributeTag): DOMString;
  191. begin
  192. result := inherited GetAttribute (HTMLAttributeTag[name]);
  193. end;
  194. procedure THtmlCustomElement.SetAttribute(const name: THTMLAttributeTag;
  195. const value: DOMString);
  196. begin
  197. inherited SetAttribute (HTMLAttributeTag[name], value);
  198. end;
  199. procedure THtmlCustomElement.RemoveAttribute(const name: THTMLAttributeTag);
  200. begin
  201. inherited RemoveAttribute (HTMLAttributeTag[name]);
  202. end;
  203. { THTMLs18nElement }
  204. function THTMLs18nElement.GetDir: THTMLDir;
  205. var r : THTMLDir;
  206. s : DOMString;
  207. begin
  208. s := GetAttribute (atDir);
  209. r := high(THTMLdir);
  210. while (r > low(THTMLDir)) and (comparetext(s,HTMLDir[r]) <> 0) do
  211. begin
  212. dec (r);
  213. end;
  214. result := r;
  215. end;
  216. procedure THTMLs18nElement.SetDir(const AValue: THTMLDir);
  217. begin
  218. if AValue = dirEmpty then
  219. RemoveAttribute(atDir)
  220. else
  221. SetAttribute (atDir, HTMLDir[AValue]);
  222. end;
  223. { THTMLCores18nElement }
  224. function THTMLCores18nElement.GetDir: THTMLDir;
  225. var r : THTMLDir;
  226. s : DOMString;
  227. begin
  228. s := GetAttribute (atDir);
  229. r := high(THTMLdir);
  230. while (r > low(THTMLDir)) and (comparetext(s,HTMLDir[r]) <> 0) do
  231. begin
  232. dec (r);
  233. end;
  234. result := r;
  235. end;
  236. procedure THTMLCores18nElement.SetDir(const AValue: THTMLDir);
  237. begin
  238. if AValue = dirEmpty then
  239. RemoveAttribute(atDir)
  240. else
  241. SetAttribute (atDir, HTMLDir[AValue]);
  242. end;
  243. // generated implementations
  244. {$i tagsimpl.inc}
  245. { THTML_text }
  246. constructor THTML_text.create (AOwner: TDOMDocument);
  247. begin
  248. inherited create (AOwner);
  249. ElementTag := ettext;
  250. end;
  251. procedure THTML_text.WriteToStream(const aStream: TStream);
  252. begin
  253. StringToStream (aStream, NodeValue+#13#10);
  254. end;
  255. { THTMLDocument }
  256. procedure THTMLDocument.SaveToStream(const aStream: TStream);
  257. var d : TDOMNode;
  258. begin
  259. d := FirstChild;
  260. while assigned(d) do
  261. begin
  262. if d is THTMLCustomElement then
  263. THTMLCustomELement(d).WriteToStream(aStream);
  264. d := d.NextSibling;
  265. end;
  266. end;
  267. procedure THTMLDocument.SaveToFile(const afilename: string);
  268. var f : TFileStream;
  269. begin
  270. f := TFileStream.Create (afilename, fmCreate);
  271. try
  272. SaveToStream (f);
  273. finally
  274. f.Free;
  275. end;
  276. end;
  277. function THTMLDocument.Asstring: string;
  278. var s : TStringStream;
  279. begin
  280. s := TStringStream.Create ('');
  281. try
  282. SaveToStream (s);
  283. result := s.DataString;
  284. finally
  285. s.Free;
  286. end;
  287. end;
  288. end.