Skip to content

Commit

Permalink
tags are more common than root nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 18, 2013
1 parent cf95187 commit 3804022
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
32 changes: 13 additions & 19 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -50,14 +50,14 @@ sub select {
while (my $current = shift @queue) {
my $type = $current->[0];

# Root
if ($type eq 'root') { unshift @queue, @$current[1 .. $#$current] }

# Tag
elsif ($type eq 'tag') {
if ($type eq 'tag') {
unshift @queue, @$current[4 .. $#$current];
push @results, $current if $self->_match($pattern, $current, $tree);
}

# Root
elsif ($type eq 'root') { unshift @queue, @$current[1 .. $#$current] }
}

return \@results;
Expand Down Expand Up @@ -150,12 +150,8 @@ sub _compile {

# Class or ID
while ($element =~ /$CLASS_ID_RE/g) {

# Class
push @$selector, ['attr', 'class', $self->_regex('~', $1)] if defined $1;

# ID
push @$selector, ['attr', 'id', $self->_regex('', $2)] if defined $2;
push @$selector, ['attr', 'id', $self->_regex('', $2)] if defined $2;
}

# Pseudo classes
Expand Down Expand Up @@ -188,20 +184,18 @@ sub _equation {
my ($self, $equation) = @_;

# "even"
my $num = [1, 1];
if ($equation =~ /^even$/i) { $num = [2, 2] }
return [2, 2] if $equation =~ /^even$/i;

# "odd"
elsif ($equation =~ /^odd$/i) { $num = [2, 1] }
return [2, 1] if $equation =~ /^odd$/i;

# Equation
elsif ($equation =~ /(?:(-?(?:\d+)?)?(n))?\s*\+?\s*(-?\s*\d+)?\s*$/i) {
$num->[0] = defined($1) && length($1) ? $1 : $2 ? 1 : 0;
$num->[0] = -1 if $num->[0] eq '-';
$num->[1] = $3 // 0;
$num->[1] =~ s/\s+//g;
}

my $num = [1, 1];
return $num if $equation !~ /(?:(-?(?:\d+)?)?(n))?\s*\+?\s*(-?\s*\d+)?\s*$/i;
$num->[0] = defined($1) && length($1) ? $1 : $2 ? 1 : 0;
$num->[0] = -1 if $num->[0] eq '-';
$num->[1] = $3 // 0;
$num->[1] =~ s/\s+//g;
return $num;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -130,7 +130,7 @@ sub parse {
# Tag
$self->_start($start, \%attrs, \$current);

# Empty element
# Element without end tag
$self->_end($start, \$current)
if (!$self->xml && $VOID{$start}) || $attr =~ m!/\s*$!;

Expand Down Expand Up @@ -242,7 +242,7 @@ sub _render {
my $attrs = join ' ', @attrs;
$content .= " $attrs" if $attrs;

# Empty tag
# Element without end tag
return $self->xml || $VOID{$tag} ? "$content />" : "$content></$tag>"
unless $tree->[4];

Expand Down

0 comments on commit 3804022

Please sign in to comment.