/
ReadHTML.pas
4989 lines (4536 loc) · 136 KB
/
ReadHTML.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
{
Version 11.6
Copyright (c) 1995-2008 by L. David Baldwin,
Copyright (c) 2008-2015 by HtmlViewer Team
*********************************************************
* *
* Thanks to Mike Lischke for his *
* assistance with the Unicode conversion *
* *
*********************************************************
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Note that the source modules HTMLGIF1.PAS and DITHERUNIT.PAS
are covered by separate copyright notices located in those modules.
}
{$I htmlcons.inc}
{
This module contains the parser which reads thru the document.
It divides it into sections storing the pertinent information in Section objects.
The document itself is then a TList of section objects.
See the HTMLSubs unit for the definition of the section objects.
Key Variables:
Sy:
An enumerated type which indicates what the current token is. For
example, a value of StringSy would indicate a hunk of text, PSy that a <P>
tag was encountered, etc.
LCh:
The next character in the stream to be analyzed. In mixed case.
LCToken:
A ThtString which is associated with the current token. If Sy is StringSy,
then LCToken contains the text.
Attributes:
A list of TAttribute's for tokens such as <img>, <a>, which have
attributes.
Section:
The current section being built.
SectionList:
The list of sections which form the document. When in a Table,
SectionList will contain the list that makes up the current cell.
Key Routines:
GetCh:
Gets the next character from the stream. Fills LCh. Skips comments.
Next:
Gets the next token. Fills Sy, LCToken, Attributes. Calls GetCh so the
next character after the present token is available. Each part of the
parser is responsible for calling Next after it does its thing.
ANGUS March 2012 - fixed THtmlParser.DoMeta to handle meta without http-equiv="Content-Type"
<meta charset="utf-8"> (used by Goggle)
}
{-$define DO_LI_INLINE}
{$ifdef DO_LI_INLINE}
{$else}
{$endif}
unit ReadHTML;
interface
uses
{$ifdef LCL}
LclIntf, LclType, HtmlMisc,
{$else}
Windows,
{$endif}
SysUtils, Math, Variants, Classes, Graphics, Controls, Contnrs,
HtmlGlobals,
HtmlBuffer,
HtmlSymb,
HTMLUn2,
HTMLSubs,
Htmlsbs1,
StyleTypes,
StyleUn;
type
TLoadStyleType = (lsFile, lsString, lsInclude);
{ THtmlParser }
THtmlParser = class
private
TitleStart: Integer;
TitleEnd: Integer;
FBase: ThtString;
FBaseTarget: ThtString;
LCh: ThtChar;
LastChar: (lcOther, lcCR, lcLF);
LCToken: TTokenObj;
Doc: TBuffer;
DocStack: TStack;
CharCount: Integer;
Sy: TElemSymb;
IsXhtmlEndSy: Boolean; // current symbol is an xhtml combined start/end tag like <tag [attr="value" ...] />
Attributes: TAttributeList;
BaseFontSize: Integer;
BodyBlock: TBodyBlock;
Section: TSection;
SectionList: TCellBasic;
CurrentURLTarget: TURLTarget;
TableLevel: Integer;
TagIndex: Integer;
InComment: Boolean;
InHref: Boolean;
InScript: Boolean; {when in a <SCRIPT>}
LinkSearch: Boolean;
ListLevel: Integer;
IncludeEvent: TIncludeType;
CallingObject: TViewerBase;
SoundEvent: TSoundType;
MetaEvent: TMetaType;
LinkEvent: TLinkType;
FUseQuirksMode : Boolean;
FPropStack: THtmlPropStack;
FNoBreak : Boolean;
FIsXHTML : Boolean;
procedure SetNoBreak(const AValue : Boolean);
procedure GetCh;
function DoCharSet(const Content: ThtString): Boolean;
function FindAlignment: ThtString;
function GetEntityStr(CodePage: Integer): ThtString;
function GetIdentifier(out Identifier: ThtString): Boolean;
// function GetValue(var S: ThtString; var Value: Integer): Boolean;
procedure CheckForAlign;
procedure DoAEnd;
procedure DoBase;
procedure DoBody(const TermSet: TElemSymbSet);
procedure DoBr(const TermSet: TElemSymbSet);
procedure DoCommonSy;
procedure DoDivEtc(Sym: TElemSymb; const TermSet: TElemSymbSet);
procedure DoFrameSet(FrameViewer: TFrameViewerBase; FrameSet: TObject; const FName: ThtString);
procedure DoListItem(
{$ifdef DO_LI_INLINE}var LiBlock: TBlockLi; var LiSection: TSection;{$endif}
BlockType, Sym: TElemSymb; LineCount: Integer; Index: ThtChar; Plain: Boolean; const TermSet: TElemSymbSet);
procedure DoLists(Sym: TElemSymb; const TermSet: TElemSymbSet);
procedure DoMap;
procedure DoMeta(Sender: TObject);
procedure DoObjectTag(var C: ThtChar; var N, IX: Integer);
procedure DoP(const TermSet: TElemSymbSet);
procedure DoScript(Ascript: TScriptEvent);
procedure DoSound;
procedure DoStyleLink;
procedure DoTable;
procedure DoText;
procedure DoTextArea(TxtArea: TTextAreaFormControlObj);
procedure DoTitle;
procedure GetOptions(Select: TOptionsFormControlObj);
procedure Next;
procedure ParseInit(ASectionList: ThtDocument; AIncludeEvent: TIncludeType);
procedure SkipWhiteSpace; {$ifdef UseInline} inline; {$endif}
procedure PushNewProp(Sy: TElemSymb; const AClass, AnID, APseudo, ATitle: ThtString; AProp: TProperties); {$ifdef UseInline} inline; {$endif}
procedure PopAProp(Sy: TElemSymb); {$ifdef UseInline} inline; {$endif}
function Peek: ThtChar;
function GetTitle: ThtString;
function PropStackIndex: Integer;
procedure PopProp;
function ExtractCharsetFromXMLProlog : Boolean;
procedure CollectPreText(S: TTokenObj);
procedure CollectNormalText(S: TTokenObj);
procedure CollectText(S: TTokenObj; AWhiteSpace : ThtWhiteSpaceStyle);
public
constructor Create(Doc: TBuffer);
destructor Destroy; override;
function ShouldUseQuirksMode: Boolean;
function IsFrame(FrameViewer: TFrameViewerBase): Boolean;
procedure ParseFrame(FrameViewer: TFrameViewerBase; FrameSet: TObject; const FName: ThtString; AMetaEvent: TMetaType);
procedure ParseHtml(ASectionList: ThtDocument; AIncludeEvent: TIncludeType; ASoundEvent: TSoundType; AMetaEvent: TMetaType; ALinkEvent: TLinkType);
procedure ParseText(ASectionList: ThtDocument);
property Base: ThtString read FBase;
property BaseTarget: ThtString read FBaseTarget;
property Title: ThtString read GetTitle;
property UseQuirksMode : Boolean read FUseQuirksMode;
property PropStack: THtmlPropStack read FPropStack;
property NoBreak : Boolean read FNoBreak write SetNoBreak;
property IsXHTML : Boolean read FIsXHTML write FIsXHTML;
end;
function IsWhiteSpace(Ch: ThtChar): Boolean; {$ifdef UseInline} inline; {$endif}
function ReplaceEntities(const Str: ThtString; CodePage: Integer): ThtString;
implementation
uses
{$ifdef Compiler24_Plus}
System.Types,
{$endif}
HtmlView, FramView, StylePars, URLSubs;
const
TableTermSet = [
TableEndSy,
TBodySy, TBodyEndSy,
TFootSy, TFootEndSy,
THeadSy, THeadEndSy,
TDSy, TDEndSy,
THSy, THEndSy,
TRSy, TREndSy,
CaptionSy, CaptionEndSy,
ColgroupSy, ColSy];
{ THtmlParser }
constructor THtmlParser.Create(Doc: TBuffer);
begin
inherited Create;
LCToken := TTokenObj.Create;
DocStack := TStack.Create;
Self.Doc := Doc;
FIsXHTML := False;
end;
destructor THtmlParser.Destroy;
begin
DocStack.Free;
LCToken.Free;
inherited;
end;
function THtmlParser.PropStackIndex: Integer;
begin
Result := FPropStack.Count - 1;
end;
//-- BG ---------------------------------------------------------- 26.12.2010 --
function THtmlParser.Peek: ThtChar; {take a look at the next ThtChar}
begin
Result := Doc.PeekChar;
while (Result = EofChar) and DocStack.AtLeast(1) do
begin
Doc.Free;
Doc := DocStack.Pop;
Result := Doc.PeekChar;
end;
end;
{-------------GetCh}
procedure THtmlParser.GetCh;
{Return next ThtChar in LCh. Ignore comments}
procedure GetChBasic; {read a character}
function ReadChar: ThtChar;
begin
repeat
if DocStack.Count = 0 then
// update document position only for outmost document
PropStack.SIndex := Doc.Position;
Result := Doc.NextChar;
if (Result = EofChar) and DocStack.AtLeast(1) then
begin
Doc.Free;
Doc := DocStack.Pop;
end
else
break;
until false;
if not LinkSearch and (PropStack.Document <> nil) then
begin
Inc(CharCount);
if (Result = EofChar) or (CharCount and $FFF = 0) {about every 4000 chars} then
if Doc.Size > 0 then
THtmlViewerBase(CallingObject).htProgress((Doc.Position * PropStack.Document.ProgressStart) div Doc.Size);
end;
end;
begin
LCh := ReadChar;
case LCh of {skip a LfChar after a CrChar or a CrChar after a LfChar}
CrChar: if LastChar = lcLF then
LCh := ReadChar;
LfChar: if LastChar = lcCR then
LCh := ReadChar;
end;
case LCh of
TabChar:
LCh := SpcChar;
LfChar:
begin
LastChar := lcLF;
LCh := CrChar;
end;
CrChar:
LastChar := lcCR;
else
begin
LastChar := lcOther;
if LCh = TabChar then
LCh := SpcChar;
end;
end;
if (LCh = EofChar) and InComment then
raise EParseError.Create('Open Comment at End of HTML File');
end;
var
Done, Comment: Boolean;
procedure DoDashDash; {do the comment after a <!-- }
begin
repeat
while LCh <> '-' do
GetChBasic; {get first '-'}
GetChBasic;
if LCh = '-' then {second '-'}
begin
while LCh = '-' do
GetChBasic; {any number of '-'}
while (LCh = SpcChar) or (LCh = CrChar) do
GetChBasic; {eat white space}
if LCh = '!' then
GetChBasic; {accept --!> also}
Done := LCh = GreaterChar;
end
else
Done := False;
until Done;
InComment := False;
end;
procedure ReadToGT; {read to the next GreaterChar }
begin
while (LCh <> GreaterChar) and (LCh <> EofChar) do
GetChBasic;
InComment := False;
end;
procedure DoInclude;
function GetNameValueParameter(out Name, Value: ThtString): Boolean;
function GetQuotedValue(var S: ThtString): Boolean;
{get a quoted ThtString but strip the quotes}
var
Term: ThtChar;
SaveSy: TElemSymb;
begin
Result := False;
Term := LCh;
if (Term <> ThtChar('"')) and (Term <> ThtChar('''')) then
Exit;
Result := True;
SaveSy := Sy;
GetCh;
while (LCh <> Term) and (LCh <> EofChar) do
begin
if LCh = AmperChar then
htAppendStr(S, GetEntityStr(FPropStack.Last.CodePage))
else
begin
if LCh = CrChar then
htAppendChr(S, SpcChar)
else
htAppendChr(S, LCh);
GetCh;
end;
end;
if LCh = Term then
GetCh; {pass termination ThtChar}
Sy := SaveSy;
end;
begin
Result := False;
SkipWhiteSpace;
if GetIdentifier(Name) then
begin
SkipWhiteSpace;
Value := '';
Result := True; {at least have an ID}
if LCh <> '=' then
Exit;
GetCh;
SkipWhiteSpace;
if not GetQuotedValue(Value) then
{in case quotes left off ThtString}
while True do
case LCh of
SpcChar, TabChar, CrChar, MinusChar, GreaterChar, EofChar: {need to exclude '-' to find '-->'}
break;
else
htAppendChr(Value, LCh);
GetCh;
end;
end;
end;
var
S, Name, Value: ThtString;
Include: TBuffer;
Params: ThtStringList;
SaveLCToken: TTokenObj;
L: Integer;
begin
S := '';
SaveLCToken := LCToken;
LCToken := TTokenObj.Create;
try
GetChBasic;
GetIdentifier(S);
if LCh = '>' then
begin
L := Length(S);
if (L > 2) and (S[L-1] = '-') and (S[L] = '-') then
begin
Doc.Position := Doc.Position - 3;
GetChBasic;
SetLength(S, L - 2);
end;
end;
// BG, 15.12.2011: Issue 88: DoInclude and FreeAndNil of SL
// Now freeing SL (renamed to Params) here and not
// relying on cooperative event doing it for us.
Params := ThtStringList.Create;
try
while GetNameValueParameter(Name, Value) do
Params.Add(Name + '=' + Value);
DoDashDash;
Include := nil;
IncludeEvent(CallingObject, S, Params, Include);
finally
Params.Free;
end;
if Include <> nil then
begin
DocStack.Push(Doc);
Doc := Include;
end;
finally
LCToken.Free;
LCToken := SaveLCToken;
end;
end;
begin {Getch}
repeat {in case a comment immediately follows another comment}
{comments may be either '<! stuff >' or '<!-- stuff -->' }
Comment := False;
GetChBasic;
if (LCh = LessChar) and not InScript then
begin
case Peek of
'!':
begin
GetChBasic;
Comment := True;
InComment := True;
GetChBasic;
if LCh = '-' then
begin
GetChBasic;
if LCh = '-' then
begin
GetChBasic;
if Assigned(IncludeEvent) and (LCh = '#') then
DoInclude
else
DoDashDash; {a <!-- comment}
end
else
ReadToGT;
end
else
ReadToGT;
end;
'%': { <%....%> regarded as comment }
begin
GetChBasic;
Comment := True;
repeat
GetChBasic;
until (LCh = '%') and (Peek = GreaterChar) or (LCh = EOFChar);
GetChBasic;
end;
end;
end;
until not Comment;
end;
//-- BG ---------------------------------------------------------- 27.03.2011 --
function THtmlParser.GetIdentifier(out Identifier: ThtString): Boolean;
begin
// An identifier can contain only the characters a..z, A..Z, 0..9, -, and _
// and start with a..z, A..Z or _] or underscore;
SetLength(Identifier, 0);
case LCh of
'A'..'Z', 'a'..'z', '_':
Result := True;
else
Result := False;
end;
// loop through all allowed characters:
while Result do
begin
case LCh of
'A'..'Z', 'a'..'z', '0'..'9', '_', '-': ;
else
break;
end;
htAppendChr(Identifier, LCh);
GetCh;
end;
if Result then
Result := Length(Identifier) > 0;
end;
function IsWhiteSpace(Ch: ThtChar): Boolean;
{$ifdef UseInline} inline; {$endif}
begin
case Ch of
SpcChar,
TabChar,
CrChar,
LfChar,
FfChar:
Result := True;
else
Result := False;
end;
end;
function THtmlParser.ExtractCharsetFromXMLProlog : Boolean;
var LId : ThtString;
procedure GetChBasic;
begin
LCh := Doc.NextChar;
end;
procedure ReadToGT; {read to the next GreaterChar }
begin
while (LCh <> GreaterChar) and (LCh <> EofChar) do
GetChBasic;
InComment := False;
end;
procedure ReadToLT; {read to the next LessChar }
begin
if not InComment then begin
while (LCh <> LessChar) and (LCh <> EofChar) do
GetChBasic;
end;
end;
procedure ScanRestOfUnquotedString(out Identifier : ThtString);
begin
SetLength(Identifier, 0);
if LCh = '"' then begin
GetChBasic;
end;
repeat
if (LCh = EofChar) then begin
break;
end;
if (LCh = '"') then begin
break;
end else begin
htAppendChr(Identifier, LCh);
end;
GetChBasic;
until False;
end;
procedure ParseXMLProlog;
var LCharset : THtString;
CP: TBuffCodePage;
begin
repeat
GetChBasic;
GetIdentifier(LId);
if LId = 'encoding' then begin
if LCh='=' then begin
GetChBasic;
if LCh='"' then begin
ScanRestOfUnquotedString(LCharset);
//Do not use DoCharSet(LCharset) here because the stack might not be set up
CP := StrToCodePage(LCharset);
Result := CP <> CP_UNKNOWN;
if Result then
begin
Doc.CodePage := CP;
end;
exit;
end;
end;
end else begin
if LCh='=' then begin
GetChBasic;
if LCh='"' then begin
ScanRestOfUnquotedString(LCharset);
end;
end;
end;
if LCh='?' then begin
GetChBasic;
end;
if (LCh = GreaterChar) or (LCh = EofChar) then begin
exit;
end;
until False;
end;
var
OldPos: Integer;
begin
FPropStack := THTMLPropStack.Create;
try
OldPos := Doc.Position;
Result := True;
repeat
//loop where parsing code goes
ReadToLT;
GetChBasic;
case LCh of
'!':
begin
GetChBasic;
GetIdentifier(LId);
if htUpperCase(LId) <> 'DOCTYPE' then
begin
InComment := True;
ReadToGT;
end
else
begin
Result := False;
break;
end;
end;
'?':
begin
GetChBasic;
GetIdentifier(LId);
if LId = 'xml' then begin
//scan for charset
ParseXMLProlog;
break;
end;
end;
EofChar:
break;
end;
GetIdentifier(LId);
SkipWhiteSpace;
LId := htUpperCase(LId);
if (LId = 'HTML') or (LId = 'HEAD') or (LId = 'BODY') then begin
break;
end;
until False;
Doc.Position := OldPos;
finally
FreeAndNil(FPropStack);
end;
end;
function THtmlParser.ShouldUseQuirksMode: Boolean;
{
This is not in ParseHTML because quirks mode effects
the CSS property initialization which is done earlier than
the parsing is and because it can dictate how HTML is parsed.
In addition, you may want to skip this detection if:
1) The document was served as "application/xhtml+xml" or is known to be
XHTML. In those cases, the document should ALWAYS be displayed in a "standards"
mode. In ideal situations, XHTML should be parsed in a stricter manner than regular
HTML and use XML rules.
2) You want to force THTMLViewer to display the document in "quirks mode"
3) You want to force THTMLViewer to display the document in a "standards"
non-quirks mode
Scan for the following DOCTYPE declarations:
<!DOCTYPE html>
<!DOCTYPE html SYSTEM "about:legacy-compat">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"
"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.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 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 HTML 4.0//EN">
<!DOCTYPE HTML SYSTEM "http://www.w3.org/TR/REC-html40/strict.dtd">
}
var LId : ThtString;
procedure GetChBasic;
begin
LCh := Doc.NextChar;
end;
procedure ReadToGT; {read to the next GreaterChar }
begin
while (LCh <> GreaterChar) and (LCh <> EofChar) do
GetChBasic;
InComment := False;
end;
procedure ReadToLT; {read to the next LessChar }
begin
if not InComment then begin
while (LCh <> LessChar) and (LCh <> EofChar) do
GetChBasic;
end;
end;
procedure ScanRestOfUnquotedString(out Identifier : ThtString);
begin
SetLength(Identifier, 0);
if LCh = '"' then begin
GetChBasic;
end;
repeat
if (LCh = EofChar) then begin
break;
end;
if (LCh = '"') then begin
break;
end else begin
htAppendChr(Identifier, LCh);
end;
GetChBasic;
until False;
end;
procedure ScanDTDIdentifier(out Identifier : ThtString);
begin
SetLength(Identifier, 0);
case LCh of
'A'..'Z', 'a'..'z', '0'..'9', '_', '/', '-','.':
Result := True;
else
Result := False;
end;
// loop through all allowed characters:
while Result do
begin
case LCh of
'A'..'Z', 'a'..'z', '0'..'9', '_', '/', '-','.': ;
else
break;
end;
htAppendChr(Identifier, LCh);
GetChBasic;
end;
if Result then
Result := Length(Identifier) > 0;
end;
function ScanDTD : Boolean;
var
LPart : ThtString;
begin
Result := False;
SkipWhiteSpace;
ScanDTDIdentifier(LPart);
if htUpperCase(LPart) = 'HTML' then
begin
GetChBasic;
if LCh = GreaterChar then
begin
//HTML5 - don't use quirks mode
Result := True;
exit;
end;
ScanDTDIdentifier(LPart);
if (htUpperCase(LPart) <> 'PUBLIC') and (htUpperCase(LPart) <> 'SYSTEM') then
exit;
SkipWhiteSpace;
if LCh = '"' then
GetChBasic;
SkipWhiteSpace;
ScanDTDIdentifier(LPart);
if (htUpperCase(LPart) = 'ABOUT') then begin
//HTML5 - don't use quirks mode
Result := True;
exit;
end;
if (htUpperCase(LPart) = 'HTTP') then begin
//probably a URL pointing to a DTD
ScanRestOfUnquotedString(LPart);
Result := TextEndsWith('html40/strict.dtd',LPart) or
TextEndsWith('xhtml1-strict.dtd',LPart) or
TextEndsWith('xhtml-basic11.dtd',LPart) or
TextEndsWith('xhtml1-transitional.dtd',LPart) or
TextEndsWith('xhtml1-frameset.dtd',LPart) or
TextEndsWith('xhtml11.dtd',LPart);
if Result then
exit;
end;
if (htUpperCase(LPart) <> '-//W3C//DTD') then
exit;
SkipWhiteSpace;
ScanDTDIdentifier(LPart);
LPart := htUpperCase(LPart);
if LPart = 'HTML' then
begin
SkipWhiteSpace;
ScanDTDIdentifier(LPart);
Result := TextStartsWith('4.01',LPart);
if Result then begin
exit;
end;
Result := TextStartsWith('4.0/',LPart);
if Result then begin
exit;
end;
Result := TextStartsWith('4.',LPart);
SkipWhiteSpace;
end;
if LPart = 'XHTML' then
begin
SkipWhiteSpace;
ScanDTDIdentifier(LPart);
if htUpperCase(LPart) = 'BASIC' then
begin
SkipWhiteSpace;
ScanDTDIdentifier(LPart);
if TextStartsWith('1.',LPart) then
Result := True;
end
else
begin
Result := TextStartsWith('1.',LPart);
end;
end;
end;
end;
var
OldPos: Integer;
begin
FPropStack := THTMLPropStack.Create;
try
OldPos := Doc.Position;
Result := True;
repeat
ReadToLT;
GetChBasic;
case LCh of
'!':
begin
GetChBasic;
GetIdentifier(LId);
if htUpperCase(LId) <> 'DOCTYPE' then
begin
InComment := True;
ReadToGT;
end
else
begin
if ScanDTD then
begin
Result := False;
break;
end;
end;
end;
EofChar:
break;
end;
GetIdentifier(LId);
SkipWhiteSpace;
LId := htUpperCase(LId);
if (LId = 'HTML') or (LId = 'HEAD') or (LId = 'BODY') then
break;
until False;
Doc.Position := OldPos;
finally
FreeAndNil(FPropStack);
end;
end;
{-------------SkipWhiteSpace}
procedure THtmlParser.SkipWhiteSpace;
begin
while IsWhiteSpace(LCh) do
GetCh;
end;
{----------------GetValue}
//-- BG ---------------------------------------------------------- 31.01.2011 --
function THtmlParser.GetTitle: ThtString;
begin
if TitleEnd > TitleStart then
Result := ReplaceEntities(Doc.GetString(TitleStart, TitleEnd), Doc.CodePage)
else
Result := '';
end;
{-----------Next}
procedure THtmlParser.Next;
{Get the next token}
procedure GetTag;
{Pick up a Tag or pass a single LessChar}
function GetAttribute(out Sym: TAttrSymb; out St: ThtString; out S: ThtString; out Val: Integer): Boolean;
function GetID(out S: ThtString): Boolean;
begin
S := '';
while True do
case LCh of
'a'..'z', 'A'..'Z', '-', '$', '0'..'9', ':':
begin
htAppendChr(S, LCh);
GetCh;
end;
else
break;
end;
Result := Length(S) > 0;
if Result then
S := htUpperCase(S);
end;
function GetQuotedStr(var S: ThtString; WantCrLf: Boolean; Sym: TAttrSymb): Boolean;
{get a quoted ThtString but strip the quotes, check to see if it is numerical}
var
Term: ThtChar;
SaveSy: TElemSymb;
begin
Result := (LCh = '"') or (LCh = '''');
if not Result then
Exit;
Term := LCh;
SaveSy := Sy;
GetCh;
while (LCh <> Term) and (LCh <> EofChar) do
begin
case LCh of
CrChar:
begin
if WantCrLf then