htmlwriter.pp 10 KB

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