-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelementtree.lua
More file actions
915 lines (754 loc) · 26.1 KB
/
elementtree.lua
File metadata and controls
915 lines (754 loc) · 26.1 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
--[[
Lua Version Check
--]]
local supported_lua_versions <const> = {['Lua 5.4']=true}
if not supported_lua_versions[_VERSION] then
warn("lua-elementtree: detected unsupported lua version: " .. tostring(_VERSION))
end
--[[
Comment
--]]
local comment_metatable <const> = {}
local comment_private <const> = setmetatable({}, {__mode='k'})
-- implementation
local comment_freeze <const> = function (self)
local private <const> = assert(comment_private[self], "Comment instance not recognized: " .. tostring(self))
private.frozen = true
return self
end
local comment_instance_methods <const> = {
['freeze'] = comment_freeze
}
local comment_internal_metatable <const> = {
__name = 'Comment',
__metatable = comment_metatable,
__index = function (self, key)
local private <const> = assert(comment_private[self], "Comment instance not recognized: " .. tostring(self))
-- content
if key == 'content' then
return private.content
-- frozen
elseif key == 'frozen' then
return private.frozen
-- instance methods
elseif comment_instance_methods[key] then
return comment_instance_methods[key]
-- invalid key
else
error("Comment invalid key: " .. key)
end
end,
__newindex = function (self, key, value)
local private <const> = assert(comment_private[self], "Comment instance not recognized: " .. tostring(self))
-- content
if key == 'content' then
if type(value) ~= 'string' or string.len(value) == 0 then
error("Comment content must be a non-empty string")
end
assert(not private.frozen, "Comment instance is frozen")
private.content = value
-- frozen
elseif key == 'frozen' then
error("Comment instance cannot be frozen directly")
-- instance methods
elseif comment_instance_methods[key] then
error("Comment instance methods cannot be modified")
-- invalid key
else
error("Comment invalid key: " .. key)
end
end,
__gc = function (self)
comment_private[self] = nil
end
}
-- public interface
local Comment <const> = setmetatable({
create = function (content)
if type(content) ~= 'string' then
error("Comment content must be a string")
end
local instance <const> = {}
comment_private[instance] = {content=content}
return setmetatable(instance, comment_internal_metatable)
end,
is = function (value)
return (getmetatable(value) == comment_metatable)
end
}, {
__call = function (self, ...)
return self.create(...)
end
})
--[[
Node
--]]
local node_metatable <const> = {}
local node_private <const> = setmetatable({}, {__mode='k'})
-- implementation
local node_set_attribute <const> = function (self, name, value)
local private <const> = assert(node_private[self], "Node instance not recognized: " .. tostring(self))
assert(not private.frozen, "Node instance is frozen")
assert(type(name) == 'string' and #name > 0, "Node attribute name must be a non-empty string")
assert(type(value) == 'string', "Node attribute value must be a string")
private.attributes[name] = value
return self
end
local node_remove_attribute <const> = function (self, name)
local private <const> = assert(node_private[self], "Node instance not recognized: " .. tostring(self))
assert(not private.frozen, "Node instance is frozen")
assert(type(name) == 'string' and #name > 0, "Node attribute name must be a non-empty string")
private.attributes[name] = nil
return self
end
local node_insert_child <const> = function (self, child, position)
local private <const> = assert(node_private[self], "Node instance not recognized: " .. tostring(self))
assert(
(type(child) == 'string' and #child > 0) or
getmetatable(child) == node_metatable or
Comment.is(child),
"Node children must be a Node instance, Comment instance, or non-empty string"
)
assert(
position == nil or math.type(position) == 'integer',
"Node children position must be nil or an integer"
)
if position ~= nil then
table.insert(private.children, position, child)
else
table.insert(private.children, child)
end
return self
end
local node_remove_child <const> = function (self, position_or_child)
local private <const> = assert(node_private[self], "Node instance not recognized: " .. tostring(self))
assert(not private.frozen, "Node instance is frozen")
removed_child = nil -- track if we successfully remove the requested child
if math.type(position_or_child) == 'integer' then
removed_child = table.remove(private.children, position)
elseif (
type(position_or_child) == 'string' or
getmetatable(position_or_child) == node_metatable or
Comment.is(position_or_child)
) then
for index, child in ipairs(private.children) do
if child == position_or_child then
removed_child = table.remove(private.children, index)
break
end
end
else
error("Node:remove_child() expects a position or child instance")
end
assert(removed_child ~= nil, "Node child not found: " .. tostring(position_or_child))
return self
end
local node_freeze <const> = function (self)
local private <const> = assert(node_private[self], "Node instance not recognized: " .. tostring(self))
-- use breadth-first search to recursively freeze this node and it's children
local nodes <const> = {self}
while #nodes > 0 do
local node <const> = table.remove(nodes, 1)
local private <const> = assert(node_private[node], "Node instance not recognized: " .. tostring(node))
private.frozen = true
for _, child in ipairs(node.children) do
if getmetatable(child) == node_metatable then
table.insert(nodes, child)
elseif Comment.is(child) then
child:freeze()
end
end
end
return self
end
local node_instance_methods <const> = {
['set_attribute'] = node_set_attribute,
['remove_attribute'] = node_remove_attribute,
['insert_child'] = node_insert_child,
['remove_child'] = node_remove_child,
['freeze'] = node_freeze
}
local node_internal_metatable <const> = {
__name = 'Node',
__metatable = node_metatable,
__index = function (self, key)
local private <const> = assert(node_private[self], "Node instance not recognized: " .. tostring(self))
-- tag
if key == 'tag' then
return private.tag
-- shallow copy of attributes
elseif key == 'attributes' then
local attributes <const> = {}
for name, value in pairs(private.attributes) do
attributes[name] = value
end
return attributes
-- shallow copy of children
elseif key == 'children' then
local children <const> = {}
for _, child in ipairs(private.children) do
table.insert(children, child)
end
return children
-- frozen
elseif key == 'frozen' then
return private.frozen
-- instance methods
elseif node_instance_methods[key] then
return node_instance_methods[key]
-- emulate an array to expose children
elseif math.type(key) == 'integer' then
return private.children[key]
-- invalid key
else
error("Node invalid key: " .. key)
end
end,
__newindex = function (self, key, value)
local private <const> = assert(node_private[self], "Node instance not recognized: " .. tostring(self))
-- tag
if key == 'tag' then
if type(value) ~= 'string' or string.len(value) then
error("Node tag must be a non-empty string")
end
assert(not private.frozen, "Node instance is frozen")
private.tag = value
-- attributes
elseif key == 'attributes' then
error("Node attributes cannot be modified directly")
-- children
elseif key == 'children' then
error("Node children cannot be modified directly")
-- frozen
elseif key == 'frozen' then
error("Node cannot be frozen directly")
-- instance methods
elseif node_instance_methods[key] then
error("Node instance methods cannot be modified")
-- invalid key
else
error("Node invalid key: " .. key)
end
end,
__len = function (self)
-- emulate an array to expose children
return #private.children
end,
__gc = function (self)
node_private[self] = nil
end
}
-- public interface
local Node <const> = setmetatable({
create = function (tag, attributes, children)
-- XXX: consider validating tag more strictly?
if type(tag) ~= 'string' or string.len(tag) == 0 then
error("Node tag must be a non-empty string")
end
if type(attributes) ~= 'table' then
error("Node attributes must be a table")
end
for name, value in pairs(attributes) do
if type(name) ~= 'string' or type(value) ~= 'string' then
error("Node attribute names and values must be strings")
end
if #name == 0 then
error("Node attribute key must be a non-empty string")
end
end
local children <const> = children or {}
if type(children) ~= 'table' then
error("Node children must be nil or a table")
end
for _, child in ipairs(children) do
if type(child) == 'string' then
if string.len(child) == 0 then
error("Node child is an empty string")
end
-- XXX: can't use Node.is() here since it hasn't been defined yet
elseif getmetatable(child) == node_metatable then
-- nothing to check
elseif Comment.is(child) then
-- nothing to check
else
error("Node children must be Comment instances, Node instances, or non-empty strings")
end
end
local instance <const> = {}
node_private[instance] = {
tag=tag,
attributes=attributes,
children=children,
frozen=false
}
return setmetatable(instance, node_internal_metatable)
end,
is = function (value)
return (getmetatable(value) == node_metatable)
end
}, {
__call = function (self, ...)
return self.create(...)
end
})
--[[
Document
--]]
local document_metatable <const> = {}
local document_private <const> = setmetatable({}, {__mode='k'})
-- implementation
local document_internal_metatable <const> = {
__name = 'Document',
__metatable = document_metatable,
__index = function (self, key)
local private <const> = assert(document_private[self], "Document instance not recognized: " .. tostring(self))
-- root node
if key == 'root' then
return private.root
-- invalid key
else
error("Document invalid key: " .. key)
end
end,
__newindex = function (self, key, value)
local private <const> = assert(document_private[self], "Document instance not recognized: " .. tostring(self))
-- root node
if key == 'root' then
if not Node.is(value) then
error("Document root must be a Node instance")
end
private.root = value
-- invalid key
else
error("Document invalid key: " .. key)
end
end,
__gc = function (self)
document_private[self] = nil
end
}
-- public interface
local Document <const> = setmetatable({
create = function (data)
local root <const> = data['root']
if not Node.is(root) then
error("Document root must be a Node instance")
end
local instance <const> = {}
document_private[instance] = {
root=root
}
return setmetatable(instance, document_internal_metatable)
end,
is = function (value)
return (getmetatable(value) == document_metatable)
end
}, {
__call = function (self, ...)
return self.create(...)
end
})
--[[
Generic Load
--]]
-- https://www.w3.org/TR/2011/WD-html5-20110525/syntax.html#attributes-0
local function node_parse_attributes(value)
-- corner case: empty value
if string.len(value) == 0 then
return {}
end
-- parse attribute value
local value_offset = 1
local attributes <const> = {}
while value_offset <= string.len(value) do
-- optional whitespace
local start_index, end_index = string.find(value, '^%s+', value_offset)
if start_index and end_index then
assert(start_index == value_offset)
value_offset = end_index + 1
end
-- required attribute name
-- https://stackoverflow.com/a/53563849
local start_index, end_index = string.find(value, '^[a-zA-Z-_]+', value_offset)
if not start_index or not end_index then
return nil, "invalid attribute data"
end
local name <const> = string.sub(value, start_index, end_index)
value_offset = end_index + 1
-- optional attribute value
-- note: you can have spaces between the name and equal sign (XXX but we may not want to support this)
local start_index, end_index = string.find(value, '^=%s*[\'"]', value_offset)
if start_index and end_index then
assert(start_index == value_offset)
value_offset = end_index + 1
local outer_quote <const> = string.sub(value, end_index, end_index)
assert(outer_quote == '"' or outer_quote == "'")
local value_pattern <const> = '^[^' .. outer_quote .. ']*' .. outer_quote
start_index, end_index = string.find(value, value_pattern, value_offset)
if not start_index or not end_index then
return nil, "attribute value with mis-matched quotes"
end
assert(start_index == value_offset)
value_offset = end_index + 1
local value <const> = string.sub(value, start_index, end_index - 1)
attributes[name] = value
goto next_attribute
else
attributes[name] = ''
goto next_attribute
end
-- unexpected characters
do
return nil, "unexpected character"
end
::next_attribute::
end
return attributes
end
local function document_load_string(value, settings)
assert(type(value) == 'string', "document_dump_string value must be a string")
assert(type(settings) == 'table', "document_dump_string settings must be a table")
local header_lines <const> = settings['header_lines'] or {}
local indent <const> = settings['indent'] or ' '
local tags <const> = settings['tags'] or {}
assert(type(header_lines) == 'table', "header_lines setting must be a table")
assert(type(indent) == 'string', "indent setting must be a string")
assert(type(tags) == 'table', "tags setting must be a table")
-- XXX: refactor all of this
local value_offset = 1
local stack <const> = {}
local root_nodes <const> = {}
while value_offset <= string.len(value) do
-- whitespace
local start_index, end_index = string.find(value, '^%s+', value_offset)
if start_index and end_index then
assert(start_index == value_offset)
value_offset = end_index + 1
goto next_token
end
-- text
local start_index, end_index = string.find(value, '^[^<>]+', value_offset)
if start_index and end_index then
assert(start_index == value_offset)
value_offset = end_index + 1
local next_text_data <const> = assert(string.match(
string.sub(value, start_index, end_index),
'^%s*(.-)%s*$'
))
-- corner case: we only captured whitespace
-- XXX: we never hit this when explicitly skipping leading whitespace above
if string.len(next_text_data) == 0 then
goto next_token
end
if #stack > 0 then
local current_node <const> = stack[#stack]
current_node:insert_child(next_text_data)
else
table.insert(root_nodes, next_text_data)
end
goto next_token
end
-- comment
local start_index, end_index = string.find(value, '^<!%-%-', value_offset)
if start_index and end_index then
assert(start_index == value_offset)
local comment_data_start_index <const> = end_index + 1
value_offset = end_index + 1
local start_index, end_index = string.find(value, '%-%->', value_offset)
if not start_index or not end_index then
return nil, "failed to find closing comment tag at offset: " .. tostring(value_offset)
end
local comment_data_end_index <const> = start_index - 1
value_offset = end_index + 1
local comment_data <const> = string.sub(value, comment_data_start_index, comment_data_end_index)
local comment_data_trimmed <const> = assert(string.match(
comment_data,
'^%s*(.-)%s*$'
))
local comment <const> = Comment(comment_data_trimmed)
if #stack > 0 then
local current_node <const> = stack[#stack]
current_node:insert_child(comment)
else
table.insert(root_nodes, comment)
end
goto next_token
end
-- next element (document type, comment, tag)
local start_index, end_index = string.find(value, '^<[^>]*>', value_offset)
if start_index and end_index then
assert(start_index == value_offset)
value_offset = end_index + 1
local next_tag_data <const> = string.sub(value, start_index, end_index)
-- XXX: special tag
local special_data <const> = string.match(next_tag_data, '^<!(.-)>$')
if special_data then
-- print("SPECIAL TAG:", special_data) -- DEBUG
goto next_token
end
-- XXX: another special tag
local extra_data <const> = string.match(next_tag_data, '^<%?(.-)%?>$')
if extra_data then
-- print("EXTRA TAG:", extra_data) -- DEBUG
goto next_token
end
-- closing tag
local closing_tag <const> = string.match(next_tag_data, '^</(.-)>$')
if closing_tag then
if #stack == 0 then
return nil, "found naked closing tag at offset: " .. tostring(value_offset)
end
if string.len(closing_tag) == 0 then
return nil, "found empty closing tag at offset: " .. tostring(value_offset)
end
local current_node <const> = stack[#stack]
if current_node.tag ~= closing_tag then
return nil, "found mis-matched closing tag at offset: " .. tostring(value_offset)
end
local current_node <const> = table.remove(stack)
if #stack == 0 then
table.insert(root_nodes, current_node)
end
goto next_token
end
-- opening tag
local opening_tag <const>, opening_attributes <const> = string.match(next_tag_data, '^<(%S+)(.-)>$')
if opening_tag then
local attributes <const>, message = node_parse_attributes(opening_attributes)
if message then
return nil, "invalid tag attributes: " .. message
end
local node <const> = Node(opening_tag, attributes, {})
if #stack > 0 then
local current_node <const> = stack[#stack]
current_node:insert_child(node)
end
local tag_settings = tags[node.tag] or {}
local tag_is_leaf = tag_settings['leaf'] or false
local tag_is_text = tag_settings['text'] or false
-- text tags contain arbitrary text and we should look for the next
-- matching closing tag without parsing anything in between
if tag_is_text then
local start_index, end_index = string.find(value, '</' .. node.tag .. '>', value_offset)
if not start_index or not end_index then
return nil, "missing closing tag for text element: " .. node.tag
end
-- extract text content
-- XXX: note that we should detect how much leading whitespace to strip
-- from the beginning of every line of text content and remove it so
-- when we dump the node the output will match the original content
local text_content <const> = string.sub(value, value_offset, start_index - 1)
value_offset = end_index + 1
if string.len(text_content) > 0 then
node:insert_child(text_content)
end
-- text elements are always leaves so continue processing the next token
goto next_token
end
if not tag_is_leaf then
table.insert(stack, node)
end
goto next_token
end
return nil, "invalid tag at offset: " .. tostring(value_offset)
end
-- unexpected character in value
-- note: we need to create a scope here to avoid leaking locals into the
-- parent block which would prevent us from jumping to the next_token label
do
return nil, "unexpected character at offset: " .. tostring(value_offset)
end
::next_token::
end
-- DEBUG DEBUG
-- for index, node in ipairs(stack) do
-- print("STACK[", index, "]:", node.tag)
-- end
-- for _, node in ipairs(root_nodes) do
-- print("ROOT NODE:", node.tag)
-- end
-- XXX
if #stack > 0 then
return nil, "mis-matched opening and closing tags"
end
if #root_nodes == 0 then
return nil, "no top-level elements found"
end
-- taking last node from root nodes if there is more than one
local root_node <const> = root_nodes[#root_nodes]
return Document{root=root_node}
end
--[[
Generic Dump
--]]
local function node_dump_attributes(node)
assert(Node.is(node), "value must be a Node instance")
local names <const> = {}
for name, _ in pairs(node.attributes) do
assert(type(name) == 'string', "node attribute name must be a string")
table.insert(names, name)
end
-- serialize attributes in a consistent order
table.sort(names)
local parts <const> = {}
for _, name in ipairs(names) do
local value <const> = node.attributes[name]
assert(type(value) == 'string', "node attribute value must be a string")
-- TODO: handle naked attributes with no value
table.insert(parts, name .. '="' .. value .. '"')
end
return table.concat(parts, ' ')
end
local function document_dump_string(document, settings)
if not Document.is(document) then
return nil, "value must be a Document instance"
end
assert(type(settings) == 'table', "document_dump_string settings must be a table")
local header_lines <const> = settings['header_lines'] or {}
local indent <const> = settings['indent'] or ' '
local tags <const> = settings['tags'] or {}
assert(type(header_lines) == 'table', "header_lines setting must be a table")
assert(type(indent) == 'string', "indent setting must be a string")
assert(type(tags) == 'table', "tags setting must be a table")
-- XXX: serialize by lines for performance
local lines <const> = {}
for _, line in ipairs(header_lines) do
table.insert(lines, line)
end
-- XXX: initial stack used as FIFO with current node on top
local stack <const> = {{level=0, node=document.root, children=nil}}
-- XXX: serialization loop
while #stack > 0 do
local entry <const> = stack[#stack]
local node <const> = entry.node
if type(node) == 'string' then
-- XXX: refactor this out somewhere
-- split text content by newlines and indent each newline to the appropriate level
for line in string.gmatch(node, "([^\n]+)") do
table.insert(lines, string.rep(indent, entry.level) .. line)
end
table.remove(stack)
goto next_node
end
if Comment.is(node) then
table.insert(lines, string.rep(indent, entry.level) .. '<!--')
-- XXX: refactor this out somewhere
-- split text content by newlines and indent each newline to the appropriate level
for line in string.gmatch(node.content, "([^\n]+)") do
table.insert(lines, string.rep(indent, entry.level) .. line)
end
table.insert(lines, string.rep(indent, entry.level) .. '-->')
table.remove(stack)
goto next_node
end
local tag_settings <const> = tags[node.tag] or {}
local tag_is_leaf <const> = tag_settings['leaf'] or false
if not entry.children then
local parts <const> = {
string.rep(indent, entry.level),
'<',
node.tag,
'>'
}
local attributes <const> = node_dump_attributes(node)
if #attributes > 0 then
table.insert(parts, #parts, ' ' .. attributes)
end
table.insert(lines, table.concat(parts, ''))
if tag_is_leaf then
if #node.children > 0 then
return nil, "leaf element '" .. node.tag .. "' has children"
end
table.remove(stack)
else
entry.children = node.children
end
goto next_node
end
if #entry.children == 0 then
table.insert(lines, string.rep(indent, entry.level) .. '</' .. node.tag .. '>')
table.remove(stack)
goto next_node
end
-- retrieve the next child to serialize in array order
local child <const> = table.remove(entry.children, 1)
table.insert(stack, {level=(entry.level + 1), node=child})
::next_node::
end
return table.concat(lines, '\n')
end
--[[
HTML5
--]]
local HTML5 <const> = {}
local HTML5_SETTINGS <const> = {
header_lines={'<!DOCTYPE html>'},
indent=' ',
-- https://html.spec.whatwg.org/multipage/syntax.html#elements-2
tags={
['area']={leaf=true},
['base']={leaf=true},
['br']={leaf=true},
['col']={leaf=true},
['embed']={leaf=true},
['hr']={leaf=true},
['img']={leaf=true},
['input']={leaf=true},
['link']={leaf=true},
['meta']={leaf=true},
['source']={leaf=true},
['track']={leaf=true},
['wbr']={leaf=true},
-- raw text elements
['script']={text=true},
['style']={text=true},
-- escapable raw text elements
-- TODO: handle the escapable part of these?
['textarea']={text=true},
['title']={text=true},
}
}
function HTML5.load_string(value)
return assert(document_load_string(value, HTML5_SETTINGS))
end
function HTML5.dump_string(document)
return assert(document_dump_string(document, HTML5_SETTINGS))
end
--[[
SVG
--]]
local SVG <const> = {}
local SVG_SETTINGS <const> = {
header_lines={
'<?xml version="1.0" encoding="UTF-8"?>'
-- XXX: does SVG need a doctype?
-- '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'
},
indent=' ',
tags={}
}
function SVG.load_string(value)
return assert(document_load_string(value, SVG_SETTINGS))
end
function SVG.dump_string(document)
return assert(document_dump_string(document, SVG_SETTINGS))
end
--[[
Module Interface
--]]
local module = {
Comment=Comment,
Node=Node,
Document=Document,
HTML5=HTML5,
SVG=SVG
}
if os.getenv('LUA_ELEMENTTREE_LEAK_INTERNALS') == "TRUE" then
-- leak internal variables and methods in order to unit test them from outside
-- of this module but at least we can use an obvious environment variable
-- and issue a warning to prevent someone from relying on this
warn("lua-elementtree: LUA_ELEMENTTREE_LEAK_INTERNALS is set so module internals are available")
-- stating the obvious but these are not part of the public interface
module['foo'] = 'bar'
end
return module