-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgf_HTMLParser.pas
711 lines (625 loc) · 20.5 KB
/
gf_HTMLParser.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
(* ************************************************************
MOZILLA PUBLIC LICENSE STATEMENT
-----------------------------------------------------------
The contents of this file are subject to the Mozilla Public
License Version 1.1 (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.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS
IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
implied. See the License for the specific language governing
rights and limitations under the License.
The Original Code is "gf_HTMLParser.pas".
The Initial Developer of the Original Code is Marek Jedlinski
<eristic@lodz.pdi.net> (Poland).
Portions created by Marek Jedlinski are
Copyright (C) 2000, 2001. All Rights Reserved.
-----------------------------------------------------------
Contributor(s):
-----------------------------------------------------------
History:
-----------------------------------------------------------
To do:
-----------------------------------------------------------
Released: 20 August 2001
-----------------------------------------------------------
URLs:
- original author's software site:
http://www.lodz.pdi.net/~eristic/free/index.html
http://go.to/generalfrenetics
Email addresses (at least one should be valid)
<eristic@lodz.pdi.net>
<cicho@polbox.com>
<cicho@tenbit.pl>
************************************************************ *)
unit gf_HTMLParser;
{$I gf_base.inc}
interface
uses Windows, SysUtils, Classes,
gf_HTMLTokens, gf_HTMLtags, gf_HTMLBase;
type
TCaseChange = (
caseAsIs, caseUpper, caseLower
);
const
CASECHANGE_NAMES : array[TCaseChange] of string = (
'As is', 'Uppercase', 'Lowercase'
);
type
TSyntaxWarningEvent = procedure(
sender : TObject; const s : string; const i : integer; var Abort : boolean
) of object;
type
TTokenEvent = procedure(
sender : TObject; Token : TToken; var Add : boolean
) of object;
type
TGFHTMLParser = class( TComponent )
private
FIsParsing : boolean;
FCaseChange : TCaseChange;
FTokenList : TTokenList;
FProgressIncrement : integer;
FExpandEntities : boolean;
FLineBreak : string;
FOnSyntaxWarning : TSyntaxWarningEvent;
FOnProgress : TNotifyEvent;
FOnToken : TTokenEvent;
function Peek( const S : TStream; const Count : byte ) : string;
function SyntaxWarning( const s : string; const i : integer ) : boolean; // abort if returns TRUE
public
property IsParsing : boolean read FIsParsing;
property TokenList : TTokenList read FTokenList;
constructor Create( AOwner : TComponent ); override;
destructor Destroy; override;
function Parse( Stream : TStream ) : integer;
function ParseFile( const FN : string ) : integer;
procedure SaveTokens( const FN : string );
procedure RebuildFile( const FN : string );
published
property CaseChange : TCaseChange read FCaseChange write FCaseChange default caseAsIs; // [x] not implemented
property ProgressIncrement : integer read FProgressIncrement write FProgressIncrement default 1024;
property ExpandEntities : boolean read FExpandEntities write FExpandEntities default false; // [x] not implemented
property LineBreak : string read FLineBreak write FLineBreak;
property OnSyntaxWarning : TSyntaxWarningEvent read FOnSyntaxWarning write FOnSyntaxWarning;
property OnProgress : TNotifyEvent read FOnProgress write FOnProgress;
property OnToken : TTokenEvent read FOnToken write FOnToken;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents( 'Custom', [TGFHTMLParser] );
end; // Register
{ TGFHTMLParser }
constructor TGFHTMLParser.Create(AOwner: TComponent);
begin
inherited Create( AOwner );
FIsParsing := false;
FCaseChange := caseAsIs;
FProgressIncrement := 1024;
FExpandEntities := false;
FOnSyntaxWarning := nil;
FOnProgress := nil;
FOnToken := nil;
FLineBreak := #13#10;
FTokenList := TTokenList.Create;
end; // CREATE
destructor TGFHTMLParser.Destroy;
begin
FTokenList.Free;
inherited Destroy;
end; // DESTROY
function TGFHTMLParser.Parse(Stream: TStream): integer;
var
InTag, InQuotes, InStyle, InScript, InComment : boolean;
ch, prevch, prevquote : char;
text, tmpstr : string;
Last3 : string[3];
i, len : integer;
LineNum, SizeRead, SizeTotal : longint;
TokenIsEOL : boolean;
{ inline procedure }
procedure FlushToken;
var
Token : TToken;
len : integer;
OKtoAdd : boolean;
begin
if ( text = '' ) then exit;
Token := TToken.Create;
Token.LineNumber := LineNum;
if InScript then
begin
Token.TokenClass := tcScript;
end
else
if InStyle then
begin
Token.TokenClass := tcStyle;
end
else
if InComment then
begin
Token.TokenClass := tcComment;
end
else
if InTag then
begin
// this may be a regular HTML tag, but it might also be
// a DTD or ASP construct. We should NOT arrive here if
// text represents part of a comment
len := length( text );
if ( len > 2 ) then
begin
case text[2] of
'!', '?' : begin
Token.TokenClass := tcDTD;
end;
'%' : begin
Token.TokenClass := tcASP;
end;
else
begin
Token.TokenClass := tcTag;
end;
end;
end
else
begin
// has length of 1 or 2 characters
// so this cannot be a valid <TAG>
Token.TokenClass := tcUndef; // flag it as a problem
SyntaxWarning( 'Illegal construct: incomplete tag', LineNum );
end;
end
else
begin
if TokenIsEOL then
begin
Token.TokenClass := tcEOL;
end
else
begin
Token.TokenClass := tcText;
end;
end;
Token.Text := Text;
text := '';
if ( Token.TokenClass = tcTag ) then
begin
case token.tag.Element of
elemScript : InScript := token.tag.IsStartTag;
elemStyle : InStyle := token.tag.IsStartTag;
end;
end;
// this allows user to filter or replace tokens
// as they are added. It also allows the component
// to be used for parsing without having to store
// the entire data in memory (a parsed token can be
// processed in FOnToken event and then discarded)
OKtoAdd := true;
if assigned( FOnToken ) then
FOnToken( self, Token, OKtoAdd );
if OKtoAdd then
FTokenList.Add( Token )
else
Token.Free;
end;// FlushToken
begin
result := 0;
SizeRead := 0;
SizeTotal := Stream.Size;
LineNum := 1;
InTag := false;
InQuotes := false;
InStyle := false;
InScript := false;
InComment := false;
FTokenList.Clear;
TokenIsEOL := false;
prevch := #0;
prevquote := #0;
Text := '';
Last3 := ' ';
FIsParsing := false;
try
repeat
if ( Stream.Read( ch, 1 ) = 0 ) then break;
inc( SizeRead );
if ( assigned( FOnProgress ) and (( SizeRead MOD FProgressIncrement ) = 0 )) then
begin
FOnProgress( self );
end;
case ch of
'<' : begin
If ( InQuotes or InComment ) then
begin
text := text + ch;
end
else
begin
// not in quotes, not in comment.
// could be the start of a new TAG, but it could also be
// some random stuff inside <SCRIPT>, for example
if ( InStyle or InScript ) then
begin
text := text + ch;
// if this is a tag CLOSING the SCRIPT or STYLE block,
// we will resolve it later
end
else
begin
if InTag then
begin
// this should NOT happen!
if SyntaxWarning( 'Illegal construct: Unquoted opening bracket inside a tag', LineNum ) then break;
// this will often happen when a tag is not closed.
// we recover by closing it.
text := text + '>';
FlushToken;
text := '<';
end
else
begin
FlushToken;
InTag := true;
text := '<';
end;
end;
end;
end; // opening bracker <
'>' : begin
If InQuotes then //
begin
text := text + ch;
end
else
begin
if InComment then
begin
// check if comment is being closed
if ( copy( Last3, 2, 2 ) = '--' ) then
begin
// yes, comment is closing
// do not append the > character. Instead, strip the comment marker.
(*
// Comments are stored without the <!-- --> delimiters,
// unless they occur inside STYLE or SCRIPT blocks
if ( not ( InStyle or InScript )) then
delete( text, ( length( text ) - 1 ), 2 )
else
*)
text := text + ch;
FlushToken;
InComment := false;
end
else
begin
// not closing comment, just add it.
text := text + ch;
end;
end
else
begin
// not in comment. A tag can be closing, but we could also be
// inside a STYLE or SCRIPT block
if InTag then // InTag is NOT set while within STYLE and SCRIPT blocks
begin
text := text + ch;
FlushToken;
InTag := false;
end
else
begin
// not inside a tag
if ( InScript or InStyle ) then
begin
// check if it is a </STYLE> or </SCRIPT> tag
// but note that the trailing > may be preceded by white space
len := length( text );
tmpstr := '';
i := len;
// skip any trailing whitespace
while (( i > 0 ) and ( text[i] in _WhiteSpace )) do
dec( i );
while ( i > 0 ) do
begin
tmpstr := text[i] + tmpstr;
if ( text[i] = '<' ) then break;
dec( i );
end;
if ( i = 0 ) then
begin
// we looked back, but did not encounter a starting <,
// so this cannot be a closing tag
text := text + ch;
end
else
begin
tmpstr := uppercase( tmpstr ) + '>';
if (( InScript and ( tmpstr = '</' + ELEMENT_NAMES[elemSCRIPT] + '>' )) or
( InStyle and ( tmpstr = '</' + ELEMENT_NAMES[elemSTYLE] + '>' ))) then
begin
// found closing </SCRIPT>
// first, flush anything that precedes the closing tag
delete( text, i, len );
FlushToken;
// then, process the closing tag
text := tmpstr;
InTag := true;
if InScript then
InScript := false
else
InStyle := false;
FlushToken;
InTag := false;
end
else
begin
// it was some sort of a <TAG>, but not a closing </STYLE> or </SCRIPT>
// so just continue
text := text + ch;
end;
end;
end
else
begin
// orphaned '>' - nowhere to place it!
if SyntaxWarning( 'Illegal construct: orphaned closing bracket', LineNum ) then break;
text := text + ch;
end;
end;
end;
end;
end; // closing bracket >
'-' : begin
if InQuotes then
begin
text := text + ch;
end
else
begin
// check if we are starting a comment, rather than a tag
if ( InTag or InScript or InStyle ) then
begin
if ( Last3 = '<!-' ) then
begin
// YES, a comment is starting
InTag := false;
InComment := true;
(*
// Comments are stored without the <!-- --> delimiters
// unless they occur inside STYLE or SCRIPT blocks
if ( not ( InStyle or InScript )) then
delete( text, ( length( text ) - 2 ), 3 )
else
*)
text := text + ch;
end
else
begin
// not starting a comment
text := text + ch;
end;
end
else
begin
text := text + ch;
end;
end;
end; // dash
#10, {#13,}
#9, #32 : begin
if ( InTag and ( not InQuotes )) then
begin
if ( prevch <> '<' ) then
begin
// within tags, we do collapse white space (spaces, tabs and linebreaks)
if ( not ( prevch in _WhiteSpace )) then
begin
text := text + #32;
end;
end
else
begin
// special case: if whitespace follows a <, this cannot be a valid
// tag, so we demote it to plain text. This is not legal, but browsers
// do interpret such orphaned <'s as plain text characters.
if SyntaxWarning( 'Illegal construct: whitespace follows unquoted opening bracket inside a tag', LineNum ) then break;
InTag := false;
text := text + ch;
end;
end
else
begin
// Spaces are NOT collapsed anywhere outside of real tags.
// However, we do have special processing for linebreaks.
if ( ch in [#10{,#13}] ) then
begin
if InQuotes then
begin
// should not really happen: linebreak inside a quoted text
// would terminate the quoted text
if SyntaxWarning( 'Illegal construct: line break inside quoted text', LineNum ) then break;
// we need this to recover:
if ( InScript or InStyle ) then
InQuotes := false;
end;
FlushToken; // store what we already have
TokenIsEOL := true;
text := FLineBreak;
FlushToken; // store the linebreak
TokenIsEOL := false;
inc( LineNum );
end
else
begin
// space or tab, not a line break
text := text + ch;
end;
end;
end; // Space or Tab
#13 : begin
// ignore
// [x] this will break on Mac files
ch := prevch;
end;
'"', '''' : begin
Text := Text + ch;
// interpret quotes ONLY in tags, not in normal text
if (( InTag or InScript or InStyle ) and ( not InComment )) then
begin
if InQuotes then
begin
if ( prevQuote = ch ) then
begin
// end quoted text
InQuotes := false;
end;
end
else
begin
// begin quoted text
InQuotes := true;
prevQuote := ch;
end;
end;
end; // single or double quote
else
begin
text := text + ch;
end; // any other character
end; // case ch
prevch := ch;
delete( Last3, 1, 1 );
Last3 := Last3 + ch;
until false;
FlushToken; // if any text remains
finally
FIsParsing := false;
end;
end; // Parse
function TGFHTMLParser.Peek(const S: TStream;
const Count: byte): string;
{ peek Count chars forward into the stream }
var
oldPos : longint;
sizeread : integer;
buf : array[0..255] of char;
begin
oldPos := S.Position;
sizeread := S.Read( buf, Count );
setlength( result, sizeread );
result := buf;
S.Position := oldPos;
end; // Peek
function TGFHTMLParser.ParseFile(const FN: string): integer;
var
Stream : TFileStream;
begin
result := -1;
Stream := TFileStream.Create( FN, ( fmOpenRead or fmShareDenyWrite ));
try
result := Parse( Stream );
finally
Stream.Free;
end;
end; // ParseFile
function TGFHTMLParser.SyntaxWarning(const s: string; const i: integer) : boolean;
begin
if assigned( FOnSyntaxWarning ) then
begin
result := false;
FOnSyntaxWarning( self, s, i, result );
end
else
begin
result := false;
end;
end; // SyntaxWarning
procedure TGFHTMLParser.SaveTokens(const FN: string);
var
f : textfile;
i, a : integer;
token : TToken;
attr : TTagAttribute;
begin
assignfile( f, FN );
rewrite( f );
try
for i := 1 to FTokenList.Count do
begin
token := FTokenList[pred( i )];
writeln( f );
writeln( f, Format(
'token %d : %s (line %d)',
[i,TOKEN_CLASSES[Token.TokenClass],Token.LineNumber]
));
case Token.TokenClass of
tcTag : begin
if token.tag.IsStartTag then
begin
writeln( f, Format(
' Element: %s (%d attributes)',
[ELEMENT_NAMES[token.Tag.Element],token.Tag.AttributeList.Count]
));
end
else
begin
writeln( f, Format(
' Element: /%s (%d attributes)',
[ELEMENT_NAMES[token.Tag.Element],token.Tag.AttributeList.Count]
));
end;
if ( token.Tag.AttributeList.Count > 0 ) then
begin
for a := 1 to token.Tag.AttributeList.Count do
begin
attr := token.Tag.AttributeList[pred( a )];
writeln( f, Format(
' attr: %s value: %s',
[attr.Name, attr.Value]
));
end;
end;
end;
tcEOL : begin
// nothing
end;
else
begin
writeln( f, Token.Text );
end;
end;
end;
finally
closefile( f );
end;
end; // SaveTokens
procedure TGFHTMLParser.RebuildFile(const FN: string);
var
f : textfile;
i : integer;
token : TToken;
begin
if ( FTokenList.Count = 0 ) then exit;
assignfile( f, FN );
rewrite( f );
try
for i := 1 to FTokenList.Count do
begin
token := FTokenList[pred( i )];
case token.TokenClass of
tcTag : begin
write( f, token.tag.text );
end;
else
write( f, token.Text );
end;
end;
finally
closefile( f );
end;
end; // RebuildFile
end.