htmlwriter.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 AddElement (tag : THTMLElementClass) : THTMLCustomElement;
  31. public
  32. function StartElement (tag : THTMLElementClass) : THTMLCustomElement;
  33. function EndElement (tag : THTMLElementClass) : THTMLCustomElement;
  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. { Other useful links to elements }
  56. function Meta (aname, ahtpequiv,acontent: DOMString) : THTML_meta;
  57. function Link (arel, ahref, athetype, amedia: DOMString) : THTML_link;
  58. function Script (s, athetype, asrc: DOMString) : THTML_script;
  59. {$i wtagsintf.inc}
  60. property Document : THTMLDocument read FDocument write SetDocument;
  61. property CurrentElement : THTMLCustomElement read FCurrentElement write SetCurrentElement;
  62. end;
  63. implementation
  64. uses HTMLDefs;
  65. resourcestring
  66. sErrNoCorespondingParent = 'No open element found with tag "%s"';
  67. { THTMLwriter }
  68. procedure THTMLwriter.SetDocument(const AValue: THTMLDocument);
  69. begin
  70. if FDocument <> AValue then
  71. begin
  72. FDocument := AValue;
  73. FCurrentElement := nil;
  74. end;
  75. end;
  76. function THTMLwriter.CreateElement(tag: THTMLElementClass; s: string): THTMLCustomElement;
  77. begin
  78. result := StartElement (tag);
  79. Text (s);
  80. EndElement (tag);
  81. end;
  82. function THTMLwriter.CreateElement(tag: THTMLElementClass; sub: THTMLCustomElement): THTMLCustomElement;
  83. begin
  84. result := StartElement (tag);
  85. AddElement (sub);
  86. EndElement (tag);
  87. end;
  88. function THTMLwriter.CreateElement(tag: THTMLElementClass; subs: array of THTMLCustomElement): THTMLCustomElement;
  89. begin
  90. result := StartElement (tag);
  91. AddElements (subs);
  92. EndElement (tag);
  93. end;
  94. function THTMLwriter.CreateElement(tag: THTMLElementClass; subs: TDOMNodelist): THTMLCustomElement;
  95. begin
  96. result := StartElement (tag);
  97. AddElements (subs);
  98. EndElement (tag);
  99. end;
  100. function THTMLwriter.StartElement(tag: THTMLElementClass): THTMLCustomElement;
  101. begin
  102. result := AddElement (tag);
  103. FCurrentElement := result;
  104. end;
  105. function THTMLwriter.EndElement(tag: THTMLElementClass): THTMLCustomElement;
  106. var d : TDOMNode;
  107. begin
  108. d := FCurrentElement;
  109. while assigned(d) and not (d is tag) do
  110. d := d.ParentNode;
  111. if assigned (d) then
  112. begin
  113. result := THTMLCustomElement(d);
  114. FCurrentElement := THTMLCustomElement(result.ParentNode);
  115. end
  116. else
  117. raise HTMLWriterException.CreateFmt (sErrNoCorespondingParent, [tag.ClassName]);
  118. end;
  119. constructor THTMLwriter.create(aDocument: THTMLDocument);
  120. begin
  121. inherited create;
  122. FDocument := aDocument;
  123. end;
  124. procedure THTMLwriter.SetCurrentElement(AValue: THTMLCustomElement);
  125. begin
  126. if not assigned (AValue) then
  127. FCurrentElement := nil
  128. else
  129. if AValue.OwnerDocument = FDocument then
  130. FCurrentElement := AValue;
  131. end;
  132. function THTMLwriter.AddElement(tag: THTMLElementClass): THTMLCustomElement;
  133. begin
  134. result := tag.Create (Document);
  135. AddElement (result);
  136. end;
  137. procedure THTMLwriter.AddElement(el: THTMLCustomElement);
  138. begin
  139. if assigned (FCurrentElement) then
  140. FCurrentElement.AppendChild (el)
  141. else
  142. FDocument.AppendChild (el);
  143. end;
  144. procedure THTMLwriter.AddElements(subs: TDOMNodelist);
  145. var r : integer;
  146. d : TDOMNode;
  147. begin
  148. for r := 0 to subs.count-1 do
  149. begin
  150. d := subs.item[r];
  151. if d is THTMLCustomElement then
  152. AddElement (THTMLCustomElement(d));
  153. end;
  154. end;
  155. procedure THTMLwriter.AddElements(subs: array of THTMLCustomElement);
  156. var r : integer;
  157. begin
  158. for r := 0 to high(subs) do
  159. AddElement (subs[r]);
  160. end;
  161. function THTMLwriter.Text (s : string): THTML_Text;
  162. begin
  163. result := THTML_text(AddElement(THTML_Text));
  164. result.NodeValue := s;
  165. end;
  166. function THTMLwriter.Text(Fmt: string; args: array of const): THTML_Text;
  167. begin
  168. result := text(format(fmt, args));
  169. end;
  170. { Form input elements }
  171. function THTMLwriter.FormText(aname, avalue: DOMstring): THTML_Input;
  172. begin
  173. result := input;
  174. with result do
  175. begin
  176. thetype := itText;
  177. name := aname;
  178. value := avalue;
  179. end;
  180. end;
  181. function THTMLwriter.FormText(aname, avalue: DOMstring; alength: integer): THTML_Input;
  182. begin
  183. result := FormText (aname, avalue);
  184. result.size := inttostr(alength);
  185. end;
  186. function THTMLwriter.FormMemo(aname, avalue: DOMstring; arows, acols: integer): THTML_Textarea;
  187. begin
  188. result := textarea(avalue);
  189. with result do
  190. begin
  191. name := aname;
  192. rows := inttostr(arows);
  193. cols := inttostr(acols);
  194. end;
  195. end;
  196. function THTMLwriter.FormSelect(aname: DOMstring; preselect, size: integer;
  197. Options: TStrings; UseValues:boolean): THTML_Select;
  198. var r : integer;
  199. n,v : string;
  200. begin
  201. result := StartSelect;
  202. result.size := inttostr(size);
  203. result.name := aname;
  204. if UseValues then
  205. for r := 0 to options.count-1 do
  206. begin
  207. Options.GetNameValue (r, v, n);
  208. with Option (n) do
  209. begin
  210. selected := (preselect = r);
  211. Value := v;
  212. end;
  213. end
  214. else
  215. for r := 0 to options.count-1 do
  216. Option (Options[r]).selected := (preselect = r);
  217. EndSelect;
  218. end;
  219. function THTMLwriter.FormSelect(aname, preselect: DOMstring; size: integer;
  220. Options: TStrings; UseValues:boolean): THTML_Select;
  221. begin
  222. if UseValues then
  223. result := FormSelect (aname, Options.IndexOfName(preselect), size, Options, UseValues)
  224. else
  225. result := FormSelect (aname, Options.IndexOf(preselect), size, Options, UseValues);
  226. end;
  227. function THTMLwriter.FormPasswd(aname: DOMstring): THTML_Input;
  228. begin
  229. result := input;
  230. with result do
  231. begin
  232. thetype := itPassword;
  233. name := aname;
  234. end;
  235. end;
  236. function THTMLwriter.FormCheckbox(aname, avalue: DOMstring; achecked: boolean): THTML_Input;
  237. begin
  238. result := input;
  239. with result do
  240. begin
  241. thetype := itCheckbox;
  242. name := aname;
  243. value := avalue;
  244. checked := achecked;
  245. end;
  246. end;
  247. function THTMLwriter.FormRadio(aname, avalue: DOMstring; achecked: boolean): THTML_Input;
  248. begin
  249. result := input;
  250. with result do
  251. begin
  252. thetype := itCheckbox;
  253. name := aname;
  254. value := avalue;
  255. checked := achecked;
  256. end;
  257. end;
  258. function THTMLwriter.FormSubmit(aname, avalue: DOMstring): THTML_Input;
  259. begin
  260. result := input;
  261. with result do
  262. begin
  263. thetype := itSubmit;
  264. name := aname;
  265. value := avalue;
  266. end;
  267. end;
  268. function THTMLwriter.FormImage(aname, imagesrc, ausemap: DOMstring): THTML_Input;
  269. begin
  270. result := input;
  271. with result do
  272. begin
  273. thetype := itimage;
  274. name := aname;
  275. src := imagesrc;
  276. usemap := ausemap;
  277. end;
  278. end;
  279. function THTMLwriter.FormReset: THTML_Input;
  280. begin
  281. result := input;
  282. result.thetype := itReset;
  283. end;
  284. function THTMLwriter.FormButton(aname, caption, aOnClick: DOMstring): THTML_Input;
  285. begin
  286. result := input;
  287. with result do
  288. begin
  289. thetype := itButton;
  290. name := aname;
  291. value := caption;
  292. onclick := aonclick;
  293. end;
  294. end;
  295. function THTMLwriter.FormHidden(aname, aValue: DOMstring): THTML_Input;
  296. begin
  297. result := Input;
  298. with result do
  299. begin
  300. thetype := itHidden;
  301. name := aname;
  302. value := avalue;
  303. end;
  304. end;
  305. function THTMLwriter.FormFile(aname, aValue: DOMstring): THTML_Input;
  306. begin
  307. result := Input;
  308. with result do
  309. begin
  310. thetype := itFile;
  311. name := aname;
  312. value := aValue;
  313. end;
  314. end;
  315. function THTMLwriter.Meta(aname, ahtpequiv, acontent: DOMString): THTML_meta;
  316. begin
  317. result := tagmeta;
  318. with result do
  319. begin
  320. name := aname;
  321. httpequiv := ahtpequiv;
  322. content := acontent;
  323. end;
  324. end;
  325. function THTMLwriter.Link(arel, ahref, athetype, amedia: DOMString): THTML_link;
  326. begin
  327. result := taglink;
  328. with result do
  329. begin
  330. rel := arel;
  331. href := ahref;
  332. thetype := athetype;
  333. media := amedia;
  334. end;
  335. end;
  336. function THTMLwriter.Script(s, athetype, asrc: DOMString): THTML_script;
  337. begin
  338. result := tagscript(s);
  339. with result do
  340. begin
  341. thetype := athetype;
  342. src := asrc;
  343. end;
  344. end;
  345. {$i wtagsimpl.inc}
  346. end.