Skip to content

Commit

Permalink
Merge pull request #688 from 2shortplanks/master
Browse files Browse the repository at this point in the history
Fixes for nested <dd>
  • Loading branch information
kraih committed Oct 14, 2014
2 parents 812ed77 + 0c8fa4d commit 9d06a53
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -56,8 +56,6 @@ my %RCDATA = map { $_ => 1 } qw(title textarea);
# HTML elements with optional end tags
my %END = (
body => ['head'],
dd => [qw(dt dd)],
dt => [qw(dt dd)],
rp => [qw(rt rp)],
rt => [qw(rt rp)]
);
Expand Down Expand Up @@ -275,6 +273,11 @@ sub _start {
if (!$xml && $$current->[0] ne 'root') {
if (my $end = $END{$start}) { _end($_, 0, $current) for @$end }

# "dd" and "dt"
elsif ($start eq 'dd' || $start eq 'dt') {
_close($current, {dd => 1, dt => 1}, {dl => 1}) for qw(dd dt);
}

# "li"
elsif ($start eq 'li') { _close($current, {li => 1}, {ul => 1, ol => 1}) }

Expand All @@ -288,7 +291,7 @@ sub _start {

# "th" and "td"
elsif ($start eq 'th' || $start eq 'td') {
_close($current, {$_ => 1}, {table => 1}) for qw(th td);
_close($current, {th => 1, td => 1}, {table => 1});
}
}

Expand Down
16 changes: 16 additions & 0 deletions t/mojo/dom.t
Expand Up @@ -1311,6 +1311,22 @@ is $dom->find('dl > dd')->[1]->text, 'D', 'right text';
is $dom->find('dl > dt')->[2]->text, 'E', 'right text';
is $dom->find('dl > dd')->[2]->text, 'F', 'right text';

# but different levels don't self-terminate to allow for nesting
$dom = Mojo::DOM->new->parse(<<EOF);
<dl>
<dt>A</dt>
<DD>
<dl>
<dt>B</dt>
<dd>C</dd>
</dl>
</dd>
</dl>
EOF
is $dom->find('dl > dd > dl > dt')->[0]->text, 'B', 'right text';
is $dom->find('dl > dd > dl > dd')->[0]->text, 'C', 'right text';
is $dom->find('dl > dt')->[0]->text, 'A', 'right text';

# Optional "rp" and "rt" tags
$dom = Mojo::DOM->new->parse(<<EOF);
<ruby>
Expand Down

0 comments on commit 9d06a53

Please sign in to comment.