Skip to content

Commit

Permalink
more consistent return values in Mojo::DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 30, 2014
1 parent 4cd0088 commit 5420cdb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
10 changes: 5 additions & 5 deletions lib/Mojo/DOM.pm
Expand Up @@ -58,7 +58,7 @@ sub attr {
return $attrs unless @_;

# Get
return $attrs->{$_[0]} // '' unless @_ > 1 || ref $_[0];
return $attrs->{$_[0]} unless @_ > 1 || ref $_[0];

# Set
my $values = ref $_[0] ? $_[0] : {@_};
Expand Down Expand Up @@ -96,7 +96,7 @@ sub match { $_[0]->_css->match($_[1]) ? $_[0] : undef }
sub namespace {
my $self = shift;

return '' if (my $tree = $self->tree)->[0] ne 'tag';
return undef if (my $tree = $self->tree)->[0] ne 'tag';

# Extract namespace prefix and search parents
my $ns = $tree->[1] =~ /^(.*?):/ ? "xmlns:$1" : undef;
Expand All @@ -110,7 +110,7 @@ sub namespace {
elsif (defined $attrs->{xmlns}) { return $attrs->{xmlns} }
}

return '';
return undef;
}

sub new {
Expand Down Expand Up @@ -178,7 +178,7 @@ sub tree { shift->_delegate(tree => @_) }

sub type {
my ($self, $type) = @_;
return '' if (my $tree = $self->tree)->[0] ne 'tag';
return undef if (my $tree = $self->tree)->[0] ne 'tag';
return $tree->[1] unless $type;
$tree->[1] = $type;
return $self;
Expand Down Expand Up @@ -683,7 +683,7 @@ L<Mojo::DOM::CSS/"SELECTORS"> are supported.
my $namespace = $dom->namespace;
Find this element's namespace.
Find this element's namespace or return C<undef> if none could be found.
# Find namespace for an element with namespace prefix
my $namespace = $dom->at('svg > svg\:circle')->namespace;
Expand Down
24 changes: 12 additions & 12 deletions t/mojo/dom.t
Expand Up @@ -95,9 +95,9 @@ $dom = Mojo::DOM->new->parse(<<EOF);
</foo>
EOF
ok !$dom->xml, 'XML mode not detected';
is $dom->type, '', 'no type';
is $dom->attr('foo'), '', 'no attribute';
is $dom->attr(foo => 'bar')->attr('foo'), '', 'no attribute';
is $dom->type, undef, 'no type';
is $dom->attr('foo'), undef, 'no attribute';
is $dom->attr(foo => 'bar')->attr('foo'), undef, 'no attribute';
is $dom->tree->[1][0], 'doctype', 'right element';
is $dom->tree->[1][1], ' foo', 'right doctype';
is "$dom", <<EOF, 'right result';
Expand Down Expand Up @@ -278,10 +278,10 @@ is $dom->contents->first->at('b'), undef, 'no result';
is $dom->contents->first->find('*')->size, 0, 'no results';
is $dom->contents->first->match('*'), undef, 'no match';
is_deeply $dom->contents->first->attr, {}, 'no attributes';
is $dom->contents->first->namespace, '', 'no namespace';
is $dom->contents->first->type, '', 'no type';
is $dom->contents->first->text, '', 'no text';
is $dom->contents->first->all_text, '', 'no text';
is $dom->contents->first->namespace, undef, 'no namespace';
is $dom->contents->first->type, undef, 'no type';
is $dom->contents->first->text, '', 'no text';
is $dom->contents->first->all_text, '', 'no text';

# Class and ID
$dom = Mojo::DOM->new('<div id="id" class="class">a</div>');
Expand Down Expand Up @@ -605,7 +605,7 @@ $dom = Mojo::DOM->new(<<EOF);
</bk:book>
EOF
ok $dom->xml, 'XML mode detected';
is $dom->namespace, '', 'no namespace';
is $dom->namespace, undef, 'no namespace';
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, '', 'no namespace';
Expand All @@ -622,9 +622,9 @@ is $dom->at('ook'), undef, 'no result';
is $dom->at('[xmlns\:bk]')->{'xmlns:bk'}, 'uri:book-ns', 'right attribute';
is $dom->at('[bk]')->{'xmlns:bk'}, 'uri:book-ns', 'right attribute';
is $dom->at('[bk]')->attr('xmlns:bk'), 'uri:book-ns', 'right attribute';
is $dom->at('[bk]')->attr('s:bk'), '', 'no attribute';
is $dom->at('[bk]')->attr('bk'), '', 'no attribute';
is $dom->at('[bk]')->attr('k'), '', 'no attribute';
is $dom->at('[bk]')->attr('s:bk'), undef, 'no attribute';
is $dom->at('[bk]')->attr('bk'), undef, 'no attribute';
is $dom->at('[bk]')->attr('k'), undef, 'no attribute';
is $dom->at('[s\:bk]'), undef, 'no result';
is $dom->at('[k]'), undef, 'no result';
is $dom->at('number')->ancestors('meta')->first->{xmlns}, 'uri:meta-ns',
Expand All @@ -646,7 +646,7 @@ is $dom->at('foo bar baz')->text, 'First', 'right text';
is $dom->at('baz')->namespace, 'uri:first', 'right namespace';
is $dom->at('foo bar ya\.da')->text, 'Second', 'right text';
is $dom->at('ya\.da')->namespace, 'uri:second', 'right namespace';
is $dom->at('foo')->namespace, '', 'no namespace';
is $dom->at('foo')->namespace, undef, 'no namespace';
is $dom->at('[xml\.s]'), undef, 'no result';
is $dom->at('b\.z'), undef, 'no result';

Expand Down

0 comments on commit 5420cdb

Please sign in to comment.