-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHTMLWriterUtils.pas
More file actions
839 lines (738 loc) · 29.4 KB
/
HTMLWriterUtils.pas
File metadata and controls
839 lines (738 loc) · 29.4 KB
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
unit HTMLWriterUtils;
{$REGION 'License'}
{
Copyright (C) 2010 Nick Hodges
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
}
{$ENDREGION}
interface
uses SysUtils, Classes, Generics.Collections;
resourcestring
strCaptionMustBeFirst = 'A <caption> tag must be the very first tag after a <table> tag.';
strATagsBracketMust = 'A tag''s bracket must be open to add an attribute. The Current tag is %s and the attribute being added is %s';
strTagNameRequired = 'The aTagName parameter of the THTMLWriter constructor cannot be an empty string.';
strOpenBracketImpossible = 'It should be impossible that the bracket is open here. Seeing this error means a very bad logic problem.';
strAMetaTagCanOnly = 'This tag can only be added inside a <head> tag.';
strThisMethodCanOnly = 'This method can only be called inside a <meta> tag.';
strClosingClosedTag = 'An attempt is being made to close a tag that is already closed.';
strMustBeInList = 'A list must be open in order to call CloseList.';
strMustBeInTable = 'A <table> tag must be open in order to call this.';
strMustBeInTableRow = 'A <tr> tag must be open to call this.';
strMustBeInComment = 'A comment must be open in order to call CloseComment';
strNotInFieldTag = 'A fieldset must be open to add this tag.';
strNoClosingTag = 'The FClosingTag field is empty, and that should never be the case.';
strNotInFrameSet = 'A <frame> tag can only be added inside of a <frameset> tag.';
strNotInMapTag = 'An <area> tag can only be added inside of a <map> tag.';
strNotInFormTag = 'A <form> tag must be open in order to call this.';
strMustBeInObject = 'An <object> tag must be open in order to call this.';
strOtherTagsOpen = 'The document cannot be closed -- there are non-<html> tags still open.';
strCantOpenCaptionOutsideTable = 'A <caption> tag can only be added immediately after a <table> tag';
strParamNameRequired = 'The name of a <param> tag cannot be empty';
strDeprecatedTag = 'The %s tag is deprecated in HTML %s.x.';
strMustBeInSelectTag = 'A <select> tag must be open in order to use this tag.';
strCantOpenColOutsideTable = 'The <col> tag must be opened inside of a <table> tag';
strBadTagAfterTableContent = 'This tag cannot be added after table content has been added (<tr>, <th>, <tbody>, <tfoot>, etc.)';
strMustBeInDefinitionList = 'This tag can only be included in a Definition List <dl> tag.';
strCannotNestDefLists = 'You cannot nest a definition list inside another definition list';
strCannotAddDefItemWithoutDefTerm = 'You cannot add a <dd> tag except right after a <dl> tag or another <dd> tag';
type
IGetHTML = interface
['{FB072C2E-B4B4-43BE-9D1A-3BB870883144}']
function AsHTML: string;
end;
TIsPercentage = (ipIsPercentage, ipIsNotPercentage);
/// <summary>A data structure that holds a width, and then publishes that width in various ways useful in
/// HTML.</summary>
THTMLWidth = record
Width: integer;
IsPercentage: TIsPercentage;
constructor Create(aWidth: integer; aIsPercentage: TIsPercentage);
function AsPercentage: string;
function WidthString: string;
function WidthAsString: string;
end;
type
EHTMLWriterException = class(Exception);
EEmptyTagHTMLWriterException = class(EHTMLWriterException); // Tested
EOpenTagRequiredHTMLWriterException = class(EHTMLWriterException); //Tested
EHeadTagRequiredHTMLException = class(EHTMLWriterException); // Tested
ETryingToCloseClosedTag = class(EHTMLWriterException); // Tested
ENotInListTagException = class(EHTMLWriterException); // Tested
ENotInTableTagException = class(EHTMLWriterException); // Tested
ENotInCommentTagException = class(EHTMLWriterException); // Tested
ENotInFieldsetTagException = class(EHTMLWriterException); // Tested
ENoClosingTagHTMLWriterException = class(EHTMLWriterException);
ENotInFrameSetHTMLException = class(EHTMLWriterException); // Tested
ENotInMapTagHTMLException = class(EHTMLWriterException); // Tested
ENotInFormTagHTMLException = class(EHTMLWriterException); // Tested
ENotInObjectTagException = class(EHTMLWriterException); // Tested
EClosingDocumentWithOpenTagsHTMLException = class(EHTMLWriterException); // Tested.
ETableTagNotOpenHTMLWriterException = class(EHTMLWriterException); // Tested
EParamNameRequiredHTMLWriterException = class(EHTMLWriterException); // Tested
ETagIsDeprecatedHTMLWriterException = class(EHTMLWriterException); // Tested
ENotInSelectTextHTMLWriterException = class(EHTMLWriterException); // Tested
ECaptionMustBeFirstHTMLWriterException = class(EHTMLWriterException); // Tested
EBadTagAfterTableContentHTMLWriter = class(EHTMLWriterException); // Tested
ENotInDefinitionListHTMLError = class(EHTMLWriterException); // Tested
ECannotNestDefinitionListsHTMLWriterException = class(EHTMLWriterException); // Tested
ECannotAddDefItemWithoutDefTermHTMLWriterException = class(EHTMLWriterException); //Tested
type
{$REGION 'Documentation'}
/// <summary>Enumeration that defines the various states that a tag can be in.</summary>
/// <remarks>This enumeration is not normally used by itself, but only as part of the TTagStates set type.</remarks>
/// <seealso cref="TTagStates">TTagStates</seealso>
{$ENDREGION}
TTagState = (
/// <summary>Indicates that the current state of the tag is that the left bracket has been added but the right has not been. (e.g. "<span " The tag is able to accept attributes at this point.</summary>
tsBracketOpen,
/// <summary>Indicates that the tag is open and the bracket has been added. (e.g. "<span>)"</summary>
tsTagOpen,
/// <summary>Indicates that the current HTML is part of a comment.</summary>
tsCommentOpen,
/// <summary>Indicates that the tag is currently closed (e.g. "<span></span>)"</summary>
tsTagClosed,
/// <summary>Indicates that the current HTML is being written inside of a <head> tag.</summary>
tsInHeadTag,
/// <summary>Indicates that the current HTML is being written inside of a <body> tag.</summary>
tsInBodyTag,
/// <summary>Indicates that the current HTML is being written inside of a <list> tag.</summary>
tsInListTag,
/// <summary>Indicates that the current HTML is being written inside of a <object> tag.</summary>
tsInObjectTag,
/// <summary>Indicates that the current HTML is being written inside of a <fieldset> tag.</summary>
tsInFieldSetTag,
/// <summary>Indicates that the current HTML is being written inside of a <frameset> tag.</summary>
tsInFrameSetTag,
/// <summary>Indicates that the current HTML is being written inside of a <map> tag.</summary>
tsInMapTag,
tsInDefinitionList,
tsHasDefinitionTerm,
tsDefTermIsCurrent,
tsDefItemIsCurrent
);
TTagStates = set of TTagState;
{$REGION 'Documentation'}
/// <summary>
/// Enumeration to define possible states of an open <table> tag.
/// </summary>
/// <remarks>
/// This enumeration is used internally and should not be used by the
/// user.
/// </remarks>
{$ENDREGION}
TTableState = (
/// <summary>
/// Indicates that the current HTML is part of a table. (<table>)
/// </summary>
tbsInTable,
/// <summary>
/// Indicates that the current HTML is part of a Table Row (<tr>)
/// </summary>
tbsInTableRowTag,
/// <summary>
/// Indicates that a <caption> tag has been added to the table tag
/// </summary>
tbsTableHasCaption,
/// <summary>
/// Indicates that a <colgroup> tag has been added to the table tag
/// </summary>
tbsTableHasColGroup,
tbsTableHasCol,
/// <summary>
/// Indicates that at least one <td> tag has been added.
/// </summary>
tbsTableHasData
);
TTableStates = set of TTableState;
/// <remarks>These tags are used to keep track of the cursor inside of a <form> tag.</remarks>
TFormState = (
/// <summary>Indicates that the cursor is curerntly inside of a <form> tag</summary>
fsInFormTag,
/// <summary>Indicates that the cursor is curerntly inside of a <select> tag</summary>
fsInSelect,
/// <summary>Indicates that the cursor is curerntly inside of a <optgroup> tag</summary>
fsInOptGroup
);
TFormStates = set of TFormState;
TCanHaveAttributes = (
/// <summary>Indicates that the given tag can accept attributes.</summary>
chaCanHaveAttributes,
/// <summary>Indicates that the given tag cannot accept attributes.</summary>
chaCannotHaveAttributes);
/// <summary>An enumeration listing the different ways that text can be formatted</summary>
TFormatType = (
/// <summary>Formats with a <b> tag</summary>
ftBold,
/// <summary>Formats with a <i> tag</summary>
ftItalic,
/// <summary>Formats with a <u> tag</summary>
ftUnderline,
/// <summary>Formats with a <em> tag</summary>
ftEmphasis,
/// <summary>Formats with a <strong> tag</summary>
ftStrong,
/// <summary>Formats with a <sub> tag</summary>
ftSubscript,
/// <summary>Formats with a <sup> tag</summary>
ftSuperscript,
/// <summary>Formats with a <pre> tag</summary>
ftPreformatted,
/// <summary>Formats with a <cite> tag</summary>
ftCitation,
/// <summary>Formats with a <acronym> tag</summary>
ftAcronym,
/// <summary>Formats with a <abbr> tag</summary>
ftAbbreviation,
/// <summary>Formats with a <address> tag</summary>
ftAddress,
/// <summary>Formats with a <bdo> tag</summary>
ftBDO,
/// <summary>Formats with a <big> tag</summary>
ftBig,
/// <summary>Formats with a <center> tag</summary>
ftCenter,
/// <summary>Formats with a <code> tag</summary>
ftCode,
/// <summary>Formats with a <del> tag</summary>
ftDelete,
/// <summary>Formats with a <dfn> tag</summary>
ftDefinition,
/// <summary>Formats with a <font> tag</summary>
ftFont,
/// <summary>Formats with a <kbd> tag</summary>
ftKeyboard,
/// <summary>Formats with a <q> tag</summary>
ftQuotation,
/// <summary>Formats with a <samp> tag</summary>
ftSample,
/// <summary>Formats with a <small> tag</summary>
ftSmall,
/// <summary>Formats with a <strike> tag</summary>
ftStrike,
/// <summary>Formats with a <tt> tag</summary>
ftTeletype,
/// <summary>Formats with a <var> tag</summary>
ftVariable,
/// <summary>Formats with a <ins> tag</summary>
ftInsert,
/// <summary>Formats with a <mark> tag</summary>
ftMark,
/// <summary>Formats with a <bdi> tag</summary>
ftBdi,
/// <summary>Formats with a <ruby> tag</summary>
ftRuby,
/// <summary>Formats with a <rt> tag</summary>
ftRt,
/// <summary>Formats with a <rp> tag</summary>
ftRp);
THeadingType = (htHeading1, htHeading2, htHeading3, htHeading4, htHeading5, htHeading6);
TClearValue = (cvNoValue, cvNone, cvLeft, cvRight, cvAll);
TIsEmptyTag = (ietIsEmptyTag, ietIsNotEmptyTag);
/// <summary>Enumeration to define the different types of bullets that can be used with the <ol> and <ul> tags.</summary>
/// <remarks>This type is used in conjunction with the OpenUnorderdedList function</remarks>
/// <seealso cref="OpenUnorderedList">OpenUnorderedList</seealso>
TBulletShape = (
/// <summary>Indicates that no bullet should be used</summary>
bsNone,
/// <summary>Indicates that a black disc should be used as the bullet</summary>
bsDisc,
/// <summary>Indicates that a small circle should be used as the bullet</summary>
bsCircle,
/// <summary>Indicates that a black square should be used as the bullet</summary>
bsSquare
);
/// <summary>Enumeration to define the type of numbering system to use in an ordered list (<ol>)</summary>
TNumberType = (
/// <summary>Indicates that no numbers should be used.</summary>
ntNone,
/// <summary>Indicates the use of Arabic numbers, i.e. 1, 2, 3, 4</summary>
ntNumber,
/// <summary>Indicates the use of capital letters, i.e. A, B, C, D</summary>
ntUpperCase,
/// <summary>Indicates the use of lower-case letters, i.e. a, b, c, d</summary>
ntLowerCase,
/// <summary>Indicates the use of upper case Roman numerals, i.e. I, II, III, IV</summary>
ntUpperRoman,
/// <summary>Indicates the use of lower case Roman numerals, i.e. i, ii, iii, iv</summary>
ntLowerRoman
);
TTargetType = (ttBlank, ttParent, ttSelf, ttTop, ttFrameName);
/// <summary>
/// Enumeration to define the kind of closing a given tag requires.
/// </summary>
/// <remarks>
/// This type is used internally, and should not normally be used by a consuer of HTMLWriter.
/// </remarks>
TCloseTagType = (
/// <summary>
/// Use the normal closing tag, i.e. ">"
/// </summary>
ctNormal,
/// <summary>
/// Use the empty closing tag, i.e. "/>"
/// </summary>
ctEmpty,
/// <summary>
/// Use the comment closing tag, i.e. "-->"
/// </summary>
ctComment
);
{$REGION 'Documentation'}
/// <summary>
/// An enumaration defining the various DocumentType tags that can be used by an HTML document.
/// </summary>
{$ENDREGION}
THTMLDocType = (
dtHTML401Strict,
dtHTML401Transitional,
dtHTML401Frameset,
cXHTML10Strict,
dtXHTML10Transitional,
dtXHTML10Frameset,
dtXHTML11,
dtHTML5
);
{$REGION 'Documentation'}
/// <summary>
/// An enumeration used to determine whether a carriage return/line feed
/// pair should be used between tags
/// </summary>
{$ENDREGION}
TUseCRLFOptions = (
/// <summary>
/// Indicates that a CRLF should be used
/// </summary>
ucoUseCRLF,
/// <summary>
/// Indicates that a CRLF should not be used
/// </summary>
ucoNoCRLF
);
/// <summary>
/// <para>Indicates the action type to be taken by a <form> tag</para>
/// </summary>
TFormMethod = (
/// <summary>Indicates no action</summary>
fmNone,
/// <summary>Indicates a GET action</summary>
fmGet,
/// <summary>Indicates a POST action</summary>
fmPost);
{$REGION 'Documentation'}
/// <summary>
/// Enumerated type to determine the type assigned to an <input> tag
/// </summary>
{$ENDREGION}
TInputType = (
/// <summary>
/// Indicates a button input type
/// </summary>
itButton,
/// <summary>
/// Indicates a checkbox input type
/// </summary>
itCheckbox,
/// <summary>
/// Indicates a file input type
/// </summary>
itFile,
/// <summary>
/// Indicates a hidden input type
/// </summary>
itHidden,
/// <summary>
/// Indicates a imgage input type
/// </summary>
itImage,
/// <summary>
/// Indicates a password input type
/// </summary>
itPassword,
/// <summary>
/// Indicates a radio button input type
/// </summary>
ctRadio,
/// <summary>
/// Indicates a reset button input type
/// </summary>
itReset,
/// <summary>
/// Indicates a submit button input type
/// </summary>
itSubmit,
/// <summary>
/// Indicates a text input type
/// </summary>
itText,
/// <summary>Indicates a color input type</summary>
itColor,
/// <summary>Indicates a date input type</summary>
itDate,
/// <summary>Indicates a datetime-local input type</summary>
itDatetimeLocal,
/// <summary>Indicates an email input type</summary>
itEmail,
/// <summary>Indicates a month input type</summary>
itMonth,
/// <summary>Indicates a number input type</summary>
itNumber,
/// <summary>Indicates a range input type</summary>
itRange,
/// <summary>Indicates a search input type</summary>
itSearch,
/// <summary>Indicates a tel input type</summary>
itTel,
/// <summary>Indicates a time input type</summary>
itTime,
/// <summary>Indicates a url input type</summary>
itUrl,
/// <summary>Indicates a week input type</summary>
itWeek
);
{$REGION 'Documentation'}
/// <summary>
/// Enumerated type defining the type of text block to be used to tag text
/// </summary>
{$ENDREGION}
TBlockType = (
/// <summary>
/// Indicates the use of the <div> tag
/// </summary>
btDiv,
/// <summary>
/// Indicates the use of the <span> tag
/// </summary>
btSpan,
/// <summary>
/// Indicates the use of the <p> tag
/// </summary>
btParagraph
);
/// <summary>
/// Enumeration of the various types of errors that can be checked for.
/// </summary>
THTMLErrorLevel = (
/// <summary>
/// Include this member if you want any error checking at all.
/// </summary>
elErrors,
/// <summary>
/// Include this member if you want your HTML to conform to the HTML 4 specification
/// </summary>
elStrictHTML4,
/// <summary>
/// Include this member if you want your code to be HTML5 compatible.
/// </summary>
elStrictHTML5,
/// <summary>
/// Include this member if you want your code to conform to the xHTML specification.
/// </summary>
elStrictxhtml
);
{$REGION 'Documentation'}
/// <summary>
/// A set containing the items from the <see cref="THTMLErrorLevel" />
/// enumeration
/// </summary>
{$ENDREGION}
THTMLErrorLevels = set of THTMLErrorLevel;
const
/// <summary>String array for use with the TFormatType</summary>
/// <seealso cref="TFormatType">TFormatType</seealso>
TFormatTypeStrings: array [TFormatType] of string = ('b', 'i', 'u', 'em', 'strong', 'sub', 'sup', 'pre', 'cite', 'acronym', 'abbr', 'address', 'bdo', 'big', 'center', 'code', 'del', 'dfn', 'font', 'kbd', 'q', 'samp', 'small', 'strike', 'tt', 'var', 'ins', 'mark', 'bdi', 'ruby', 'rt', 'rp');
{$REGION 'Documentation'}
/// <summary>
/// Strings to be used with the THeadingType
/// </summary>
{$ENDREGION}
THeadingTypeStrings: array [THeadingType] of string = ('h1', 'h2', 'h3', 'h4', 'h5', 'h6');
{$REGION 'Documentation'}
/// <summary>
/// Strings to be used with the TClearValue type
/// </summary>
{$ENDREGION}
TClearValueStrings: array [TClearValue] of string = ('', 'none', 'left', 'right', 'all');
{$REGION 'Documentation'}
/// <summary>
/// Strings to be used for the TBulletShape type
/// </summary>
{$ENDREGION}
TBulletShapeStrings: array [TBulletShape] of string = ('', 'disc', 'circle', 'square');
{$REGION 'Documentation'}
/// <summary>
/// Strings to be used for the TNumberType type
/// </summary>
{$ENDREGION}
TNumberTypeStrings: array [TNumberType] of string = ('', '1', 'A', 'a', 'I', 'i');
{$REGION 'Documentation'}
/// <summary>
/// Strings to be used for the TBlockType type
/// </summary>
{$ENDREGION}
TBlockTypeStrings: array [TBlockType] of string = ('div', 'span', 'p');
{$REGION 'Documentation'}
/// <summary>
/// Strings to be used for the TTargetType type
/// </summary>
{$ENDREGION}
TTargetTypeStrings: array [TTargetType] of string = ('_blank', '_parent', '_self', '_target', '');
{$REGION 'Documentation'}
/// <summary>
/// Strings to be used with the TInputType type
/// </summary>
{$ENDREGION}
TInputTypeStrings: array [TInputType] of string = ('button', 'checkbox', 'file', 'hidden', 'image', 'password', 'radio', 'reset', 'submit', 'text', 'color', 'date', 'datetime-local', 'email', 'month', 'number', 'range', 'search', 'tel', 'time', 'url', 'week');
{$REGION 'Documentation'}
/// <summary>
/// Strings to be used with the TFormMethod type.
/// </summary>
{$ENDREGION}
TFormMethodStrings: array [TFormMethod] of string = ('', 'get', 'post');
{$REGION 'Documentation'}
/// <summary>
/// Strings to be used with the THTMLErrorLevel type
/// </summary>
{$ENDREGION}
THTMLErrorLevelStrings: array [THTMLErrorLevel] of string = ('', '4.x', '5.x', 'xhmtl');
cHTML = 'html';
cHead = 'head';
cBody = 'body';
cBlockQuote = 'blockquote';
cClass = 'class';
cStyle = 'style';
cID = 'id';
cMeta = 'meta';
cName = 'name';
cContent = 'content';
cform = 'form';
cInput = 'input';
cButton = 'button';
cFieldSet = 'fieldset';
cLegend = 'legend';
cFrameset = 'frameset';
cFrame = 'frame';
cNoFrames = 'noframes';
cMap = 'map';
cArea = 'area';
cAlt = 'alt';
cBaseFont = 'basefont';
cLink = 'link';
cObject = 'object';
cParam = 'param';
cAction = 'action';
cMethod = 'method';
cValue = 'value';
cCRLF = #13#10;
cAnchor = 'a';
cHREF = 'href';
cImage = 'img';
cURL = 'url';
cSource = 'src';
cHardRule = 'hr';
cBreak = 'br';
cUnorderedList = 'ul';
cOrderedList = 'ol';
cListItem = 'li';
cClear = 'clear';
cType = 'type';
cTable = 'table';
cBorder = 'border';
cCellPadding = 'cellpadding';
cCellSpacing = 'cellspacing';
cWidth = 'width';
cHeight = 'height';
cTableRow = 'tr';
cTableData = 'td';
cTableHead = 'thead';
cTableBody = 'tbody';
cTableFoot = 'tfoot';
cTableHeader = 'th';
cTitle = 'title';
cScript = 'script';
cNoScript = 'noscript';
cIFrame = 'iframe';
cBase = 'base';
cTarget = 'target';
cOption = 'option';
cOptGroup = 'optgroup';
cCaption = 'caption';
cLabel = 'label';
cTextArea = 'textarea';
cSelect = 'select';
cFor = 'for';
cCols = 'cols';
cRows = 'rows';
cColGroup = 'colgroup';
cCol = 'col';
cDD = 'dd';
cDT = 'dt';
cDL = 'dl';
// HTML5 Semantic Structure Elements
cArticle = 'article';
cAside = 'aside';
cDetails = 'details';
cFigCaption = 'figcaption';
cFigure = 'figure';
cFooter = 'footer';
cHeader = 'header';
cMain = 'main';
cNav = 'nav';
cSection = 'section';
cSummary = 'summary';
cDialog = 'dialog';
// HTML5 Text-Level Semantic Elements
cMark = 'mark';
cTime = 'time';
cWbr = 'wbr';
cBdi = 'bdi';
cRuby = 'ruby';
cRt = 'rt';
cRp = 'rp';
cOutput = 'output';
// HTML5 Media Elements
cAudio = 'audio';
cVideo = 'video';
cSourceElement = 'source';
cTrack = 'track';
cCanvas = 'canvas';
// HTML5 Form Elements
cDatalist = 'datalist';
cProgress = 'progress';
cMeter = 'meter';
// HTML5 Attribute Constants
cDatetime = 'datetime';
cMin = 'min';
cMax = 'max';
cLow = 'low';
cHigh = 'high';
cOptimum = 'optimum';
cControls = 'controls';
cAutoplay = 'autoplay';
cLoop = 'loop';
cMuted = 'muted';
cPoster = 'poster';
cPreload = 'preload';
cOpen = 'open';
cList = 'list';
cPlaceholder = 'placeholder';
cRequired = 'required';
cRole = 'role';
cData = 'data-';
cAria = 'aria-';
cDisabled = 'disabled';
cAutofocus = 'autofocus';
cHidden = 'hidden';
cReadonly = 'readonly';
cMultiple = 'multiple';
cNovalidate = 'novalidate';
cPicture = 'picture';
cTemplate = 'template';
cEmbed = 'embed';
cOpenBracket = '<';
cCloseBracket = '>';
cCloseSlashBracket = '/>';
cComment = '!--';
cCloseComment = '-->';
cSpace = ' ';
cEmptyString = '';
{$REGION 'Documentation'}
/// <summary>
/// An array of strings corresponding to the <see cref="THTMLDocType" />
/// enumeration
/// </summary>
{$ENDREGION}
THTMLDocTypeStrings: array [THTMLDocType] of string = ('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">', '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">', '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">', '<!DOCTYPE html>');
type
/// <summary>
/// Type that determines if spaces should count as content or as empty space
/// </summary>
/// <remarks>
/// Used with TStringIsEmpty class functions
/// </remarks>
TCountSpaces = (
/// <summary>
/// Spaces should count as empty space. Use this member when you want a string to be considered empty if it contains spaces. For instance, a string that consists of nothing but three spaces will be considered empty when this member is passed.
/// </summary>
csSpacesCountAsEmpty,
/// <summary>
/// Spaces should count as content. Use this member when you want spaces to indicate that a string is not empty. For instance, a string with nothing but three spaces in it would be considered not empty when this member is chosen.
/// </summary>
csSpacesCountAsContent
);
type
/// <summary>Simple static class to add functionality to string.</summary>
TStringDecorator = class
/// <summary>
/// Determines if a string has content in it or not.
/// </summary>
/// <param name="aString">
/// The string to be examined for emptiness.
/// </param>
/// <param name="aSpacesAreContent">
/// An optional parameter that determines if empty spaces should be included.
/// </param>
class function StringIsEmpty(aString: string; aSpacesAreContent: TCountSpaces = csSpacesCountAsEmpty): Boolean;
class function StringIsNotEmpty(aString: string; aSpacesAreContent: TCountSpaces = csSpacesCountAsEmpty): Boolean;
end;
type
/// <summary>A class for producing edge tags, including opening and closing tags.</summary>
TTagMaker = class
class function MakeOpenTag(aTag: string): string; static;
class function MakeCloseTag(aTag: string): string; static;
class function MakeSlashCloseTag: string; static;
class function MakeCommentCloseTag: string; static;
end;
implementation
class function TStringDecorator.StringIsEmpty(aString: string; aSpacesAreContent: TCountSpaces = csSpacesCountAsEmpty): Boolean;
begin
Result := aString = EmptyStr;
if (not Result) and (aSpacesAreContent = csSpacesCountAsContent) then
begin
Result := Trim(aString) = EmptyStr;
end;
end;
class function TStringDecorator.StringIsNotEmpty(aString: string; aSpacesAreContent: TCountSpaces = csSpacesCountAsEmpty): Boolean;
begin
Result := not StringIsEmpty(aString, aSpacesAreContent);
end;
class function TTagMaker.MakeCommentCloseTag: string;
begin
Result := cSpace + cCloseComment;
end;
class function TTagMaker.MakeOpenTag(aTag: string): string;
begin
Result := Format('<%s>', [aTag]);
end;
class function TTagMaker.MakeSlashCloseTag: string;
begin
Result := cSpace + cCloseSlashBracket;
end;
class function TTagMaker.MakeCloseTag(aTag: string): string;
begin
Result := Format('</%s>', [aTag]);
end;
{ THTMLWidth }
function THTMLWidth.AsPercentage: string;
begin
Result := '';
if IsPercentage = ipIsPercentage then
begin
Result := Format('%d%%', [Width]);
end;
end;
constructor THTMLWidth.Create(aWidth: integer; aIsPercentage: TIsPercentage);
begin
Width := aWidth;
IsPercentage := aIsPercentage;
end;
function THTMLWidth.WidthAsString: string;
begin
Result := IntToStr(Width);
end;
function THTMLWidth.WidthString: string;
begin
Result := Format('width="%s"', [IntToStr(Width)]);
end;
end.