, [undef, $token->{tag_name}]);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$insert->($el);
push @{$self->{open_elements}}, [$el, $token->{tag_name}];
}
$token = $self->_get_next_token;
return;
}
} elsif ($token->{type} eq 'end tag') {
if ($token->{tag_name} eq 'body') {
if (@{$self->{open_elements}} > 1 and
$self->{open_elements}->[1]->[1] eq 'body') {
for (@{$self->{open_elements}}) {
unless ({
dd => 1, dt => 1, li => 1, p => 1, td => 1,
th => 1, tr => 1, body => 1, html => 1,
tbody => 1, tfoot => 1, thead => 1,
}->{$_->[1]}) {
$self->{parse_error}-> (type => 'not closed:'.$_->[1]);
}
}
$self->{insertion_mode} = 'after body';
$token = $self->_get_next_token;
return;
} else {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
return;
}
} elsif ($token->{tag_name} eq 'html') {
if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
## ISSUE: There is an issue in the spec.
if ($self->{open_elements}->[-1]->[1] ne 'body') {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
}
$self->{insertion_mode} = 'after body';
## reprocess
return;
} else {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
return;
}
} elsif ({
address => 1, blockquote => 1, center => 1, dir => 1,
div => 1, dl => 1, fieldset => 1, listing => 1,
menu => 1, ol => 1, pre => 1, ul => 1,
p => 1,
dd => 1, dt => 1, li => 1,
button => 1, marquee => 1, object => 1,
}->{$token->{tag_name}}) {
## has an element in scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq $token->{tag_name}) {
## generate implied end tags
if ({
dd => ($token->{tag_name} ne 'dd'),
dt => ($token->{tag_name} ne 'dt'),
li => ($token->{tag_name} ne 'li'),
p => ($token->{tag_name} ne 'p'),
td => 1, th => 1, tr => 1,
tbody => 1, tfoot=> 1, thead => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
unshift @{$self->{token}}, $token;
$token = {type => 'end tag',
tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
return;
}
$i = $_;
last INSCOPE unless $token->{tag_name} eq 'p';
} elsif ({
table => 1, caption => 1, td => 1, th => 1,
button => 1, marquee => 1, object => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
if (defined $i) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
} else {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
}
}
if (defined $i) {
splice @{$self->{open_elements}}, $i;
} elsif ($token->{tag_name} eq 'p') {
## As if , then reprocess the current token
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, 'p']);
$insert->($el);
}
$clear_up_to_marker->()
if {
button => 1, marquee => 1, object => 1,
}->{$token->{tag_name}};
$token = $self->_get_next_token;
return;
} elsif ($token->{tag_name} eq 'form') {
## has an element in scope
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq $token->{tag_name}) {
## generate implied end tags
if ({
dd => 1, dt => 1, li => 1, p => 1,
td => 1, th => 1, tr => 1,
tbody => 1, tfoot=> 1, thead => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
unshift @{$self->{token}}, $token;
$token = {type => 'end tag',
tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
return;
}
last INSCOPE;
} elsif ({
table => 1, caption => 1, td => 1, th => 1,
button => 1, marquee => 1, object => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
pop @{$self->{open_elements}};
} else {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
}
undef $self->{form_element};
$token = $self->_get_next_token;
return;
} elsif ({
h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
}->{$token->{tag_name}}) {
## has an element in scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ({
h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
}->{$node->[1]}) {
## generate implied end tags
if ({
dd => 1, dt => 1, li => 1, p => 1,
td => 1, th => 1, tr => 1,
tbody => 1, tfoot=> 1, thead => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
unshift @{$self->{token}}, $token;
$token = {type => 'end tag',
tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
return;
}
$i = $_;
last INSCOPE;
} elsif ({
table => 1, caption => 1, td => 1, th => 1,
button => 1, marquee => 1, object => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
}
splice @{$self->{open_elements}}, $i if defined $i;
$token = $self->_get_next_token;
return;
} elsif ({
a => 1,
b => 1, big => 1, em => 1, font => 1, i => 1,
nobr => 1, s => 1, small => 1, strile => 1,
strong => 1, tt => 1, u => 1,
}->{$token->{tag_name}}) {
$formatting_end_tag->($token->{tag_name});
return;
} elsif ($token->{tag_name} eq 'br') {
$self->{parse_error}-> (type => 'unmatched end tag:br');
## As if
$reconstruct_active_formatting_elements->($insert_to_current);
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, 'br']);
$insert->($el);
## Ignore the token.
$token = $self->_get_next_token;
return;
} elsif ({
caption => 1, col => 1, colgroup => 1, frame => 1,
frameset => 1, head => 1, option => 1, optgroup => 1,
tbody => 1, td => 1, tfoot => 1, th => 1,
thead => 1, tr => 1,
area => 1, basefont => 1, bgsound => 1,
embed => 1, hr => 1, iframe => 1, image => 1,
img => 1, input => 1, isindex => 1, noembed => 1,
noframes => 1, param => 1, select => 1, spacer => 1,
table => 1, textarea => 1, wbr => 1,
noscript => 0, ## TODO: if scripting is enabled
}->{$token->{tag_name}}) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
return;
## ISSUE: Issue on HTML5 new elements in spec
} else {
## Step 1
my $node_i = -1;
my $node = $self->{open_elements}->[$node_i];
## Step 2
S2: {
if ($node->[1] eq $token->{tag_name}) {
## Step 1
## generate implied end tags
if ({
dd => 1, dt => 1, li => 1, p => 1,
td => 1, th => 1, tr => 1,
tbody => 1, tfoot=> 1, thead => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
unshift @{$self->{token}}, $token;
$token = {type => 'end tag',
tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
return;
}
## Step 2
if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
}
## Step 3
splice @{$self->{open_elements}}, $node_i;
$token = $self->_get_next_token;
last S2;
} else {
## Step 3
if (not $formatting_category->{$node->[1]} and
#not $phrasing_category->{$node->[1]} and
($special_category->{$node->[1]} or
$scoping_category->{$node->[1]})) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
last S2;
}
}
## Step 4
$node_i--;
$node = $self->{open_elements}->[$node_i];
## Step 5;
redo S2;
} # S2
return;
}
}
}; # $in_body
B: {
if ($token->{type} eq 'DOCTYPE') {
$self->{parse_error}-> (type => 'DOCTYPE in the middle');
## Ignore the token
## Stay in the phase
$token = $self->_get_next_token;
redo B;
} elsif ($token->{type} eq 'end-of-file') {
if ($token->{insertion_mode} ne 'trailing end') {
## Generate implied end tags
if ({
dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
tbody => 1, tfoot=> 1, thead => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
unshift @{$self->{token}}, $token;
$token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};
redo B;
}
if (@{$self->{open_elements}} > 2 or
(@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
} elsif (defined $self->{inner_html_node} and
@{$self->{open_elements}} > 1 and
$self->{open_elements}->[1]->[1] ne 'body') {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
}
## ISSUE: There is an issue in the spec.
}
## Stop parsing
last B;
} elsif ($token->{type} eq 'start tag' and
$token->{tag_name} eq 'html') {
if ($self->{insertion_mode} eq 'trailing end') {
## Turn into the main phase
$self->{parse_error}-> (type => 'after html:html');
$self->{insertion_mode} = $previous_insertion_mode;
}
## ISSUE: "aa" is not a parse error.
## ISSUE: "" in fragment is not a parse error.
unless ($token->{first_start_tag}) {
$self->{parse_error}-> (type => 'not first start tag');
}
my $top_el = $self->{open_elements}->[0]->[0];
for my $attr_name (keys %{$token->{attributes}}) {
unless ($top_el->has_attribute_ns (undef, $attr_name)) {
$top_el->set_attribute_ns
(undef, [undef, $attr_name],
$token->{attributes}->{$attr_name}->{value});
}
}
$token = $self->_get_next_token;
redo B;
} elsif ($token->{type} eq 'comment') {
my $comment = $self->{document}->create_comment ($token->{data});
if ($self->{insertion_mode} eq 'trailing end') {
$self->{document}->append_child ($comment);
} elsif ($self->{insertion_mode} eq 'after body') {
$self->{open_elements}->[0]->[0]->append_child ($comment);
} else {
$self->{open_elements}->[-1]->[0]->append_child ($comment);
}
$token = $self->_get_next_token;
redo B;
} elsif ($self->{insertion_mode} eq 'before head') {
if ($token->{type} eq 'character') {
if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
$self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
unless (length $token->{data}) {
$token = $self->_get_next_token;
redo B;
}
}
## As if
$self->{head_element} = $self->{document}->create_element_ns
(q, [undef, 'head']);
$self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
$self->{insertion_mode} = 'in head';
## reprocess
redo B;
} elsif ($token->{type} eq 'start tag') {
my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};
$self->{head_element} = $self->{document}->create_element_ns
(q, [undef, 'head']);
for my $attr_name (keys %{ $attr}) {
$self->{head_element}->set_attribute_ns (undef, [undef, $attr_name],
$attr ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
$self->{insertion_mode} = 'in head';
if ($token->{tag_name} eq 'head') {
$token = $self->_get_next_token;
#} elsif ({
# base => 1, link => 1, meta => 1,
# script => 1, style => 1, title => 1,
# }->{$token->{tag_name}}) {
# ## reprocess
} else {
## reprocess
}
redo B;
} elsif ($token->{type} eq 'end tag') {
if ({
head => 1, body => 1, html => 1,
p => 1, br => 1,
}->{$token->{tag_name}}) {
## As if
$self->{head_element} = $self->{document}->create_element_ns
(q, [undef, 'head']);
$self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
$self->{insertion_mode} = 'in head';
## reprocess
redo B;
} else {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token ## ISSUE: An issue in the spec.
$token = $self->_get_next_token;
redo B;
}
} else {
die "$0: $token->{type}: Unknown type";
}
} elsif ($self->{insertion_mode} eq 'in head' or
$self->{insertion_mode} eq 'in head noscript' or
$self->{insertion_mode} eq 'after head') {
if ($token->{type} eq 'character') {
if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
$self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
unless (length $token->{data}) {
$token = $self->_get_next_token;
redo B;
}
}
#
} elsif ($token->{type} eq 'start tag') {
if ({base => ($self->{insertion_mode} eq 'in head' or
$self->{insertion_mode} eq 'after head'),
link => 1}->{$token->{tag_name}}) {
## NOTE: There is a "as if in head" code clone.
if ($self->{insertion_mode} eq 'after head') {
$self->{parse_error}-> (type => 'after head:'.$token->{tag_name});
push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
}
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, $token->{tag_name}]);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, $token->{tag_name}];
}
pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
pop @{$self->{open_elements}}
if $self->{insertion_mode} eq 'after head';
$token = $self->_get_next_token;
redo B;
} elsif ($token->{tag_name} eq 'meta') {
## NOTE: There is a "as if in head" code clone.
if ($self->{insertion_mode} eq 'after head') {
$self->{parse_error}-> (type => 'after head:'.$token->{tag_name});
push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
}
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, $token->{tag_name}]);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, $token->{tag_name}];
}
pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
unless ($self->{confident}) {
my $charset;
if ($token->{attributes}->{charset}) { ## TODO: And if supported
$charset = $token->{attributes}->{charset}->{value};
}
if ($token->{attributes}->{'http-equiv'}) {
## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
if ($token->{attributes}->{'http-equiv'}->{value}
=~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
[\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
$charset = defined $1 ? $1 : defined $2 ? $2 : $3;
} ## TODO: And if supported
}
## TODO: Change the encoding
}
## TODO: Extracting |charset| from |meta|.
pop @{$self->{open_elements}}
if $self->{insertion_mode} eq 'after head';
$token = $self->_get_next_token;
redo B;
} elsif ($token->{tag_name} eq 'title' and
$self->{insertion_mode} eq 'in head') {
## NOTE: There is a "as if in head" code clone.
if ($self->{insertion_mode} eq 'after head') {
$self->{parse_error}-> (type => 'after head:'.$token->{tag_name});
push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
}
my $parent = defined $self->{head_element} ? $self->{head_element}
: $self->{open_elements}->[-1]->[0];
$parse_rcdata->(RCDATA_CONTENT_MODEL,
sub { $parent->append_child ($_[0]) });
pop @{$self->{open_elements}}
if $self->{insertion_mode} eq 'after head';
redo B;
} elsif ($token->{tag_name} eq 'style') {
## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
## insertion mode 'in head')
## NOTE: There is a "as if in head" code clone.
if ($self->{insertion_mode} eq 'after head') {
$self->{parse_error}-> (type => 'after head:'.$token->{tag_name});
push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
}
$parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
pop @{$self->{open_elements}}
if $self->{insertion_mode} eq 'after head';
redo B;
} elsif ($token->{tag_name} eq 'noscript') {
if ($self->{insertion_mode} eq 'in head') {
## NOTE: and scripting is disalbed
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, $token->{tag_name}]);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, $token->{tag_name}];
}
$self->{insertion_mode} = 'in head noscript';
$token = $self->_get_next_token;
redo B;
} elsif ($self->{insertion_mode} eq 'in head noscript') {
$self->{parse_error}-> (type => 'in noscript:noscript');
## Ignore the token
$token = $self->_get_next_token;
redo B;
} else {
#
}
} elsif ($token->{tag_name} eq 'head' and
$self->{insertion_mode} ne 'after head') {
$self->{parse_error}-> (type => 'in head:head'); # or in head noscript
## Ignore the token
$token = $self->_get_next_token;
redo B;
} elsif ($self->{insertion_mode} ne 'in head noscript' and
$token->{tag_name} eq 'script') {
if ($self->{insertion_mode} eq 'after head') {
$self->{parse_error}-> (type => 'after head:'.$token->{tag_name});
push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
}
## NOTE: There is a "as if in head" code clone.
$script_start_tag->($insert_to_current);
pop @{$self->{open_elements}}
if $self->{insertion_mode} eq 'after head';
redo B;
} elsif ($self->{insertion_mode} eq 'after head' and
$token->{tag_name} eq 'body') {
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, 'body']);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, 'body'];
}
$self->{insertion_mode} = 'in body';
$token = $self->_get_next_token;
redo B;
} elsif ($self->{insertion_mode} eq 'after head' and
$token->{tag_name} eq 'frameset') {
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, 'frameset']);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, 'frameset'];
}
$self->{insertion_mode} = 'in frameset';
$token = $self->_get_next_token;
redo B;
} else {
#
}
} elsif ($token->{type} eq 'end tag') {
if ($self->{insertion_mode} eq 'in head' and
$token->{tag_name} eq 'head') {
pop @{$self->{open_elements}};
$self->{insertion_mode} = 'after head';
$token = $self->_get_next_token;
redo B;
} elsif ($self->{insertion_mode} eq 'in head noscript' and
$token->{tag_name} eq 'noscript') {
pop @{$self->{open_elements}};
$self->{insertion_mode} = 'in head';
$token = $self->_get_next_token;
redo B;
} elsif ($self->{insertion_mode} eq 'in head' and
{
body => 1, html => 1,
p => 1, br => 1,
}->{$token->{tag_name}}) {
#
} elsif ($self->{insertion_mode} eq 'in head noscript' and
{
p => 1, br => 1,
}->{$token->{tag_name}}) {
#
} elsif ($self->{insertion_mode} ne 'after head') {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
} else {
#
}
} else {
#
}
## As if or or
if ($self->{insertion_mode} eq 'in head') {
pop @{$self->{open_elements}};
$self->{insertion_mode} = 'after head';
} elsif ($self->{insertion_mode} eq 'in head noscript') {
pop @{$self->{open_elements}};
$self->{parse_error}-> (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));
$self->{insertion_mode} = 'in head';
} else { # 'after head'
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, 'body']);
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, 'body'];
}
$self->{insertion_mode} = 'in body';
}
## reprocess
redo B;
## ISSUE: An issue in the spec.
} elsif ($self->{insertion_mode} eq 'in body') {
if ($token->{type} eq 'character') {
## NOTE: There is a code clone of "character in body".
$reconstruct_active_formatting_elements->($insert_to_current);
$self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
$token = $self->_get_next_token;
redo B;
} else {
$in_body->($insert_to_current);
redo B;
}
} elsif ($self->{insertion_mode} eq 'in table') {
if ($token->{type} eq 'character') {
## NOTE: There are "character in table" code clones.
if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
$self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
unless (length $token->{data}) {
$token = $self->_get_next_token;
redo B;
}
}
$self->{parse_error}-> (type => 'in table:#character');
## As if in body, but insert into foster parent element
## ISSUE: Spec says that "whenever a node would be inserted
## into the current node" while characters might not be
## result in a new Text node.
$reconstruct_active_formatting_elements->($insert_to_foster);
if ({
table => 1, tbody => 1, tfoot => 1,
thead => 1, tr => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
# MUST
my $foster_parent_element;
my $next_sibling;
my $prev_sibling;
OE: for (reverse 0..$#{$self->{open_elements}}) {
if ($self->{open_elements}->[$_]->[1] eq 'table') {
my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
if (defined $parent and $parent->node_type == 1) {
$foster_parent_element = $parent;
$next_sibling = $self->{open_elements}->[$_]->[0];
$prev_sibling = $next_sibling->previous_sibling;
} else {
$foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
$prev_sibling = $foster_parent_element->last_child;
}
last OE;
}
} # OE
$foster_parent_element = $self->{open_elements}->[0]->[0] and
$prev_sibling = $foster_parent_element->last_child
unless defined $foster_parent_element;
if (defined $prev_sibling and
$prev_sibling->node_type == 3) {
$prev_sibling->manakai_append_text ($token->{data});
} else {
$foster_parent_element->insert_before
($self->{document}->create_text_node ($token->{data}),
$next_sibling);
}
} else {
$self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
}
$token = $self->_get_next_token;
redo B;
} elsif ($token->{type} eq 'start tag') {
if ({
caption => 1,
colgroup => 1,
tbody => 1, tfoot => 1, thead => 1,
}->{$token->{tag_name}}) {
## Clear back to table context
while ($self->{open_elements}->[-1]->[1] ne 'table' and
$self->{open_elements}->[-1]->[1] ne 'html') {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
pop @{$self->{open_elements}};
}
push @$active_formatting_elements, ['#marker', '']
if $token->{tag_name} eq 'caption';
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, $token->{tag_name}]);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, $token->{tag_name}];
}
$self->{insertion_mode} = {
caption => 'in caption',
colgroup => 'in column group',
tbody => 'in table body',
tfoot => 'in table body',
thead => 'in table body',
}->{$token->{tag_name}};
$token = $self->_get_next_token;
redo B;
} elsif ({
col => 1,
td => 1, th => 1, tr => 1,
}->{$token->{tag_name}}) {
## Clear back to table context
while ($self->{open_elements}->[-1]->[1] ne 'table' and
$self->{open_elements}->[-1]->[1] ne 'html') {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
pop @{$self->{open_elements}};
}
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, $token->{tag_name} eq 'col' ? 'colgroup' : 'tbody']);
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, $token->{tag_name} eq 'col' ? 'colgroup' : 'tbody'];
}
$self->{insertion_mode} = $token->{tag_name} eq 'col'
? 'in column group' : 'in table body';
## reprocess
redo B;
} elsif ($token->{tag_name} eq 'table') {
## NOTE: There are code clones for this "table in table"
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
## As if
## have a table element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq 'table') {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:table');
## Ignore tokens
$token = $self->_get_next_token;
redo B;
}
## generate implied end tags
if ({
dd => 1, dt => 1, li => 1, p => 1,
td => 1, th => 1, tr => 1,
tbody => 1, tfoot=> 1, thead => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
unshift @{$self->{token}}, $token; #
$token = {type => 'end tag', tag_name => 'table'};
unshift @{$self->{token}}, $token;
$token = {type => 'end tag',
tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
redo B;
}
if ($self->{open_elements}->[-1]->[1] ne 'table') {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
}
splice @{$self->{open_elements}}, $i;
$self->_reset_insertion_mode;
## reprocess
redo B;
} else {
#
}
} elsif ($token->{type} eq 'end tag') {
if ($token->{tag_name} eq 'table') {
## have a table element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq $token->{tag_name}) {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## generate implied end tags
if ({
dd => 1, dt => 1, li => 1, p => 1,
td => 1, th => 1, tr => 1,
tbody => 1, tfoot=> 1, thead => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
unshift @{$self->{token}}, $token;
$token = {type => 'end tag',
tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
redo B;
}
if ($self->{open_elements}->[-1]->[1] ne 'table') {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
}
splice @{$self->{open_elements}}, $i;
$self->_reset_insertion_mode;
$token = $self->_get_next_token;
redo B;
} elsif ({
body => 1, caption => 1, col => 1, colgroup => 1,
html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,
thead => 1, tr => 1,
}->{$token->{tag_name}}) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
} else {
#
}
} else {
#
}
$self->{parse_error}-> (type => 'in table:'.$token->{tag_name});
$in_body->($insert_to_foster);
redo B;
} elsif ($self->{insertion_mode} eq 'in caption') {
if ($token->{type} eq 'character') {
## NOTE: This is a code clone of "character in body".
$reconstruct_active_formatting_elements->($insert_to_current);
$self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
$token = $self->_get_next_token;
redo B;
} elsif ($token->{type} eq 'start tag') {
if ({
caption => 1, col => 1, colgroup => 1, tbody => 1,
td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
}->{$token->{tag_name}}) {
$self->{parse_error}-> (type => 'not closed:caption');
## As if
## have a table element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq 'caption') {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:caption');
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## generate implied end tags
if ({
dd => 1, dt => 1, li => 1, p => 1,
td => 1, th => 1, tr => 1,
tbody => 1, tfoot=> 1, thead => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
unshift @{$self->{token}}, $token; # >
$token = {type => 'end tag', tag_name => 'caption'};
unshift @{$self->{token}}, $token;
$token = {type => 'end tag',
tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
redo B;
}
if ($self->{open_elements}->[-1]->[1] ne 'caption') {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
}
splice @{$self->{open_elements}}, $i;
$clear_up_to_marker->();
$self->{insertion_mode} = 'in table';
## reprocess
redo B;
} else {
#
}
} elsif ($token->{type} eq 'end tag') {
if ($token->{tag_name} eq 'caption') {
## have a table element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq $token->{tag_name}) {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## generate implied end tags
if ({
dd => 1, dt => 1, li => 1, p => 1,
td => 1, th => 1, tr => 1,
tbody => 1, tfoot=> 1, thead => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
unshift @{$self->{token}}, $token;
$token = {type => 'end tag',
tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
redo B;
}
if ($self->{open_elements}->[-1]->[1] ne 'caption') {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
}
splice @{$self->{open_elements}}, $i;
$clear_up_to_marker->();
$self->{insertion_mode} = 'in table';
$token = $self->_get_next_token;
redo B;
} elsif ($token->{tag_name} eq 'table') {
$self->{parse_error}-> (type => 'not closed:caption');
## As if
## have a table element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq 'caption') {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:caption');
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## generate implied end tags
if ({
dd => 1, dt => 1, li => 1, p => 1,
td => 1, th => 1, tr => 1,
tbody => 1, tfoot=> 1, thead => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
unshift @{$self->{token}}, $token; #
$token = {type => 'end tag', tag_name => 'caption'};
unshift @{$self->{token}}, $token;
$token = {type => 'end tag',
tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
redo B;
}
if ($self->{open_elements}->[-1]->[1] ne 'caption') {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
}
splice @{$self->{open_elements}}, $i;
$clear_up_to_marker->();
$self->{insertion_mode} = 'in table';
## reprocess
redo B;
} elsif ({
body => 1, col => 1, colgroup => 1,
html => 1, tbody => 1, td => 1, tfoot => 1,
th => 1, thead => 1, tr => 1,
}->{$token->{tag_name}}) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
} else {
#
}
} else {
#
}
$in_body->($insert_to_current);
redo B;
} elsif ($self->{insertion_mode} eq 'in column group') {
if ($token->{type} eq 'character') {
if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
$self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
unless (length $token->{data}) {
$token = $self->_get_next_token;
redo B;
}
}
#
} elsif ($token->{type} eq 'start tag') {
if ($token->{tag_name} eq 'col') {
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, $token->{tag_name}]);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, $token->{tag_name}];
}
pop @{$self->{open_elements}};
$token = $self->_get_next_token;
redo B;
} else {
#
}
} elsif ($token->{type} eq 'end tag') {
if ($token->{tag_name} eq 'colgroup') {
if ($self->{open_elements}->[-1]->[1] eq 'html') {
$self->{parse_error}-> (type => 'unmatched end tag:colgroup');
## Ignore the token
$token = $self->_get_next_token;
redo B;
} else {
pop @{$self->{open_elements}}; # colgroup
$self->{insertion_mode} = 'in table';
$token = $self->_get_next_token;
redo B;
}
} elsif ($token->{tag_name} eq 'col') {
$self->{parse_error}-> (type => 'unmatched end tag:col');
## Ignore the token
$token = $self->_get_next_token;
redo B;
} else {
#
}
} else {
#
}
## As if
if ($self->{open_elements}->[-1]->[1] eq 'html') {
$self->{parse_error}-> (type => 'unmatched end tag:colgroup');
## Ignore the token
$token = $self->_get_next_token;
redo B;
} else {
pop @{$self->{open_elements}}; # colgroup
$self->{insertion_mode} = 'in table';
## reprocess
redo B;
}
} elsif ($self->{insertion_mode} eq 'in table body') {
if ($token->{type} eq 'character') {
## NOTE: This is a "character in table" code clone.
if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
$self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
unless (length $token->{data}) {
$token = $self->_get_next_token;
redo B;
}
}
$self->{parse_error}-> (type => 'in table:#character');
## As if in body, but insert into foster parent element
## ISSUE: Spec says that "whenever a node would be inserted
## into the current node" while characters might not be
## result in a new Text node.
$reconstruct_active_formatting_elements->($insert_to_foster);
if ({
table => 1, tbody => 1, tfoot => 1,
thead => 1, tr => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
# MUST
my $foster_parent_element;
my $next_sibling;
my $prev_sibling;
OE: for (reverse 0..$#{$self->{open_elements}}) {
if ($self->{open_elements}->[$_]->[1] eq 'table') {
my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
if (defined $parent and $parent->node_type == 1) {
$foster_parent_element = $parent;
$next_sibling = $self->{open_elements}->[$_]->[0];
$prev_sibling = $next_sibling->previous_sibling;
} else {
$foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
$prev_sibling = $foster_parent_element->last_child;
}
last OE;
}
} # OE
$foster_parent_element = $self->{open_elements}->[0]->[0] and
$prev_sibling = $foster_parent_element->last_child
unless defined $foster_parent_element;
if (defined $prev_sibling and
$prev_sibling->node_type == 3) {
$prev_sibling->manakai_append_text ($token->{data});
} else {
$foster_parent_element->insert_before
($self->{document}->create_text_node ($token->{data}),
$next_sibling);
}
} else {
$self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
}
$token = $self->_get_next_token;
redo B;
} elsif ($token->{type} eq 'start tag') {
if ({
tr => 1,
th => 1, td => 1,
}->{$token->{tag_name}}) {
unless ($token->{tag_name} eq 'tr') {
$self->{parse_error}-> (type => 'missing start tag:tr');
}
## Clear back to table body context
while (not {
tbody => 1, tfoot => 1, thead => 1, html => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
pop @{$self->{open_elements}};
}
$self->{insertion_mode} = 'in row';
if ($token->{tag_name} eq 'tr') {
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, $token->{tag_name}]);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, $token->{tag_name}];
}
$token = $self->_get_next_token;
} else {
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, 'tr']);
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, 'tr'];
}
## reprocess
}
redo B;
} elsif ({
caption => 1, col => 1, colgroup => 1,
tbody => 1, tfoot => 1, thead => 1,
}->{$token->{tag_name}}) {
## have an element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ({
tbody => 1, thead => 1, tfoot => 1,
}->{$node->[1]}) {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## Clear back to table body context
while (not {
tbody => 1, tfoot => 1, thead => 1, html => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
pop @{$self->{open_elements}};
}
## As if <{current node}>
## have an element in table scope
## true by definition
## Clear back to table body context
## nop by definition
pop @{$self->{open_elements}};
$self->{insertion_mode} = 'in table';
## reprocess
redo B;
} elsif ($token->{tag_name} eq 'table') {
## NOTE: This is a code clone of "table in table"
$self->{parse_error}-> (type => 'not closed:table');
## As if
## have a table element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq 'table') {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:table');
## Ignore tokens
$token = $self->_get_next_token;
redo B;
}
## generate implied end tags
if ({
dd => 1, dt => 1, li => 1, p => 1,
td => 1, th => 1, tr => 1,
tbody => 1, tfoot=> 1, thead => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
unshift @{$self->{token}}, $token; #
$token = {type => 'end tag', tag_name => 'table'};
unshift @{$self->{token}}, $token;
$token = {type => 'end tag',
tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
redo B;
}
if ($self->{open_elements}->[-1]->[1] ne 'table') {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
}
splice @{$self->{open_elements}}, $i;
$self->_reset_insertion_mode;
## reprocess
redo B;
} else {
#
}
} elsif ($token->{type} eq 'end tag') {
if ({
tbody => 1, tfoot => 1, thead => 1,
}->{$token->{tag_name}}) {
## have an element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq $token->{tag_name}) {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## Clear back to table body context
while (not {
tbody => 1, tfoot => 1, thead => 1, html => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
pop @{$self->{open_elements}};
}
pop @{$self->{open_elements}};
$self->{insertion_mode} = 'in table';
$token = $self->_get_next_token;
redo B;
} elsif ($token->{tag_name} eq 'table') {
## have an element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ({
tbody => 1, thead => 1, tfoot => 1,
}->{$node->[1]}) {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## Clear back to table body context
while (not {
tbody => 1, tfoot => 1, thead => 1, html => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
pop @{$self->{open_elements}};
}
## As if <{current node}>
## have an element in table scope
## true by definition
## Clear back to table body context
## nop by definition
pop @{$self->{open_elements}};
$self->{insertion_mode} = 'in table';
## reprocess
redo B;
} elsif ({
body => 1, caption => 1, col => 1, colgroup => 1,
html => 1, td => 1, th => 1, tr => 1,
}->{$token->{tag_name}}) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
} else {
#
}
} else {
#
}
## As if in table
$self->{parse_error}-> (type => 'in table:'.$token->{tag_name});
$in_body->($insert_to_foster);
redo B;
} elsif ($self->{insertion_mode} eq 'in row') {
if ($token->{type} eq 'character') {
## NOTE: This is a "character in table" code clone.
if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
$self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
unless (length $token->{data}) {
$token = $self->_get_next_token;
redo B;
}
}
$self->{parse_error}-> (type => 'in table:#character');
## As if in body, but insert into foster parent element
## ISSUE: Spec says that "whenever a node would be inserted
## into the current node" while characters might not be
## result in a new Text node.
$reconstruct_active_formatting_elements->($insert_to_foster);
if ({
table => 1, tbody => 1, tfoot => 1,
thead => 1, tr => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
# MUST
my $foster_parent_element;
my $next_sibling;
my $prev_sibling;
OE: for (reverse 0..$#{$self->{open_elements}}) {
if ($self->{open_elements}->[$_]->[1] eq 'table') {
my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
if (defined $parent and $parent->node_type == 1) {
$foster_parent_element = $parent;
$next_sibling = $self->{open_elements}->[$_]->[0];
$prev_sibling = $next_sibling->previous_sibling;
} else {
$foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
$prev_sibling = $foster_parent_element->last_child;
}
last OE;
}
} # OE
$foster_parent_element = $self->{open_elements}->[0]->[0] and
$prev_sibling = $foster_parent_element->last_child
unless defined $foster_parent_element;
if (defined $prev_sibling and
$prev_sibling->node_type == 3) {
$prev_sibling->manakai_append_text ($token->{data});
} else {
$foster_parent_element->insert_before
($self->{document}->create_text_node ($token->{data}),
$next_sibling);
}
} else {
$self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
}
$token = $self->_get_next_token;
redo B;
} elsif ($token->{type} eq 'start tag') {
if ($token->{tag_name} eq 'th' or
$token->{tag_name} eq 'td') {
## Clear back to table row context
while (not {
tr => 1, html => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
pop @{$self->{open_elements}};
}
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, $token->{tag_name}]);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, $token->{tag_name}];
}
$self->{insertion_mode} = 'in cell';
push @$active_formatting_elements, ['#marker', ''];
$token = $self->_get_next_token;
redo B;
} elsif ({
caption => 1, col => 1, colgroup => 1,
tbody => 1, tfoot => 1, thead => 1, tr => 1,
}->{$token->{tag_name}}) {
## As if
## have an element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq 'tr') {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmacthed end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## Clear back to table row context
while (not {
tr => 1, html => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
pop @{$self->{open_elements}};
}
pop @{$self->{open_elements}}; # tr
$self->{insertion_mode} = 'in table body';
## reprocess
redo B;
} elsif ($token->{tag_name} eq 'table') {
## NOTE: This is a code clone of "table in table"
$self->{parse_error}-> (type => 'not closed:table');
## As if
## have a table element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq 'table') {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:table');
## Ignore tokens
$token = $self->_get_next_token;
redo B;
}
## generate implied end tags
if ({
dd => 1, dt => 1, li => 1, p => 1,
td => 1, th => 1, tr => 1,
tbody => 1, tfoot=> 1, thead => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
unshift @{$self->{token}}, $token; #
$token = {type => 'end tag', tag_name => 'table'};
unshift @{$self->{token}}, $token;
$token = {type => 'end tag',
tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
redo B;
}
if ($self->{open_elements}->[-1]->[1] ne 'table') {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
}
splice @{$self->{open_elements}}, $i;
$self->_reset_insertion_mode;
## reprocess
redo B;
} else {
#
}
} elsif ($token->{type} eq 'end tag') {
if ($token->{tag_name} eq 'tr') {
## have an element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq $token->{tag_name}) {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## Clear back to table row context
while (not {
tr => 1, html => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
pop @{$self->{open_elements}};
}
pop @{$self->{open_elements}}; # tr
$self->{insertion_mode} = 'in table body';
$token = $self->_get_next_token;
redo B;
} elsif ($token->{tag_name} eq 'table') {
## As if
## have an element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq 'tr') {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{type});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## Clear back to table row context
while (not {
tr => 1, html => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
pop @{$self->{open_elements}};
}
pop @{$self->{open_elements}}; # tr
$self->{insertion_mode} = 'in table body';
## reprocess
redo B;
} elsif ({
tbody => 1, tfoot => 1, thead => 1,
}->{$token->{tag_name}}) {
## have an element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq $token->{tag_name}) {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## As if
## have an element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq 'tr') {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:tr');
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## Clear back to table row context
while (not {
tr => 1, html => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
pop @{$self->{open_elements}};
}
pop @{$self->{open_elements}}; # tr
$self->{insertion_mode} = 'in table body';
## reprocess
redo B;
} elsif ({
body => 1, caption => 1, col => 1,
colgroup => 1, html => 1, td => 1, th => 1,
}->{$token->{tag_name}}) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
} else {
#
}
} else {
#
}
## As if in table
$self->{parse_error}-> (type => 'in table:'.$token->{tag_name});
$in_body->($insert_to_foster);
redo B;
} elsif ($self->{insertion_mode} eq 'in cell') {
if ($token->{type} eq 'character') {
## NOTE: This is a code clone of "character in body".
$reconstruct_active_formatting_elements->($insert_to_current);
$self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
$token = $self->_get_next_token;
redo B;
} elsif ($token->{type} eq 'start tag') {
if ({
caption => 1, col => 1, colgroup => 1,
tbody => 1, td => 1, tfoot => 1, th => 1,
thead => 1, tr => 1,
}->{$token->{tag_name}}) {
## have an element in table scope
my $tn;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq 'td' or $node->[1] eq 'th') {
$tn = $node->[1];
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $tn) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## Close the cell
unshift @{$self->{token}}, $token; # >
$token = {type => 'end tag', tag_name => $tn};
redo B;
} else {
#
}
} elsif ($token->{type} eq 'end tag') {
if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
## have an element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq $token->{tag_name}) {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## generate implied end tags
if ({
dd => 1, dt => 1, li => 1, p => 1,
td => ($token->{tag_name} eq 'th'),
th => ($token->{tag_name} eq 'td'),
tr => 1,
tbody => 1, tfoot=> 1, thead => 1,
}->{$self->{open_elements}->[-1]->[1]}) {
unshift @{$self->{token}}, $token;
$token = {type => 'end tag',
tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
redo B;
}
if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
$self->{parse_error}-> (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
}
splice @{$self->{open_elements}}, $i;
$clear_up_to_marker->();
$self->{insertion_mode} = 'in row';
$token = $self->_get_next_token;
redo B;
} elsif ({
body => 1, caption => 1, col => 1,
colgroup => 1, html => 1,
}->{$token->{tag_name}}) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
} elsif ({
table => 1, tbody => 1, tfoot => 1,
thead => 1, tr => 1,
}->{$token->{tag_name}}) {
## have an element in table scope
my $i;
my $tn;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq $token->{tag_name}) {
$i = $_;
last INSCOPE;
} elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
$tn = $node->[1];
## NOTE: There is exactly one |td| or |th| element
## in scope in the stack of open elements by definition.
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## Close the cell
unshift @{$self->{token}}, $token; # ?>
$token = {type => 'end tag', tag_name => $tn};
redo B;
} else {
#
}
} else {
#
}
$in_body->($insert_to_current);
redo B;
} elsif ($self->{insertion_mode} eq 'in select') {
if ($token->{type} eq 'character') {
$self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
$token = $self->_get_next_token;
redo B;
} elsif ($token->{type} eq 'start tag') {
if ($token->{tag_name} eq 'option') {
if ($self->{open_elements}->[-1]->[1] eq 'option') {
## As if
pop @{$self->{open_elements}};
}
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, $token->{tag_name}]);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, $token->{tag_name}];
}
$token = $self->_get_next_token;
redo B;
} elsif ($token->{tag_name} eq 'optgroup') {
if ($self->{open_elements}->[-1]->[1] eq 'option') {
## As if
pop @{$self->{open_elements}};
}
if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
## As if
pop @{$self->{open_elements}};
}
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, $token->{tag_name}]);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, $token->{tag_name}];
}
$token = $self->_get_next_token;
redo B;
} elsif ($token->{tag_name} eq 'select') {
$self->{parse_error}-> (type => 'not closed:select');
## As if instead
## have an element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq $token->{tag_name}) {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:select');
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
splice @{$self->{open_elements}}, $i;
$self->_reset_insertion_mode;
$token = $self->_get_next_token;
redo B;
} else {
#
}
} elsif ($token->{type} eq 'end tag') {
if ($token->{tag_name} eq 'optgroup') {
if ($self->{open_elements}->[-1]->[1] eq 'option' and
$self->{open_elements}->[-2]->[1] eq 'optgroup') {
## As if
splice @{$self->{open_elements}}, -2;
} elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
pop @{$self->{open_elements}};
} else {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
}
$token = $self->_get_next_token;
redo B;
} elsif ($token->{tag_name} eq 'option') {
if ($self->{open_elements}->[-1]->[1] eq 'option') {
pop @{$self->{open_elements}};
} else {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
}
$token = $self->_get_next_token;
redo B;
} elsif ($token->{tag_name} eq 'select') {
## have an element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq $token->{tag_name}) {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
splice @{$self->{open_elements}}, $i;
$self->_reset_insertion_mode;
$token = $self->_get_next_token;
redo B;
} elsif ({
caption => 1, table => 1, tbody => 1,
tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
}->{$token->{tag_name}}) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## have an element in table scope
my $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq $token->{tag_name}) {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
## As if
## have an element in table scope
undef $i;
INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
my $node = $self->{open_elements}->[$_];
if ($node->[1] eq 'select') {
$i = $_;
last INSCOPE;
} elsif ({
table => 1, html => 1,
}->{$node->[1]}) {
last INSCOPE;
}
} # INSCOPE
unless (defined $i) {
$self->{parse_error}-> (type => 'unmatched end tag:select');
## Ignore the token
$token = $self->_get_next_token; ## TODO: ok?
redo B;
}
splice @{$self->{open_elements}}, $i;
$self->_reset_insertion_mode;
## reprocess
redo B;
} else {
#
}
} else {
#
}
$self->{parse_error}-> (type => 'in select:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
} elsif ($self->{insertion_mode} eq 'after body') {
if ($token->{type} eq 'character') {
if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
my $data = $1;
## As if in body
$reconstruct_active_formatting_elements->($insert_to_current);
$self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
unless (length $token->{data}) {
$token = $self->_get_next_token;
redo B;
}
}
#
$self->{parse_error}-> (type => 'after body:#character');
} elsif ($token->{type} eq 'start tag') {
$self->{parse_error}-> (type => 'after body:'.$token->{tag_name});
#
} elsif ($token->{type} eq 'end tag') {
if ($token->{tag_name} eq 'html') {
if (defined $self->{inner_html_node}) {
$self->{parse_error}-> (type => 'unmatched end tag:html');
## Ignore the token
$token = $self->_get_next_token;
redo B;
} else {
$previous_insertion_mode = $self->{insertion_mode};
$self->{insertion_mode} = 'trailing end';
$token = $self->_get_next_token;
redo B;
}
} else {
$self->{parse_error}-> (type => 'after body:/'.$token->{tag_name});
}
} else {
die "$0: $token->{type}: Unknown token type";
}
$self->{insertion_mode} = 'in body';
## reprocess
redo B;
} elsif ($self->{insertion_mode} eq 'in frameset') {
if ($token->{type} eq 'character') {
if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
$self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
unless (length $token->{data}) {
$token = $self->_get_next_token;
redo B;
}
}
$self->{parse_error}-> (type => 'in frameset:#character');
## Ignore the token
$token = $self->_get_next_token;
redo B;
} elsif ($token->{type} eq 'start tag') {
if ($token->{tag_name} eq 'frameset') {
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, $token->{tag_name}]);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, $token->{tag_name}];
}
$token = $self->_get_next_token;
redo B;
} elsif ($token->{tag_name} eq 'frame') {
{
my $el;
$el = $self->{document}->create_element_ns
(q, [undef, $token->{tag_name}]);
for my $attr_name (keys %{ $token->{attributes}}) {
$el->set_attribute_ns (undef, [undef, $attr_name],
$token->{attributes} ->{$attr_name}->{value});
}
$self->{open_elements}->[-1]->[0]->append_child ($el);
push @{$self->{open_elements}}, [$el, $token->{tag_name}];
}
pop @{$self->{open_elements}};
$token = $self->_get_next_token;
redo B;
} elsif ($token->{tag_name} eq 'noframes') {
$in_body->($insert_to_current);
redo B;
} else {
$self->{parse_error}-> (type => 'in frameset:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
} elsif ($token->{type} eq 'end tag') {
if ($token->{tag_name} eq 'frameset') {
if ($self->{open_elements}->[-1]->[1] eq 'html' and
@{$self->{open_elements}} == 1) {
$self->{parse_error}-> (type => 'unmatched end tag:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
} else {
pop @{$self->{open_elements}};
$token = $self->_get_next_token;
}
if (not defined $self->{inner_html_node} and
$self->{open_elements}->[-1]->[1] ne 'frameset') {
$self->{insertion_mode} = 'after frameset';
}
redo B;
} else {
$self->{parse_error}-> (type => 'in frameset:/'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
} else {
die "$0: $token->{type}: Unknown token type";
}
} elsif ($self->{insertion_mode} eq 'after frameset') {
if ($token->{type} eq 'character') {
if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
$self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
unless (length $token->{data}) {
$token = $self->_get_next_token;
redo B;
}
}
if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
$self->{parse_error}-> (type => 'after frameset:#character');
## Ignore the token.
if (length $token->{data}) {
## reprocess the rest of characters
} else {
$token = $self->_get_next_token;
}
redo B;
}
die qq[$0: Character "$token->{data}"];
} elsif ($token->{type} eq 'start tag') {
if ($token->{tag_name} eq 'noframes') {
$in_body->($insert_to_current);
redo B;
} else {
$self->{parse_error}-> (type => 'after frameset:'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
} elsif ($token->{type} eq 'end tag') {
if ($token->{tag_name} eq 'html') {
$previous_insertion_mode = $self->{insertion_mode};
$self->{insertion_mode} = 'trailing end';
$token = $self->_get_next_token;
redo B;
} else {
$self->{parse_error}-> (type => 'after frameset:/'.$token->{tag_name});
## Ignore the token
$token = $self->_get_next_token;
redo B;
}
} else {
die "$0: $token->{type}: Unknown token type";
}
## ISSUE: An issue in spec here
} elsif ($self->{insertion_mode} eq 'trailing end') {
## states in the main stage is preserved yet # MUST
if ($token->{type} eq 'character') {
if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
my $data = $1;
## As if in the main phase.
## NOTE: The insertion mode in the main phase
## just before the phase has been changed to the trailing
## end phase is either "after body" or "after frameset".
$reconstruct_active_formatting_elements->($insert_to_current);
$self->{open_elements}->[-1]->[0]->manakai_append_text ($data);
unless (length $token->{data}) {
$token = $self->_get_next_token;
redo B;
}
}
$self->{parse_error}-> (type => 'after html:#character');
$self->{insertion_mode} = $previous_insertion_mode;
## reprocess
redo B;
} elsif ($token->{type} eq 'start tag') {
$self->{parse_error}-> (type => 'after html:'.$token->{tag_name});
$self->{insertion_mode} = $previous_insertion_mode;
## reprocess
redo B;
} elsif ($token->{type} eq 'end tag') {
$self->{parse_error}-> (type => 'after html:/'.$token->{tag_name});
$self->{insertion_mode} = $previous_insertion_mode;
## reprocess
redo B;
} else {
die "$0: $token->{type}: Unknown token";
}
} else {
die "$0: $self->{insertion_mode}: Unknown insertion mode";
}
} # B
## Stop parsing # MUST
## TODO: script stuffs
} # _tree_construct_main
sub set_inner_html ($$$) {
my $class = shift;
my $node = shift;
my $s = \$_[0];
my $onerror = $_[1];
my $nt = $node->node_type;
if ($nt == 9) {
# MUST
## Step 1 # MUST
## TODO: If the document has an active parser, ...
## ISSUE: There is an issue in the spec.
## Step 2 # MUST
my @cn = @{$node->child_nodes};
for (@cn) {
$node->remove_child ($_);
}
## Step 3, 4, 5 # MUST
$class->parse_string ($$s => $node, $onerror);
} elsif ($nt == 1) {
## TODO: If non-html element
## NOTE: Most of this code is copied from |parse_string|
## Step 1 # MUST
my $this_doc = $node->owner_document;
my $doc = $this_doc->implementation->create_document;
$doc->manakai_is_html (1);
my $p = $class->new;
$p->{document} = $doc;
## Step 9 # MUST
my $i = 0;
my $line = 1;
my $column = 0;
$p->{set_next_input_character} = sub {
my $self = shift;
pop @{$self->{prev_input_character}};
unshift @{$self->{prev_input_character}}, $self->{next_input_character};
$self->{next_input_character} = -1 and return if $i >= length $$s;
$self->{next_input_character} = ord substr $$s, $i++, 1;
$column++;
if ($self->{next_input_character} == 0x000A) { # LF
$line++;
$column = 0;
} elsif ($self->{next_input_character} == 0x000D) { # CR
$i++ if substr ($$s, $i, 1) eq "\x0A";
$self->{next_input_character} = 0x000A; # LF # MUST
$line++;
$column = 0;
} elsif ($self->{next_input_character} > 0x10FFFF) {
$self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
} elsif ($self->{next_input_character} == 0x0000) { # NULL
$self->{parse_error}-> (type => 'NULL');
$self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
}
};
$p->{prev_input_character} = [-1, -1, -1];
$p->{next_input_character} = -1;
my $ponerror = $onerror || sub {
my (%opt) = @_;
warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";
};
$p->{parse_error} = sub {
$ponerror->(@_, line => $line, column => $column);
};
$p->_initialize_tokenizer;
$p->_initialize_tree_constructor;
## Step 2
my $node_ln = $node->local_name;
$p->{content_model} = {
title => RCDATA_CONTENT_MODEL,
textarea => RCDATA_CONTENT_MODEL,
style => CDATA_CONTENT_MODEL,
script => CDATA_CONTENT_MODEL,
xmp => CDATA_CONTENT_MODEL,
iframe => CDATA_CONTENT_MODEL,
noembed => CDATA_CONTENT_MODEL,
noframes => CDATA_CONTENT_MODEL,
noscript => CDATA_CONTENT_MODEL,
plaintext => PLAINTEXT_CONTENT_MODEL,
}->{$node_ln};
$p->{content_model} = PCDATA_CONTENT_MODEL
unless defined $p->{content_model};
## ISSUE: What is "the name of the element"? local name?
$p->{inner_html_node} = [$node, $node_ln];
## Step 4
my $root = $doc->create_element_ns
('http://www.w3.org/1999/xhtml', [undef, 'html']);
## Step 5 # MUST
$doc->append_child ($root);
## Step 6 # MUST
push @{$p->{open_elements}}, [$root, 'html'];
undef $p->{head_element};
## Step 7 # MUST
$p->_reset_insertion_mode;
## Step 8 # MUST
my $anode = $node;
AN: while (defined $anode) {
if ($anode->node_type == 1) {
my $nsuri = $anode->namespace_uri;
if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
if ($anode->local_name eq 'form') { ## TODO: case?
$p->{form_element} = $anode;
last AN;
}
}
}
$anode = $anode->parent_node;
} # AN
## Step 3 # MUST
## Step 10 # MUST
{
my $self = $p;
$token = $self->_get_next_token;
}
$p->_tree_construction_main;
## Step 11 # MUST
my @cn = @{$node->child_nodes};
for (@cn) {
$node->remove_child ($_);
}
## ISSUE: mutation events? read-only?
## Step 12 # MUST
@cn = @{$root->child_nodes};
for (@cn) {
$this_doc->adopt_node ($_);
$node->append_child ($_);
}
## ISSUE: mutation events?
$p->_terminate_tree_constructor;
} else {
die "$0: |set_inner_html| is not defined for node of type $nt";
}
} # set_inner_html
} # tree construction stage
sub get_inner_html ($$$) {
my (undef, $node, $on_error) = @_;
## Step 1
my $s = '';
my $in_cdata;
my $parent = $node;
while (defined $parent) {
if ($parent->node_type == 1 and
$parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and
{
style => 1, script => 1, xmp => 1, iframe => 1,
noembed => 1, noframes => 1, noscript => 1,
}->{$parent->local_name}) { ## TODO: case thingy
$in_cdata = 1;
}
$parent = $parent->parent_node;
}
## Step 2
my @node = @{$node->child_nodes};
C: while (@node) {
my $child = shift @node;
unless (ref $child) {
if ($child eq 'cdata-out') {
$in_cdata = 0;
} else {
$s .= $child; # end tag
}
next C;
}
my $nt = $child->node_type;
if ($nt == 1) { # Element
my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
$s .= '<' . $tag_name;
## NOTE: Non-HTML case:
##
my @attrs = @{$child->attributes}; # sort order MUST be stable
for my $attr (@attrs) { # order is implementation dependent
my $attr_name = $attr->name; ## TODO: manakai_name
$s .= ' ' . $attr_name . '="';
my $attr_value = $attr->value;
## escape
$attr_value =~ s/&/&/g;
$attr_value =~ s/</g;
$attr_value =~ s/>/>/g;
$attr_value =~ s/"/"/g;
$s .= $attr_value . '"';
}
$s .= '>';
next C if {
area => 1, base => 1, basefont => 1, bgsound => 1,
br => 1, col => 1, embed => 1, frame => 1, hr => 1,
img => 1, input => 1, link => 1, meta => 1, param => 1,
spacer => 1, wbr => 1,
}->{$tag_name};
$s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';
if (not $in_cdata and {
style => 1, script => 1, xmp => 1, iframe => 1,
noembed => 1, noframes => 1, noscript => 1,
plaintext => 1,
}->{$tag_name}) {
unshift @node, 'cdata-out';
$in_cdata = 1;
}
unshift @node, @{$child->child_nodes}, '' . $tag_name . '>';
} elsif ($nt == 3 or $nt == 4) {
if ($in_cdata) {
$s .= $child->data;
} else {
my $value = $child->data;
$value =~ s/&/&/g;
$value =~ s/</g;
$value =~ s/>/>/g;
$value =~ s/"/"/g;
$s .= $value;
}
} elsif ($nt == 8) {
$s .= '';
} elsif ($nt == 10) {
$s .= 'name . '>';
} elsif ($nt == 5) { # entrefs
push @node, @{$child->child_nodes};
} else {
$on_error->($child) if defined $on_error;
}
## ISSUE: This code does not support PIs.
} # C
## Step 3
return \$s;
} # get_inner_html
1;
# $Date: 2007/07/21 06:04:07 $