-
Notifications
You must be signed in to change notification settings - Fork 10
/
luaxml-mod-html.lua
2226 lines (2013 loc) · 64.6 KB
/
luaxml-mod-html.lua
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
--- HTML parsing module for LuaXML
-- @module luaxml-mod-html
-- @author Michal Hoftich <michal.h21@gmail.com
-- Copyright Michal Hoftich, 2022
-- HTML parser inspired by https://browser.engineering/html.html
-- but then redone using https://html.spec.whatwg.org/multipage/parsing.html
--
-- There main purpose of this module is to create an useful DOM for later processing
-- using LuaXML functions. Either for cleanup, or for translation to output formats,
-- for example LaTeX.
--
-- It should be possible to serialize DOM back to the original HTML code.
--
-- We attempt to do some basic fixes, like to close paragraphs or list items that
-- aren't closed correctly in the original code. We don't fix tables or
-- formatting elements (see https://html.spec.whatwg.org/multipage/parsing.html#the-list-of-active-formatting-elements)
-- as these features don't seem necessary for the purpose of this module. We may change
-- this policy in the future, if it turns out that they are necessary.
--
--
local M = {}
-- use local copies of utf8 functions
local ucodepoint = utf8.codepoint
local utfchar = utf8.char
local function uchar(codepoint)
if codepoint and codepoint > -1 then
return utfchar(codepoint)
end
return ""
end
-- declare namespaces
local xmlns = {
HTML = "http://www.w3.org/1999/xhtml",
MathML = "http://www.w3.org/1998/Math/MathML",
SVG = "http://www.w3.org/2000/svg",
XLink = "http://www.w3.org/1999/xlink",
XML = "http://www.w3.org/XML/1998/namespace",
XMLNS = "http://www.w3.org/2000/xmlns/",
}
-- we must make search tree for named entities, as their support
-- is quite messy
local named_entities
if kpse then
named_entities = require "luaxml-namedentities"
else
named_entities = require "luaxml.namedentities"
end
local entity_tree = {children = {}}
local function update_tree(tree, char)
local children = tree.children or {}
local current = children[char] or {}
children[char] = current
tree.children = children
return current
end
-- loop over named entities and update tree
for entity, char in pairs(named_entities) do
local tree = entity_tree
for char in entity:gmatch(".") do
tree = update_tree(tree,char)
end
tree.entity = entity
tree.char = char
end
local function search_entity_tree(tbl)
-- get named entity for the list of characters
local tree = entity_tree
for _,char in ipairs(tbl) do
if tree.children then
tree = tree.children[char]
if not tree then return nil end
else
return nil
end
end
-- print("tree", tree.char)
return tree
end
-- declare basic node types
local Root = {
_type = "root",
xmlns = xmlns.HTML
}
function Root:init()
local o = {}
setmetatable(o, self)
self.__index = self
self.__tostring = function (x) return "_ROOT" end
o.children = {}
return o
end
function Root:add_child(node)
table.insert(self.children, node)
end
local Doctype = {
_type = "doctype"
}
function Doctype:init(name, parent)
local o = {}
setmetatable(o, self)
self.__index = self
self.__tostring = function (x)
if x.data then
return "<!DOCTYPE " .. x.name .. " " .. x.data .. ">"
else
return "<!DOCTYPE " .. x.name .. ">"
end
end
self.add_child = Root.add_child
o.parent = parent
o.name = name
o.children = {}
return o
end
function Doctype:add_data(data)
self.data = data
end
local Text = {
_type = "text"
}
function Text:init(text, parent)
local o = {}
setmetatable(o, self)
self.__index = self
o.text = text
self.__tostring = function (x) return "'" .. x.text .. "'" end
self.add_child = Root.add_child
o.parent = parent
o.children = {}
return o
end
local Comment = {
_type = "comment"
}
function Comment:init(text, parent)
local o = {}
setmetatable(o, self)
self.__index = self
o.text = text
self.__tostring = function (x) return "<!--" .. x.text .. "-->" end
self.add_child = Root.add_child
o.parent = parent
o.children = {}
return o
end
local Element = {
_type = "element"
}
function Element:init(tag, parent)
local o = {}
setmetatable(o, self)
self.__index = self
-- tag can be table with unicode characters
if type(tag) == "table" then
o.tag = table.concat(tag)
else
o.tag = tag
end
self.__tostring = function(x)
local attr = {}
for _, el in ipairs(x.attr) do
-- handle attributes
local value
if el.value:match('"') then
value = "'" .. el.value .. "'"
else
value = '"' .. el.value .. '"'
end
attr[#attr+1] = el.name .. "=" .. value
end
local closing = ">"
if x.self_closing then
closing = " />"
end
if #attr > 0 then
return "<" .. x.tag .. " " .. table.concat(attr, " ") .. closing
else
return "<" .. x.tag .. closing
end
end
self.add_child = Root.add_child
o.children = {}
o.attr = {}
o.parent = parent
-- default xmlns
o.xmlns = xmlns.HTML
return o
end
-- state machine functions
-- each function takes HtmlParser as an argument
local HtmlStates = {}
-- declare codepoints for more efficient processing
local less_than = ucodepoint("<")
local greater_than = ucodepoint(">")
local amperesand = ucodepoint("&")
local exclam = ucodepoint("!")
local question = ucodepoint("?")
local solidus = ucodepoint("/")
local equals = ucodepoint("=")
local quoting = ucodepoint('"')
local apostrophe = ucodepoint("'")
local semicolon = ucodepoint(";")
local hyphen = ucodepoint("-")
local dash = ucodepoint("-")
local numbersign = ucodepoint("#")
local smallx = ucodepoint("x")
local bigx = ucodepoint("X")
local right_square = ucodepoint("]")
local EOF = -1 -- special character, meaning end of stream
local null = 0
local function is_upper_alpha(codepoint)
if (64 < codepoint and codepoint < 91) then
return true
end
end
local function is_lower_alpha(codepoint)
if (96 < codepoint and codepoint < 123) then
return true
end
end
local function is_alpha(codepoint)
-- detect if codepoint is alphanumeric
if is_upper_alpha(codepoint) or
is_lower_alpha(codepoint) then
return true
end
return false
end
local function is_numeric(codepoint)
if 47 < codepoint and codepoint < 58 then
return true
end
end
local function is_upper_hex(codepoint)
if 64 < codepoint and codepoint < 71 then
return true
end
end
local function is_lower_hex(codepoint)
if 96 < codepoint and codepoint < 103 then
return true
end
end
local function is_hexadecimal(codepoint)
if is_numeric(codepoint) or
is_lower_hex(codepoint) or
is_upper_hex(codepoint)
then
return true
end
end
local function is_alphanumeric(codepoint)
return is_alpha(codepoint) or is_numeric(codepoint)
end
local function is_space(codepoint)
-- detect space characters
if codepoint==0x0009 or codepoint==0x000A or codepoint==0x000C or codepoint==0x0020 then
return true
end
return false
end
local function is_surrogate(codepoint)
return 0xD800 <= codepoint and codepoint <= 0xDFFF
end
character_entity_replace_table = {
[0x80] = 0x20AC,
[0x82] = 0x201A,
[0x83] = 0x0192,
[0x84] = 0x201E,
[0x85] = 0x2026,
[0x86] = 0x2020,
[0x87] = 0x2021,
[0x88] = 0x02C6,
[0x89] = 0x2030,
[0x8A] = 0x0160,
[0x8B] = 0x2039,
[0x8C] = 0x0152,
[0x8E] = 0x017D,
[0x91] = 0x2018,
[0x92] = 0x2019,
[0x93] = 0x201C,
[0x94] = 0x201D,
[0x95] = 0x2022,
[0x96] = 0x2013,
[0x97] = 0x2014,
[0x98] = 0x02DC,
[0x99] = 0x2122,
[0x9A] = 0x0161,
[0x9B] = 0x203A,
[0x9C] = 0x0153,
[0x9E] = 0x017E,
[0x9F] = 0x0178
}
local function fix_null(codepoint)
if codepoint == null then
return 0xFFFD
else
return codepoint
end
end
HtmlStates.data = function(parser)
-- this is the default state
local codepoint = parser.codepoint
-- print("codepoint", parser.codepoint)
if codepoint == less_than then
-- start of tag
return "tag_open"
elseif codepoint == amperesand then
-- we must save the current state
-- what we will return to after entity
parser.return_state = "data"
return "character_reference"
elseif codepoint == EOF then
parser:emit_eof()
else
parser:emit_character(uchar(codepoint))
end
return "data"
end
HtmlStates.tag_open = function(parser)
-- parse tag contents
local codepoint = parser.codepoint
if codepoint == exclam then
return "markup_declaration_open"
elseif codepoint == solidus then
return "end_tag_open"
elseif codepoint == question then
parser:start_token("comment",{data={}})
return "bogus_comment"
elseif is_alpha(codepoint) then
local data = {
name = {},
attr = {},
current_attr_name = {},
current_attr_value = {},
self_closing = false
}
parser:start_token("start_tag", data)
return parser:tokenize("tag_name")
elseif codepoint == EOF then
parser:emit_character(">")
parser:emit_eof()
else
-- invalid tag
-- emit "<" and reconsume current character as data
parser:emit_character("<")
return parser:tokenize("data")
end
end
HtmlStates.character_reference = function(parser)
-- parse HTML entities
-- initialize temp buffer
parser.temp_buffer = {"&"}
local codepoint = parser.codepoint
if is_alphanumeric(codepoint) then
return parser:tokenize("named_character_reference")
elseif codepoint == numbersign then
table.insert(parser.temp_buffer, uchar(codepoint))
return "numeric_character_reference"
else
parser:flush_temp_buffer()
return parser:tokenize(parser.return_state)
end
end
HtmlStates.named_character_reference = function(parser)
-- named entity parsing is pretty complicated
-- https://html.spec.whatwg.org/multipage/parsing.html#named-character-reference-state
local codepoint = parser.codepoint
-- test if the current entity name is included in the named entity list
local search_table = {}
-- first char in temp buffer is &, which we don't want to lookup in the search tree
for i=2, #parser.temp_buffer do search_table[#search_table+1] = parser.temp_buffer[i] end
if codepoint == semicolon then
-- close named entity
local entity = search_entity_tree(search_table)
if entity and entity.char then
parser:add_entity(entity.char)
else
-- if the current name doesn't correspond to any named entity, flush everything into text
parser:flush_temp_buffer()
return parser:tokenize(parser.return_state)
end
return parser.return_state
else
local char = uchar(codepoint)
-- try if the current entity name is in the named entity search tree
table.insert(search_table, char)
local entity = search_entity_tree(search_table)
if entity then
-- keep parsing name entity while we match a name
table.insert(parser.temp_buffer, char)
return "named_character_reference"
else
-- here this will be more complicated
if #search_table > 1 then
local token = parser.current_token
if token.type == "start_tag" and (codepoint == equals or is_alphanumeric(codepoint)) then
-- in attribute value, flush characters and retokenize
parser:flush_temp_buffer()
return parser:tokenize(parser.return_state)
else
-- try to get entity for characters preceding the current character
table.remove(search_table)
local newentity = search_entity_tree(search_table)
if newentity and newentity.char then
parser:add_entity(newentity.char)
else
-- we need to find if parts of the current substring match a named entity
-- for example ¬it; -> ¬it; but ∉ -> ∉
local rest = {}
-- loop over the table with characters, and try to find if it matches entity
for i = #search_table, 1,-1 do
local removed_char = table.remove(search_table)
--
table.insert(rest, 1, removed_char)
newentity = search_entity_tree(search_table)
if newentity and newentity.char then
parser:add_entity(newentity.char)
parser.temp_buffer = rest
break
end
end
-- replace temporary buffer witch characters that followed the matched entity
parser:flush_temp_buffer()
end
return parser:tokenize(parser.return_state)
end
else
-- search table contains only the current character
parser:flush_temp_buffer()
return parser:tokenize(parser.return_state)
end
end
end
end
HtmlStates.numeric_character_reference = function(parser)
-- this variable will hold the number
local codepoint = parser.codepoint
parser.character_reference_code = 0
if codepoint == smallx or codepoint == bigx then
-- hexadecimal entity
table.insert(parser.temp_buffer, uchar(codepoint))
return "hexadecimal_character_reference_start"
else
-- try decimal entity
return parser:tokenize("decimal_character_reference_start")
end
end
HtmlStates.hexadecimal_character_reference_start = function(parser)
local codepoint = parser.codepoint
if is_hexadecimal(codepoint) then
return parser:tokenize("hexadecimal_character_reference")
else
parser:flush_temp_buffer()
return parser:tokenize(parser.return_state)
end
end
HtmlStates.decimal_character_reference_start = function(parser)
local codepoint = parser.codepoint
if is_numeric(codepoint) then
return parser:tokenize("decimal_character_reference")
else
parser:flush_temp_buffer()
return parser:tokenize(parser.return_state)
end
end
HtmlStates.decimal_character_reference = function(parser)
local codepoint = parser.codepoint
-- helper functions for easier working with the character_reference_code
local function multiply(number)
parser.character_reference_code = parser.character_reference_code * number
end
local function add(number)
parser.character_reference_code = parser.character_reference_code + number
end
if is_numeric(codepoint) then
multiply(10)
add(codepoint - 0x30)
elseif codepoint == semicolon then
return "numeric_reference_end_state"
else
-- this adds current entity
parser:tokenize("numeric_reference_end_state")
-- now tokenize the current character
return parser:tokenize(parser.return_state)
end
return "decimal_character_reference"
end
HtmlStates.hexadecimal_character_reference = function(parser)
local codepoint = parser.codepoint
-- helper functions for easier working with the character_reference_code
local function multiply(number)
parser.character_reference_code = parser.character_reference_code * number
end
local function add(number)
parser.character_reference_code = parser.character_reference_code + number
end
if is_numeric(codepoint) then
multiply(16)
add(codepoint - 0x30)
elseif is_upper_hex(codepoint) then
multiply(16)
add(codepoint - 0x37)
elseif is_lower_hex(codepoint) then
multiply(16)
add(codepoint - 0x57)
elseif codepoint == semicolon then
return "numeric_reference_end_state"
else
-- this adds current entity
parser:tokenize("numeric_reference_end_state")
-- now tokenize the current character
return parser:tokenize(parser.return_state)
end
return "hexadecimal_character_reference"
end
HtmlStates.numeric_reference_end_state = function(parser)
-- in this state, we don't need to
local character = parser.character_reference_code
-- we need to clean invalid character codes
if character == 0x00 or
character > 0x10FFFF or
is_surrogate(character)
then
character = 0xFFFD
-- should we add special support for "noncharacter"? I think we can pass them to the output anyway
elseif character_entity_replace_table[character] then
character = character_entity_replace_table[character]
end
parser:add_entity(uchar(character))
return parser.return_state
end
HtmlStates.markup_declaration_open = function(parser)
-- started by <!
-- we now need to find the following text, to find if we started comment, doctype, or cdata
local comment_pattern = "^%-%-"
local doctype_pattern = "^[Dd][Oo][Cc][Tt][Yy][Pp][Ee]"
local cdata_pattern = "^%[CDATA%["
local start_pos = parser.position
local text = parser.body
if text:match(comment_pattern, start_pos) then
-- local _, newpos = text:find(comment_pattern, start_pos)
-- we need to ignore next few characters
parser.ignored_pos = start_pos + 1
parser:start_token("comment", {data = {}})
return "comment_start"
elseif text:match(doctype_pattern, start_pos) then
parser.ignored_pos = start_pos + 6
parser:start_token("doctype", {name = {}, data = {}, force_quirks = false})
return "doctype"
elseif text:match(cdata_pattern, start_pos) then
parser.ignored_pos = start_pos + 6
local current_element = parser:current_node()
if current_element.xmlns == xmlns.HTML or not current_element.xmlns then
-- we change CDATA simply to comments
parser:start_token("comment", {data = {"[CDATA["}})
return "bogus_comment"
else
-- we are in XML mode, this happens for included SVG or MathML
return "cdata_section"
end
else
parser:start_token("comment", {data = {}})
return "bogus_comment"
end
-- local start, stop = string.find(parser.body, comment_pattern, parser.position)
end
HtmlStates.cdata_section = function(parser)
local codepoint = parser.codepoint
if codepoint == right_square then
return "cdata_section_bracket"
elseif codepoint == EOF then
parser:emit_eof()
else
parser:emit_character(uchar(codepoint))
return "cdata_section"
end
end
HtmlStates.cdata_section_bracket = function(parser)
local codepoint = parser.codepoint
if codepoint == right_square then
return "cdata_section_end"
else
parser:emit_character("]")
return parser:tokenize("cdata_section")
end
end
HtmlStates.cdata_section_end = function(parser)
local codepoint = parser.codepoint
if codepoint == right_square then
parser:emit_character("]")
return "cdata_section_end"
elseif codepoint == greater_than then
return "data"
else
parser:emit_character("]")
return parser:tokenize("cdata_section")
end
end
HtmlStates.comment_start = function(parser)
local codepoint = parser.codepoint
if codepoint == hyphen then
return "comment_start_dash"
elseif codepoint == greater_than then
parser:emit()
return "data"
else
return parser:tokenize("comment")
end
end
HtmlStates.comment_start_dash = function(parser)
local codepoint = parser.codepoint
if codepoint == hyphen then
return "comment_end"
elseif codepoint == greater_than then
parser:emit()
return data
elseif codepoint == EOF then
parser:emit()
parser:emit_eof()
else
parser:append_token_data("data", "-")
return parser:tokenize("comment")
end
end
HtmlStates.comment = function(parser)
local codepoint = parser.codepoint
codepoint = fix_null(codepoint)
if codepoint == less_than then
parser:append_token_data("data", uchar(codepoint))
return "comment_less_than"
elseif codepoint == hyphen then
return "comment_end_dash"
elseif codepoint == EOF then
parser:emit()
parser:emit_eof()
else
parser:append_token_data("data", uchar(codepoint))
end
return "comment"
end
HtmlStates.comment_less_than = function(parser)
local codepoint = parser.codepoint
if codepoint == exclam then
parser:append_token_data("data", uchar(codepoint))
return "comment_less_than_bang"
elseif codepoint == less_than then
parser:append_token_data("data", uchar(codepoint))
return "comment_less_than"
else
return parser:tokenize("comment")
end
end
HtmlStates.comment_less_than_bang = function(parser)
local codepoint = parser.codepoint
if codepoint == hyphen then
return "comment_less_than_bang_dash"
else
return parser:tokenize("comment")
end
end
HtmlStates.comment_less_than_bang_dash = function(parser)
local codepoint = parser.codepoint
if codepoint == hyphen then
return "comment_less_than_bang_dash_dash"
else
return parser:tokenize("comment_end_dash")
end
end
HtmlStates.comment_less_than_bang_dash_dash = function(parser)
-- these comment states start to be ridiculous
local codepoint = parser.codepoint
if codepoint == greater_than or codepoint == EOF then
return parser:tokenize("comment_end")
else
return parser:tokenize("comment_end")
end
end
HtmlStates.comment_end_dash = function(parser)
local codepoint = parser.codepoint
if codepoint == hyphen then
return "comment_end"
elseif codepoint == EOF then
parser:emit()
parser:emit_eof()
else
parser:append_token_data("data", uchar(codepoint))
return parser:tokenize("comment")
end
end
HtmlStates.comment_end = function(parser)
local codepoint = parser.codepoint
if codepoint == greater_than then
parser:emit()
return "data"
elseif codepoint == exclam then
return "comment_end_bang"
elseif codepoint == hyphen then
parser:append_token_data("data", "-")
return "comment_end"
elseif codepoint == EOF then
parser:emit()
parser:emit_eof()
else
parser:append_token_data("data", "--")
return parser:tokenize("comment")
end
end
HtmlStates.comment_end_bang = function(parser)
local codepoint = parser.codepoint
if codepoint == hyphen then
parser:append_token_data("data", "--!")
return "comment_end_dash"
elseif codepoint == greater_than then
parser:emit()
return "data"
elseif codepoint == EOF then
parser:emit()
parser:emit_eof()
else
parser:append_token_data("data", "--!")
return parser:tokenize("comment")
end
end
HtmlStates.end_tag_open = function(parser)
local codepoint = parser.codepoint
if is_alpha(codepoint) then
local data = {
name = {}
}
parser:start_token("end_tag", data)
return parser:tokenize("tag_name")
elseif codepoint == greater_than then
return "data"
elseif codepoint == EOF then
parser:discard_token()
parser:emit_character("</")
parser:emit_eof()
else
data = {
data = {}
}
parser:start_token("comment", data)
return parser:tokenize("bogus_comment")
end
end
HtmlStates.bogus_comment = function(parser)
-- started by <?
local codepoint = parser.codepoint
codepoint = fix_null(codepoint)
if codepoint == greater_than then
parser:emit()
return "data"
elseif codepoint == EOF then
parser:emit()
parser:emit_eof()
else
parser:append_token_data("data", uchar(codepoint))
return "bogus_comment"
end
end
local function doctype_eof(parser)
parser:set_token_data("force_quirks", true)
parser:emit()
parser:emit_eof()
end
HtmlStates.doctype = function(parser)
local codepoint = parser.codepoint
if is_space(codepoint) then
return "before_doctype_name"
elseif codepoint == greater_than then
return parser:tokenize("before_doctype_name")
elseif codepoint == EOF then
doctype_eof(parser)
else
return parser:tokenize("before_doctype_name")
end
end
HtmlStates.before_doctype_name = function(parser)
local codepoint = parser.codepoint
codepoint = fix_null(codepoint)
if is_space(codepoint) then
return "before_doctype_name"
elseif codepoint == greater_than then
parser:set_token_data("force_quirks", true)
parser:emit()
return "data"
elseif codepoint == EOF then
doctype_eof(parser)
elseif is_upper_alpha(codepoint) then
-- add lowercase name
parser:append_token_data("name", uchar(codepoint + 0x20))
return "doctype_name"
else
parser:append_token_data("name", uchar(codepoint))
return "doctype_name"
end
end
HtmlStates.doctype_name = function(parser)
local codepoint = parser.codepoint
codepoint = fix_null(codepoint)
if is_space(codepoint) then
return "after_doctype_name"
elseif codepoint == greater_than then
parser:emit()
return "data"
elseif codepoint == EOF then
doctype_eof(parser)
elseif is_upper_alpha(codepoint) then
-- add lowercase name
parser:append_token_data("name", uchar(codepoint + 0x20))
return "doctype_name"
else
parser:append_token_data("name", uchar(codepoint))
return "doctype_name"
end
end
HtmlStates.after_doctype_name = function(parser)
local codepoint = parser.codepoint
if is_space(codepoint) then
return "after_doctype_name"
elseif codepoint == greater_than then
parser:emit()
return "data"
elseif codepoint == EOF then
doctype_eof(parser)
else
parser:append_token_data("data", uchar(codepoint))
-- there are lot of complicated rules how to consume doctype,
-- but I think that for our purpose they aren't interesting.
-- so everything until EOF or > is consumed as token.data
return "consume_doctype_data"
end
end
HtmlStates.consume_doctype_data = function(parser)
-- this state just reads everything inside doctype as data
local codepoint = parser.codepoint
if codepoint == greater_than then
parser:emit()
return "data"
elseif codepoint == EOF then
doctype_eof(parser)
else
parser:append_token_data("data", uchar(codepoint))
return "consume_doctype_data"
end
end
HtmlStates.tag_name = function(parser)
local codepoint = parser.codepoint
codepoint = fix_null(codepoint)
if is_space(codepoint) then
return "before_attribute_name"
elseif codepoint == solidus then
return "self_closing_tag"
elseif codepoint == greater_than then
parser:emit()
return "data"
elseif is_upper_alpha(codepoint) then
local lower = string.lower(uchar(codepoint))
parser:append_token_data("name", lower)
elseif codepoint==EOF then
parser:emit()
parser:emit_eof()
else
local char = uchar(codepoint)
parser:append_token_data("name", char)
end
return "tag_name"
end
HtmlStates.self_closing_tag = function(parser)
local codepoint = parser.codepoint
if codepoint == greater_than then
parser.current_token.self_closing = true
parser:emit()
return "data"
else
return parser:tokenize("before_attribute_name")
end
end
HtmlStates.before_attribute_name = function(parser)
local codepoint = parser.codepoint
if is_space(codepoint) then
-- ignore spacing
return "before_attribute_name"
elseif codepoint == solidus or codepoint == greater_than then
-- reconsume in after_attribute_name
return parser:tokenize("after_attribute_name")
elseif codepoint == equals then
-- ToDo: handle https://html.spec.whatwg.org/multipage/parsing.html#parse-error-unexpected-equals-sign-before-attribute-name
else
-- start new attribute
parser:start_attribute()
return parser:tokenize("attribute_name")
end
end
HtmlStates.attribute_name = function(parser)
local codepoint = parser.codepoint
if is_space(codepoint)
or codepoint == solidus
or codepoint == greater_than
then
return parser:tokenize("after_attribute_name")
elseif codepoint == equals then
return "before_attribute_value"
elseif is_upper_alpha(codepoint) then
-- lowercase attribute names
local lower = string.lower(uchar(codepoint))
parser:append_token_data("current_attr_name", lower)
return "attribute_name"
else
parser:append_token_data("current_attr_name", uchar(codepoint))
return "attribute_name"
end