-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathHtmlParser.fs
More file actions
1228 lines (1087 loc) · 41.4 KB
/
HtmlParser.fs
File metadata and controls
1228 lines (1087 loc) · 41.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
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
#nowarn "10001"
namespace FSharp.Data
open System
open System.IO
open System.Text
open System.Text.RegularExpressions
open FSharp.Data
open FSharp.Data.Runtime
open System.Runtime.InteropServices
open System.Collections.Generic
// --------------------------------------------------------------------------------------
module private TextParser =
let toPattern f c = if f c then ValueSome c else ValueNone
[<return: Struct>]
let (|EndOfFile|_|) (c: char) =
let value = c |> int
if (value = -1 || value = 65535) then
ValueSome c
else
ValueNone
[<return: Struct>]
let (|Whitespace|_|) = toPattern Char.IsWhiteSpace
[<return: Struct>]
let (|LetterDigit|_|) = toPattern Char.IsLetterOrDigit
[<return: Struct>]
let (|Letter|_|) = toPattern Char.IsLetter
// --------------------------------------------------------------------------------------
module internal HtmlParser =
let wsRegex = lazy Regex("\\s+", RegexOptions.Compiled)
let invalidTypeNameRegex = lazy Regex("[^0-9a-zA-Z_]+", RegexOptions.Compiled)
let headingRegex = lazy Regex("""h\d""", RegexOptions.Compiled)
type HtmlToken =
| DocType of string
| Tag of isSelfClosing: bool * name: string * attrs: HtmlAttribute list
| TagEnd of string
| Text of string
| Comment of string
| CData of string
| InlineWhitespace // normalised whitespace-only DefaultMode text; kept only between inline siblings
| EOF
override x.ToString() =
match x with
| DocType dt -> sprintf "doctype %s" dt
| Tag(selfClose, name, _) -> sprintf "tag %b %s" selfClose name
| TagEnd name -> sprintf "tagEnd %s" name
| Text _ -> "text"
| Comment _ -> "comment"
| EOF -> "eof"
| CData _ -> "cdata"
| InlineWhitespace -> "inlineWhitespace"
member x.IsEndTag name =
match x with
| TagEnd(endName) when name = endName -> true
| _ -> false
type TextReader with
member x.PeekChar() = x.Peek() |> char
member x.ReadChar() = x.Read() |> char
member x.ReadNChar(n) =
let buffer = Array.zeroCreate n
x.ReadBlock(buffer, 0, n) |> ignore
String(buffer)
type CharList =
{ mutable Contents: StringBuilder }
static member Empty = { Contents = StringBuilder() }
override x.ToString() = x.Contents.ToString()
member x.Cons(c: char) = x.Contents.Append(c) |> ignore
member x.Length = x.Contents.Length
member x.Clear() = x.Contents.Clear() |> ignore
type InsertionMode =
| DefaultMode
| ScriptMode
| CharRefMode
| CommentMode
| DocTypeMode
| CDATAMode
override x.ToString() =
match x with
| DefaultMode -> "default"
| ScriptMode -> "script"
| CharRefMode -> "charref"
| CommentMode -> "comment"
| DocTypeMode -> "doctype"
| CDATAMode -> "cdata"
type HtmlState =
{ mutable Attributes: (CharList * CharList) list
mutable CurrentTag: CharList
mutable Content: CharList
mutable HasFormattedParent: bool
mutable InsertionMode: InsertionMode
mutable Tokens: HtmlToken list
Reader: TextReader }
static member Create(reader: TextReader) =
{ Attributes = []
CurrentTag = { Contents = StringBuilder() }
Content = { Contents = StringBuilder() }
HasFormattedParent = false
InsertionMode = DefaultMode
Tokens = []
Reader = reader }
member x.Pop() = x.Reader.Read() |> ignore
member x.Peek() = x.Reader.PeekChar()
member x.Pop(count) =
Array.init count (fun _ -> x.Reader.ReadChar())
member x.Contents = x.Content.ToString()
member x.ContentLength = x.Content.Length
member x.NewAttribute() =
x.Attributes <- ({ Contents = StringBuilder() }, { Contents = StringBuilder() }) :: x.Attributes
member x.ConsAttrName() =
match x.Attributes with
| [] ->
x.NewAttribute()
x.ConsAttrName()
| (h, _) :: _ -> h.Cons(Char.ToLowerInvariant(x.Reader.ReadChar()))
member x.CurrentTagName() = x.CurrentTag.ToString().Trim()
member x.CurrentAttrName() =
match x.Attributes with
| [] -> String.Empty
| (h, _) :: _ -> h.ToString()
member x.ConsAttrValue(c) =
match x.Attributes with
| [] ->
x.NewAttribute()
x.ConsAttrValue(c)
| (_, h) :: _ -> h.Cons(c)
member x.ConsAttrValue() = x.ConsAttrValue(x.Reader.ReadChar())
member x.GetAttributes() =
x.Attributes
|> List.choose (fun (key, value) ->
if key.Length > 0 then
Some <| HtmlAttribute(key.ToString(), value.ToString())
else
None)
|> List.rev
member x.EmitSelfClosingTag() =
let name = x.CurrentTag.ToString().Trim()
let result = Tag(true, name, x.GetAttributes())
x.CurrentTag <- { Contents = StringBuilder() }
x.InsertionMode <- DefaultMode
x.Attributes <- []
x.Tokens <- result :: x.Tokens
member x.IsFormattedTag =
match x.CurrentTagName().ToLowerInvariant() with
| "pre" -> true
| _ -> false
member x.IsScriptTag =
match x.CurrentTagName().ToLowerInvariant() with
| "script"
| "style" -> true
| _ -> false
member x.EmitTag(isEnd) =
let name = x.CurrentTag.ToString().Trim()
let result =
if isEnd then
if x.ContentLength > 0 then
x.Emit()
TagEnd(name)
else
TagEnd(name)
else
Tag(false, name, x.GetAttributes())
// pre is the only default formatted tag, nested pres are not
// allowed in the spec.
if x.IsFormattedTag then
x.HasFormattedParent <- not isEnd
else
x.HasFormattedParent <- x.HasFormattedParent || x.IsFormattedTag
x.InsertionMode <-
if x.IsScriptTag && (not isEnd) then
ScriptMode
else
DefaultMode
x.CurrentTag <- { Contents = StringBuilder() }
x.Attributes <- []
x.Tokens <- result :: x.Tokens
member x.EmitToAttributeValue() =
assert (x.InsertionMode = InsertionMode.CharRefMode)
let content = x.Content.ToString() |> HtmlCharRefs.substitute
for c in content do
x.ConsAttrValue c
x.Content <- { Contents = StringBuilder() }
x.InsertionMode <- DefaultMode
member x.Emit() : unit =
let result =
let content = x.Content.ToString()
match x.InsertionMode with
| DefaultMode ->
if x.HasFormattedParent then
Text content
else
let normalizedContent = wsRegex.Value.Replace(content, " ")
if normalizedContent = " " then
InlineWhitespace // inter-element whitespace; kept only between inline siblings
else
Text normalizedContent
| ScriptMode -> content |> Text
| CharRefMode -> content.Trim() |> HtmlCharRefs.substitute |> Text
| CommentMode -> Comment content
| DocTypeMode -> DocType content
| CDATAMode -> CData(content.Replace("<![CDATA[", "").Replace("]]>", ""))
x.Content <- { Contents = StringBuilder() }
x.InsertionMode <- DefaultMode
match result with
| Text t when String.IsNullOrEmpty(t) -> ()
| _ -> x.Tokens <- result :: x.Tokens
member x.Cons() = x.Content.Cons(x.Reader.ReadChar())
member x.Cons(char: char) = x.Content.Cons(char)
member x.Cons(chars: char array) =
x.Content.Contents.Append(chars) |> ignore
member x.Cons(chars: string) =
x.Content.Contents.Append(chars) |> ignore
member x.ConsTag() =
match x.Reader.ReadChar() with
| TextParser.Whitespace _ -> ()
| a -> x.CurrentTag.Cons(Char.ToLowerInvariant a)
member x.ClearContent() = x.Content.Clear()
// Tokenises a stream into a sequence of HTML tokens.
let private tokenise reader =
let state = HtmlState.Create reader
let rec data (state: HtmlState) =
match state.Peek() with
| '<' ->
if state.ContentLength > 0 then
state.Emit()
else
state.Pop()
tagOpen state
| TextParser.EndOfFile _ -> state.Tokens <- EOF :: state.Tokens
| '&' ->
if state.ContentLength > 0 then
state.Emit()
else
state.InsertionMode <- CharRefMode
charRef state
| _ ->
match state.InsertionMode with
| DefaultMode ->
state.Cons()
data state
| ScriptMode -> script state
| CharRefMode -> charRef state
| DocTypeMode -> docType state
| CommentMode -> comment state
| CDATAMode -> data state
and script state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| ''' ->
state.Cons()
scriptSingleQuoteString state
| '"' ->
state.Cons()
scriptDoubleQuoteString state
| '/' ->
state.Cons()
scriptSlash state
| '<' ->
state.Pop()
scriptLessThanSign state
| _ ->
state.Cons()
script state
and scriptSingleQuoteString state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| ''' ->
state.Cons()
script state
| '\\' ->
state.Cons()
scriptSingleQuoteStringBackslash state
| _ ->
state.Cons()
scriptSingleQuoteString state
and scriptDoubleQuoteString state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| '"' ->
state.Cons()
script state
| '\\' ->
state.Cons()
scriptDoubleQuoteStringBackslash state
| _ ->
state.Cons()
scriptDoubleQuoteString state
and scriptSingleQuoteStringBackslash state =
match state.Peek() with
| _ ->
state.Cons()
scriptSingleQuoteString state
and scriptDoubleQuoteStringBackslash state =
match state.Peek() with
| _ ->
state.Cons()
scriptDoubleQuoteString state
and scriptSlash state =
match state.Peek() with
| '/' ->
state.Cons()
scriptSingleLineComment state
| '*' ->
state.Cons()
scriptMultiLineComment state
| _ -> script state
and scriptMultiLineComment state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| '*' ->
state.Cons()
scriptMultiLineCommentStar state
| _ ->
state.Cons()
scriptMultiLineComment state
and scriptMultiLineCommentStar state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| '/' ->
state.Cons()
script state
| _ -> scriptMultiLineComment state
and scriptSingleLineComment state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| '\n' ->
state.Cons()
script state
| _ ->
state.Cons()
scriptSingleLineComment state
and scriptLessThanSign state =
match state.Peek() with
| '/' ->
state.Pop()
scriptEndTagOpen state
| '!' ->
state.Cons('<')
state.Cons()
scriptDataEscapeStart state
| _ ->
state.Cons('<')
state.Cons()
script state
and scriptDataEscapeStart state =
match state.Peek() with
| '-' ->
state.Cons()
scriptDataEscapeStartDash state
| _ -> script state
and scriptDataEscapeStartDash state =
match state.Peek() with
| '-' ->
state.Cons()
scriptDataEscapedDashDash state
| _ -> script state
and scriptDataEscapedDashDash state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| '-' ->
state.Cons()
scriptDataEscapedDashDash state
| '<' ->
state.Pop()
scriptDataEscapedLessThanSign state
| '>' ->
state.Cons()
script state
| _ ->
state.Cons()
scriptDataEscaped state
and scriptDataEscapedLessThanSign state =
match state.Peek() with
| '/' ->
state.Pop()
scriptDataEscapedEndTagOpen state
| TextParser.Letter _ ->
state.Cons('<')
state.Cons()
scriptDataDoubleEscapeStart state
| _ ->
state.Cons('<')
state.Cons()
scriptDataEscaped state
and scriptDataDoubleEscapeStart state =
match state.Peek() with
| TextParser.Whitespace _
| '/'
| '>' when state.IsScriptTag ->
state.Cons()
scriptDataDoubleEscaped state
| TextParser.Letter _ ->
state.Cons()
scriptDataDoubleEscapeStart state
| _ ->
state.Cons()
scriptDataEscaped state
and scriptDataDoubleEscaped state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| '-' ->
state.Cons()
scriptDataDoubleEscapedDash state
| '<' ->
state.Cons()
scriptDataDoubleEscapedLessThanSign state
| _ ->
state.Cons()
scriptDataDoubleEscaped state
and scriptDataDoubleEscapedDash state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| '-' ->
state.Cons()
scriptDataDoubleEscapedDashDash state
| '<' ->
state.Cons()
scriptDataDoubleEscapedLessThanSign state
| _ ->
state.Cons()
scriptDataDoubleEscaped state
and scriptDataDoubleEscapedLessThanSign state =
match state.Peek() with
| '/' ->
state.Cons()
scriptDataDoubleEscapeEnd state
| _ ->
state.Cons()
scriptDataDoubleEscaped state
and scriptDataDoubleEscapeEnd state =
match state.Peek() with
| TextParser.Whitespace _
| '/'
| '>' when state.IsScriptTag ->
state.Cons()
scriptDataDoubleEscaped state
| TextParser.Letter _ ->
state.Cons()
scriptDataDoubleEscapeEnd state
| _ ->
state.Cons()
scriptDataDoubleEscaped state
and scriptDataDoubleEscapedDashDash state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| '-' ->
state.Cons()
scriptDataDoubleEscapedDashDash state
| '<' ->
state.Cons()
scriptDataDoubleEscapedLessThanSign state
| '>' ->
state.Cons()
script state
| _ ->
state.Cons()
scriptDataDoubleEscaped state
and scriptDataEscapedEndTagOpen state =
match state.Peek() with
| TextParser.Letter _ -> scriptDataEscapedEndTagName state
| _ ->
state.Cons([| '<'; '/' |])
state.Cons()
scriptDataEscaped state
and scriptDataEscapedEndTagName state =
match state.Peek() with
| TextParser.Whitespace _ when state.IsScriptTag ->
state.Pop()
beforeAttributeName state
| '/' when state.IsScriptTag ->
state.Pop()
selfClosingStartTag state
| '>' when state.IsScriptTag ->
state.Pop()
state.EmitTag(true)
| '>' ->
state.Cons([| '<'; '/' |])
state.Cons(state.CurrentTagName())
state.CurrentTag.Clear()
script state
| TextParser.Letter _ ->
state.ConsTag()
scriptDataEscapedEndTagName state
| _ ->
state.Cons([| '<'; '/' |])
state.Cons()
scriptDataEscaped state
and scriptDataEscaped state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| '-' ->
state.Cons()
scriptDataEscapedDash state
| '<' -> scriptDataEscapedLessThanSign state
| _ ->
state.Cons()
scriptDataEscaped state
and scriptDataEscapedDash state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| '-' ->
state.Cons()
scriptDataEscapedDashDash state
| '<' -> scriptDataEscapedLessThanSign state
| _ ->
state.Cons()
scriptDataEscaped state
and scriptEndTagOpen state =
match state.Peek() with
| TextParser.Letter _ -> scriptEndTagName state
| _ ->
state.Cons('<')
state.Cons('/')
script state
and scriptEndTagName state =
match state.Peek() with
| TextParser.Whitespace _ ->
state.Pop()
beforeAttributeName state
| '/' when state.IsScriptTag ->
state.Pop()
selfClosingStartTag state
| '>' when state.IsScriptTag ->
state.Pop()
state.EmitTag(true)
| TextParser.Letter _ ->
state.ConsTag()
scriptEndTagName state
| _ ->
state.Cons([| '<'; '/' |])
state.Cons(state.CurrentTagName())
state.CurrentTag.Clear()
script state
and charRef state =
match state.Peek() with
| ';' ->
state.Cons()
state.Emit()
| '<' -> state.Emit()
// System.IO.TextReader.Read() returns -1
// at end of stream, and -1 cast to char is \uffff.
| '\uffff' -> state.Emit()
| _ ->
state.Cons()
charRef state
and tagOpen state =
match state.Peek() with
| '!' ->
state.Pop()
markupDeclaration state
| '/' ->
state.Pop()
endTagOpen state
| '?' ->
state.Pop()
bogusComment state
| TextParser.Letter _ ->
state.ConsTag()
tagName false state
| _ ->
state.Cons('<')
data state
and bogusComment state =
let rec bogusComment' (state: HtmlState) =
let exitBogusComment state =
state.InsertionMode <- CommentMode
state.Emit()
match state.Peek() with
| '>' ->
state.Cons()
exitBogusComment state
| TextParser.EndOfFile _ -> exitBogusComment state
| _ ->
state.Cons()
bogusComment' state
bogusComment' state
and markupDeclaration state =
match state.Pop(2) with
| [| '-'; '-' |] -> comment state
| current ->
match new String(Array.append current (state.Pop(5))) with
| "DOCTYPE" -> docType state
| "[CDATA[" ->
state.Cons("<![CDATA[")
cData 0 state
| _ -> bogusComment state
and cData i (state: HtmlState) =
match state.Peek() with
| ']' when i = 0 || i = 1 ->
state.Cons()
cData (i + 1) state
| '>' when i = 2 ->
state.Cons()
state.InsertionMode <- CDATAMode
state.Emit()
| TextParser.EndOfFile _ ->
state.InsertionMode <- CDATAMode
state.Emit()
| _ ->
state.Cons()
cData 0 state
and docType state =
match state.Peek() with
| '>' ->
state.Pop()
state.InsertionMode <- DocTypeMode
state.Emit()
| _ ->
state.Cons()
docType state
and comment state =
match state.Peek() with
| '-' ->
state.Pop()
commentEndDash state
| TextParser.EndOfFile _ ->
state.InsertionMode <- CommentMode
state.Emit()
| _ ->
state.Cons()
comment state
and commentEndDash state =
match state.Peek() with
| '-' ->
state.Pop()
commentEndState state
| TextParser.EndOfFile _ ->
state.InsertionMode <- CommentMode
state.Emit()
| _ ->
state.Cons()
comment state
and commentEndState state =
match state.Peek() with
| '>' ->
state.Pop()
state.InsertionMode <- CommentMode
state.Emit()
| TextParser.EndOfFile _ ->
state.InsertionMode <- CommentMode
state.Emit()
| _ ->
state.Cons()
comment state
and tagName isEndTag state =
match state.Peek() with
| TextParser.Whitespace _ ->
state.Pop()
beforeAttributeName state
| TextParser.EndOfFile _ -> state.EmitTag(isEndTag)
| '/' ->
state.Pop()
selfClosingStartTag state
| '>' ->
state.Pop()
state.EmitTag(isEndTag)
| _ ->
state.ConsTag()
tagName isEndTag state
and selfClosingStartTag state =
match state.Peek() with
| '>' ->
state.Pop()
state.EmitSelfClosingTag()
| TextParser.EndOfFile _ -> data state
| _ -> beforeAttributeName state
and endTagOpen state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| TextParser.Letter _ ->
state.ConsTag()
tagName true state
| '>' ->
state.Pop()
data state
| _ -> comment state
and beforeAttributeName state =
match state.Peek() with
| TextParser.Whitespace _ ->
state.Pop()
beforeAttributeName state
| '/' ->
state.Pop()
selfClosingStartTag state
| '>' ->
state.Pop()
state.EmitTag(false)
| _ -> attributeName state
and attributeName state =
match state.Peek() with
| '=' ->
state.Pop()
beforeAttributeValue state
| '/' ->
state.Pop()
selfClosingStartTag state
| '>' ->
state.Pop()
state.EmitTag(false)
| TextParser.LetterDigit _ ->
state.ConsAttrName()
attributeName state
| TextParser.Whitespace _ -> afterAttributeName state
| TextParser.EndOfFile _ -> state.EmitTag(false)
| _ ->
state.ConsAttrName()
attributeName state
and afterAttributeName state =
match state.Peek() with
| TextParser.Whitespace _ ->
state.Pop()
afterAttributeName state
| '/' ->
state.Pop()
selfClosingStartTag state
| '>' ->
state.Pop()
state.EmitTag(false)
| '=' ->
state.Pop()
beforeAttributeValue state
| _ ->
state.NewAttribute()
attributeName state
and beforeAttributeValue state =
match state.Peek() with
| TextParser.Whitespace _ ->
state.Pop()
beforeAttributeValue state
| TextParser.EndOfFile _ -> state.EmitTag(false)
| '/' ->
state.Pop()
selfClosingStartTag state
| '>' ->
state.Pop()
state.EmitTag(false)
| '"' ->
state.Pop()
attributeValueQuoted '"' state
| '\'' ->
state.Pop()
attributeValueQuoted '\'' state
| _ -> attributeValueUnquoted state
and attributeValueUnquoted state =
match state.Peek() with
| TextParser.Whitespace _ ->
state.Pop()
state.NewAttribute()
beforeAttributeName state
| '/' ->
state.Pop()
attributeValueUnquotedSlash state
| '>' ->
state.Pop()
state.EmitTag(false)
| '&' ->
assert (state.ContentLength = 0)
state.InsertionMode <- InsertionMode.CharRefMode
attributeValueUnquotedCharRef [ '/'; '>' ] state
| _ ->
state.ConsAttrValue()
attributeValueUnquoted state
and attributeValueUnquotedSlash state =
match state.Peek() with
| '>' -> selfClosingStartTag state
| _ ->
state.ConsAttrValue('/')
state.ConsAttrValue()
attributeValueUnquoted state
and attributeValueQuoted quote state =
match state.Peek() with
| TextParser.EndOfFile _ -> data state
| c when c = quote ->
state.Pop()
afterAttributeValueQuoted state
| '&' ->
assert (state.ContentLength = 0)
state.InsertionMode <- InsertionMode.CharRefMode
attributeValueQuotedCharRef quote state
| _ ->
state.ConsAttrValue()
attributeValueQuoted quote state
and attributeValueQuotedCharRef quote state =
match state.Peek() with
| ';' ->
state.Cons()
state.EmitToAttributeValue()
attributeValueQuoted quote state
| TextParser.EndOfFile _ ->
state.EmitToAttributeValue()
attributeValueQuoted quote state
| c when c = quote ->
state.EmitToAttributeValue()
attributeValueQuoted quote state
| _ ->
state.Cons()
attributeValueQuotedCharRef quote state
and attributeValueUnquotedCharRef stop state =
match state.Peek() with
| ';' ->
state.Cons()
state.EmitToAttributeValue()
attributeValueUnquoted state
| TextParser.EndOfFile _ ->
state.EmitToAttributeValue()
attributeValueUnquoted state
| c when List.exists ((=) c) stop ->
state.EmitToAttributeValue()
attributeValueUnquoted state
| _ ->
state.Cons()
attributeValueUnquotedCharRef stop state
and afterAttributeValueQuoted state =
match state.Peek() with
| TextParser.Whitespace _ ->
state.Pop()
state.NewAttribute()
afterAttributeValueQuoted state
| '/' ->
state.Pop()
selfClosingStartTag state
| '>' ->
state.Pop()
state.EmitTag(false)
| _ ->
state.NewAttribute()
attributeName state
let mutable next = state.Reader.Peek()
while next <> -1 do
data state
next <- state.Reader.Peek()
state.Tokens |> List.rev
// Block-level HTML elements. Whitespace-only text nodes that are siblings of
// these elements are inter-element whitespace and are insignificant.
let private blockLevelElements =
set
[ "address"
"article"
"aside"
"blockquote"
"body"
"caption"
"col"
"colgroup"
"dd"
"details"
"dialog"
"dir"
"div"
"dl"
"dt"
"fieldset"
"figcaption"
"figure"
"footer"
"form"
"frameset"
"h1"
"h2"
"h3"
"h4"
"h5"
"h6"
"head"
"header"
"hgroup"
"html"
"legend"
"li"
"link"
"main"
"menu"
"meta"
"nav"
"noscript"
"ol"
"optgroup"
"option"
"p"
"pre"
"script"
"section"
"style"
"summary"
"table"
"tbody"
"td"
"tfoot"
"th"