Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
more Mojo::DOM examples
  • Loading branch information
kraih committed Jan 9, 2012
1 parent 5adc2b2 commit cf958bb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
31 changes: 18 additions & 13 deletions lib/Mojo/DOM.pm
Expand Up @@ -67,8 +67,7 @@ sub attrs {
my $self = shift;

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

# Hash
my $attrs = $tree->[2];
Expand All @@ -79,9 +78,7 @@ sub attrs {

# Set
my $values = ref $_[0] ? $_[0] : {@_};
for my $key (keys %$values) {
$attrs->{$key} = $values->{$key};
}
$attrs->{$_} = $values->{$_} for keys %$values;

return $self;
}
Expand Down Expand Up @@ -152,8 +149,7 @@ sub namespace {
my $self = shift;

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

Expand Down Expand Up @@ -181,8 +177,7 @@ sub parent {
my $self = shift;

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

# Parent
return $self->new->charset($self->charset)->tree($tree->[3])
Expand Down Expand Up @@ -280,8 +275,7 @@ sub type {
my ($self, $type) = @_;

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

# Get
return $tree->[1] unless $type;
Expand All @@ -307,8 +301,7 @@ sub _add {
$new = $self->_parse("$new");

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

# Find
my $parent = $tree->[3];
Expand Down Expand Up @@ -478,6 +471,12 @@ Extract all text content from DOM structure, smart whitespace trimming is
activated by default. Note that the trim argument of this method is
EXPERIMENTAL and might change without warning!
# "test 123"
$dom->parse("<div>test\n<p>123</p></div>")->div->all_text;
# "test\n123"
$dom->parse("<div>test\n<p>123</p></div>")->div->all_text(0);
=head2 C<append>
$dom = $dom->append('<p>Hi!</p>');
Expand Down Expand Up @@ -615,6 +614,12 @@ Extract text content from element only (not including child elements), smart
whitespace trimming is activated by default. Note that the trim argument of
this method is EXPERIMENTAL and might change without warning!
# "test"
$dom->parse("<div>test\n<p>123</p></div>")->div->text;
# "test\n"
$dom->parse("<div>test\n<p>123</p></div>")->div->text(0);
=head2 C<to_xml>
my $xml = $dom->to_xml;
Expand Down
1 change: 1 addition & 0 deletions lib/Mojo/Message.pm
Expand Up @@ -682,6 +682,7 @@ perform a C<find> on it right away, which returns a collection.
# Use everything else Mojo::DOM has to offer
say $message->dom->at('title')->text;
$message->dom->html->body->children->each(sub { say $_->type });
=head2 C<error>
Expand Down

0 comments on commit cf958bb

Please sign in to comment.