/
HTML.pm6
670 lines (551 loc) · 19.1 KB
/
HTML.pm6
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
unit module DOM::Tiny::HTML;
use v6;
use DOM::Tiny::Entities;
grammar XMLTokenizer {
token TOP { <ml-token>* }
token ml-token { <markup> }
proto token markup { * }
token markup:sym<doctype> { :i '<!DOCTYPE' <.ws> <doctype> <.ws> '>' }
token markup:sym<comment> { '<!--' $<comment> = [ .*? ] '--' <.ws> '>' }
token markup:sym<cdata> { '<![CDATA[' $<cdata> = [ .*? ] ']]>' }
token markup:sym<pi> { '<?' $<pi> = [ .*? ] '?>' }
token markup:sym<tag> {
'<' <.ws> <end-mark>? <.ws> <tag-name> <.ws>
[ <attr> <.ws> ]* <.ws> <empty-tag-mark>? '>'
}
token markup:sym<text> { <-[ < ]>+ }
rule doctype {
<root-element>
[ <pub-priv>? <external-id> +% <.ws> ]?
[ '[' <internal-subset> ']' ]?
}
token root-element { \w+ }
token pub-priv { \w+ }
token external-id { '"' <-["]>* '"' | "'" <-[']>* "'" }
token doctype-uri { <-[ \] ]>+ }
token internal-subset { <-[ \] ]>+ }
token end-mark { '/' }
token empty-tag-mark { '/' }
token tag-name { <-[ < > \s / ]>+ }
rule attr {
|| <attr-key> [ '=' <attr-value> ]?
|| <attr-broken> [ '=' <attr-value> ]?
}
token attr-key { <-[ < > = \s / ]>+ }
token attr-value {
| [ '"' $<raw-value> = [ .*? ] '"' ]
| [ "'" $<raw-value> = [ .*? ] "'" ]
| [ $<raw-value> = <-[ > \s ]>* ]
}
token attr-broken {
[
| <-[ \s < > = / ]>
| '/' <!before <.ws> '>' >
]+
}
}
grammar HTMLTokenizer is XMLTokenizer {
token tag-name {
<!before <.raw-tag> | <.rcdata-tag> > <-[ < > \s / ]>+
}
token markup:sym<rcdata> { :i
'<' <.ws> $<start> = <rcdata-tag> <.ws>
[ <attr> <.ws> ]* <.ws> '>'
{ $ = $<start> } # Why does this fix the regex?
$<rcdata-text> = [ .*? ]
[
| '<' <.ws> '/' <.ws> "$<start>" <.ws> '>'
| $
]
}
token markup:sym<raw> { :i
'<' <.ws> $<start> = <raw-tag> <.ws>
[ <attr> <.ws> ]* <.ws> '>'
{ $ = $<start> } # Why does this fix the regex?
$<raw-text> = [ .*? ]
[
| '<' <.ws> '/' <.ws> "$<start>" <.ws> '>'
| $
]
}
token rcdata-tag { :i [ title | textarea ] }
token raw-tag { :i [ script | style ] }
token ml-token { <markup> || <runaway-lt> }
token runaway-lt { '<' }
}
# HTML elements that only contain raw text
my %RAW = set <script style>;
# HTML elements that only contain raw text and entities
my %RCDATA = set <title textarea>;
# HTML elements with optional end tags
my %END = body => 'head', optgroup => 'optgroup', option => 'option';
# HTML elements that break paragraphs
%END{$_} = 'p' for <
address article aside blockquote dir div dl fieldset footer form h1 h2
h3 h4 h5 h6 header hr main menu nav ol p pre section table ul
>;
# HTML table elements with optional end tags
my %TABLE = set <colgroup tbody td tfoot th thead tr>;
# HTML elements with optional end tags and scoping rules
my %CLOSE = li => ((set <li>), (set <ul ol>)),
tr => ((set <tr>), (set <table>));
%CLOSE{$_} = %TABLE, (set <table>) for <colgroup tbody tfoot thead>;
%CLOSE{$_} = (set <dd dt>), (set <dl>) for <dd dt>;
%CLOSE{$_} = (set <rp rt>), (set <ruby>) for <rp rt>;
%CLOSE{$_} = (set <th td>), (set <table>) for <td th>;
# HTML elements without end tags
my %EMPTY = set <
area base br col embed hr img input keygen link menuitem meta param
source track wbr
>;
# HTML elements categorized as phrasing content (and obsolete inline elements)
my @PHRASING = <
a abbr area audio b bdi bdo br button canvas cite code data datalist
del dfn em embed i iframe img input ins kbd keygen label link map mark
math meta meter noscript object output picture progress q ruby s samp
script select slot small span strong sub sup svg template textarea time u
var video wbr
>;
my @OBSOLETE = < acronym applet basefont big font strike tt >;
my %PHRASING = set @OBSOLETE, @PHRASING;
# HTML elements that don't get their self-closing flag acknowledged
my %BLOCK = set <
a address applet article aside b big blockquote body button caption
center code col colgroup dd details dialog dir div dl dt em fieldset
figcaption figure font footer form frameset h1 h2 h3 h4 h5 h6 head
header hgroup html i iframe li listing main marquee menu nav nobr
noembed noframes noscript object ol optgroup option p plaintext pre rp
rt s script section select small strike strong style summary table
tbody td template textarea tfoot th thead title tr tt u ul xmp
>;
my class Runaway { }
class Node is export {
method render(:$xml) { ... }
method Str { self.render }
}
class Root { ... }
class Tag { ... }
role HasChildren { ... }
class DocumentNode is export is Node {
has HasChildren $.parent is rw;
method root(DocumentNode:D:) {
given $!parent {
when not .defined { Nil }
when Root { $!parent }
default { $!parent.root }
}
}
method ancestor-nodes(DocumentNode:D: Bool :$root = False, Bool :$context = False) {
return () if $context && $*TREE-CONTEXT && self === $*TREE-CONTEXT;
my $parent = $!parent;
(gather repeat {
take $parent if $parent ~~ DocumentNode || $root;
last if $context && $parent === $*TREE-CONTEXT;
} while $parent ~~ DocumentNode &&
?($parent = $parent.parent)).cache;
}
method trimmable(DocumentNode:D:) returns Bool:D {
$.parent.trimmable
}
method siblings(DocumentNode:D: Bool :$tags-only = False, Bool :$including-self = True) {
my $siblings = $!parent.child-nodes(:$tags-only);
$siblings.=grep({ $_ !=== self }).=cache unless $including-self;
|$siblings
}
method split-siblings(DocumentNode:D: Bool :$tags-only) {
my @us = $!parent.child-nodes;
my $pos = @us.first({ $_ === self }, :k);
my %result = before => @us[0 .. $pos - 1],
after => @us[$pos + 1 .. *];
if $tags-only {
%result<before> .= grep(Tag) .= cache;
%result<after> .= grep(Tag) .= cache;
}
%result;
}
}
class Text { ... }
role TextNode { ... }
role HasChildren is export {
has DocumentNode @.children is rw;
method descendant-nodes(HasChildren:D: Bool :$tags-only = False) {
(flat self.child-nodes(:$tags-only).map(-> $node {
if $node.WHAT ~~ Tag {
($node, $node.descendant-nodes(:$tags-only))
}
else {
$node
}
})).cache;
}
method child-nodes(HasChildren:D: Bool :$tags-only = False) {
if $tags-only {
@!children.grep(Tag).cache;
}
else {
@!children;
}
}
multi method content(HasChildren:D:) {
self.render-children;
}
multi method content(HasChildren:D: Str:D $html) {
my $xml;
my $tree = DOM::Tiny::HTML::_parse($html, :$xml);
@!children = $tree.children;
}
multi method content(HasChildren:D: Node:D @children) {
@!children = @children;
}
method !read-text(:$recurse, :$trim is copy) {
$trim &&= self.trimmable;
my $i = 0;
MERGE: while @!children[$i + 1] -> $next {
if @!children[$i] ~~ Text && $next ~~ Text {
splice @!children, $i, 2, Text.new(
text => @!children[$i].text ~ $next.text,
parent => self,
);
next MERGE;
}
$i++;
}
my @nodes = $recurse ?? self.descendant-nodes !! self.child-nodes;
my $previous-chunk = '';
[~] gather for @nodes -> $node {
next unless $node ~~ TextNode;
my $chunk = $node.text(:$trim);
next if $chunk.chars == 0;
if $previous-chunk ~~ / \S $ / && $chunk ~~ /^ <-[ . ! ? , ; : \s ]>+ / {
take " $chunk";
}
else {
take $chunk;
}
$previous-chunk = $chunk;
}
}
method text(HasChildren:D: Bool :$recurse = False, Bool :$trim = False) is rw {
my $tree = self;
Proxy.new(
FETCH => method () { $tree!read-text(:$recurse, :$trim) },
STORE => method ($t) {
@!children = Text.new(text => $t);
},
);
}
method render-children(:$xml) { [~] @!children.map({ .render(:$xml) }); }
}
role TextNode is export {
has Str $.text is rw = '';
method !squished-text { $!text.trim.subst(/\s+/, ' ', :global) }
multi method text(TextNode:D: Bool :$trim is copy = False) {
$trim &&= self.trimmable;
$trim ?? self!squished-text !! $!text
}
multi method text(TextNode:D:) is rw { return-rw $!text }
multi method content(TextNode:D:) { $!text }
multi method content(TextNode:D: Str:D $text) { $!text = $text }
multi method content(TextNode:D: Node:D @children) { $!text = [~] @children».Str }
}
class CDATA is export is DocumentNode does TextNode {
method trimmable(CDATA:D:) { False }
method render(:$xml) { '<![CDATA[' ~ $.text ~ ']]>' }
}
class Comment is export is DocumentNode {
has Str $.comment is rw = '';
multi method content(Comment:D:) { $!comment }
multi method content(Comment:D: Str:D $text) { $!comment = $text }
multi method content(Comment:D: Node:D @children) { $!comment = [~] @children».Str }
method render(:$xml) { '<!--' ~ $!comment ~ '-->' }
}
class Doctype is export is DocumentNode {
has Str $.doctype is rw = '';
multi method content(Doctype:D:) { $!doctype }
multi method content(Doctype:D: Str:D $text) { $!doctype = $text }
multi method content(Doctype:D: Node:D @children) { $!doctype = [~] @children».Str }
method render(:$xml) { '<!DOCTYPE ' ~ $!doctype ~ '>' }
}
class PI is export is DocumentNode {
has Str $.pi is rw = '';
multi method content(PI:D:) { $!pi }
multi method content(PI:D: Str:D $text) { $!pi = $text }
multi method content(PI:D: Node:D @children) { $!pi = [~] @children».Str }
method render(:$xml) { '<?' ~ $!pi ~ '?>' }
}
class Raw is export is DocumentNode does TextNode {
method trimmable() { False }
method render(:$xml) { $!text }
}
class Tag is export is DocumentNode does HasChildren {
has Str $.tag is rw is required;
has %.attr is rw;
has Bool $.rcdata is rw = False;
method trimmable(Tag:D:) returns Bool {
return False if $.rcdata;
return False if $!tag eq 'pre';
$.parent.trimmable;
}
method render(:$xml) {
# Start tag
my $result = "<$!tag";
# Attributes
$result ~= [~] gather for %!attr.sort».kv -> ($key, $value) {
with $value {
take qq{ $key="} ~ html-escape($value) ~ '"';
}
elsif $xml {
take qq{ $key="$key"};
}
else {
take " $key";
}
}
# No children
return $xml ?? "$result />"
!! %EMPTY{$!tag} ?? "$result>"
!! "$result></$!tag>"
unless @!children.elems > 0;
# Children
$result ~= '>' ~ self.render-children(:$xml);
# End tag
"$result\</$!tag>";
}
}
class Text is export is DocumentNode does TextNode {
method render(:$xml) {
$.parent ~~ Tag
&& $.parent.rcdata ?? $!text !! html-escape $!text;
}
}
class Root is export is Node does HasChildren {
method trimmable(Root:D:) { True }
method ancestor-nodes(Root:D:) { () }
method render(:$xml) { self.render-children(:$xml) }
}
class TreeMaker {
has Bool $.xml = False;
my sub _end($end, $xml, $current is rw) {
# Search stack for start tag
my $next = $current;
repeat {
# Ignore useless end tag
return if $next ~~ Root;
# Right tag
return $current = $next.parent if $next.tag eq $end;
# Phrasing content can only cross phrasing content
return if !$xml && %PHRASING{$end} && !%PHRASING{$next.tag};
} while ?($next = $next.parent);
# The above loop runs not without this? WTH?
return;
}
my sub _start($start, %attr, $xml, $current is rw) {
# Autoclose optional HTML elements
if !$xml && $current !~~ Root {
if %END{$start} -> $end {
_end($end, False, $current);
}
elsif %CLOSE{$start} -> $close {
my ($allowed, $scope) = |$close;
# Close allowed parent elements in scope
my $parent = $current;
while $parent !~~ Root && $scope ∌ $parent.tag {
_end($parent.tag, False, $current) if $allowed ∋ $parent.tag;
$parent = $parent.parent;
}
}
}
# New tag
$current.children.push: my $new = Tag.new(
tag => $start,
attr => %attr,
parent => $current,
);
$current = $new;
}
method TOP($/) {
my $current = my $tree = Root.new;
my $xml = $.xml;
for $<ml-token>».made -> %markup {
given %markup<type> {
when Tag {
if %markup<rcdata> {
my $start = %markup<tag>;
my %attr = %markup<attr>;
my $text = %markup<rcdata>;
_start($start, %attr, $xml, $current);
$current.rcdata = True;
$current.children = Text.new(
text => html-unescape($text),
parent => $current,
);
_end($start, $xml, $current);
}
# End
elsif %markup<end> {
_end(%markup<tag>, $xml, $current);
}
# Start
else {
my $start = %markup<tag>;
my %attr = %markup<attr>;
my $closing = %markup<empty>;
# "image" is an alias for "img"
$start = 'img' if !$xml && $start eq 'image';
_start($start, %attr, $xml, $current);
# Element without end tag (self-closing)
_end($start, $xml, $current)
if (!$xml && %EMPTY ∋ $start)
|| (($xml || %BLOCK ∌ $start) && $closing);
}
}
when Raw {
my $raw-tag = Tag.new(
tag => %markup<tag>,
attr => %markup<attr>,
parent => $current,
);
$raw-tag.children.push: Raw.new(
text => %markup<raw>,
parent => $raw-tag,
);
$current.children.push: $raw-tag;
}
when Doctype {
$current.children.push: Doctype.new(
doctype => %markup<doctype>,
parent => $current,
);
}
when Comment {
$current.children.push: Comment.new(
comment => %markup<comment>,
parent => $current,
);
}
when CDATA {
$current.children.push: CDATA.new(
text => %markup<cdata>,
parent => $current,
);
}
when Text {
$current.children.push: Text.new(
text => html-unescape(%markup<text>),
parent => $current,
);
}
when PI {
$current.children.push: PI.new(
pi => %markup<pi>,
parent => $current,
);
}
when Runaway {
$current.children.push: Text.new(
text => '<',
parent => $current,
);
}
}
}
make $tree;
}
method ml-token($/) {
if $<markup> {
make $<markup>.made;
}
else {
make $<runaway-lt>.made;
}
}
method markup:sym<text>($/) {
make {
type => Text,
text => ~$/,
}
}
method markup:sym<raw>($/) {
make {
type => Raw,
tag => (~$<start>).lc,
attr => Hash.new($<attr>».made.grep(*.defined)),
raw => ~$<raw-text>,
};
}
method markup:sym<rcdata>($/) {
make {
type => Tag,
tag => (~$<start>).lc,
attr => Hash.new($<attr>».made.grep(*.defined)),
rcdata => ~$<rcdata-text>,
};
}
method markup:sym<tag>($/) {
make {
type => Tag,
end => ?$<end-mark>,
tag => $.xml ?? ~$<tag-name> !! (~$<tag-name>).lc,
attr => Hash.new($<attr>».made.grep(*.defined)),
empty => ?$<empty-tag-mark>,
}
}
method markup:sym<doctype>($/) {
make {
type => Doctype,
doctype => ~$<doctype>,
}
}
method markup:sym<comment>($/) {
make {
type => Comment,
comment => ~$<comment>,
}
}
method markup:sym<cdata>($/) {
make {
type => CDATA,
cdata => ~$<cdata>,
}
}
method markup:sym<pi>($/) {
make {
type => PI,
pi => ~$<pi>,
}
}
method runaway-lt($/) {
make { type => Runaway }
}
method attr($/) {
if $<attr-broken> {
if not $<attr-broken>.made {
make ();
}
elsif $<attr-value> {
make $<attr-broken>.made => $<attr-value>.made;
}
else {
make $<attr-broken>.made => Nil;
}
}
elsif $<attr-value> {
make $<attr-key>.made => $<attr-value>.made;
}
else {
make $<attr-key>.made => Nil;
}
}
# TODO It would be nicer if we had a case-insensitive hash
method attr-key($/) { make $!xml ?? ~$/ !! (~$/).lc }
method attr-value($/) { make html-unescape ~$<raw-value> }
method attr-broken($/) {
make (~$/).comb(/ <-[ < > = \s / ]>+ /).first(*.chars > 0)
}
}
our sub _parse($html, Bool :$xml! is rw) {
$xml = ?($html ~~ /^ \s* '<?xml' /) without $xml;
my $grammar = $xml ?? XMLTokenizer !! HTMLTokenizer;
my $actions = DOM::Tiny::HTML::TreeMaker.new(:$xml);
$grammar.parse($html, :$actions).made;
}