Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 12, 2014
1 parent 0edeca7 commit 34cff29
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
18 changes: 7 additions & 11 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -298,17 +298,15 @@ sub _selector {
sub _sibling {
my ($selectors, $current, $tree, $immediate) = @_;

my $parent = $current->[3];
my $found;
for my $n (@$parent[($parent->[0] eq 'root' ? 1 : 4) .. $#$parent]) {
return $found if $n eq $current;
next unless $n->[0] eq 'tag';
for my $sibling (@{_siblings($current)}) {
return $found if $sibling eq $current;

# "+" (immediately preceding sibling)
if ($immediate) { $found = _combinator($selectors, $n, $tree) }
if ($immediate) { $found = _combinator($selectors, $sibling, $tree) }

# "~" (preceding sibling)
else { return 1 if _combinator($selectors, $n, $tree) }
else { return 1 if _combinator($selectors, $sibling, $tree) }
}

return undef;
Expand All @@ -317,12 +315,10 @@ sub _sibling {
sub _siblings {
my ($current, $type) = @_;

my @siblings;
my $parent = $current->[3];
for my $sibling (@$parent[($parent->[0] eq 'root' ? 1 : 4) .. $#$parent]) {
next if $sibling->[0] ne 'tag';
push @siblings, $sibling unless defined $type && $type ne $sibling->[1];
}
my @siblings = grep { $_->[0] eq 'tag' }
@$parent[($parent->[0] eq 'root' ? 1 : 4) .. $#$parent];
@siblings = grep { $type eq $_->[1] } @siblings if defined $type;

return \@siblings;
}
Expand Down
23 changes: 9 additions & 14 deletions lib/Mojo/Exception.pm
Expand Up @@ -42,38 +42,33 @@ sub trace {
return $self->frames(\@frames);
}

sub _append {
my ($stack, $line) = @_;
chomp $line;
push @$stack, $line;
}

sub _context {
my ($self, $num, $lines) = @_;

# Line
return unless defined $lines->[0][$num - 1];
$self->line([$num]);
for my $line (@$lines) {
chomp(my $code = $line->[$num - 1]);
push @{$self->line}, $code;
}
_append($self->line, $_->[$num - 1]) for @$lines;

# Before
for my $i (2 .. 6) {
last if ((my $previous = $num - $i) < 0);
next unless defined $lines->[0][$previous];
unshift @{$self->lines_before}, [$previous + 1];
for my $line (@$lines) {
chomp(my $code = $line->[$previous]);
push @{$self->lines_before->[0]}, $code;
}
_append($self->lines_before->[0], $_->[$previous]) for @$lines;
}

# After
for my $i (0 .. 4) {
next if ((my $next = $num + $i) < 0);
next unless defined $lines->[0][$next];
push @{$self->lines_after}, [$next + 1];
for my $line (@$lines) {
last unless defined(my $code = $line->[$next]);
chomp $code;
push @{$self->lines_after->[-1]}, $code;
}
_append($self->lines_after->[-1], $_->[$next]) for @$lines;
}
}

Expand Down
10 changes: 10 additions & 0 deletions t/mojo/template.t
Expand Up @@ -1071,6 +1071,16 @@ is $output->lines_before->[0][1], '☃', 'right line';
is $output->line->[1], '% die;♥', 'right line';
is $output->lines_after->[0][1], '', 'right line';

# Exception in first line with bad message
$mt = Mojo::Template->new;
$output = $mt->render('<% die "Test at template line 99\n"; %>');
isa_ok $output, 'Mojo::Exception', 'right exception';
is $output->message, "Test at template line 99\n", 'right message';
is $output->lines_before->[0], undef, 'no lines before';
is $output->line->[0], 1, 'right number';
is $output->line->[1], '<% die "Test at template line 99\n"; %>', 'right line';
is $output->lines_after->[0], undef, 'no lines after';

# Different encodings
$mt = Mojo::Template->new(encoding => 'shift_jis');
$file = catfile(splitdir($FindBin::Bin), qw(templates utf8_exception.mt));
Expand Down

0 comments on commit 34cff29

Please sign in to comment.