htmlelements.pp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. function GetNodeName: DOMString; override;
  29. procedure StringToStream (const aStream : TStream; s : string);
  30. procedure StringToStream (const aStream : TStream; Fmt : string; Args : array of const);
  31. function EscapeString (s : string) : string;
  32. public
  33. constructor create (AOwner: TDOMDocument); virtual;
  34. function AsString : string;
  35. procedure WriteToStream (const aStream : TStream); virtual;
  36. function GetAttribute(const name: THTMLAttributeTag): DOMString;
  37. procedure SetAttribute(const name:THTMLAttributeTag; const value: DOMString);
  38. procedure RemoveAttribute(const name: THTMLAttributeTag);
  39. property ElementTag : THTMLElementTag read FElementTag write FElementTag;
  40. property TagName : 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. function THtmlCustomElement.GetNodeName: DOMString;
  151. begin
  152. Result:=GetTagName;
  153. end;
  154. procedure THtmlCustomElement.StringToStream(const aStream: TStream; s: string);
  155. begin
  156. if s <> '' then
  157. astream.WriteBuffer (s[1], length(s));
  158. end;
  159. procedure THtmlCustomElement.StringToStream(const aStream: TStream; Fmt: string;
  160. Args: array of const);
  161. begin
  162. StringToStream (aStream, format (Fmt, args));
  163. end;
  164. function THtmlCustomElement.EscapeString(s: string): string;
  165. begin
  166. result := s;
  167. //TODO: Needs to convert all the special signs to their html names ("<" has to be "&lt;" etc.)
  168. end;
  169. constructor THtmlCustomElement.create(AOwner: TDOMDocument);
  170. begin
  171. inherited create (AOwner);
  172. FElementTag := etUnknown;
  173. end;
  174. function THtmlCustomElement.AsString: string;
  175. var s : TStringStream;
  176. begin
  177. s := TStringStream.Create ('');
  178. try
  179. WriteToStream (s);
  180. result := s.datastring;
  181. finally
  182. s.free;
  183. end;
  184. end;
  185. procedure THtmlCustomElement.WriteToStream(const aStream: TStream);
  186. var f : THTMLElementFlags;
  187. begin
  188. StringToStream (aStream, '<%s', [TagName]);
  189. WriteAttributes (aStream);
  190. StringToStream (aStream, '>'#13#10);
  191. f := HTMLElementProps[FELementTag].flags;
  192. if (efSubelementContent in f) or (efPCDATAContent in f) then
  193. begin
  194. WriteSubNodes (aStream);
  195. StringToStream (aStream, '</%s>'#13#10, [TagName]);
  196. end;
  197. end;
  198. function THtmlCustomElement.GetAttribute(const name: THTMLAttributeTag): DOMString;
  199. begin
  200. result := inherited GetAttribute (HTMLAttributeTag[name]);
  201. end;
  202. procedure THtmlCustomElement.SetAttribute(const name: THTMLAttributeTag;
  203. const value: DOMString);
  204. begin
  205. inherited SetAttribute (HTMLAttributeTag[name], value);
  206. end;
  207. procedure THtmlCustomElement.RemoveAttribute(const name: THTMLAttributeTag);
  208. begin
  209. inherited RemoveAttribute (HTMLAttributeTag[name]);
  210. end;
  211. { THTMLs18nElement }
  212. function THTMLs18nElement.GetDir: THTMLDir;
  213. var r : THTMLDir;
  214. s : DOMString;
  215. begin
  216. s := GetAttribute (atDir);
  217. r := high(THTMLdir);
  218. while (r > low(THTMLDir)) and (comparetext(s,HTMLDir[r]) <> 0) do
  219. begin
  220. dec (r);
  221. end;
  222. result := r;
  223. end;
  224. procedure THTMLs18nElement.SetDir(const AValue: THTMLDir);
  225. begin
  226. if AValue = dirEmpty then
  227. RemoveAttribute(atDir)
  228. else
  229. SetAttribute (atDir, HTMLDir[AValue]);
  230. end;
  231. { THTMLCores18nElement }
  232. function THTMLCores18nElement.GetDir: THTMLDir;
  233. var r : THTMLDir;
  234. s : DOMString;
  235. begin
  236. s := GetAttribute (atDir);
  237. r := high(THTMLdir);
  238. while (r > low(THTMLDir)) and (comparetext(s,HTMLDir[r]) <> 0) do
  239. begin
  240. dec (r);
  241. end;
  242. result := r;
  243. end;
  244. procedure THTMLCores18nElement.SetDir(const AValue: THTMLDir);
  245. begin
  246. if AValue = dirEmpty then
  247. RemoveAttribute(atDir)
  248. else
  249. SetAttribute (atDir, HTMLDir[AValue]);
  250. end;
  251. // generated implementations
  252. {$i tagsimpl.inc}
  253. { THTML_text }
  254. function THTML_text.GetNodeValue: DOMString;
  255. begin
  256. Result := FNodeValue;
  257. end;
  258. procedure THTML_text.SetNodeValue(const AValue: DOMString);
  259. begin
  260. FNodeValue := AValue;
  261. end;
  262. constructor THTML_text.create (AOwner: TDOMDocument);
  263. begin
  264. inherited create (AOwner);
  265. ElementTag := ettext;
  266. end;
  267. procedure THTML_text.WriteToStream(const aStream: TStream);
  268. begin
  269. StringToStream (aStream, NodeValue+#13#10);
  270. end;
  271. { THTMLDocument }
  272. procedure THTMLDocument.SaveToStream(const aStream: TStream);
  273. var d : TDOMNode;
  274. begin
  275. d := FirstChild;
  276. while assigned(d) do
  277. begin
  278. if d is THTMLCustomElement then
  279. THTMLCustomELement(d).WriteToStream(aStream);
  280. d := d.NextSibling;
  281. end;
  282. end;
  283. procedure THTMLDocument.SaveToFile(const afilename: string);
  284. var f : TFileStream;
  285. begin
  286. f := TFileStream.Create (afilename, fmCreate);
  287. try
  288. SaveToStream (f);
  289. finally
  290. f.Free;
  291. end;
  292. end;
  293. function THTMLDocument.Asstring: string;
  294. var s : TStringStream;
  295. begin
  296. s := TStringStream.Create ('');
  297. try
  298. SaveToStream (s);
  299. result := s.DataString;
  300. finally
  301. s.Free;
  302. end;
  303. end;
  304. end.