-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathksHtmlBuilder.pas
2026 lines (1754 loc) · 52.3 KB
/
ksHtmlBuilder.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ ******************************************************************************
* *
* ksHtmlBuilder *
* *
* https://github.com/gmurt/ksHtmlBuilder *
* *
* Copyright 2023 Graham Murt *
* *
* email: [email protected] *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
******************************************************************************* }
unit ksHtmlBuilder;
interface
{$DEFINE USE_JSONDATAOBJECTS}
uses Windows, Classes, System.Generics.Collections, Graphics
{$IFDEF USE_JSONDATAOBJECTS}
, JsonDataObjects
{$ELSE}
, Json
{$ENDIF}
;
type
IHtmlDocument = interface;
THtmlCssStyle = class;
THtmlElement = class;
THtmlRawElement = class;
THtmlDivElement = class;
THtmlSpacerElement = class;
THtmlHeaderElement = class;
THtmlHrElement = class;
THtmlBrElement = class;
THtmlImageElement = class;
THtmlParagraphElement = class;
THtmlAlertElement = class;
THtmlButtonElement = class;
THtmlLinkElement = class;
THtmlSocialIcons = class;
THtmlElementClass = class of THtmlElement;
// THtmlRenderTarget = (htmlBrowser, htmlEmail);
THtmlSection = (htmlBody, htmlHead, htmlHtml, htmlScript, htmlStyle);
THtmlHeaderType = (h1, h2, h3, h4, h5, h6);
THtmlCssAttribute = (cssBackground, cssBackgroundColor, cssBorder,
cssBorderCollapse, cssBorderColor, cssBorderRadius, cssBorderStyle,
cssBorderWidth, cssColor, cssCursor, cssDisplay, cssFontFamily, cssFontSize,
cssFontStyle, cssFontWeight, cssHeight, cssLineHeight, cssMargin,
cssMarginBottom, cssMarginTop, cssMaxHeight, cssMaxWidth, cssMinHeight,
cssObjectFit, cssPadding, cssPaddingTop, cssTextAlign, cssTextAlignLast,
cssTextDecoration, cssVerticalAlign, cssWidth, cssWhiteSpace);
THtmlTagAttribute = (attAlign, attAlt, attBgColor, attCellSpacing,
attCellPadding, attHeight, attHref, attID, attSrc, attType, attWidth,
attValue);
THtmlButtonStyle = (btnPrimary, btnSecondary, btnSuccess, btnDanger,
btnWarning, btnInfo, btnLight, btnDark, btnLink);
THtmlAlertStyle = (asSuccess, asDanger, asWarning);
THtmlSocialUrls = record
FFacebookUrl: string;
FInstagramUrl: string;
FTwitterUrl: string;
end;
THtmlFooterImage = record
FImgSource: string;
FTargetUrl: string;
end;
TCssAttributeList = class(TDictionary<THtmlCssAttribute, string>)
private
function GetAsSingleLine: string;
public
procedure SaveToJson(AJson: TJsonArray);
procedure LoadFromJson(AJson: TJsonArray);
property AsSingleLine: string read GetAsSingleLine;
end;
THtmlAttributeList = class(TDictionary<THtmlTagAttribute, string>)
public
procedure LoadFromJson(AArray: TJsonArray);
procedure SaveToJson(AArray: TJsonArray);
end;
TCssStyleList = class(TObjectDictionary<string, THtmlCssStyle>)
private
function GetStyle(AName: string): THtmlCssStyle;
procedure BuildDefaultStyles;
public
procedure LoadFromJson(AJson: TJsonArray);
procedure SaveToJson(AJson: TJsonArray);
procedure GetHtml(AStrings: TStrings);
procedure SetAllHeaders(AAttribute: THtmlCssAttribute; AValue: string);
property Style[AName: string]: THtmlCssStyle read GetStyle; default;
end;
THtmlElementList = class(TObjectList<THtmlElement>)
private
[weak]
FDocument: IHtmlDocument;
FOwner: THtmlElement;
function AddImageStream(AStream: TStream): THtmlImageElement;
function CreateElement(AObj: string): THtmlElement;
function CreateClass(AClass: THtmlElementClass): THtmlElement;
function GetElementByID(AID: string): THtmlElement;
public
constructor Create(AOwner: THtmlElement; ADocument: IHtmlDocument);
function AddButton(AText, AUrl: string; AStyle: THtmlButtonStyle)
: THtmlButtonElement;
function AddDiv: THtmlDivElement;
function AddHtml(AHtml: string): THtmlRawElement;
function AddSpacer(AHeight: integer): THtmlSpacerElement;
function AddHeader(AType: THtmlHeaderType; AText: string)
: THtmlHeaderElement;
function AddHr: THtmlHrElement;
function AddBr: THtmlBrElement;
function AddImage(AImg: TGraphic): THtmlImageElement; overload;
function AddImageFromFile(AFilename: string): THtmlImageElement; overload;
function AddImageFromUrl(ASrc: string; const AInline: Boolean = False)
: THtmlImageElement; overload;
function AddParagraph(AText: string): THtmlParagraphElement;
function AddAlert(AText: string; AAlertStyle: THtmlAlertStyle)
: THtmlAlertElement;
function AddSocialIcons(ADetails: THtmlSocialUrls): THtmlSocialIcons;
function AddLink(AText, AUrl: string): THtmlLinkElement;
procedure LoadFromJson(AJson: TJsonArray);
procedure SaveToJson(AJson: TJsonArray);
property ElementByID[AID: string]: THtmlElement read GetElementByID;
end;
THtmlCssStyle = class
private
FAttributes: TCssAttributeList;
function GetAttribute(AName: THtmlCssAttribute): string;
procedure SetAttribute(AName: THtmlCssAttribute; const Value: string);
public
constructor Create; virtual;
destructor Destroy; override;
procedure LoadFromJson(AJson: TJsonObject);
procedure SaveToJson(AJson: TJsonObject);
procedure GetHtml(AStrings: TStrings);
procedure SetBorderAttributes(AColor, AWidth, AStyle: string);
procedure SetFontAttributes(AFamily, AColor, ASize: string);
property Attribute[AName: THtmlCssAttribute]: string read GetAttribute
write SetAttribute;
end;
THtmlElement = class(TPersistent)
private
[weak]
FDocument: IHtmlDocument;
FParent: THtmlElement;
FElements: THtmlElementList;
FContent: string;
FClass: TStrings;
FAttributes: THtmlAttributeList;
FStyles: TCssAttributeList;
function GetStyle(AStyle: THtmlCssAttribute): string;
function GetAttributesSingleLine: string;
function GetStylesSingleLine: string;
procedure SetStyle(AStyle: THtmlCssAttribute; const Value: string);
function GetClassSingleLine: string;
function GetAttribute(AAttribute: THtmlTagAttribute): string;
procedure SetAttribute(AAttribute: THtmlTagAttribute; const Value: string);
protected
function GetTag: string; virtual; abstract;
function HasClosingTag: Boolean; virtual;
procedure GetHtml(AHtml: TStrings); virtual;
procedure GetInternalHtml(AHtml: TStrings); virtual;
public
constructor Create(ADocument: IHtmlDocument); virtual;
procedure Clear;
procedure LoadFromJson(AJson: TJsonObject); virtual;
procedure SaveToJson(AJson: TJsonObject); virtual;
destructor Destroy; override;
property Content: string read FContent write FContent;
property Elements: THtmlElementList read FElements;
property Attribute[AAttribute: THtmlTagAttribute]: string read GetAttribute
write SetAttribute;
procedure SetBorderAttributes(AColor, AWidth, AStyle: string);
procedure SetFontAttributes(AFamily, AColor, ASize: string);
property Style[AStyle: THtmlCssAttribute]: string read GetStyle
write SetStyle;
property CssClass: TStrings read FClass;
end;
THtmlRawElement = class(THtmlElement)
protected
function GetTag: string; override;
end;
THtmlDivElement = class(THtmlElement)
protected
function GetTag: string; override;
end;
THtmlSpacerElement = class(THtmlDivElement)
protected
procedure GetHtml(AHtml: TStrings); override;
end;
THtmlHrElement = class(THtmlElement)
protected
function GetTag: string; override;
function HasClosingTag: Boolean; override;
procedure GetHtml(AHtml: TStrings); override;
end;
THtmlBrElement = class(THtmlElement)
protected
function GetTag: string; override;
function HasClosingTag: Boolean; override;
end;
THtmlImageElement = class(THtmlElement)
private
function GetSrc: string;
procedure SetSrc(const Value: string);
protected
function GetTag: string; override;
function HasClosingTag: Boolean; override;
procedure GetHtml(AHtml: TStrings); override;
public
constructor Create(ADocument: IHtmlDocument); override;
property Src: string read GetSrc write SetSrc;
end;
THtmlTextElement = class(THtmlElement)
private
function GetText: string;
procedure SetText(const Value: string);
protected
property Text: string read GetText write SetText;
end;
THtmlHeaderElement = class(THtmlTextElement)
private
FHeaderType: THtmlHeaderType;
protected
function GetTag: string; override;
public
property HeaderType: THtmlHeaderType read FHeaderType write FHeaderType;
end;
THtmlParagraphElement = class(THtmlTextElement)
protected
function GetTag: string; override;
end;
THtmlAlertElement = class(THtmlTextElement)
private
FStyle: THtmlAlertStyle;
protected
function GetTag: string; override;
public
property Style: THtmlAlertStyle read FStyle write FStyle;
end;
THtmlLinkElement = class(THtmlTextElement)
protected
function GetTag: string; override;
end;
THtmlButtonElement = class(THtmlDivElement)
protected
FUrl: string;
protected
function GetTag: string; override;
procedure GetHtml(AHtml: TStrings); override;
public
procedure LoadFromJson(AJson: TJsonObject); override;
procedure SaveToJson(AJson: TJsonObject); override;
end;
THtmlSocialIcons = class(THtmlDivElement)
private
FSocialUrls: THtmlSocialUrls;
protected
procedure GetInternalHtml(AHtml: TStrings); override;
public
property SocialUrls: THtmlSocialUrls read FSocialUrls write FSocialUrls;
end;
THtmlHeadSection = class(THtmlElement)
private
FTitle: string;
FMetaData: TStrings;
FCssStyles: TCssStyleList;
protected
function GetTag: string; override;
procedure GetInternalHtml(AStrings: TStrings); override;
public
constructor Create(ADocument: IHtmlDocument); override;
destructor Destroy; override;
procedure Clear;
procedure LoadFromJson(AJson: TJsonObject); override;
procedure SaveToJson(AJson: TJsonObject); override;
property MetaData: TStrings read FMetaData;
property Styles: TCssStyleList read FCssStyles;
end;
IHtmlDocument = interface
['{6B7716EE-3A39-493F-89D4-60077631259E}']
function GetHead: THtmlHeadSection;
function GetElements: THtmlElementList;
function GetAsJson: string;
function GetAsHtml: string;
function GetReference: string;
procedure SetAsJson(const Value: string);
procedure SetReference(const Value: string);
procedure LoadFromJson(AJson: TJsonObject);
procedure SaveToJson(AJson: TJsonObject);
procedure SaveHtmlToFile(AFilename: string);
procedure Clear;
procedure SetFooterImage(AImg, AUrl: string);
property Head: THtmlHeadSection read GetHead;
property Elements: THtmlElementList read GetElements;
property AsHtml: string read GetAsHtml;
property AsJson: string read GetAsJson write SetAsJson;
property Reference: string read GetReference write SetReference;
end;
function CreateHtmlDocument(AMaxWidth: integer): IHtmlDocument;
implementation
{$R *.res}
uses SysUtils, Rtti, Net.HttpClient, System.NetEncoding, Jpeg, System.TypInfo;
const
C_FACEBOOK_ICON_URL =
'https://user-images.githubusercontent.com/1161351/215111435-0983d7e8-4804-41d5-b82c-2161777abe20.png';
C_INSTAGRAM_ICON_URL =
'https://user-images.githubusercontent.com/1161351/215112166-fb15ffe7-f6dd-4077-9efe-4d5a7ef1df7c.png';
C_TWITTER_ICON_URL =
'https://user-images.githubusercontent.com/1161351/215112156-2a73e843-4345-441f-b5e1-91c8ac30083c.png';
type
THtmlCssAttributeMap = TDictionary<THtmlCssAttribute, string>;
THtmlDocument = class(TInterfacedObject, IHtmlDocument)
private
FHead: THtmlHeadSection;
FBackgroundColor: string;
FElements: THtmlElementList;
FReference: string;
FFooterImage: THtmlFooterImage;
FMaxWidth: integer;
function GetAsHtml: string;
function GetHead: THtmlHeadSection;
function GetAsJson: string;
procedure SetAsJson(const Value: string);
function GetElements: THtmlElementList;
function GetBackgroundColor: string;
procedure SetBackgroundColor(const Value: string);
function GetReference: string;
procedure SetReference(const Value: string);
protected
procedure Clear;
procedure SetFooterImage(AImg, AUrl: string);
procedure SaveHtmlToFile(AFilename: string);
procedure LoadFromJson(AJson: TJsonObject);
procedure SaveToJson(AJson: TJsonObject);
property Head: THtmlHeadSection read GetHead;
property BackgroundColor: string read GetBackgroundColor
write SetBackgroundColor;
property Reference: string read GetReference write SetReference;
property AsHtml: string read GetAsHtml;
property AsJson: string read GetAsJson write SetAsJson;
public
constructor Create(AMaxWidth: integer); virtual;
destructor Destroy; override;
end;
var
InternalHtmlCssAttributeMap: THtmlCssAttributeMap;
function CreateHtmlDocument(AMaxWidth: integer): IHtmlDocument;
begin
Result := THtmlDocument.Create(AMaxWidth);
end;
{ THtmlDivElement }
function HtmlSectionToString(AElement: THtmlElement): string;
begin
Result := TRttiEnumerationType.GetName(AElement);
Result := StringReplace(Result, 'html', '', []).ToLower;
end;
function HtmlTagStringToAttribute(AStr: string): THtmlTagAttribute;
begin
AStr := AStr.ToLower;
if Pos('att', AStr) <> 1 then
AStr := 'att' + AStr;
Result := THtmlTagAttribute(GetEnumValue(TypeInfo(THtmlTagAttribute), AStr));;
end;
function HtmlTagAttributeToString(AAttribute: THtmlTagAttribute): string;
begin
Result := StringReplace(TRttiEnumerationType.GetName(AAttribute), 'att', '',
[rfReplaceAll]);
Result := Result.ToLower;
end;
function CssAttributeNameToString(AStyle: THtmlCssAttribute): string;
begin
Result := '';
InternalHtmlCssAttributeMap.TryGetValue(AStyle, Result);
end;
function CssAttributeNameFromString(AStr: string): THtmlCssAttribute;
begin
AStr := AStr.ToLower;
AStr := StringReplace(AStr, '-', '', [rfReplaceAll]);
if Pos('css', AStr) <> 1 then
AStr := 'css' + AStr;
Result := THtmlCssAttribute(GetEnumValue(TypeInfo(THtmlCssAttribute), AStr));
end;
function ButtonStyleToString(AStyle: THtmlButtonStyle): string;
begin
Result := LowerCase(StringReplace(TRttiEnumerationType.GetName(AStyle), 'btn',
'btn-', [rfIgnoreCase]));
end;
function AlertStyleToString(AStyle: THtmlAlertStyle): string;
begin
Result := '';
case AStyle of
asSuccess:
Result := 'alert-success';
asDanger:
Result := 'alert-danger';
asWarning:
Result := 'alert-warning';
end;
end;
function THtmlDivElement.GetTag: string;
begin
// Result := 'div';
// if ATarget = htmlEmail then
Result := 'table';
end;
{ THtmlElement }
procedure THtmlElement.Clear;
begin
FElements.Clear;
FContent := '';
FClass.Clear;
FAttributes.Clear;
FStyles.Clear;
end;
constructor THtmlElement.Create(ADocument: IHtmlDocument);
begin
inherited Create;
FDocument := ADocument;
FElements := THtmlElementList.Create(Self, FDocument);
FClass := TStringList.Create;
FAttributes := THtmlAttributeList.Create;
FStyles := TCssAttributeList.Create;
end;
destructor THtmlElement.Destroy;
begin
FElements.Free;
FAttributes.Free;
FStyles.Free;
FClass.Free;
inherited;
end;
procedure THtmlElement.GetHtml(AHtml: TStrings);
var
AElement: THtmlElement;
ABlock: string;
_ATag: string;
APadding: string;
ACellCss: string;
begin
_ATag := GetTag;
if (Self is THtmlDivElement) then
begin
_ATag := 'table';
Attribute[attCellSpacing] := '0';
Style[cssBorderCollapse] := 'collapse';
Style[cssWidth] := '100%';
end;
if _ATag <> '' then
begin
ABlock := '<' + _ATag;
ABlock := Trim(ABlock + ' ' + GetClassSingleLine);
ABlock := Trim(ABlock + ' ' + GetAttributesSingleLine);
ABlock := Trim(ABlock + ' ' + GetStylesSingleLine);
if _ATag = 'table' then
begin
ACellCss := '';
APadding := Style[cssPadding];
if APadding <> '' then
ACellCss := 'style="padding:' + APadding + ';" ';
ABlock := ABlock + '><tr><td valign="top"' + ACellCss;
end;
case HasClosingTag of
True:
ABlock := ABlock + '>';
False:
ABlock := ABlock + '/>';
end;
AHtml.Add(ABlock);
end;
if Trim(FContent) <> '' then
begin
AHtml.Add(Trim(FContent));
end;
GetInternalHtml(AHtml);
for AElement in Elements do
begin
AElement.GetHtml(AHtml);
end;
if HasClosingTag then
begin
ABlock := '</' + _ATag + '>';
if _ATag = 'table' then
begin
ABlock := '</td></tr>' + ABlock;
end;
AHtml.Add(ABlock);
end
// else
// AHtml.Text := AHtml.Text + '
end;
procedure THtmlElement.GetInternalHtml(AHtml: TStrings);
begin
// overridden in descendant classes
end;
function THtmlElement.GetStyle(AStyle: THtmlCssAttribute): string;
begin
FStyles.TryGetValue(AStyle, Result);
end;
function THtmlElement.GetAttribute(AAttribute: THtmlTagAttribute): string;
begin
FAttributes.TryGetValue(AAttribute, Result);
end;
function THtmlElement.GetAttributesSingleLine: string;
var
ATagAttribute: THtmlTagAttribute;
begin
Result := '';
if FAttributes.Count > 0 then
begin
for ATagAttribute in FAttributes.Keys do
begin
if FAttributes[ATagAttribute] <> '' then
begin
Result := Trim(Result + ' ' + HtmlTagAttributeToString(ATagAttribute) +
'="' + FAttributes.Items[ATagAttribute] + '" ');
end;
end;
end;
end;
function THtmlElement.GetClassSingleLine: string;
var
AClass: string;
begin
if FClass.Count > 0 then
begin
Result := 'class="';
for AClass in FClass do
Result := Result + StringReplace(AClass, '.', '', []) + ' ';
Result := Trim(Result) + '" ';
end;
end;
function THtmlElement.GetStylesSingleLine: string;
var
// AStyle: THtmlCssAttribute;
AStyleStrings: TStrings;
AClasses: TStrings;
// AClassStyle: THtmlCssStyle;
begin
Result := '';
AStyleStrings := TStringList.Create;
AClasses := TStringList.Create;
try
// extract css styles from stylesheet to inline on element for email output
AClasses.Add(GetTag);
for var s in FClass do
begin
AClasses.Add(s);
AClasses.Add('.' + s);
end;
for var AClass in AClasses do
begin
var
AClassStyle := FDocument.Head.FCssStyles.GetStyle(AClass);
if AClassStyle <> nil then
begin
for var AStyle in AClassStyle.FAttributes.Keys do
begin
AStyleStrings.Values[CssAttributeNameToString(AStyle)] :=
AClassStyle.FAttributes.Items[AStyle];
end;
end;
end;
for var AStyle in FStyles.Keys do
begin
if FStyles[AStyle] <> '' then
begin
AStyleStrings.Values[CssAttributeNameToString(AStyle)] :=
FStyles.Items[AStyle];
end;
end;
if AStyleStrings.Count > 0 then
begin
Result := Result + ' style="';
for var ICount := 0 to AStyleStrings.Count - 1 do
begin
Result := Trim(Result + ' ' + AStyleStrings.Names[ICount] + ': ' +
AStyleStrings.ValueFromIndex[ICount] + ';');
end;
Result := Result + '" ';
end;
finally
AClasses.Free;
AStyleStrings.Free;
end;
end;
function THtmlElement.HasClosingTag: Boolean;
begin
Result := GetTag <> '';
end;
procedure THtmlElement.LoadFromJson(AJson: TJsonObject);
begin
{$IFDEF USE_JSONDATAOBJECTS}
FClass.CommaText := AJson.s['class'];
FAttributes.LoadFromJson(AJson.A['attributes']);
FStyles.LoadFromJson(AJson.A['_styles']);
FContent := AJson.s['content'];
FElements.LoadFromJson(AJson.A['elements']);
{$ELSE}
if AJson.FindValue('class') <> nil then
FClass.CommaText := AJson.Values['class'].Value;
if AJson.FindValue('attributes') <> nil then
FAttributes.LoadFromJson(AJson.Values['attributes'].AsType<TJsonArray>);
if AJson.FindValue('_styles') <> nil then
FStyles.LoadFromJson(AJson.Values['_styles'].AsType<TJsonArray>);
if AJson.FindValue('content') <> nil then
FContent := AJson.Values['content'].Value;
if AJson.FindValue('elements') <> nil then
FElements.LoadFromJson(AJson.Values['elements'].AsType<TJsonArray>);
{$ENDIF}
end;
procedure THtmlElement.SaveToJson(AJson: TJsonObject);
{$IFNDEF USE_JSONDATAOBJECTS}
var
AArray: TJsonArray;
{$ENDIF}
begin
{$IFDEF USE_JSONDATAOBJECTS}
AJson.s['obj'] := ClassName;
if FClass.Count > 0 then
AJson.s['class'] := FClass.CommaText;
if FAttributes.Count > 0 then
FAttributes.SaveToJson(AJson.A['attributes']);
if FStyles.Count > 0 then
FStyles.SaveToJson(AJson.A['_styles']);
if FContent <> '' then
AJson.s['content'] := FContent;
if FElements.Count > 0 then
FElements.SaveToJson(AJson.A['elements']);
{$ELSE}
AJson.AddPair('obj', ClassName);
if FClass.Count > 0 then
AJson.AddPair('class', FClass.CommaText);
if FContent <> '' then
AJson.AddPair('content', FContent);
if FAttributes.Count > 0 then
begin
AArray := TJsonArray.Create;
FAttributes.SaveToJson(AArray);
AJson.AddPair('attributes', AArray);
end;
if FStyles.Count > 0 then
begin
AArray := TJsonArray.Create;
FStyles.SaveToJson(AArray);
AJson.AddPair('_styles', AArray);
end;
if FElements.Count > 0 then
begin
AArray := TJsonArray.Create;
FElements.SaveToJson(AArray);
AJson.AddPair('elements', AArray);
end;
{$ENDIF}
end;
procedure THtmlElement.SetAttribute(AAttribute: THtmlTagAttribute;
const Value: string);
begin
FAttributes.AddOrSetValue(AAttribute, Value);
end;
procedure THtmlElement.SetBorderAttributes(AColor, AWidth, AStyle: string);
begin
if AColor <> '' then
Style[cssBorderColor] := AColor;
if AWidth <> '' then
Style[cssBorderWidth] := AWidth;
if AStyle <> '' then
Style[cssBorderStyle] := AStyle;
end;
procedure THtmlElement.SetFontAttributes(AFamily, AColor, ASize: string);
begin
if AFamily <> '' then
Style[cssFontFamily] := AFamily;
if AColor <> '' then
Style[cssColor] := AColor;
if ASize <> '' then
Style[cssFontSize] := ASize;
end;
procedure THtmlElement.SetStyle(AStyle: THtmlCssAttribute; const Value: string);
begin
FStyles.AddOrSetValue(AStyle, Value);
end;
{ THtmlHeadSection }
procedure THtmlHeadSection.Clear;
begin
FTitle := '';
FCssStyles.Clear;
FCssStyles.BuildDefaultStyles;
end;
constructor THtmlHeadSection.Create;
begin
inherited;
FMetaData := TStringList.Create;
FTitle := '';
FMetaData.Add
('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />');
FMetaData.Add
('<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />');
FMetaData.Add
('<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>');
FCssStyles := TCssStyleList.Create([doOwnsValues]);
FCssStyles.BuildDefaultStyles;
end;
destructor THtmlHeadSection.Destroy;
begin
FCssStyles.Free;
FMetaData.Free;
inherited;
end;
procedure THtmlHeadSection.GetInternalHtml(AStrings: TStrings);
begin
inherited;
AStrings.Add('<title>' + FTitle + '</title>');
AStrings.AddStrings(FMetaData);
// if ATarget <> htmlEmail then
// begin
// FCssStyles.GetHtml(AStrings);
// end;
end;
function THtmlHeadSection.GetTag: string;
begin
Result := 'head';
end;
procedure THtmlHeadSection.LoadFromJson(AJson: TJsonObject);
{$IFNDEF USE_JSONDATAOBJECTS}
var
AArray: TJsonArray;
{$ENDIF}
begin
inherited;
{$IFDEF USE_JSONDATAOBJECTS}
FTitle := AJson.s['title'];
FCssStyles.LoadFromJson(AJson.A['styles']);
{$ELSE}
AArray := AJson.GetValue('styles').AsType<TJsonArray>;
FCssStyles.LoadFromJson(AArray);
{$ENDIF}
end;
procedure THtmlHeadSection.SaveToJson(AJson: TJsonObject);
{$IFNDEF USE_JSONDATAOBJECTS}
var
AArray: TJsonArray;
{$ENDIF}
begin
inherited;
{$IFDEF USE_JSONDATAOBJECTS}
AJson.s['title'] := FTitle;
FCssStyles.SaveToJson(AJson.A['styles']);
{$ELSE}
AArray := TJsonArray.Create;
FCssStyles.SaveToJson(AArray);
AJson.AddPair('styles', AArray);
{$ENDIF}
end;
{ THtmlDocument }
procedure THtmlDocument.Clear;
begin
FHead.Clear;
FElements.Clear;
FReference := '';
FFooterImage.FImgSource := '';
FFooterImage.FTargetUrl := '';
end;
constructor THtmlDocument.Create(AMaxWidth: integer);
begin
inherited Create;
FHead := THtmlHeadSection.Create(Self);
FHead.FDocument := Self;
FMaxWidth := AMaxWidth;
FHead.FParent := nil;
FElements := THtmlElementList.Create(nil, Self);
FBackgroundColor := '#eeeeee';
end;
destructor THtmlDocument.Destroy;
begin
FHead.Free;
FElements.Free;
// FBody.Free;
inherited;
end;
function THtmlDocument.GetAsHtml: string;
var
AStrings: TStrings;
AElement: THtmlElement;
begin
AStrings := TStringList.Create;
try
AStrings.Add
('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
AStrings.Add('<html xmlns="http://www.w3.org/1999/xhtml">');
FHead.GetHtml(AStrings);
AStrings.Add('<body align="center" bgcolor="' + FBackgroundColor +'" style="background-color: ' + FBackgroundColor + '; ">');
AStrings.Add('<table style="' + FHead.FCssStyles.GetStyle('body')
.FAttributes.GetAsSingleLine +
' padding: 10px; width:100%; background-color: ' + FBackgroundColor + '; '
+ 'border: 1px solid ' + FBackgroundColor + '; ' +
'font-family: Tahoma, Geneva, sans-serif; font-size: 14px; line-height: 24px;">');
AStrings.Add('<tr><td>');
AStrings.Add
('<table style="padding: 16px; text-align: center; border: 1px solid #cccccc; width: 100%; max-width: '+FMaxWidth.ToString+'px; margin: 0 auto; background-color: white; "');
// 'border-collapse: collapse; border-spacing: 0; mso-table-lspace: 0pt !important; mso-table-rspace: 0pt !important;">
// ');
AStrings.Add('<tr><td style="line-height: 24px;">');
// elements
for AElement in FElements do
begin
AElement.GetHtml(AStrings);
end;
AStrings.Add('</td></tr>');
AStrings.Add('</table>');
AStrings.Add('</td></tr>');
AStrings.Add('</table>');
if FFooterImage.FImgSource <> '' then
begin
AStrings.Add('<a target="_blank" href="' + FFooterImage.FTargetUrl +
'"><img alt="footer image" src="' + FFooterImage.FImgSource + '"/></a>');
end;
if FReference <> '' then
AStrings.Add
('<br><br><p align="center" style="font-size: 9px; color:#000000;">Ref: '
+ FReference + '</p><br>');
AStrings.Add('</body>');
AStrings.Add('</html>');
Result := AStrings.Text;
finally
AStrings.Free;
end;
end;
function THtmlDocument.GetAsJson: string;
var
AJson: TJsonObject;
begin
AJson := TJsonObject.Create;
try
SaveToJson(AJson);
Result := AJson.ToJSON;
finally
AJson.Free;
end;
end;
function THtmlDocument.GetBackgroundColor: string;
begin
Result := FBackgroundColor;
end;
function THtmlDocument.GetElements: THtmlElementList;
begin
Result := FElements;
end;
{
function THtmlDocument.GetContainer: THtmlDivElement;
begin
Result := FBody.Elements.ElementByID['_container'] as THtmlDivElement;
end;