Skip to content

Commit

Permalink
renamed value method to content
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 19, 2014
1 parent ba0ed6f commit 6ed2fc2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

4.68 2014-01-19
4.68 2014-01-20
- Added Mojo::DOM::Node.
- Added contents and node methods to Mojo::DOM.
- Removed deprecated http_proxy attribute from Mojo::UserAgent.
Expand Down
34 changes: 17 additions & 17 deletions lib/Mojo/DOM/Node.pm
@@ -1,9 +1,16 @@
package Mojo::DOM::Node;
use Mojo::Base -base;
use overload bool => sub {1}, '""' => sub { shift->value }, fallback => 1;
use overload bool => sub {1}, '""' => sub { shift->content }, fallback => 1;

has [qw(parent tree)];

sub content {
my $self = shift;
return $self->tree->[1] unless @_;
$self->tree->[1] = shift;
return $self;
}

sub node { shift->tree->[0] }

sub remove {
Expand All @@ -18,13 +25,6 @@ sub remove {
return $parent;
}

sub value {
my $self = shift;
return $self->tree->[1] unless @_;
$self->tree->[1] = shift;
return $self;
}

1;

=encoding utf8
Expand All @@ -38,7 +38,7 @@ Mojo::DOM::Node - DOM Node
use Mojo::DOM::Node;
my $node = Mojo::DOM::Node->new(parent => $parent, tree => $tree);
say $node->value;
say $node->content;
=head1 DESCRIPTION
Expand Down Expand Up @@ -68,6 +68,14 @@ carefully since it is very dynamic.
L<Mojo::DOM::Node> inherits all methods from L<Mojo::Base> and implements the
following new ones.
=head2 content
my $content = $node->content;
$node = $node->content('foo');
my $content = "$node";
Node content.
=head2 node
my $type = $node->node;
Expand All @@ -80,14 +88,6 @@ Node type, usually C<cdata>, C<comment>, C<doctype>, C<pi>, C<raw> or C<text>.
Remove node and return L<Mojo::DOM> object for parent of node.
=head2 value
my $value = $node->value;
$node = $node->value('foo');
my $value = "$node";
Node value.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
27 changes: 14 additions & 13 deletions t/mojo/dom.t
Expand Up @@ -146,27 +146,28 @@ ok !$dom->at('simple')->ancestors->first->xml, 'XML mode not active';
$dom = Mojo::DOM->new(
'<!DOCTYPE before><p>test<![CDATA[123]]><!-- 456 --></p><?after?>');
is $dom->contents->[1]->contents->first->parent->type, 'p', 'right type';
is $dom->contents->[1]->contents->first->value, 'test', 'right value';
is $dom->contents->[1]->contents->first, 'test', 'right value';
is $dom->contents->[1]->contents->first->content, 'test', 'right content';
is $dom->contents->[1]->contents->first, 'test', 'right content';
is $dom->at('p')->contents->first->node, 'text', 'right node';
is $dom->at('p')->contents->first->remove->type, 'p', 'right type';
is $dom->at('p')->contents->first->node, 'cdata', 'right node';
is $dom->at('p')->contents->first->value, '123', 'right value';
is $dom->at('p')->contents->[1]->node, 'comment', 'right node';
is $dom->at('p')->contents->[1]->value, ' 456 ', 'right value';
is $dom->contents->first->node, 'doctype', 'right node';
is $dom->contents->first->value, ' before', 'right value';
is $dom->contents->[2]->node, 'pi', 'right node';
is $dom->contents->[2]->value, 'after', 'right value';
is $dom->contents->first->value(' again')->value, ' again', 'right value';
is $dom->at('p')->contents->first->node, 'cdata', 'right node';
is $dom->at('p')->contents->first->content, '123', 'right content';
is $dom->at('p')->contents->[1]->node, 'comment', 'right node';
is $dom->at('p')->contents->[1]->content, ' 456 ', 'right content';
is $dom->contents->first->node, 'doctype', 'right node';
is $dom->contents->first->content, ' before', 'right content';
is $dom->contents->[2]->node, 'pi', 'right node';
is $dom->contents->[2]->content, 'after', 'right content';
is $dom->contents->first->content(' again')->content, ' again',
'right content';
is $dom->contents->grep(sub { $_->node eq 'pi' })->remove->first->node,
'root', 'right node';
is "$dom", '<!DOCTYPE again><p><![CDATA[123]]><!-- 456 --></p>',
'right result';
$dom = Mojo::DOM->new('<script>la<la>la</script>');
is $dom->at('script')->node, 'tag', 'right node';
is $dom->at('script')->contents->first->node, 'raw', 'right node';
is $dom->at('script')->contents->first->value, 'la<la>la', 'right value';
is $dom->at('script')->contents->first->node, 'raw', 'right node';
is $dom->at('script')->contents->first->content, 'la<la>la', 'right content';
is "$dom", '<script>la<la>la</script>', 'right result';

# Class and ID
Expand Down

0 comments on commit 6ed2fc2

Please sign in to comment.