Skip to content

Commit

Permalink
fixed small text extraction bug in Mojo::DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 3, 2012
1 parent 44bf388 commit 102567b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -6,6 +6,7 @@
- Improved documentation.
- Improved tests.
- Fixed JSON Pointer escaping.
- Fixed small text extraction bug in Mojo::DOM.
- Fixed small inconsistency between routes and static dispatchers.

3.01 2012-07-01
Expand Down
14 changes: 8 additions & 6 deletions lib/Mojo/DOM.pm
Expand Up @@ -133,12 +133,12 @@ sub namespace {
my $self = shift;

# Prefix
return if (my $current = $self->tree)->[0] eq 'root';
return '' if (my $current = $self->tree)->[0] eq 'root';
my $prefix = $current->[1] =~ /^(.*?)\:/ ? $1 : '';

# Walk tree
while ($current) {
return if $current->[0] eq 'root';
return '' if $current->[0] eq 'root';
my $attrs = $current->[2];

# Namespace for prefix
Expand All @@ -147,11 +147,13 @@ sub namespace {
}

# Namespace attribute
elsif (defined $attrs->{xmlns}) { return $attrs->{xmlns} || undef }
elsif (defined $attrs->{xmlns}) { return $attrs->{xmlns} }

# Parent
$current = $current->[3];
}

return '';
}

sub parent {
Expand Down Expand Up @@ -280,7 +282,7 @@ sub type {
my ($self, $type) = @_;

# Not a tag
return if (my $tree = $self->tree)->[0] eq 'root';
return '' if (my $tree = $self->tree)->[0] eq 'root';

# Get
return $tree->[1] unless $type;
Expand Down Expand Up @@ -318,7 +320,7 @@ sub _add {
}

sub _elements {
my $e = shift;
return [] unless my $e = shift;
return [@$e[($e->[0] eq 'root' ? 1 : 4) .. $#$e]];
}

Expand Down Expand Up @@ -391,7 +393,7 @@ sub _trim {
my ($e, $trim) = @_;

# Disabled
return 0 unless $trim = defined $trim ? $trim : 1;
return 0 unless $e && ($trim = defined $trim ? $trim : 1);

# Detect "pre" tag
while ($e->[0] eq 'tag') {
Expand Down
20 changes: 13 additions & 7 deletions t/mojo/dom.t
Expand Up @@ -2,7 +2,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 768;
use Test::More tests => 774;

# "Homer gave me a kidney: it wasn't his, I didn't need it,
# and it came postage due- but I appreciated the gesture!"
Expand Down Expand Up @@ -122,11 +122,17 @@ $simple->parent->attrs(bar => 'baz')->attrs({this => 'works', too => 'yea'});
is $simple->parent->attrs('bar'), 'baz', 'right parent attribute';
is $simple->parent->attrs('this'), 'works', 'right parent attribute';
is $simple->parent->attrs('too'), 'yea', 'right parent attribute';
is $dom->at('test#test')->type, 'test', 'right type';
is $dom->at('[class$="ing"]')->type, 'simple', 'right type';
is $dom->at('[class="working"]')->type, 'simple', 'right type';
is $dom->at('[class$=ing]')->type, 'simple', 'right type';
is $dom->at('[class=working]')->type, 'simple', 'right type';
is $dom->at('test#test')->type, 'test', 'right type';
is $dom->at('[class$="ing"]')->type, 'simple', 'right type';
is $dom->at('[class="working"]')->type, 'simple', 'right type';
is $dom->at('[class$=ing]')->type, 'simple', 'right type';
is $dom->at('[class=working]')->type, 'simple', 'right type';
is $dom->at('[class=working]')->namespace, '', 'no namespace';
is $dom->at('foo')->namespace, '', 'no namespace';
is $dom->namespace, '', 'no namespace';
is $dom->type, '', 'no type';
is $dom->text_before, '', 'no text';
is $dom->text_after, '', 'no text';

# Class and ID
$dom = Mojo::DOM->new->parse('<div id="id" class="class">a</div>');
Expand Down Expand Up @@ -418,7 +424,7 @@ EOF
is $dom->xml, 1, 'xml mode detected';
is $dom->at('book comment')->namespace, 'uri:default-ns', 'right namespace';
is $dom->at('book comment')->text, 'rocks!', 'right text';
is $dom->at('book nons section')->namespace, undef, 'no namespace';
is $dom->at('book nons section')->namespace, '', 'no namespace';
is $dom->at('book nons section')->text, 'Nothing', 'right text';
is $dom->at('book meta number')->namespace, 'uri:isbn-ns', 'right namespace';
is $dom->at('book meta number')->text, '978-0596000271', 'right text';
Expand Down

0 comments on commit 102567b

Please sign in to comment.