1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
{
$Id: header,v 1.1 2000/07/13 06:33:45 michael Exp $
This file is part of the Free Component Library (FCL)
Copyright (c) 1999-2000 by the Free Pascal development team
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit htmlwriter;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, DOM, htmlelements;
type
HTMLWriterException = class (exception);
{ THTMLwriter }
THTMLwriter = class
private
FCurrentElement : THTMLCustomElement;
FDocument: THTMLDocument;
procedure SetDocument(const AValue: THTMLDocument);
procedure SetCurrentElement (AValue : THTMLCustomElement);
protected
function CreateElement (tag : THTMLElementClass; s : string) : THTMLCustomElement;
function CreateElement (tag : THTMLElementClass; sub : THTMLCustomElement) : THTMLCustomElement;
function CreateElement (tag : THTMLElementClass; subs : Array of THTMLCustomElement) : THTMLCustomElement;
function CreateElement (tag : THTMLElementClass; subs : TDOMNodelist) : THTMLCustomElement;
function AddElement (tag : THTMLElementClass) : THTMLCustomElement;
public
function StartElement (tag : THTMLElementClass) : THTMLCustomElement;
function EndElement (tag : THTMLElementClass) : THTMLCustomElement;
constructor create (aDocument : THTMLDocument);
procedure AddElement (el : THTMLCustomElement);
procedure AddElements (subs : TDOMNodelist);
procedure AddElements (subs : array of THTMLCustomElement);
function Text (s : string) : THTML_Text;
function Text (Fmt : string; args : array of const) : THTML_Text;
{ Form input elements }
function FormText (aname, avalue: DOMstring) : THTML_Input;
function FormText (aname, avalue: DOMstring; alength : integer) : THTML_Input;
function FormMemo (aname, avalue: DOMstring; arows,acols: integer) : THTML_Textarea;
function FormSelect (aname: DOMstring; preselect, size: integer; Options: TStrings; UseValues:boolean) : THTML_Select;
function FormSelect (aname, preselect: DOMstring; size: integer; Options: TStrings; UseValues:boolean) : THTML_Select;
function FormPasswd (aname: DOMstring) : THTML_Input;
function FormCheckbox (aname, avalue: DOMstring; achecked: boolean) : THTML_Input;
function FormRadio (aname, avalue: DOMstring; achecked: boolean) : THTML_Input;
function FormSubmit (aname, avalue: DOMstring) : THTML_Input;
function FormImage (aname, imagesrc, ausemap: DOMstring) : THTML_Input;
function FormReset : THTML_Input;
function FormButton (aname, caption, aOnClick: DOMstring) : THTML_Input;
function FormHidden (aname, aValue: DOMstring) : THTML_Input;
function FormFile (aname, aValue:DOMstring) : THTML_Input;
{ Other useful links to elements }
function Meta (aname, ahtpequiv,acontent: DOMString) : THTML_meta;
function Link (arel, ahref, athetype, amedia: DOMString) : THTML_link;
function Script (s, athetype, asrc: DOMString) : THTML_script;
{$i wtagsintf.inc}
property Document : THTMLDocument read FDocument write SetDocument;
property CurrentElement : THTMLCustomElement read FCurrentElement write SetCurrentElement;
end;
implementation
uses HTMLDefs;
resourcestring
sErrNoCorespondingParent = 'No open element found with tag "%s"';
{ THTMLwriter }
procedure THTMLwriter.SetDocument(const AValue: THTMLDocument);
begin
if FDocument <> AValue then
begin
FDocument := AValue;
FCurrentElement := nil;
end;
end;
function THTMLwriter.CreateElement(tag: THTMLElementClass; s: string): THTMLCustomElement;
begin
result := StartElement (tag);
Text (s);
EndElement (tag);
end;
function THTMLwriter.CreateElement(tag: THTMLElementClass; sub: THTMLCustomElement): THTMLCustomElement;
begin
result := StartElement (tag);
AddElement (sub);
EndElement (tag);
end;
function THTMLwriter.CreateElement(tag: THTMLElementClass; subs: array of THTMLCustomElement): THTMLCustomElement;
begin
result := StartElement (tag);
AddElements (subs);
EndElement (tag);
end;
function THTMLwriter.CreateElement(tag: THTMLElementClass; subs: TDOMNodelist): THTMLCustomElement;
begin
result := StartElement (tag);
AddElements (subs);
EndElement (tag);
end;
function THTMLwriter.StartElement(tag: THTMLElementClass): THTMLCustomElement;
begin
result := AddElement (tag);
FCurrentElement := result;
end;
function THTMLwriter.EndElement(tag: THTMLElementClass): THTMLCustomElement;
var d : TDOMNode;
begin
d := FCurrentElement;
while assigned(d) and not (d is tag) do
d := d.ParentNode;
if assigned (d) then
begin
result := THTMLCustomElement(d);
if result.ParentNode = FDocument then
FCurrentElement := nil
else
FCurrentElement := THTMLCustomElement(result.ParentNode);
end
else
raise HTMLWriterException.CreateFmt (sErrNoCorespondingParent, [tag.ClassName]);
end;
constructor THTMLwriter.create(aDocument: THTMLDocument);
begin
inherited create;
FDocument := aDocument;
end;
procedure THTMLwriter.SetCurrentElement(AValue: THTMLCustomElement);
begin
if not assigned (AValue) then
FCurrentElement := nil
else
if AValue.OwnerDocument = FDocument then
FCurrentElement := AValue;
end;
function THTMLwriter.AddElement(tag: THTMLElementClass): THTMLCustomElement;
begin
result := tag.Create (Document);
AddElement (result);
end;
procedure THTMLwriter.AddElement(el: THTMLCustomElement);
begin
if assigned (FCurrentElement) then
FCurrentElement.AppendChild (el)
else
FDocument.AppendChild (el);
end;
procedure THTMLwriter.AddElements(subs: TDOMNodelist);
var r : integer;
d : TDOMNode;
begin
for r := 0 to subs.count-1 do
begin
d := subs.item[r];
if d is THTMLCustomElement then
AddElement (THTMLCustomElement(d));
end;
end;
procedure THTMLwriter.AddElements(subs: array of THTMLCustomElement);
var r : integer;
begin
for r := 0 to high(subs) do
AddElement (subs[r]);
end;
function THTMLwriter.Text (s : string): THTML_Text;
begin
result := THTML_text(AddElement(THTML_Text));
result.NodeValue := s;
end;
function THTMLwriter.Text(Fmt: string; args: array of const): THTML_Text;
begin
result := text(format(fmt, args));
end;
{ Form input elements }
function THTMLwriter.FormText(aname, avalue: DOMstring): THTML_Input;
begin
result := input;
with result do
begin
thetype := itText;
name := aname;
value := avalue;
end;
end;
function THTMLwriter.FormText(aname, avalue: DOMstring; alength: integer): THTML_Input;
begin
result := FormText (aname, avalue);
result.size := inttostr(alength);
end;
function THTMLwriter.FormMemo(aname, avalue: DOMstring; arows, acols: integer): THTML_Textarea;
begin
result := textarea(avalue);
with result do
begin
name := aname;
rows := inttostr(arows);
cols := inttostr(acols);
end;
end;
function THTMLwriter.FormSelect(aname: DOMstring; preselect, size: integer;
Options: TStrings; UseValues:boolean): THTML_Select;
var r : integer;
n,v : string;
begin
result := StartSelect;
result.size := inttostr(size);
result.name := aname;
if UseValues then
for r := 0 to options.count-1 do
begin
Options.GetNameValue (r, v, n);
with Option (n) do
begin
selected := (preselect = r);
Value := v;
end;
end
else
for r := 0 to options.count-1 do
Option (Options[r]).selected := (preselect = r);
EndSelect;
end;
function THTMLwriter.FormSelect(aname, preselect: DOMstring; size: integer;
Options: TStrings; UseValues:boolean): THTML_Select;
begin
if UseValues then
result := FormSelect (aname, Options.IndexOfName(preselect), size, Options, UseValues)
else
result := FormSelect (aname, Options.IndexOf(preselect), size, Options, UseValues);
end;
function THTMLwriter.FormPasswd(aname: DOMstring): THTML_Input;
begin
result := input;
with result do
begin
thetype := itPassword;
name := aname;
end;
end;
function THTMLwriter.FormCheckbox(aname, avalue: DOMstring; achecked: boolean): THTML_Input;
begin
result := input;
with result do
begin
thetype := itCheckbox;
name := aname;
value := avalue;
checked := achecked;
end;
end;
function THTMLwriter.FormRadio(aname, avalue: DOMstring; achecked: boolean): THTML_Input;
begin
result := input;
with result do
begin
thetype := itCheckbox;
name := aname;
value := avalue;
checked := achecked;
end;
end;
function THTMLwriter.FormSubmit(aname, avalue: DOMstring): THTML_Input;
begin
result := input;
with result do
begin
thetype := itSubmit;
name := aname;
value := avalue;
end;
end;
function THTMLwriter.FormImage(aname, imagesrc, ausemap: DOMstring): THTML_Input;
begin
result := input;
with result do
begin
thetype := itimage;
name := aname;
src := imagesrc;
usemap := ausemap;
end;
end;
function THTMLwriter.FormReset: THTML_Input;
begin
result := input;
result.thetype := itReset;
end;
function THTMLwriter.FormButton(aname, caption, aOnClick: DOMstring): THTML_Input;
begin
result := input;
with result do
begin
thetype := itButton;
name := aname;
value := caption;
onclick := aonclick;
end;
end;
function THTMLwriter.FormHidden(aname, aValue: DOMstring): THTML_Input;
begin
result := Input;
with result do
begin
thetype := itHidden;
name := aname;
value := avalue;
end;
end;
function THTMLwriter.FormFile(aname, aValue: DOMstring): THTML_Input;
begin
result := Input;
with result do
begin
thetype := itFile;
name := aname;
value := aValue;
end;
end;
function THTMLwriter.Meta(aname, ahtpequiv, acontent: DOMString): THTML_meta;
begin
result := tagmeta;
with result do
begin
name := aname;
httpequiv := ahtpequiv;
content := acontent;
end;
end;
function THTMLwriter.Link(arel, ahref, athetype, amedia: DOMString): THTML_link;
begin
result := taglink;
with result do
begin
rel := arel;
href := ahref;
thetype := athetype;
media := amedia;
end;
end;
function THTMLwriter.Script(s, athetype, asrc: DOMString): THTML_script;
begin
result := tagscript(s);
with result do
begin
thetype := athetype;
src := asrc;
end;
end;
{$i wtagsimpl.inc}
end.
|