Skip to content

Commit

Permalink
improved Mojo::DOM::HTML performance
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 16, 2013
1 parent 532ed95 commit 5578336
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

4.58 2013-11-16
- Improved IIS and WebSphere compatibility of Mojo::Message::Request.
- Improved Mojo::DOM::HTML performance.
- Fixed recursion bug in Mojo::Reactor::EV where timers could run more than
once.

Expand Down
38 changes: 14 additions & 24 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -50,11 +50,22 @@ my $TOKEN_RE = qr/
/xis;

# HTML elements that break paragraphs
my %PARAGRAPH = map { $_ => 1 } (
my @PARAGRAPH = (
qw(address article aside blockquote dir div dl fieldset footer form h1 h2),
qw(h3 h4 h5 h6 header hr main menu nav ol p pre section table ul)
);

# 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)]
);
$END{$_} = [$_] for qw(optgroup option);
$END{$_} = ['p'] for @PARAGRAPH;

# HTML table elements with optional end tags
my %TABLE = map { $_ => 1 } qw(colgroup tbody td tfoot th thead tr);

Expand Down Expand Up @@ -260,21 +271,10 @@ sub _start {

# Autoclose optional HTML elements
if (!$self->xml && $$current->[0] ne 'root') {
if ($END{$start}) { $self->_end($_, $current) for @{$END{$start}} }

# "li"
if ($start eq 'li') { $self->_close($current, {li => 1}, 'ul') }

# "p"
elsif ($PARAGRAPH{$start}) { $self->_end('p', $current) }

# "head"
elsif ($start eq 'body') { $self->_end('head', $current) }

# "optgroup"
elsif ($start eq 'optgroup') { $self->_end('optgroup', $current) }

# "option"
elsif ($start eq 'option') { $self->_end('option', $current) }
elsif ($start eq 'li') { $self->_close($current, {li => 1}, 'ul') }

# "colgroup", "thead", "tbody" and "tfoot"
elsif (grep { $_ eq $start } qw(colgroup thead tbody tfoot)) {
Expand All @@ -288,16 +288,6 @@ sub _start {
elsif ($start eq 'th' || $start eq 'td') {
$self->_close($current, {$_ => 1}, 'table') for qw(th td);
}

# "dt" and "dd"
elsif ($start eq 'dt' || $start eq 'dd') {
$self->_end($_, $current) for qw(dt dd);
}

# "rt" and "rp"
elsif ($start eq 'rt' || $start eq 'rp') {
$self->_end($_, $current) for qw(rt rp);
}
}

# New tag
Expand Down

0 comments on commit 5578336

Please sign in to comment.