diff --git a/lib/Mojo/DOM/HTML.pm b/lib/Mojo/DOM/HTML.pm
index eb4f99272a..cc088fd387 100644
--- a/lib/Mojo/DOM/HTML.pm
+++ b/lib/Mojo/DOM/HTML.pm
@@ -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)]
);
@@ -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}) }
@@ -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});
}
}
diff --git a/t/mojo/dom.t b/t/mojo/dom.t
index 3a18141ffd..edc37b68cb 100644
--- a/t/mojo/dom.t
+++ b/t/mojo/dom.t
@@ -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(<
+ A
+
+
+ - B
+ - C
+
+
+
+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(<