htmlwriter.pp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 htmlwriter;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, DOM, htmlelements;
  16. type
  17. HTMLWriterException = class (exception);
  18. { THTMLwriter }
  19. THTMLwriter = class
  20. private
  21. FCurrentElement : THTMLCustomElement;
  22. FDocument: THTMLDocument;
  23. procedure SetDocument(const AValue: THTMLDocument);
  24. procedure SetCurrentElement (AValue : THTMLCustomElement);
  25. protected
  26. function CreateElement (tag : THTMLElementClass; s : string) : THTMLCustomElement;
  27. function CreateElement (tag : THTMLElementClass; sub : THTMLCustomElement) : THTMLCustomElement;
  28. function CreateElement (tag : THTMLElementClass; subs : Array of THTMLCustomElement) : THTMLCustomElement;
  29. function CreateElement (tag : THTMLElementClass; subs : TDOMNodelist) : THTMLCustomElement;
  30. function StartElement (tag : THTMLElementClass) : THTMLCustomElement;
  31. function EndElement (tag : THTMLElementClass) : THTMLCustomElement;
  32. function AddElement (tag : THTMLElementClass) : THTMLCustomElement;
  33. public
  34. constructor create (aDocument : THTMLDocument);
  35. procedure AddElement (el : THTMLCustomElement);
  36. procedure AddElements (subs : TDOMNodelist);
  37. procedure AddElements (subs : array of THTMLCustomElement);
  38. function Text (s : string) : THTML_Text;
  39. function Text (Fmt : string; args : array of const) : THTML_Text;
  40. { Form input elements }
  41. function FormText (aname, avalue: DOMstring) : THTML_Input;
  42. function FormText (aname, avalue: DOMstring; alength : integer) : THTML_Input;
  43. function FormMemo (aname, avalue: DOMstring; arows,acols: integer) : THTML_Textarea;
  44. function FormSelect (aname: DOMstring; preselect, size: integer; Options: TStrings; UseValues:boolean) : THTML_Select;
  45. function FormSelect (aname, preselect: DOMstring; size: integer; Options: TStrings; UseValues:boolean) : THTML_Select;
  46. function FormPasswd (aname: DOMstring) : THTML_Input;
  47. function FormCheckbox (aname, avalue: DOMstring; achecked: boolean) : THTML_Input;
  48. function FormRadio (aname, avalue: DOMstring; achecked: boolean) : THTML_Input;
  49. function FormSubmit (aname, avalue: DOMstring) : THTML_Input;
  50. function FormImage (aname, imagesrc, ausemap: DOMstring) : THTML_Input;
  51. function FormReset : THTML_Input;
  52. function FormButton (aname, caption, aOnClick: DOMstring) : THTML_Input;
  53. function FormHidden (aname, aValue: DOMstring) : THTML_Input;
  54. function FormFile (aname, aValue:DOMstring) : THTML_Input;
  55. {$i wtagsintf.inc}
  56. property Document : THTMLDocument read FDocument write SetDocument;
  57. property CurrentElement : THTMLCustomElement read FCurrentElement write SetCurrentElement;
  58. end;
  59. implementation
  60. uses HTMLDefs;
  61. resourcestring
  62. sErrNoCorespondingParent = 'No open element found with tag "%s"';
  63. { THTMLwriter }
  64. procedure THTMLwriter.SetDocument(const AValue: THTMLDocument);
  65. begin
  66. if FDocument <> AValue then
  67. begin
  68. FDocument := AValue;
  69. FCurrentElement := nil;
  70. end;
  71. end;
  72. function THTMLwriter.CreateElement(tag: THTMLElementClass; s: string): THTMLCustomElement;
  73. begin
  74. result := StartElement (tag);
  75. Text (s);
  76. EndElement (tag);
  77. end;
  78. function THTMLwriter.CreateElement(tag: THTMLElementClass; sub: THTMLCustomElement): THTMLCustomElement;
  79. begin
  80. result := StartElement (tag);
  81. AddElement (sub);
  82. EndElement (tag);
  83. end;
  84. function THTMLwriter.CreateElement(tag: THTMLElementClass; subs: array of THTMLCustomElement): THTMLCustomElement;
  85. begin
  86. result := StartElement (tag);
  87. AddElements (subs);
  88. EndElement (tag);
  89. end;
  90. function THTMLwriter.CreateElement(tag: THTMLElementClass; subs: TDOMNodelist): THTMLCustomElement;
  91. begin
  92. result := StartElement (tag);
  93. AddElements (subs);
  94. EndElement (tag);
  95. end;
  96. function THTMLwriter.StartElement(tag: THTMLElementClass): THTMLCustomElement;
  97. begin
  98. result := AddElement (tag);
  99. FCurrentElement := result;
  100. end;
  101. function THTMLwriter.EndElement(tag: THTMLElementClass): THTMLCustomElement;
  102. var d : TDOMNode;
  103. begin
  104. d := FCurrentElement;
  105. while assigned(d) and not (d is tag) do
  106. d := d.ParentNode;
  107. if assigned (d) then
  108. begin
  109. result := THTMLCustomElement(d);
  110. FCurrentElement := THTMLCustomElement(result.ParentNode);
  111. end
  112. else
  113. raise HTMLWriterException.CreateFmt (sErrNoCorespondingParent, [tag.ClassName]);
  114. end;
  115. constructor THTMLwriter.create(aDocument: THTMLDocument);
  116. begin
  117. inherited create;
  118. FDocument := aDocument;
  119. end;
  120. procedure THTMLwriter.SetCurrentElement(AValue: THTMLCustomElement);
  121. begin
  122. if not assigned (AValue) then
  123. FCurrentElement := nil
  124. else
  125. if AValue.OwnerDocument = FDocument then
  126. FCurrentElement := AValue;
  127. end;
  128. function THTMLwriter.AddElement(tag: THTMLElementClass): THTMLCustomElement;
  129. begin
  130. result := tag.Create (Document);
  131. AddElement (result);
  132. end;
  133. procedure THTMLwriter.AddElement(el: THTMLCustomElement);
  134. begin
  135. if assigned (FCurrentElement) then
  136. FCurrentElement.AppendChild (el)
  137. else
  138. FDocument.AppendChild (el);
  139. end;
  140. procedure THTMLwriter.AddElements(subs: TDOMNodelist);
  141. var r : integer;
  142. d : TDOMNode;
  143. begin
  144. for r := 0 to subs.count-1 do
  145. begin
  146. d := subs.item[r];
  147. if d is THTMLCustomElement then
  148. AddElement (THTMLCustomElement(d));
  149. end;
  150. end;
  151. procedure THTMLwriter.AddElements(subs: array of THTMLCustomElement);
  152. var r : integer;
  153. begin
  154. for r := 0 to high(subs) do
  155. AddElement (subs[r]);
  156. end;
  157. function THTMLwriter.Text (s : string): THTML_Text;
  158. begin
  159. result := THTML_text(AddElement(THTML_Text));
  160. result.NodeValue := s;
  161. end;
  162. function THTMLwriter.Text(Fmt: string; args: array of const): THTML_Text;
  163. begin
  164. result := text(format(fmt, args));
  165. end;
  166. { Form input elements }
  167. function THTMLwriter.FormText(aname, avalue: DOMstring): THTML_Input;
  168. begin
  169. result := input;
  170. with result do
  171. begin
  172. thetype := itText;
  173. name := aname;
  174. value := avalue;
  175. end;
  176. end;
  177. function THTMLwriter.FormText(aname, avalue: DOMstring; alength: integer): THTML_Input;
  178. begin
  179. result := FormText (aname, avalue);
  180. result.size := inttostr(alength);
  181. end;
  182. function THTMLwriter.FormMemo(aname, avalue: DOMstring; arows, acols: integer): THTML_Textarea;
  183. begin
  184. result := textarea(avalue);
  185. with result do
  186. begin
  187. name := aname;
  188. rows := inttostr(arows);
  189. cols := inttostr(acols);
  190. end;
  191. end;
  192. function THTMLwriter.FormSelect(aname: DOMstring; preselect, size: integer;
  193. Options: TStrings; UseValues:boolean): THTML_Select;
  194. var r : integer;
  195. n,v : string;
  196. begin
  197. result := StartSelect;
  198. result.size := inttostr(size);
  199. result.name := aname;
  200. if UseValues then
  201. for r := 0 to options.count-1 do
  202. begin
  203. Options.GetNameValue (r, v, n);
  204. with Option (n) do
  205. begin
  206. selected := (preselect = r);
  207. Value := v;
  208. end;
  209. end
  210. else
  211. for r := 0 to options.count-1 do
  212. Option (Options[r]).selected := (preselect = r);
  213. EndSelect;
  214. end;
  215. function THTMLwriter.FormSelect(aname, preselect: DOMstring; size: integer;
  216. Options: TStrings; UseValues:boolean): THTML_Select;
  217. begin
  218. if UseValues then
  219. result := FormSelect (aname, Options.IndexOfName(preselect), size, Options, UseValues)
  220. else
  221. result := FormSelect (aname, Options.IndexOf(preselect), size, Options, UseValues);
  222. end;
  223. function THTMLwriter.FormPasswd(aname: DOMstring): THTML_Input;
  224. begin
  225. result := input;
  226. with result do
  227. begin
  228. thetype := itPassword;
  229. name := aname;
  230. end;
  231. end;
  232. function THTMLwriter.FormCheckbox(aname, avalue: DOMstring; achecked: boolean): THTML_Input;
  233. begin
  234. result := input;
  235. with result do
  236. begin
  237. thetype := itCheckbox;
  238. name := aname;
  239. value := avalue;
  240. checked := achecked;
  241. end;
  242. end;
  243. function THTMLwriter.FormRadio(aname, avalue: DOMstring; achecked: boolean): THTML_Input;
  244. begin
  245. result := input;
  246. with result do
  247. begin
  248. thetype := itCheckbox;
  249. name := aname;
  250. value := avalue;
  251. checked := achecked;
  252. end;
  253. end;
  254. function THTMLwriter.FormSubmit(aname, avalue: DOMstring): THTML_Input;
  255. begin
  256. result := input;
  257. with result do
  258. begin
  259. thetype := itSubmit;
  260. name := aname;
  261. value := avalue;
  262. end;
  263. end;
  264. function THTMLwriter.FormImage(aname, imagesrc, ausemap: DOMstring): THTML_Input;
  265. begin
  266. result := input;
  267. with result do
  268. begin
  269. thetype := itimage;
  270. name := aname;
  271. src := imagesrc;
  272. usemap := ausemap;
  273. end;
  274. end;
  275. function THTMLwriter.FormReset: THTML_Input;
  276. begin
  277. result := input;
  278. result.thetype := itReset;
  279. end;
  280. function THTMLwriter.FormButton(aname, caption, aOnClick: DOMstring): THTML_Input;
  281. begin
  282. result := input;
  283. with result do
  284. begin
  285. thetype := itButton;
  286. name := aname;
  287. value := caption;
  288. onclick := aonclick;
  289. end;
  290. end;
  291. function THTMLwriter.FormHidden(aname, aValue: DOMstring): THTML_Input;
  292. begin
  293. result := Input;
  294. with result do
  295. begin
  296. thetype := itHidden;
  297. name := aname;
  298. value := avalue;
  299. end;
  300. end;
  301. function THTMLwriter.FormFile(aname, aValue: DOMstring): THTML_Input;
  302. begin
  303. result := Input;
  304. with result do
  305. begin
  306. thetype := itFile;
  307. name := aname;
  308. value := aValue;
  309. end;
  310. end;
  311. {$i wtagsimpl.inc}
  312. end.