-
Notifications
You must be signed in to change notification settings - Fork 14
/
html5-parser.lisp
2854 lines (2460 loc) · 101 KB
/
html5-parser.lisp
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
;;;; HTML5 parser for Common Lisp
;;;;
;;;; Copyright (C) 2012 Thomas Bakketun <[email protected]>
;;;; Copyright (C) 2012 Asgeir Bjørlykke <[email protected]>
;;;; Copyright (C) 2012 Mathias Hellevang
;;;; Copyright (C) 2012 Stian Sletner <[email protected]>
;;;;
;;;; This library is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Lesser General Public License as published
;;;; by the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this library. If not, see <http://www.gnu.org/licenses/>.
(in-package :html5-parser)
;; external interface
(defun parse-html5 (source &key encoding strictp container dom)
(parse-html5-from-source source
:encoding encoding
:strictp strictp
:container container
:dom dom))
(defun parse-html5-fragment (source &key encoding strictp (container "div") dom)
(parse-html5-from-source source
:encoding encoding
:strictp strictp
:container container
:dom dom))
(defgeneric transform-html5-dom (to-type node &key)
(:method ((to-type cons) node &key)
(apply #'transform-html5-dom (car to-type) node (cdr to-type)))
(:method (to-type node &key &allow-other-keys)
(error "No TRANSFORM-HTML5-DOM method defined for dom type ~S." to-type)))
;; internal
(defun parse-html5-from-source (source &key container encoding strictp dom)
(let ((*parser* (make-instance 'html-parser
:strict strictp)))
(parser-parse source
:fragment-p container
:encoding encoding)
(with-slots (open-elements errors) *parser*
(let ((document
(if container
(let ((fragment (make-fragment (document*))))
(node-reparent-children (first open-elements) fragment)
fragment)
(document*))))
(values (if dom
(transform-html5-dom dom document)
document)
(reverse errors))))))
(defvar *phase*)
(defun ascii-ichar= (char1 char2)
"ASCII case-insensitive char="
(or (char= char1 char2)
(and (or (char<= #\A char1 #\Z)
(char<= #\A char2 #\Z))
(char= (char-downcase char1)
(char-downcase char2)))))
(defun ascii-istring= (string1 string2)
"ASCII case-insensitive string="
(every #'ascii-ichar= string1 string2))
(defun cdata-switch-helper ()
(and (last-open-element)
(not (equal (node-namespace (last-open-element))
(slot-value *parser* 'html-namespace)))))
(defun parser-parse (source &key fragment-p encoding)
(with-slots (inner-html-mode container tokenizer)
*parser*
(setf inner-html-mode fragment-p)
(when (stringp fragment-p)
(setf container fragment-p))
(setf tokenizer (make-html-tokenizer source
:encoding encoding
:cdata-switch-helper #'cdata-switch-helper))
(parser-reset)
(loop
;; The input stream will throw please-reparse with result true
;; if the encoding is changed
while (catch 'please-reparse
(main-loop)
nil)
do (parser-reset))))
(defun parser-reset ()
(with-slots (open-elements active-formatting-elements
head-pointer form-pointer insert-from-table
first-start-tag errors compat-mode inner-html-mode
inner-html container tokenizer phase last-phase
before-rcdata-phase frameset-ok
html-namespace)
*parser*
(setf open-elements '())
(setf active-formatting-elements '())
(setf head-pointer nil)
(setf form-pointer nil)
(setf insert-from-table nil)
(setf first-start-tag nil)
(setf errors '())
(setf compat-mode :no-quirks)
(cond (inner-html-mode
(setf inner-html (string-downcase container))
(cond ((member inner-html +cdata-elements+ :test #'string=)
(setf (slot-value tokenizer 'state) :rcdata-state))
((member inner-html +rcdata-elements+ :test #'string=)
(setf (slot-value tokenizer 'state) :rawtext-state))
((string= inner-html "plaintext")
(setf (slot-value tokenizer 'state) :plaintext-state)))
(insert-root (implied-tag-token "html" :start-tag))
(setf phase :before-head)
(reset-insertion-mode))
(t
(setf inner-html nil)
(setf phase :initial)))
(setf last-phase nil)
(setf before-rcdata-phase nil)
(setf frameset-ok t)))
(defun is-html-integration-point (element)
(if (and (string= (node-name element) "annotation-xml")
(string= (node-namespace element) (find-namespace "mathml")))
(and (element-attribute element "encoding")
(member (ascii-upper-2-lower (element-attribute element "encoding"))
'("text/html" "application/xhtml+xml")
:test #'string=))
(member (node-name-tuple element)
+html-integration-point-elements+
:test #'equal)))
(defun is-math-ml-text-integration-point (element)
(member (node-name-tuple element)
+mathml-text-integration-point-elements+
:test #'equal))
(defun main-loop ()
(with-slots (tokenizer phase)
*parser*
(map-tokens tokenizer (lambda (token)
(process-token (normalize-token token))))
(loop with reprocess = t
with phases = '()
while reprocess do
(push phase phases)
(setf reprocess (process-eof nil :phase phase))
(when reprocess
(assert (not (member phase phases)))))))
(defun process-token (token)
(with-slots (tokenizer last-open-element html-namespace)
*parser*
(let ((new-token token)
(type))
(loop while new-token do
(let* ((current-node (last-open-element))
(current-node-namespace (if current-node (node-namespace current-node)))
(current-node-name (if current-node (node-name current-node))))
(setf type (getf new-token :type))
(cond ((eql type :parse-error)
(parser-parse-error (getf token :data) (getf token :datavars))
(setf new-token nil))
(t
(let (phase)
(if (or (null (slot-value *parser* 'open-elements))
(equal current-node-namespace html-namespace)
(and (is-math-ml-text-integration-point current-node)
(or (and (eql type :start-tag)
(not (member (getf token :name) '("mglyph" "malignmark") :test #'string=)))
(eql type :characters)
(eql type :space-characters)))
(and (equal current-node-namespace (find-namespace "mathml"))
(equal current-node-name "annotation-xml")
(eql type :start-tag)
(equal (getf token :name) "svg"))
(and (is-html-integration-point current-node)
(member type '(:start-tag :characters :space-characters))))
(setf phase (slot-value *parser* 'phase))
(setf phase :in-foreign-content))
;(format t "~&phase ~S token ~S~%" phase new-token)
(setf new-token
(ecase type
(:characters
(process-characters new-token :phase phase))
(:space-characters
(process-space-characters new-token :phase phase))
(:start-tag
(process-start-tag new-token :phase phase))
(:end-tag
(process-end-tag new-token :phase phase))
(:comment
(process-comment new-token :phase phase))
(:doctype
(process-doctype new-token :phase phase))))
;(format t " phase returned ~S new-token ~S~%" phase new-token)
))))
(when (and (eql type :start-tag)
(getf token :self-closing)
(not (getf token :self-closing-acknowledged)))
(parser-parse-error :non-void-element-with-trailing-solidus
`(:name ,(getf token :name))))))))
(defun parser-parse-error (error-code &optional datavars)
(with-slots (errors) *parser*
(push (list error-code datavars) errors)))
;; TODO rename to a longer and more descriptive name when we are done writing the code
(defun perror (error-code &rest datavars)
(parser-parse-error error-code datavars))
(defun normalize-token (token)
(when (getf token :start-tag)
;; Remove duplicate attributes
(setf (getf token :data) (remove-duplicates (getf token :data)
:key #'car
:test #'string=
:from-end t)))
token)
(defun adjust-attributes (token replacements)
(setf (getf token :data)
(loop for (name . value) in (getf token :data)
collect (cons (or (cdr (assoc name replacements :test #'string=))
name)
value))))
(defun adjust-math-ml-attributes (token)
(adjust-attributes token '(("definitionurl" ."definitionURL"))))
(defun adjust-svg-attributes (token)
(adjust-attributes token '(("attributename" . "attributeName")
("attributetype" . "attributeType")
("basefrequency" . "baseFrequency")
("baseprofile" . "baseProfile")
("calcmode" . "calcMode")
("clippathunits" . "clipPathUnits")
("contentscripttype" . "contentScriptType")
("contentstyletype" . "contentStyleType")
("diffuseconstant" . "diffuseConstant")
("edgemode" . "edgeMode")
("externalresourcesrequired" . "externalResourcesRequired")
("filterres" . "filterRes")
("filterunits" . "filterUnits")
("glyphref" . "glyphRef")
("gradienttransform" . "gradientTransform")
("gradientunits" . "gradientUnits")
("kernelmatrix" . "kernelMatrix")
("kernelunitlength" . "kernelUnitLength")
("keypoints" . "keyPoints")
("keysplines" . "keySplines")
("keytimes" . "keyTimes")
("lengthadjust" . "lengthAdjust")
("limitingconeangle" . "limitingConeAngle")
("markerheight" . "markerHeight")
("markerunits" . "markerUnits")
("markerwidth" . "markerWidth")
("maskcontentunits" . "maskContentUnits")
("maskunits" . "maskUnits")
("numoctaves" . "numOctaves")
("pathlength" . "pathLength")
("patterncontentunits" . "patternContentUnits")
("patterntransform" . "patternTransform")
("patternunits" . "patternUnits")
("pointsatx" . "pointsAtX")
("pointsaty" . "pointsAtY")
("pointsatz" . "pointsAtZ")
("preservealpha" . "preserveAlpha")
("preserveaspectratio" . "preserveAspectRatio")
("primitiveunits" . "primitiveUnits")
("refx" . "refX")
("refy" . "refY")
("repeatcount" . "repeatCount")
("repeatdur" . "repeatDur")
("requiredextensions" . "requiredExtensions")
("requiredfeatures" . "requiredFeatures")
("specularconstant" . "specularConstant")
("specularexponent" . "specularExponent")
("spreadmethod" . "spreadMethod")
("startoffset" . "startOffset")
("stddeviation" . "stdDeviation")
("stitchtiles" . "stitchTiles")
("surfacescale" . "surfaceScale")
("systemlanguage" . "systemLanguage")
("tablevalues" . "tableValues")
("targetx" . "targetX")
("targety" . "targetY")
("textlength" . "textLength")
("viewbox" . "viewBox")
("viewtarget" . "viewTarget")
("xchannelselector" . "xChannelSelector")
("ychannelselector" . "yChannelSelector")
("zoomandpan" . "zoomAndPan"))))
(defun adjust-foreign-attributes (token)
(adjust-attributes token `(("xlink:actuate" . ("xlink" "actuate" ,(find-namespace "xlink")))
("xlink:arcrole" . ("xlink" "arcrole" ,(find-namespace "xlink")))
("xlink:href" . ("xlink" "href" ,(find-namespace "xlink")))
("xlink:role" . ("xlink" "role" ,(find-namespace "xlink")))
("xlink:show" . ("xlink" "show" ,(find-namespace "xlink")))
("xlink:title" . ("xlink" "title" ,(find-namespace "xlink")))
("xlink:type" . ("xlink" "type" ,(find-namespace "xlink")))
("xml:base" . ("xml" "base" ,(find-namespace "xml")))
("xml:lang" . ("xml" "lang" ,(find-namespace "xml")))
("xml:space" . ("xml" "space" ,(find-namespace "xml")))
("xmlns" . (nil "xmlns" ,(find-namespace "xmlns")))
("xmlns:xlink" . ("xmlns" "xlink" ,(find-namespace "xmlns"))))))
(defun reset-insertion-mode ()
(with-slots (inner-html html-namespace phase open-elements) *parser*
(let ((last nil)
(new-phase nil)
(new-modes '(("select" . :in-select)
("td" . :in-cell)
("th" . :in-cell)
("tr" . :in-row)
("tbody" . :in-table-body)
("thead" . :in-table-body)
("tfoot" . :in-table-body)
("caption" . :in-caption)
("colgroup" . :in-column-group)
("table" . :in-table)
("head" . :in-body)
("body" . :in-body)
("frameset" . :in-frameset)
("html" . :before-head))))
(loop for node in (reverse open-elements)
for node-name = (node-name node)
do
(when (eql node (first open-elements))
(assert inner-html)
(setf last t)
(setf node-name inner-html))
;; Check for conditions that should only happen in the innerHTML
;; case
(when (member node-name '("select" "colgroup" "head" "html") :test #'string=)
(assert inner-html))
(unless (and (not last)
(string/= (node-namespace node) html-namespace))
(let ((match (cdr (assoc node-name new-modes :test #'string=))))
(when match
(setf new-phase match)
(return))
(when last
(setf new-phase :in-body)
(return)))))
(setf phase new-phase))))
(defun parse-rc-data-raw-text (token content-type)
(assert (member content-type '(:rawtext :rcdata)))
(with-slots (tokenizer original-phase phase) *parser*
(insert-element token)
(setf (tokenizer-state tokenizer) (ecase content-type
(:rawtext :rawtext-state)
(:rcdata :rcdata-state)))
(setf original-phase phase)
(setf phase :text)
nil))
;; Phases --------------------------------------------------------------------
(defun implied-tag-token (name &optional (type :end-tag))
(list :type type :name name :data '() :self-closing nil))
(defun implied-tag-token/full (name type
&key (attributes '()) (self-closing nil))
(list :type type :name name :data attributes :self-closing self-closing))
(eval-when (:compile-toplevel :execute)
(defun phase-process-method-name (function-name)
(intern (concatenate 'string
"%"
(symbol-name function-name))
(symbol-package function-name))))
(defvar *phase-indent* 0)
(defun call-phase-method (name phase token)
;(format *trace-output* "~&~vTcall: ~S ~S ~S" *phase-indent* name phase token)
;(break)
(let ((result (let ((*phase-indent* (+ 4 *phase-indent*)))
(funcall name phase token))))
;(format *trace-output* "~&~vTreturn: ~S ~S" *phase-indent* name result)
result))
(defmacro define-phase-process-functions (&body defs)
`(progn
,@(loop for function-name in defs
for method-name = (phase-process-method-name function-name)
collect `(defgeneric ,method-name (phase token))
collect `(defun ,function-name (token &key (phase *phase*))
(call-phase-method #',method-name phase token)))))
(define-phase-process-functions
add-formatting-element
end-tag-applet-marquee-object
end-tag-block
end-tag-body
end-tag-br
end-tag-caption
end-tag-col
end-tag-colgroup
end-tag-form
end-tag-formatting
end-tag-frameset
end-tag-head
end-tag-heading
end-tag-html
end-tag-html-body-br
end-tag-ignore
end-tag-imply
end-tag-imply-head
end-tag-list-item
end-tag-optgroup
end-tag-option
end-tag-other
end-tag-p
end-tag-script
end-tag-select
end-tag-table
end-tag-table-cell
end-tag-table-row-group
end-tag-tr
insert-text
process-characters
process-comment
process-doctype
process-end-tag
process-eof
process-space-characters
process-start-tag
start-tag-a
start-tag-applet-marquee-object
start-tag-base-link-command
start-tag-body
start-tag-button
start-tag-caption
start-tag-close-p
start-tag-col
start-tag-colgroup
start-tag-form
start-tag-formatting
start-tag-frame
start-tag-frameset
start-tag-from-head
start-tag-head
start-tag-heading
start-tag-hr
start-tag-html
start-tag-i-frame
start-tag-image
start-tag-imply-tbody
start-tag-input
start-tag-is-index
start-tag-list-item
start-tag-math
start-tag-meta
start-tag-misplaced
start-tag-no-script-no-frames-style
start-tag-nobr
start-tag-noframes
start-tag-opt
start-tag-optgroup
start-tag-option
start-tag-other
start-tag-param-source
start-tag-plaintext
start-tag-pre-listing
start-tag-process-in-head
start-tag-rawtext
start-tag-row-group
start-tag-rp-rt
start-tag-script
start-tag-select
start-tag-style-script
start-tag-svg
start-tag-table
start-tag-table-cell
start-tag-table-element
start-tag-table-other
start-tag-textarea
start-tag-title
start-tag-tr
start-tag-void-formatting
start-tag-xmp)
(defmacro def (phase name (&rest slots) &body body)
`(defmethod ,(phase-process-method-name name) ((*phase* (eql ,phase)) token)
(with-slots (,@slots) *parser*
,@body)))
(defmacro tagname-dispatch (phase name &body cases)
`(def ,phase ,name ()
(let ((tagname (getf token :name)))
(declare (ignorable tagname))
,(let* ((default '(error "Unhandled tag ~S" tagname))
(string-cases
(loop for (tagnames function) in cases
append (cond ((stringp tagnames)
`((,tagnames (,function token))))
((consp tagnames)
(loop for tag in tagnames
collect `(,tag (,function token))))
((eql 'default tagnames)
(setf default `(,function token))
nil)
(t (error "Invalid tag name clause ~S" tagnames))))))
(if (not string-cases)
default
`(string-case:string-case
(tagname :default ,default)
,@string-cases))))))
;; Default methods
(defmethod %process-comment (*phase* token)
;; For most phases the following is correct. Where it's not it will be
;; overridden.
(insert-comment token (last-open-element))
nil)
(defmethod %process-doctype (*phase* token)
(parser-parse-error :unexpected-doctype)
nil)
(defmethod %process-characters (*phase* token)
(parser-insert-text (getf token :data))
nil)
(defmethod %process-space-characters (*phase* token)
(parser-insert-text (getf token :data))
nil)
(defmethod %start-tag-html (*phase* token)
(with-slots (first-start-tag open-elements)
*parser*
(when (and (not first-start-tag)
(string= (getf token :name) "html"))
(parser-parse-error :non-html-root))
;; XXX Need a check here to see if the first start tag token emitted is
;; this token... If it's not, invoke self.parser.parseError().
(let ((root-element (first open-elements)))
(loop for (name . value) in (getf token :data)
do (unless (element-attribute root-element name)
(setf (element-attribute root-element name) value))))
(setf first-start-tag nil)
nil))
;; InitialPhase
(def :initial process-space-characters ()
nil)
(def :initial process-comment ()
(insert-comment token (document*))
nil)
(def :initial process-doctype (compat-mode phase)
(destructuring-bind (&key name public-id system-id correct &allow-other-keys)
token
(when (or (string/= name "html")
public-id
(and system-id (string/= system-id "about:legacy-compat")))
(parser-parse-error :unknown-doctype))
(unless public-id
(setf public-id ""))
(insert-doctype token)
(setf public-id (ascii-upper-2-lower public-id))
(cond ((or (not correct)
(string/= name "html")
(cl-ppcre:scan +quirks-mode-doctypes-regexp+ public-id)
(member public-id '("-//w3o//dtd w3 html strict 3.0//en//"
"-/w3c/dtd html 4.0 transitional/en"
"html")
:test #'string=)
(and (not system-id)
(cl-ppcre:scan '(:sequence :start-anchor (:alternation
"-//w3c//dtd html 4.01 frameset//"
"-//w3c//dtd html 4.01 transitional//"))
public-id))
(and system-id
(equal (ascii-upper-2-lower system-id)
"http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd")))
(setf compat-mode :quirks))
((or (cl-ppcre:scan '(:sequence :start-anchor (:alternation
"-//w3c//dtd xhtml 1.0 frameset//"
"-//w3c//dtd xhtml 1.0 transitional//"))
public-id)
(and system-id
(cl-ppcre:scan '(:sequence :start-anchor (:alternation
"-//w3c//dtd html 4.01 frameset//"
"-//w3c//dtd html 4.01 transitional//"))
public-id)))
(setf compat-mode :limited-quirks)))
(setf phase :before-html)
nil))
(flet ((anything-else ()
(with-slots (compat-mode phase)
*parser*
(setf compat-mode :quirks)
(setf phase :before-html))))
(def :initial process-characters ()
(parser-parse-error :expected-doctype-but-got-chars)
(anything-else)
token)
(def :initial process-start-tag ()
(parser-parse-error :expected-doctype-but-got-start-tag
(list :name (getf token :name)))
(anything-else)
token)
(def :initial process-end-tag ()
(parser-parse-error :expected-doctype-but-got-end-tag
(list :name (getf token :name)))
(anything-else)
token)
(def :initial process-eof ()
(parser-parse-error :expected-doctype-but-got-eof)
(anything-else)
t))
;; BeforeHtmlPhase
(flet ((insert-html-element ()
(insert-root (implied-tag-token "html" :start-tag))
(setf (parser-phase *parser*) :before-head)))
(def :before-html process-eof ()
(insert-html-element)
t)
(def :before-html process-comment ()
(insert-comment token (document*))
nil)
(def :before-html process-space-characters ()
nil)
(def :before-html process-characters ()
(insert-html-element)
token)
(def :before-html process-start-tag (first-start-tag)
(when (string= (getf token :name) "html")
(setf first-start-tag t))
(insert-html-element)
token)
(def :before-html process-end-tag ()
(cond ((not (member (getf token :name) '("head" "body" "html" "br") :test #'string=))
(parser-parse-error :unexpected-end-tag-before-html `(:name ,(getf token :name)))
nil)
(t
(insert-html-element)
token))))
;; BeforeHeadPhase
(tagname-dispatch :before-head process-start-tag
("html" start-tag-html)
("head" start-tag-head token)
(default start-tag-other))
(tagname-dispatch :before-head process-end-tag
(("head" "body" "html" "br") end-tag-imply-head)
(default end-tag-other))
(def :before-head process-eof ()
(start-tag-head (implied-tag-token "head" :start-tag))
t)
(def :before-head process-space-characters ()
nil)
(def :before-head process-characters ()
(start-tag-head (implied-tag-token "head" :start-tag))
token)
(def :before-head start-tag-html ()
(process-start-tag token :phase :in-body))
(def :before-head start-tag-head (head-pointer)
(insert-element token)
(setf head-pointer (last-open-element))
(setf (parser-phase *parser*) :in-head)
nil)
(def :before-head start-tag-other ()
(start-tag-head (implied-tag-token "head" :start-tag))
token)
(def :before-head end-tag-imply-head ()
(start-tag-head (implied-tag-token "head" :start-tag))
token)
(def :before-head end-tag-other ()
(parser-parse-error :end-tag-after-implied-root `(:name ,(getf token :name)))
nil)
;; InHeadPhase
(tagname-dispatch :in-head process-start-tag
("html" start-tag-html)
("title" start-tag-title)
(("noscript" "noframes" "style") start-tag-no-script-no-frames-style)
("script" start-tag-script)
(("base" "basefont" "bgsound" "command" "link") start-tag-base-link-command)
("meta" start-tag-meta)
("head" start-tag-head)
(default start-tag-other))
(tagname-dispatch :in-head process-end-tag
("head" end-tag-head)
(("br" "html" "body") end-tag-html-body-br)
(default end-tag-other))
(flet ((anything-else ()
(end-tag-head (implied-tag-token "head"))))
;; the real thing
(def :in-head process-eof ()
(anything-else)
t)
(def :in-head process-characters ()
(anything-else)
token)
(def :in-head start-tag-html ()
(process-start-tag token :phase :in-body))
(def :in-head start-tag-head ()
(parser-parse-error :two-heads-are-not-better-than-one)
nil)
(def :in-head start-tag-base-link-command (open-elements)
(insert-element token)
(pop-end open-elements)
(setf (getf token :self-closing-acknowledged) t)
nil)
(defun parse-content-attr (string)
"The algorithm for extracting an encoding from a meta element"
(let ((position 0)) ; Step 1
(labels ((char-at (index)
(and (< position (length string))
(char string index)))
(skip-space ()
(loop while (member (char-at position) +space-characters+)
do (incf position))))
;; Step 2
(loop
(setf position (search "charset" string :start2 position))
(unless position
(return-from parse-content-attr))
;; Set position to after charset
(incf position 7)
;; Step 3
(skip-space)
;; Step 4
(when (eql (char-at position) #\=)
(return))
(decf position))
;; Step 5
(incf position)
(skip-space)
;; Step 6
(let ((next-char (char-at position)))
(cond ((or (eql #\' next-char)
(eql #\" next-char))
(incf position)
(let ((end (position next-char string :start position)))
(when end
(subseq string position end))))
(next-char
(let ((start position))
(loop until (or (= position (length string))
(member (char-at position) +space-characters+))
do (incf position))
(subseq string start position))))))))
(def :in-head start-tag-meta (tokenizer open-elements)
(insert-element token)
(pop-end open-elements)
(setf (getf token :self-closing-acknowledged) t)
(let ((attributes (getf token :data)))
(when (eql (cdr (html5-stream-encoding (tokenizer-stream tokenizer))) :tentative)
(cond ((assoc "charset" attributes :test #'string=)
(html5-stream-change-encoding (tokenizer-stream tokenizer)
(cdr (assoc "charset" attributes :test #'string=))))
((and (assoc "http-equiv" attributes :test #'string=)
(ascii-istring= (cdr (assoc "http-equiv" attributes :test #'string=))
"Content-Type")
(assoc "content" attributes :test #'string=))
(let* ((content (cdr (assoc "content" attributes :test #'string=)))
(new-encoding (parse-content-attr content)))
(if new-encoding
(html5-stream-change-encoding (tokenizer-stream tokenizer)
new-encoding)
(parser-parse-error :invalid-encoding-declaration
`(:content ,content))))))))
nil)
(def :in-head start-tag-title ()
(parse-rc-data-raw-text token :rcdata)
nil)
(def :in-head start-tag-no-script-no-frames-style ()
;; Need to decide whether to implement the scripting-disabled case
(parse-rc-data-raw-text token :rawtext))
(def :in-head start-tag-script (tokenizer original-phase phase)
(insert-element token)
(setf (tokenizer-state tokenizer) :script-data-state)
(setf original-phase phase)
(setf phase :text)
nil)
(def :in-head start-tag-other ()
(anything-else)
token)
(def :in-head end-tag-head (phase open-elements)
(let ((node (pop-end open-elements)))
(assert (string= (node-name node) "head") () "Expected head got ~S" (node-name node))
(setf phase :after-head)
nil))
(def :in-head end-tag-html-body-br ()
(anything-else)
token)
(def :in-head end-tag-other ()
(parser-parse-error :unexpected-end-tag `(:name ,(getf token :name)))
nil))
;; XXX If we implement a parser for which scripting is disabled we need to
;; implement this phase.
;;
;; InHeadNoScriptPhase
;; AfterHeadPhase
(tagname-dispatch :after-head process-start-tag
("html" start-tag-html)
("body" start-tag-body)
("frameset" start-tag-frameset)
(("base" "basefont" "bgsound" "link" "meta"
"noframes" "script" "style" "title")
start-tag-from-head)
("head" start-tag-head)
(default start-tag-other))
(tagname-dispatch :after-head process-end-tag
(("body" "html" "br") end-tag-html-body-br)
(default end-tag-other))
(flet ((anything-else ()
(with-slots (phase frameset-ok) *parser*
(insert-element (implied-tag-token "body" :start-tag))
(setf phase :in-body)
(setf frameset-ok t))))
(def :after-head process-eof ()
(anything-else)
t)
(def :after-head process-characters ()
(anything-else)
token)
(def :after-head start-tag-html ()
(process-start-tag token :phase :in-body))
(def :after-head start-tag-body (phase frameset-ok)
(setf frameset-ok nil)
(insert-element token)
(setf phase :in-body)
nil)
(def :after-head start-tag-frameset (phase)
(insert-element token)
(setf phase :in-frameset)
nil)
(def :after-head start-tag-from-head (head-pointer open-elements)
(parser-parse-error :unexpected-start-tag-out-of-my-head
`(:name ,(getf token :name)))
(push-end head-pointer open-elements)
(process-start-tag token :phase :in-head)
(loop for node in (reverse open-elements)
do (when (string= "head" (node-name node))
(setf open-elements
(remove node open-elements :test #'equal))
(return)))
nil)
(def :after-head start-tag-head ()
(parser-parse-error :unexpected-start-tag
`(:name ,(getf token :name)))
nil)
(def :after-head start-tag-other ()
(anything-else)
token)
(def :after-head end-tag-html-body-br ()
(anything-else)
token)
(def :after-head end-tag-other ()
(parser-parse-error :unexpected-end-tag
`(:name ,(getf token :name)))
nil))
;; InBodyPhase
(tagname-dispatch :in-body process-start-tag
("html" start-tag-html)
(("base" "basefont" "bgsound" "command" "link"
"meta" "noframes" "script" "style" "title")
start-tag-process-in-head)
("body" start-tag-body)
("frameset" start-tag-frameset)
(("address" "article" "aside" "blockquote" "center" "details"
"dir" "div" "dl" "fieldset" "figcaption" "figure"
"footer" "header" "hgroup" "menu" "nav" "ol" "p"
"section" "summary" "ul")
start-tag-close-p)
(#.+heading-elements+ start-tag-heading)
(("pre" "listing") start-tag-pre-listing)
("form" start-tag-form)
(("li" "dd" "dt") start-tag-list-item)
("plaintext" start-tag-plaintext)
("a" start-tag-a)
(("b" "big" "code" "em" "font" "i" "s" "small" "strike"
"strong" "tt" "u")
start-tag-formatting)
("nobr" start-tag-nobr)
("button" start-tag-button)
(("applet" "marquee" "object") start-tag-applet-marquee-object)
("xmp" start-tag-xmp)
("table" start-tag-table)
(("area" "br" "embed" "img" "keygen" "wbr")
start-tag-void-formatting)
(("param" "source" "track") start-tag-param-source)
("input" start-tag-input)
("hr" start-tag-hr)
("image" start-tag-image)
("isindex" start-tag-is-index)
("textarea" start-tag-textarea)
("iframe" start-tag-i-frame)
(("noembed" "noscript") start-tag-rawtext)
("select" start-tag-select)
(("rp" "rt") start-tag-rp-rt)
(("option" "optgroup") start-tag-opt)
(("math") start-tag-math)
(("svg") start-tag-svg)
(("caption" "col" "colgroup" "frame" "head"
"tbody" "td" "tfoot" "th" "thead"
"tr")
start-tag-misplaced)
(default start-tag-other))
(tagname-dispatch :in-body process-end-tag