htmlelements.pp 10 KB

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