htmlelements.pp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 AttributeNames [index:integer] : DOMString read GetAttributeName;
  42. property AttributeValues [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. FNodeValue : DOMString;
  97. protected
  98. function GetNodeValue: DOMString; override;
  99. procedure SetNodeValue(const AValue: DOMString); override;
  100. public
  101. constructor create (AOwner: TDOMDocument); override;
  102. procedure WriteToStream (const aStream : TStream); override;
  103. end;
  104. implementation
  105. { THtmlCustomElement }
  106. function THtmlCustomElement.GetAttributeName(index:integer): DOMString;
  107. var d : TDOMNode;
  108. begin
  109. d := TDOMNode(Attributes[index]);
  110. result := d.NodeName;
  111. end;
  112. function THtmlCustomElement.GetAttributeValue(index:integer): DOMString;
  113. var d : TDOMNode;
  114. begin
  115. d := TDOMNode(Attributes[index]);
  116. result := d.NodeValue;
  117. end;
  118. function THtmlCustomElement.GetTagName: DOMString;
  119. begin
  120. result := HTMLElementProps[FElementTag].Name
  121. end;
  122. procedure THtmlCustomElement.WriteAttributes(const aStream: TStream);
  123. var a : THTMLAttributeTag;
  124. attrs : THTMLAttributeSet;
  125. s : DOMstring;
  126. begin
  127. attrs := HTMLElementProps[ElementTag].Attributes;
  128. for a := low(THTMLAttributeTag) to high(THTMLAttributeTag) do
  129. if a in attrs then
  130. begin
  131. s := GetAttribute (a);
  132. if s <> '' then
  133. if a in booleanAttributes then
  134. StringToStream (aStream, ' %s', [HTMLAttributeTag[a]])
  135. else
  136. StringToStream (aStream, ' %s="%s"', [HTMLAttributeTag[a], s]);
  137. end;
  138. end;
  139. procedure THtmlCustomElement.WriteSubNodes(const aStream: TStream);
  140. var d : TDomNode;
  141. begin
  142. d := GetFirstChild;
  143. while assigned (d) do
  144. begin
  145. if d is THtmlCustomElement then
  146. THtmlCustomElement(d).writetostream (aStream);
  147. d := d.NextSibling;
  148. end;
  149. end;
  150. procedure THtmlCustomElement.StringToStream(const aStream: TStream; s: string);
  151. begin
  152. if s <> '' then
  153. astream.WriteBuffer (s[1], length(s));
  154. end;
  155. procedure THtmlCustomElement.StringToStream(const aStream: TStream; Fmt: string;
  156. Args: array of const);
  157. begin
  158. StringToStream (aStream, format (Fmt, args));
  159. end;
  160. function THtmlCustomElement.EscapeString(s: string): string;
  161. begin
  162. result := s;
  163. //TODO: Needs to convert all the special signs to their html names ("<" has to be "&lt;" etc.)
  164. end;
  165. constructor THtmlCustomElement.create(AOwner: TDOMDocument);
  166. begin
  167. inherited create (AOwner);
  168. FElementTag := etUnknown;
  169. end;
  170. function THtmlCustomElement.AsString: string;
  171. var s : TStringStream;
  172. begin
  173. s := TStringStream.Create ('');
  174. try
  175. WriteToStream (s);
  176. result := s.datastring;
  177. finally
  178. s.free;
  179. end;
  180. end;
  181. procedure THtmlCustomElement.WriteToStream(const aStream: TStream);
  182. var f : THTMLElementFlags;
  183. begin
  184. StringToStream (aStream, '<%s', [TagName]);
  185. WriteAttributes (aStream);
  186. StringToStream (aStream, '>'#13#10);
  187. f := HTMLElementProps[FELementTag].flags;
  188. if (efSubelementContent in f) or (efPCDATAContent in f) then
  189. begin
  190. WriteSubNodes (aStream);
  191. StringToStream (aStream, '</%s>'#13#10, [TagName]);
  192. end;
  193. end;
  194. function THtmlCustomElement.GetAttribute(const name: THTMLAttributeTag): DOMString;
  195. begin
  196. result := inherited GetAttribute (HTMLAttributeTag[name]);
  197. end;
  198. procedure THtmlCustomElement.SetAttribute(const name: THTMLAttributeTag;
  199. const value: DOMString);
  200. begin
  201. inherited SetAttribute (HTMLAttributeTag[name], value);
  202. end;
  203. procedure THtmlCustomElement.RemoveAttribute(const name: THTMLAttributeTag);
  204. begin
  205. inherited RemoveAttribute (HTMLAttributeTag[name]);
  206. end;
  207. { THTMLs18nElement }
  208. function THTMLs18nElement.GetDir: THTMLDir;
  209. var r : THTMLDir;
  210. s : DOMString;
  211. begin
  212. s := GetAttribute (atDir);
  213. r := high(THTMLdir);
  214. while (r > low(THTMLDir)) and (comparetext(s,HTMLDir[r]) <> 0) do
  215. begin
  216. dec (r);
  217. end;
  218. result := r;
  219. end;
  220. procedure THTMLs18nElement.SetDir(const AValue: THTMLDir);
  221. begin
  222. if AValue = dirEmpty then
  223. RemoveAttribute(atDir)
  224. else
  225. SetAttribute (atDir, HTMLDir[AValue]);
  226. end;
  227. { THTMLCores18nElement }
  228. function THTMLCores18nElement.GetDir: THTMLDir;
  229. var r : THTMLDir;
  230. s : DOMString;
  231. begin
  232. s := GetAttribute (atDir);
  233. r := high(THTMLdir);
  234. while (r > low(THTMLDir)) and (comparetext(s,HTMLDir[r]) <> 0) do
  235. begin
  236. dec (r);
  237. end;
  238. result := r;
  239. end;
  240. procedure THTMLCores18nElement.SetDir(const AValue: THTMLDir);
  241. begin
  242. if AValue = dirEmpty then
  243. RemoveAttribute(atDir)
  244. else
  245. SetAttribute (atDir, HTMLDir[AValue]);
  246. end;
  247. // generated implementations
  248. {$i tagsimpl.inc}
  249. { THTML_text }
  250. function THTML_text.GetNodeValue: DOMString;
  251. begin
  252. Result := FNodeValue;
  253. end;
  254. procedure THTML_text.SetNodeValue(const AValue: DOMString);
  255. begin
  256. FNodeValue := AValue;
  257. end;
  258. constructor THTML_text.create (AOwner: TDOMDocument);
  259. begin
  260. inherited create (AOwner);
  261. ElementTag := ettext;
  262. end;
  263. procedure THTML_text.WriteToStream(const aStream: TStream);
  264. begin
  265. StringToStream (aStream, NodeValue+#13#10);
  266. end;
  267. { THTMLDocument }
  268. procedure THTMLDocument.SaveToStream(const aStream: TStream);
  269. var d : TDOMNode;
  270. begin
  271. d := FirstChild;
  272. while assigned(d) do
  273. begin
  274. if d is THTMLCustomElement then
  275. THTMLCustomELement(d).WriteToStream(aStream);
  276. d := d.NextSibling;
  277. end;
  278. end;
  279. procedure THTMLDocument.SaveToFile(const afilename: string);
  280. var f : TFileStream;
  281. begin
  282. f := TFileStream.Create (afilename, fmCreate);
  283. try
  284. SaveToStream (f);
  285. finally
  286. f.Free;
  287. end;
  288. end;
  289. function THTMLDocument.Asstring: string;
  290. var s : TStringStream;
  291. begin
  292. s := TStringStream.Create ('');
  293. try
  294. SaveToStream (s);
  295. result := s.DataString;
  296. finally
  297. s.Free;
  298. end;
  299. end;
  300. end.