htmlelements.pp 10 KB

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