whtml.pp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. {
  2. This file is part of the Free Component Library (Fcl)
  3. Copyright (c) 1999-2000 by the Free Pascal development team
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************}
  10. {$IFNDEF FPC_DOTTEDUNITS}
  11. unit whtml;
  12. {$ENDIF FPC_DOTTEDUNITS}
  13. {$ifdef fpc}
  14. {$mode objfpc}
  15. {$endif}
  16. interface
  17. {$IFDEF FPC_DOTTEDUNITS}
  18. uses Fcl.Wformat,System.Classes,System.SysUtils;
  19. {$ELSE FPC_DOTTEDUNITS}
  20. uses wformat,Classes,SysUtils;
  21. {$ENDIF FPC_DOTTEDUNITS}
  22. Type
  23. THTMLWriter=Class(TFormattingWriter)
  24. Public
  25. Constructor Create (AStream : TStream); override;
  26. Procedure TagStart(Const Name, Attrs : String);
  27. Procedure TagEnd(Const Name : String);
  28. Function EscapeText (AText : String) : String; override;
  29. Procedure DocumentStart(Const Title : String); override;
  30. Procedure DocumentEnd; override;
  31. Procedure HeaderStart(Alevel : Integer); override;
  32. Procedure HeaderEnd(Alevel : Integer); override;
  33. Procedure ParagraphStart; override;
  34. Procedure ParagraphEnd; override;
  35. Procedure LineBreak; override;
  36. Procedure Rule; override;
  37. Procedure BoldStart; override;
  38. Procedure BoldEnd;override;
  39. Procedure ItalicStart;override;
  40. Procedure ItalicEnd;override;
  41. Procedure UnderlineStart;override;
  42. Procedure UnderlineEnd;override;
  43. Procedure PreformatStart; override;
  44. Procedure PreformatEnd; override;
  45. Procedure TableStart( NoCols: Integer; Border : Boolean); override;
  46. Procedure TableEnd; override;
  47. Procedure RowStart; override;
  48. Procedure RowEnd; override;
  49. Procedure CellStart; override;
  50. Procedure CellEnd; override;
  51. Procedure HeaderCellStart; override;
  52. Procedure HeaderCellEnd; override;
  53. Procedure ListStart(ListType : TListType); override;
  54. Procedure ListEnd(ListType : TListType); override;
  55. Procedure ListItemStart; override;
  56. Procedure ListItemEnd; override;
  57. Procedure DefinitionItem(Const Aname,AText : String); override;
  58. // Form support
  59. Procedure FormStart(Const Action,Method : String);
  60. Procedure FormEnd;
  61. Procedure EmitInput(Const Name,Value : String);
  62. Procedure EmitInput(Const Name,Value, Attrs : String);
  63. Procedure EmitPasswordInput(Const Name,Value : String);
  64. Procedure EmitCheckBox(Const Name,Value : String);
  65. Procedure EmitCheckBox(Const Name,Value : String; Checked : Boolean);
  66. Procedure EmitRadioButton(Const Name,Value : String);
  67. Procedure EmitRadioButton(Const Name,Value : String; Checked : Boolean);
  68. Procedure EmitArea(Const Name,Value : String; Rows,Cols : Integer);
  69. Procedure EmitComboBox(Const Name, Value : String; Items : TStrings; UseValues : Boolean);
  70. Procedure EmitComboBox(Const Name, Value : String; Items : TStrings);
  71. Procedure EmitButton(Const Name,ButtonType,Value : String);
  72. Procedure EmitSubmitButton(Const Name,Value : String);
  73. Procedure EmitResetButton(Const Name,Value : String);
  74. Procedure EmitHiddenVar(Const Name,Value: String);
  75. end;
  76. Const
  77. ListTags : Array[TListType] of string[2] = ('OL','UL','DL');
  78. implementation
  79. { THTMLWriter }
  80. procedure THTMLWriter.BoldEnd;
  81. begin
  82. TagEnd('B');
  83. end;
  84. procedure THTMLWriter.BoldStart;
  85. begin
  86. TagStart('B','');
  87. end;
  88. procedure THTMLWriter.CellEnd;
  89. begin
  90. TagEnd('TD');
  91. end;
  92. procedure THTMLWriter.CellStart;
  93. begin
  94. TagStart('TD','');
  95. end;
  96. constructor THTMLWriter.Create(AStream: TStream);
  97. begin
  98. inherited;
  99. end;
  100. procedure THTMLWriter.DefinitionItem(const Aname, AText: String);
  101. begin
  102. TagStart('DT','');
  103. Write(Aname);
  104. TagEnd('DT');
  105. TagStart('DD','');
  106. Write(AText);
  107. TagEnd('DD');
  108. end;
  109. procedure THTMLWriter.DocumentEnd;
  110. begin
  111. TagEnd('BODY');
  112. TagEnd('HTML');
  113. end;
  114. procedure THTMLWriter.DocumentStart(const Title: String);
  115. begin
  116. inherited;
  117. TagStart('HTML','');
  118. TagStart('TITLE','');
  119. Write(Title);
  120. TagEnd('TITLE');
  121. TagStart('BODY','');
  122. end;
  123. function THTMLWriter.EscapeText(AText: String): String;
  124. begin
  125. // replace by a more sensitive method.
  126. Result:=StringReplace(AText,'&','&',[rfReplaceAll]);
  127. Result:=StringReplace(Result,'<','&lt;',[rfReplaceAll]);
  128. Result:=StringReplace(Result,'>','&gt;',[rfReplaceAll]);
  129. Result:=StringReplace(Result,#10,'<BR>',[rfreplaceAll]);
  130. end;
  131. procedure THTMLWriter.HeaderCellEnd;
  132. begin
  133. TagEnd('TH');
  134. end;
  135. procedure THTMLWriter.HeaderCellStart;
  136. begin
  137. TagStart('TH','');
  138. end;
  139. procedure THTMLWriter.HeaderEnd(Alevel: Integer);
  140. begin
  141. TagEnd(Format('H%d',[ALevel]));
  142. end;
  143. procedure THTMLWriter.HeaderStart(Alevel: Integer);
  144. begin
  145. TagStart(Format('H%d',[ALevel]),'');
  146. end;
  147. procedure THTMLWriter.ItalicEnd;
  148. begin
  149. TagEnd('I');
  150. end;
  151. procedure THTMLWriter.ItalicStart;
  152. begin
  153. TagStart('I','');
  154. end;
  155. procedure THTMLWriter.LineBreak;
  156. begin
  157. TagStart('BR','');
  158. end;
  159. procedure THTMLWriter.ListEnd(ListType: TListType);
  160. begin
  161. TagEnd(ListTags[ListType]);
  162. end;
  163. procedure THTMLWriter.ListItemEnd;
  164. begin
  165. TagEnd('LI');
  166. end;
  167. procedure THTMLWriter.ListItemStart;
  168. begin
  169. TagStart('LI','');
  170. end;
  171. procedure THTMLWriter.ListStart(ListType: TListType);
  172. begin
  173. TagStart(ListTags[ListType],'');
  174. end;
  175. procedure THTMLWriter.ParagraphEnd;
  176. begin
  177. TagEnd('P')
  178. end;
  179. procedure THTMLWriter.ParagraphStart;
  180. begin
  181. TagStart('P','')
  182. end;
  183. procedure THTMLWriter.PreformatEnd;
  184. begin
  185. TagEnd('PRE')
  186. end;
  187. procedure THTMLWriter.PreformatStart;
  188. begin
  189. TagStart('PRE','');
  190. end;
  191. procedure THTMLWriter.RowEnd;
  192. begin
  193. TagEnd('TR')
  194. end;
  195. procedure THTMLWriter.RowStart;
  196. begin
  197. TagStart('TR','')
  198. end;
  199. procedure THTMLWriter.Rule;
  200. begin
  201. TagStart('HR','');
  202. end;
  203. procedure THTMLWriter.TableStart(NoCols: Integer; Border: Boolean);
  204. Var
  205. Attr : string;
  206. begin
  207. if Border then
  208. Attr:='BORDER=1'
  209. else
  210. Attr:='';
  211. TagStart('TABLE',Attr);
  212. end;
  213. procedure THTMLWriter.TableEnd;
  214. begin
  215. TagEnd('TABLE');
  216. end;
  217. procedure THTMLWriter.TagEnd(const Name : String);
  218. begin
  219. Dump('</'+Name+'>');
  220. end;
  221. procedure THTMLWriter.TagStart(const Name, Attrs: String);
  222. begin
  223. Dump('<'+Name);
  224. If Attrs<>'' then
  225. begin
  226. Dump(' ');
  227. Dump(Attrs);
  228. end;
  229. Dump('>');
  230. end;
  231. procedure THTMLWriter.UnderlineEnd;
  232. begin
  233. TagEnd('U');
  234. end;
  235. procedure THTMLWriter.UnderlineStart;
  236. begin
  237. TagStart('U','');
  238. end;
  239. // Form support.
  240. Procedure THTMLWriter.FormStart(Const Action,Method : String);
  241. Var
  242. A : String;
  243. begin
  244. A:='ACTION="'+Action+'"';
  245. If (Method<>'') then
  246. A:=A+' METHOD="'+Method+'"';
  247. TagStart('FORM',A);
  248. end;
  249. Procedure THTMLWriter.FormEnd;
  250. begin
  251. Tagend('FORM');
  252. end;
  253. Procedure THTMLWriter.EmitInput(Const Name,Value : String);
  254. begin
  255. EmitInput(Name,Value,'');
  256. end;
  257. Procedure THTMLWriter.EmitPasswordInput(Const Name,Value : String);
  258. begin
  259. EmitInput(Name,Value,'TYPE="password"');
  260. end;
  261. Procedure THTMLWriter.EmitInput(Const Name,Value, Attrs : String);
  262. Var
  263. A : String;
  264. begin
  265. A:='NAME="'+Name+'"';
  266. If (Value<>'') then
  267. A:=A+' VALUE="'+Value+'"';
  268. If (Attrs<>'') then
  269. A:=A+' '+Attrs;
  270. TagStart('INPUT',A);
  271. end;
  272. Procedure THTMLWriter.EmitCheckBox(Const Name,Value : String);
  273. begin
  274. EmitCheckBox(Name,Value,False);
  275. end;
  276. Procedure THTMLWriter.EmitCheckBox(Const Name,Value : String; Checked : Boolean);
  277. Var
  278. A : String;
  279. begin
  280. A:='NAME="'+Name+'" TYPE="checkbox" VALUE="'+Value+'"';
  281. If Checked then
  282. A:=A+' CHECKED="checked"';
  283. TagStart('INPUT',A);
  284. end;
  285. Procedure THTMLWriter.EmitRadioButton(Const Name,Value : String);
  286. begin
  287. EmitRadioButton(Name,Value,False);
  288. end;
  289. Procedure THTMLWriter.EmitRadioButton(Const Name,Value : String; Checked : Boolean);
  290. Var
  291. A : String;
  292. begin
  293. A:='NAME="'+Name+'" TYPE="checkbox" VALUE="'+Value+'"';
  294. If Checked then
  295. A:=A+' CHECKED="checked"';
  296. TagStart('INPUT',A);
  297. end;
  298. Procedure THTMLWriter.EmitArea(Const Name,Value : String; Rows,Cols : Integer);
  299. Var
  300. A : String;
  301. begin
  302. A:='NAME="'+Name+'"';
  303. If (Rows<>0) and (cols<>0) then
  304. A:=A+Format(' ROWS=%d COLS=%d',[Rows,Cols]);
  305. TagStart('TEXTAREA',A);
  306. Write(Value);
  307. TagEnd('TEXTAREA');
  308. end;
  309. Procedure THTMLWriter.EmitComboBox(Const Name, Value : String; Items : TStrings);
  310. begin
  311. EmitComboBox(Name,Value,Items,False);
  312. end;
  313. Procedure THTMLWriter.EmitComboBox(Const Name, Value : String; Items : TStrings; UseValues : Boolean);
  314. Var
  315. A,S,V : String;
  316. I,P : Integer;
  317. begin
  318. TagStart('SELECT','NAME='+Name+'"');
  319. A:='';
  320. For I:=0 to Items.Count-1 do
  321. begin
  322. S:=Items[I];
  323. If UseValues then
  324. begin
  325. P:=Pos('=',S);
  326. If P>0 then
  327. begin
  328. V:=Copy(S,1,P-1);
  329. Delete(S,1,P);
  330. A:='VALUE="'+Copy(S,1,P-1)+'"';
  331. end
  332. else
  333. begin
  334. A:='';
  335. V:=S;
  336. end;
  337. end;
  338. If (Value<>'') and (V=Value) then
  339. A:=A+' SELECTED="selected"';
  340. TagStart('OPTION',A);
  341. end;
  342. TagEnd('SELECT')
  343. end;
  344. Procedure THTMLWriter.EmitSubmitButton(Const Name,Value : String);
  345. begin
  346. EmitButton(Name,'submit',Value)
  347. end;
  348. Procedure THTMLWriter.EmitResetButton(Const Name,Value : String);
  349. begin
  350. EmitButton(Name,'reset',Value)
  351. end;
  352. Procedure THTMLWriter.EmitButton(Const Name,ButtonType,Value : String);
  353. Var
  354. A : String;
  355. begin
  356. A:='TYPE="'+ButtonType+'"';
  357. If (Value<>'') then
  358. A:=A+' VALUE="'+Value+'"';
  359. If (Name<>'') then
  360. A:=A+' NAME="'+Name+'"';
  361. TagStart('INPUT',A)
  362. end;
  363. Procedure THTMLWriter.EmitHiddenVar(Const Name,Value: String);
  364. Var
  365. A : String;
  366. begin
  367. A:='TYPE="hidden" NAME="'+Name+'" VALUE="'+Value+'"';
  368. TagStart('INPUT',A);
  369. end;
  370. end.