-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHTML4Parser.pas
More file actions
590 lines (493 loc) · 15.6 KB
/
HTML4Parser.pas
File metadata and controls
590 lines (493 loc) · 15.6 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
unit Html4Parser;
interface
uses
DomCore, HtmlReader, HtmlTags;
{
Parses an HTML string, and generates a new TDocument object.
This class uses THTMLReader, and its SAX-like eventing system,
to build a TDocument from the supplied HTML string.
Sample usage
------------
var
doc: TDocument;
doc := THtml4Parser.Parse('<HTML><BODY>Hello, world!</BODY></HTML>');
TODO
----
- Add a ParseStream static method (requires THTMLReader to support streams)
- Nodes added after the final BODY are appended as a child of the BODY
- If the final node of the BODY is a #text node, and we're appending a #text
node, consolidate the text nodes
- nodes added before the body are appended to the body
Version History
===============
12/30/2021
- If we've already processed a doctype node, ignore any subsequent ones
- Drop any whitespace we encounter before the HEAD element
- if a text node appears before the BODY element, then switch immediately to the BODY
}
type
THtml4Parser = class
private
FHtmlReader: THtmlReader;
FHtmlDocument: TDocument;
// FHead: TElement;
FBody: TElement;
FInBody: Boolean;
FCurrentNode: TNode;
FCurrentTag: THtmlTag;
function RequireHtmlElement: TElement;
function RequireHeadElement: TElement;
function RequireBodyElement: TElement;
function FindParent(TagNumber: TTagID): TElement;
function FindParentElement(tagList: THtmlTagSet): TElement;
function GetDefaultParent(TagNumber: TTagID): TNode;
function FindTableParent: TElement;
function FindThisElement(FindTagName: TDomString): TElement;
procedure LogFmt(const Fmt: string; const Args: array of const);
//HtmlReader SAX callback handlers
procedure ProcessDocType(Sender: TObject); // <!doctype html PUBLIC "[publicID]" "[systemID]">
procedure ProcessElementStart(Sender: TObject); // <DIV - the start tag of an element
procedure ProcessElementEnd(Sender: TObject); // <DIV> - the end of the start tag of an element
procedure ProcessEndElement(Sender: TObject); // </DIV> - the end tag of an element
procedure ProcessAttributeStart(Sender: TObject); // [nodeName]="..."
procedure ProcessAttributeValue(Sender: TObject); // id=[NodeValue]
procedure ProcessAttributeEnd(Sender: TObject);
procedure ProcessTextNode(Sender: TObject);
procedure ProcessComment(Sender: TObject);
//procedure ProcessEntityReference(Sender: TObject); removed in HTML5
//procedure ProcessCDataSection(Sender: TObject);
protected
function ParseString(const htmlStr: TDomString): TDocument;
property HtmlDocument: TDocument read FHtmlDocument;
public
constructor Create;
destructor Destroy; override;
class function Parse(const HtmlStr: TDomString): TDocument;
end;
implementation
uses
{IFDEF UnitTests}HtmlParserTests,{ENDIF} SysUtils, Windows;
const
htmlTagName = 'html';
headTagName = 'head';
bodyTagName = 'body';
SBoolean: array[Boolean] of string = ('False', 'True');
constructor THtml4Parser.Create;
begin
inherited Create;
FHtmlReader := THtmlReader.Create;
with FHtmlReader do
begin
OnDocType := ProcessDocType; //"<!DOCTYPE ..."
OnElementEnd := ProcessElementEnd; //"<P ...
OnElementStart := ProcessElementStart; //"<P>
OnEndElement := ProcessEndElement; //"</P>"
OnAttributeStart := ProcessAttributeStart;
OnAttributeValue := ProcessAttributeValue;
OnAttributeEnd := ProcessAttributeEnd;
OnTextNode := ProcessTextNode;
OnComment := ProcessComment;
// OnEntityReference := ProcessEntityReference; //Removed in HTML5
// OnCDataSection := ProcessCDataSection; //CDATA doesn't happen in HTML, only XML (i.e. SVG and MathML). HTML treats <![CDATA[...]]> sections as comments
end
end;
destructor THtml4Parser.Destroy;
begin
FreeAndNil(FHtmlReader);
inherited Destroy
end;
function THtml4Parser.GetDefaultParent(TagNumber: TTagID): TNode;
begin
{
Get the default fallback position a node should go under:
HEAD, BODY ==> HTML
HeadTags ==> HEAD (if we've not yet reached the BODY)
(else) ==> BODY
TODO: Once we have left the HEAD element, we never return.
At that point every element is added to BODY, even if it's TITLE, LINK, META, etc.
}
if TagNumber in [HTML_TAG] then
begin
//HTML go under the document
Result := FHtmlDocument; //document isn't an Element, it descends from Node
end
else if TagNumber in [HEAD_TAG, BODY_TAG] then
begin
//HEAD and BODY go under the HTML documentElement.
Result := RequireHtmlElement;
end
else if (TagNumber in HeadTags) and (FHtmlDocument.Body = nil) then
begin
//If it's something that can go in the HEAD, and we've not yet moved onto the BODY, add it to the HEAD.
RequireHeadElement;
if TagNumber in NeedFindParentTags + BlockTags then
Result := FindParent(TagNumber)
else
Result := nil;
if Result = nil then
Result := HtmlDocument.Head;
end
else
begin
//It's going into the BODY somewhere.
RequireBodyElement;
if TagNumber in NeedFindParentTags + BlockTags then
Result := Self.FindParent(TagNumber)
else
Result := FCurrentNode;
if Result = nil then
Result := FHtmlDocument.Body;
end
end;
function THtml4Parser.FindParent(TagNumber: TTagID): TElement;
begin
if (TagNumber = P_TAG) or (TagNumber in BlockTags) then
Result := FindParentElement(BlockParentTags)
else if TagNumber = LI_TAG then
Result := FindParentElement(ListItemParentTags)
else if TagNumber in [DD_TAG, DT_TAG] then
Result := FindParentElement(DefItemParentTags)
else if TagNumber in [TD_TAG, TH_TAG] then
Result := FindParentElement(CellParentTags)
else if TagNumber = TR_TAG then
Result := FindParentElement(RowParentTags)
else if TagNumber = COL_TAG then
Result := FindParentElement(ColParentTags)
else if TagNumber in [COLGROUP_TAG, THEAD_TAG, TFOOT_TAG, TBODY_TAG] then
Result := FindParentElement(TableSectionParentTags)
else if TagNumber = TABLE_TAG then
Result := FindTableParent
else if TagNumber = OPTION_TAG then
Result := FindParentElement(OptionParentTags)
else if TagNumber in [HEAD_TAG, BODY_TAG] then
Result := FHtmlDocument.documentElement as TElement
else
Result := nil;
end;
function THtml4Parser.FindParentElement(tagList: THtmlTagSet): TElement;
var
node: TNode;
htmlTag: THtmlTag;
begin
node := FCurrentNode;
while node.NodeType = ELEMENT_NODE do
begin
htmlTag := HtmlTagList.GetTagByName(node.NodeName);
if htmlTag.Number in tagList then
begin
Result := node as TElement;
Exit
end;
//Don't go higher than the body (if we're already in the body)
if htmlTag.Number = BODY_TAG then
begin
Result := node as TElement;
Exit;
end;
node := node.ParentNode
end;
Result := nil
end;
function THtml4Parser.FindTableParent: TElement;
var
node: TNode;
htmlTag: THtmlTag;
begin
node := FCurrentNode;
while node.NodeType = ELEMENT_NODE do
begin
htmlTag := HtmlTagList.GetTagByName(node.NodeName);
if (htmlTag.Number = TD_TAG) or (htmlTag.Number in BlockTags) then
begin
Result := node as TElement;
Exit
end;
node := node.ParentNode
end;
Result := FHtmlDocument.Body;
end;
function THtml4Parser.FindThisElement(FindTagName: TDomString): TElement;
var
node: TNode;
begin
{
Starting from CurrentNode, walk up the tree looking for an Element with a tagName of FindTagName.
}
node := FCurrentNode;
while node.NodeType = ELEMENT_NODE do
begin
Result := node as TElement;
if SameText(Result.tagName, FindTagName) then
Exit;
node := node.ParentNode
end;
Result := nil
end;
procedure THtml4Parser.LogFmt(const Fmt: string; const Args: array of const);
//var
// s: string;
begin
if IsDebuggerPresent then
begin
// s := Format(Fmt, Args);
// OutputDebugString(PChar('[THtml4Parser] '+s));
end;
end;
procedure THtml4Parser.ProcessAttributeEnd(Sender: TObject);
begin
LogFmt('ProcessAttributeEnd', []);
FCurrentNode := (FCurrentNode as TAttr).ownerElement
end;
procedure THtml4Parser.ProcessAttributeStart(Sender: TObject);
var
attr: TAttr;
begin
LogFmt('ProcessAttributeStart (%s=...)', [FHtmlReader.NodeName]);
attr := FHtmlDocument.createAttribute(FHtmlReader.nodeName);
(FCurrentNode as TElement).setAttributeNode(attr);
FCurrentNode := attr
end;
procedure THtml4Parser.ProcessAttributeValue(Sender: TObject);
var
parent: TNode;
begin
LogFmt('ProcessAttributeValue value="%s"', [FHtmlReader.nodeValue]);
parent := FCurrentNode;
if parent = nil then
raise Exception.Create('Attempt to add attribute value when current node is nil');
if parent.NodeType <> ATTRIBUTE_NODE then
raise Exception.Create('Attempt to add attribute value when current node is not an ATTRIBUTE node');
parent.NodeValue := FHtmlReader.nodeValue;
end;
(*procedure THtml4Parser.ProcessCDataSection(Sender: TObject);
var
CDataSection: TCDataSection;
begin
LogFmt('ProcessCDataSection (%s)', [FHtmlReader.nodeValue]);
CDataSection := FHtmlDocument.createCDATASection(FHtmlReader.nodeValue);
FCurrentNode.AppendChild(CDataSection)
end;*)
procedure THtml4Parser.ProcessComment(Sender: TObject);
var
comment: TComment;
begin
LogFmt('ProcessComment (%s)', [FHtmlReader.nodeValue]);
comment := FHtmlDocument.CreateComment(FHtmlReader.nodeValue);
FCurrentNode.AppendChild(comment)
end;
procedure THtml4Parser.ProcessDocType(Sender: TObject);
var
docType: TDocumentType;
oldDocType: TDocumentType;
begin
LogFmt('ProcessDocType: %s', [FHtmlReader.nodeName]);
//If the tree already has a doctype, then ignore any subsequent ones
if FHtmlDocument.Doctype <> nil then
Exit;
//If we're at the DocumentElement (i.e. html) node, then its too late for doctypes
if FHtmlDocument.DocumentElement <> nil then
Exit;
docType := DomImplementation.createDocumentType(
FHtmlReader.nodeName,
FHtmlReader.publicID,
FHtmlReader.systemID);
oldDocType := FHtmlDocument.DocType;
if oldDocType <> nil then
FHtmlDocument.ReplaceChild(docType, oldDocType)
else
FHtmlDocument.InsertBefore(docType, nil);
{
The way to set a document's type is not through the (readonly) .DocType property,
but by adding the DocType node in the tree in its appropriate spot.
DONE: What is the correct spot in the DOM tree for a doctype node?
Answer: the first child node of the document:
- Document
- doctype
- html
- head
- body
}
end;
procedure THtml4Parser.ProcessElementEnd(Sender: TObject);
begin
{
Event occurs at the end of a starting tag:
<DIV></DIV>
^
<DIV/>
^
Valid reader properties:
- reader.NodeType
- reader.IsEmptyElement
}
LogFmt('ProcessElementEnd(IsEmptyElement=%s)', [SBoolean[FHtmlReader.isEmptyElement]]);
if FHtmlReader.IsEmptyElement or (FCurrentTag.Number in EmptyTags) then
begin
if (FCurrentNode <> FHtmlDocument.DocumentElement) and (FCurrentNode <> FHtmlDocument.Head) and (FCurrentNode <> FHtmlDocument.Body) then
FCurrentNode := FCurrentNode.ParentNode;
end;
FCurrentTag := nil
end;
procedure THtml4Parser.ProcessElementStart(Sender: TObject);
var
element: TElement;
parent: TNode;
begin
LogFmt('ProcessElementStart <%s>', [FHtmlReader.NodeName]);
FCurrentTag := HtmlTagList.GetTagByName(FHtmlReader.nodeName);
parent := GetDefaultParent(FCurrentTag.Number);
if parent <> nil then
FCurrentNode := parent;
element := FHtmlDocument.createElement(FHtmlReader.nodeName);
FCurrentNode.AppendChild(element);
FCurrentNode := element
end;
procedure THtml4Parser.ProcessEndElement(Sender: TObject);
var
element: TElement;
begin
LogFmt('ProcessEndElement </%s>', [FHtmlReader.nodeName]);
element := FindThisElement(FHtmlReader.nodeName);
if Assigned(element) then
begin
//Now that we're closing an element, go to its parent.
//Unless we're closing the HEAD or BODY elements, in which case stay in their context.
if (element <> FHtmlDocument.DocumentElement) and (element <> FHtmlDocument.Head) and (element <> FHtmlDocument.Body) then
FCurrentNode := element.ParentNode;
end;
{ else
if IsBlockTagName(FHtmlReader.nodeName) then
raise DomException.Create(HIERARCHY_REQUEST_ERR)}
end;
//procedure THtml4Parser.ProcessEntityReference(Sender: TObject);
//var
// entityReference: TEntityReference;
//begin
// Removed in HTML5
// LogFmt('ProcessEntityReference (%s)', [FHtmlReader.nodeName]);
//
// entityReference := FHtmlDocument.createEntityReference(FHtmlReader.nodeName);
// FCurrentNode.AppendChild(entityReference)
//end;
procedure THtml4Parser.ProcessTextNode(Sender: TObject);
var
textNode: TTextNode;
s: TDomString;
parent: TNode;
begin
// LogFmt('ProcessTextNode #text="%s"', [FHtmlReader.nodeValue]);
LogFmt('ProcessTextNode', []);
s := FHtmlReader.NodeValue;
//parent := nil;
if FHtmlDocument.Body <> nil then
begin
//If we're in the body, add it to current node.
//Note: we can't just add it to the current node.
//We have to use the rules of FindParent to walk up the tree to find the A tag
//that this text is going under
parent := FCurrentNode;
if parent = nil then
raise Exception.Create('In body, but no current node assigned');
end
else if FHtmlDocument.Head <> nil then
begin
//If we're in the HEAD, add it to the current node.
//This can even be whitespace after </HEAD> but before <BODY>.
//The whitespace is added into the (now closed) HEAD.
parent := FCurrentNode;
if parent = nil then
raise Exception.Create('In head, but no current node assigned');
end
else if FHtmlDocument.Head = nil then
begin
//If we're before the head, then add it to head.
//Before the HEAD any leading whitespace on text nodes is trimmed.
//Before the HEAD any empty text nodes are ignored.
s := TrimLeft(s);
if s = '' then
Exit;
parent := RequireHeadElement;
end
else
raise Exception.Create('Got a text node with no place to put it');
if parent = nil then
raise Exception.Create('Text node parent is nil');
//Check if the existing node is a #text. If so then append our text to that one
if (parent.ChildNodes.Length > 0) and (parent.LastChild.NodeType = TEXT_NODE) then
begin
textNode := parent.LastChild as TTextNode;
textNode.NodeValue := textNode.NodeValue + s;
Exit;
end;
textNode := FHtmlDocument.createTextNode(s);
parent.AppendChild(textNode);
end;
function THtml4Parser.RequireBodyElement: TElement;
var
body: TElement;
html: TElement;
begin
body := FHtmlDocument.Body;
if body = nil then
begin
//html := RequireHtmlElement;
RequireHeadElement;
body := FHtmlDocument.DocumentElement.AppendChild(FHtmlDocument.createElement(bodyTagName)) as TElement;
FCurrentNode := body;
end;
Result := body;
end;
function THtml4Parser.RequireHeadElement: TElement;
var
head: TElement;
html: TElement;
begin
head := FHtmlDocument.Head;
if head = nil then
begin
html := RequireHtmlElement;
head := html.AppendChild(FHtmlDocument.createElement(headTagName)) as TElement;
FCurrentNode := head;
end;
Result := head;
end;
function THtml4Parser.RequireHtmlElement: TElement;
var
html: TElement;
begin
html := FHtmlDocument.DocumentElement;
if html = nil then
html := FHtmlDocument.AppendChild(FHtmlDocument.createElement(htmlTagName)) as TElement;
Result := html;
end;
class function THtml4Parser.Parse(const HtmlStr: TDomString): TDocument;
var
parser: THtml4Parser;
begin
parser := THtml4Parser.Create;
try
Result := parser.ParseString(HtmlStr);
finally
parser.Free;
end;
end;
function THtml4Parser.parseString(const htmlStr: TDomString): TDocument;
begin
FHtmlReader.htmlStr := htmlStr;
// FHtmlDocument := DomImplementation.createHtmlDocument(''); //we can't use CreateHtmlDocument because that also creates a doctype.
FHtmlDocument := DomImplementation.createEmptyDocument(nil);
// FHead := nil; //whether we've gotten to a head yet
FBody := nil; //whether we've gotten to a body yet
FInBody := False;
FCurrentNode := FHtmlDocument;
try
while FHtmlReader.Read do;
except
begin
// TODO: Add event ?
raise;
end;
end;
Result := FHtmlDocument;
end;
end.