-
Notifications
You must be signed in to change notification settings - Fork 10
/
DWElementTag.pas
488 lines (420 loc) · 14 KB
/
DWElementTag.pas
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
unit DWElementTag;
interface
uses Classes, System.SysUtils, DWMarkupLinguageTag, DWRenderStream;
type
TDWElementTagCollection = class;
TDWElementTag = class(TDWCustomElement)
private
FTag: string;
FParams: TStrings;
FNoValueAttributes: TStrings;
FContents: TDWElementTagCollection;
protected
procedure SetParentElement(const Value: TDWCustomElement); override;
public
constructor CreateHTMLTag(ATag: string; aParentTag: TDWElementTag = nil;
aCloseTag: TDWClosingTag = ctdwAuto); virtual;
Destructor Destroy; override;
procedure Add(aParamName: string);
procedure AddClassParam(aClassName: string);
procedure AddStringParam(aParamName: string; aParamValue: String; AllowEmpty: Boolean = False);
procedure AddIntegerParam(aParamName: string; aParamValue: Integer);
procedure AddBooleanParam(aParamName: string; aParamValue: Boolean);
procedure Render(ABuffer: TDWStream); overload; override;
function Render: String; overload; override;
procedure Clear;
property Contents: TDWElementTagCollection read FContents;
property Params: TStrings read FParams;
end;
TDWElementText = class(TDWCustomElement)
private
FText: String;
protected
procedure SetParentElement(const Value: TDWCustomElement); override;
procedure RenderElement(ABuffer: TDWStream; AIndent: Integer = 0); override;
public
procedure Assign(ASource: TPersistent); override;
procedure Render(ABuffer: TDWStream); overload; override;
function Render: String; overload; override;
procedure Clear;
property Text: string read FText write FText;
end;
TDWElementBinary = class(TDWCustomElement)
private
FBuffer: TDWStream;
protected
procedure SetParentElement(const Value: TDWCustomElement); override;
procedure RenderElement(ABuffer: TDWStream; AIndent: Integer = 0); override;
procedure InitializeElement(AParentElement: TDWCustomElement); override;
public
destructor Destroy; override;
procedure Render(ABuffer: TDWStream); overload; override;
function Render: String; overload; override;
procedure Clear;
property Buffer: TDWStream read FBuffer write FBuffer;
end;
TDWElementTagCollection = class(Classes.TList)
private
FParentElement: TDWCustomElement;
protected
function GetHTMLElement(AIndex: Integer): TDWCustomElement;
public
constructor Create(AParentElement: TDWCustomElement); virtual;
destructor Destroy; override;
procedure Clear; override;
procedure Assign(ASource: TDWElementTagCollection);
function AddElemetAsObject(const ATag: TDWCustomElement;
const ASetParentElement: Boolean = True): TDWCustomElement;
function AddElement(const ATag: String; const ASetParentElement: Boolean = True): TDWElementTag;
function AddText(const AText: String; const ASetParentElement: Boolean = True): TDWElementText;
function AddBuffer(const ABuffer: TDWStream; const ASetParentElement: Boolean)
: TDWElementBinary;
procedure AddElementCollection(AElementCollection: TDWElementTagCollection;
AFreeSourceCollection: Boolean; const ASetParentElement: Boolean);
function AddHiddenField(aId: string; aName: string; aValue: string): TDWElementTag;
property Items[AIndex: Integer]: TDWCustomElement read GetHTMLElement; default;
end;
TDWElementXHTMLTag = class(TDWElementTag)
private
FCDATA: Boolean;
procedure SetCDATA(const Value: Boolean);
public
constructor CreateHTMLTag(ATag: string; aParentTag: TDWElementTag = nil;
AsCDATA: Boolean = True); reintroduce;
function Render: String; overload; override;
property CDATA: Boolean read FCDATA write SetCDATA default True;
end;
implementation
{ TDWHTMLTag }
procedure TDWElementTag.Add(aParamName: string);
begin
FNoValueAttributes.Add(aParamName);
end;
procedure TDWElementTag.AddBooleanParam(aParamName: string; aParamValue: Boolean);
begin
if aParamValue then
FParams.Values[aParamName] := '"true"'
else
FParams.Values[aParamName] := '"false"';
Changed;
end;
procedure TDWElementTag.AddClassParam(aClassName: string);
var
Aux: string;
begin
aClassName := Trim(aClassName);
if aClassName <> '' then
begin
if FParams.Values['class'] <> '' then
begin
Aux := StringReplace(FParams.Values['class'], '"', '', [rfReplaceAll]);
Aux := Trim(Aux);
FParams.Values['class'] := '"' + Aux + ' ' + aClassName + '"';
end
else
FParams.Values['class'] := '"' + aClassName + '"';
Changed;
end;
end;
procedure TDWElementTag.AddIntegerParam(aParamName: string; aParamValue: Integer);
begin
FParams.Values[aParamName] := InttoStr(aParamValue);
Changed;
end;
procedure TDWElementTag.AddStringParam(aParamName, aParamValue: String;
AllowEmpty: Boolean = False);
var
LIndex: Integer;
begin
if (aParamValue = '') and (not AllowEmpty) then
begin
LIndex := FParams.IndexOf(aParamName);
if LIndex > -1 then
FParams.Delete(LIndex);
end
else
FParams.Values[aParamName] := '"' + aParamValue + '"';
Changed;
end;
procedure TDWElementTag.Clear;
begin
FParams.Clear;
FContents.Clear;
Changed;
end;
constructor TDWElementTag.CreateHTMLTag(ATag: string; aParentTag: TDWElementTag = nil;
aCloseTag: TDWClosingTag = ctdwAuto);
begin
inherited Create(aParentTag);
FTag := ATag;
FParams := TStringList.Create;
FNoValueAttributes := TStringList.Create;
FParams.NameValueSeparator := '=';
FParams.Delimiter := ' ';
FParams.StrictDelimiter := True;
FParams.QuoteChar := #0;
FContents := TDWElementTagCollection.Create(Self);
// FContent := '';
end;
destructor TDWElementTag.Destroy;
begin
FNoValueAttributes.Free;
FParams.Free;
inherited;
end;
procedure TDWElementTag.Render(ABuffer: TDWStream);
begin
end;
function TDWElementTag.Render: String;
var
LContent: string;
I: Integer;
begin
LContent := '';
for I := 0 to FContents.Count - 1 do
LContent := LContent + FContents[I].Render;
Result := '<' + FTag;
for I := 0 to FNoValueAttributes.Count - 1 do
begin
Result := Result + ' ' + FNoValueAttributes[I];
end;
if FParams.Count > 0 then
Result := Result + ' ' + FParams.DelimitedText;
if (FClosingTag = ctdwAuto) then
{ TODO 1 -oDELCIO -cIMPLEMENTAR : change for for tags of img, br, etc }
Result := Result + '>' + LContent + '</' + FTag + '>';
if FClosingTag = ctdwTrue then
Result := Result + '>' + LContent + '</' + FTag + '>';
if FClosingTag = ctdwFalse then
{ TODO 1 -oDELCIO -cIMPLEMENTAR : change for for tags of img, br, etc }
Result := Result + '>' + LContent;
if FClosingTag = ctdwSimpleClose then
Result := Result + ' ' + LContent + '>';
end;
procedure TDWElementTag.SetParentElement(const Value: TDWCustomElement);
var
OldIndex: Integer;
begin
// remove from Older Parent Content List
if (ParentElement <> nil) and (ParentElement is TDWElementTag) then
begin
// Check if it's already in the content list of Parent
OldIndex := (ParentElement as TDWElementTag).Contents.IndexOf(Self);
if OldIndex > -1 then
// Remove from Content list
(ParentElement as TDWElementTag).Contents.Delete(OldIndex);
end;
// Set ParentElement Property
inherited;
// add to new Parent Content List
if (ParentElement <> nil) and (ParentElement is TDWElementTag) then
begin
// Check if it's not already in the content list
if (ParentElement as TDWElementTag).Contents.IndexOf(Self) < 0 then
// Add to Content list
(ParentElement as TDWElementTag).Contents.AddElemetAsObject(Self, False);
end;
end;
{ TDWElementTagCollection }
function TDWElementTagCollection.AddBuffer(const ABuffer: TDWStream;
const ASetParentElement: Boolean): TDWElementBinary;
var
EL: TDWElementBinary;
begin
EL := TDWElementBinary.Create(nil);
EL.Buffer.CopyFrom(ABuffer, 0);
if ASetParentElement then
EL.ParentElement := FParentElement;
Result := EL;
end;
function TDWElementTagCollection.AddElement(const ATag: String;
const ASetParentElement: Boolean = True): TDWElementTag;
var
EL: TDWElementTag;
begin
EL := TDWElementTag.CreateHTMLTag(ATag);
inherited Add(EL);
if ASetParentElement then
EL.ParentElement := FParentElement;
Result := EL;
end;
procedure TDWElementTagCollection.AddElementCollection(AElementCollection: TDWElementTagCollection;
AFreeSourceCollection: Boolean; const ASetParentElement: Boolean);
begin
end;
function TDWElementTagCollection.AddElemetAsObject(const ATag: TDWCustomElement;
const ASetParentElement: Boolean = True): TDWCustomElement;
begin
inherited Add(ATag);
if ASetParentElement then
ATag.ParentElement := FParentElement;
Result := ATag;
end;
function TDWElementTagCollection.AddHiddenField(aId, aName, aValue: string): TDWElementTag;
begin
Result := nil;
if ((aId <> '') or (aName <> '')) then
begin
Result := TDWElementTag.CreateHTMLTag('input');
if aId <> '' then
Result.AddStringParam('id', aId);
if aName <> '' then
Result.AddStringParam('name', aName);
Result.AddStringParam('type', 'hidden');
inherited Add(Result);
Result.ParentElement := FParentElement;
end;
end;
function TDWElementTagCollection.AddText(const AText: String;
const ASetParentElement: Boolean = True): TDWElementText;
var
EL: TDWElementText;
begin
Result := nil;
if AText <> '' then
begin
EL := TDWElementText.Create(nil);
EL.Text := AText;
inherited Add(EL);
if ASetParentElement then
EL.ParentElement := FParentElement;
Result := EL;
end;
end;
procedure TDWElementTagCollection.Assign(ASource: TDWElementTagCollection);
begin
inherited Assign(ASource, laCopy);
end;
procedure TDWElementTagCollection.Clear;
begin
inherited Clear;
end;
constructor TDWElementTagCollection.Create(AParentElement: TDWCustomElement);
begin
inherited Create;
FParentElement := AParentElement;
end;
destructor TDWElementTagCollection.Destroy;
begin
inherited;
end;
function TDWElementTagCollection.GetHTMLElement(AIndex: Integer): TDWCustomElement;
begin
Result := TDWCustomElement(inherited Get(AIndex));
end;
{ TDWElementText }
procedure TDWElementText.Assign(ASource: TPersistent);
begin
inherited;
end;
procedure TDWElementText.Render(ABuffer: TDWStream);
begin
end;
function TDWElementText.Render: String;
begin
Result := Text;
end;
procedure TDWElementText.RenderElement(ABuffer: TDWStream; AIndent: Integer);
begin
end;
procedure TDWElementText.SetParentElement(const Value: TDWCustomElement);
var
OldIndex: Integer;
begin
// remove from Older Parent Content List
if (ParentElement <> nil) and (ParentElement is TDWElementTag) then
begin
// Check if it's already in the content list of Parent
OldIndex := (ParentElement as TDWElementTag).Contents.IndexOf(Self);
if OldIndex > 0 then
// Remove from Content list
(ParentElement as TDWElementTag).Contents.Delete(OldIndex);
end;
// Set ParentElement Property
inherited;
// add to new Parent Content List
if (ParentElement <> nil) and (ParentElement is TDWElementTag) then
begin
// Check if it's not already in the content list
if (ParentElement as TDWElementTag).Contents.IndexOf(Self) < 0 then
// Add to Content list
(ParentElement as TDWElementTag).Contents.AddElemetAsObject(Self, False);
end;
end;
procedure TDWElementText.Clear;
begin
FText := '';
Changed;
end;
{ TDWElementBinary }
destructor TDWElementBinary.Destroy;
begin
inherited;
end;
procedure TDWElementBinary.InitializeElement(AParentElement: TDWCustomElement);
begin
// inherited;
end;
procedure TDWElementBinary.Render(ABuffer: TDWStream);
begin
end;
function TDWElementBinary.Render: String;
begin
Result := FBuffer.DataString;
end;
procedure TDWElementBinary.RenderElement(ABuffer: TDWStream; AIndent: Integer);
begin
// inherited;
end;
procedure TDWElementBinary.SetParentElement(const Value: TDWCustomElement);
var
OldIndex: Integer;
begin
// remove from Older Parent Content List
if (ParentElement <> nil) and (ParentElement is TDWElementTag) then
begin
// Check if it's already in the content list of Parent
OldIndex := (ParentElement as TDWElementTag).Contents.IndexOf(Self);
if OldIndex > 0 then
// Remove from Content list
(ParentElement as TDWElementTag).Contents.Delete(OldIndex);
end;
// Set ParentElement Property
inherited;
// add to new Parent Content List
if (ParentElement <> nil) and (ParentElement is TDWElementTag) then
begin
// Check if it's not already in the content list
if (ParentElement as TDWElementTag).Contents.IndexOf(Self) < 0 then
// Add to Content list
(ParentElement as TDWElementTag).Contents.AddElemetAsObject(Self, False);
end;
end;
procedure TDWElementBinary.Clear;
begin
FBuffer.Clear;
Changed;
end;
{ TDWElementXHTMLTag }
constructor TDWElementXHTMLTag.CreateHTMLTag(ATag: string; aParentTag: TDWElementTag;
AsCDATA: Boolean);
begin
inherited CreateHTMLTag(ATag, aParentTag, ctdwAuto);
FCDATA := AsCDATA;
end;
function TDWElementXHTMLTag.Render: String;
begin
if FCDATA then
begin
{ TODO 1 -oDELCIO -cIMPROVE : SpeedUp this }
Result := StringReplace(inherited Render, '<item>', '<item><![CDATA[', [rfReplaceAll]);
Result := StringReplace(Result, '</item>', ']]></item>', [rfReplaceAll]);
end
else
Result := inherited Render;
end;
procedure TDWElementXHTMLTag.SetCDATA(const Value: Boolean);
begin
FCDATA := Value;
end;
end.