Skip to content

Commit

Permalink
deprecated Mojo::DOM->new with arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 28, 2011
1 parent 6b04c9c commit e0f8e54
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 34 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@
This file documents the revision history for Perl extension Mojolicious.

2.14 2011-10-28 00:00:00
- Deprecated Mojo::DOM->new with arguments.
- Added EXPERIMENTAL --verbose flag to test command.

2.13 2011-10-28 00:00:00
Expand Down
44 changes: 15 additions & 29 deletions lib/Mojo/DOM.pm
Expand Up @@ -32,18 +32,14 @@ sub DESTROY { }
# I dunno. Internet?"
sub new {
my $class = shift;
my $self = bless [], ref $class || $class;
my $self = bless [Mojo::DOM::HTML->new], ref $class || $class;

# Input
# DEPRECATED in Leaf Fluttering In Wind!
my $input;
$input = shift if @_ % 2;

# Attributes
warn "Mojo::DOM->new with arguments is DEPRECATED!\n" if @_;
my %attrs = (@_);
my $html = $self->[0] = Mojo::DOM::HTML->new;
$html->tree($attrs{tree}) if $attrs{tree};
$html->charset($attrs{charset}) if exists $attrs{charset};
$html->xml($attrs{xml}) if exists $attrs{xml};
exists($attrs{$_}) and $self->$_($attrs{$_}) for qw/tree charset xml/;

# Parse right away
$self->parse($input) if defined $input;
Expand Down Expand Up @@ -126,7 +122,7 @@ sub children {

# Add child
push @children,
$self->new(charset => $self->charset, tree => $e, xml => $self->xml);
$self->new->charset($self->charset)->tree($e)->xml($self->xml);
}

return Mojo::Collection->new(@children);
Expand Down Expand Up @@ -158,9 +154,9 @@ sub find {
my $results = Mojo::DOM::CSS->new(tree => $self->tree)->select($selector);

# Upgrade results
@$results = map {
$self->new(charset => $self->charset, tree => $_, xml => $self->xml)
} @$results;
@$results =
map { $self->new->charset($self->charset)->tree($_)->xml($self->xml) }
@$results;

return Mojo::Collection->new(@$results);
}
Expand Down Expand Up @@ -210,11 +206,8 @@ sub parent {
return if $tree->[0] eq 'root';

# Parent
return $self->new(
charset => $self->charset,
tree => $tree->[3],
xml => $self->xml
);
return $self->new->charset($self->charset)->tree($tree->[3])
->xml($self->xml);
}

sub parse {
Expand Down Expand Up @@ -296,11 +289,7 @@ sub root {
$root = $parent;
}

return $self->new(
charset => $self->charset,
tree => $root,
xml => $self->xml
);
return $self->new->charset($self->charset)->tree($root)->xml($self->xml);
}

sub text {
Expand Down Expand Up @@ -508,9 +497,7 @@ following new ones.
=head2 C<new>
my $dom = Mojo::DOM->new;
my $dom = Mojo::DOM->new(xml => 1);
my $dom = Mojo::DOM->new('<foo bar="baz">test</foo>');
my $dom = Mojo::DOM->new('<foo bar="baz">test</foo>', xml => 1);
Construct a new L<Mojo::DOM> object.
Expand Down Expand Up @@ -567,7 +554,7 @@ Element attributes.
my $charset = $dom->charset;
$dom = $dom->charset('UTF-8');
Charset used for decoding and encoding HTML5/XML.
Alias for L<Mojo::DOM::HTML/"charset">.
=head2 C<children>
Expand Down Expand Up @@ -617,7 +604,7 @@ Parent of element.
$dom = $dom->parse('<foo bar="baz">test</foo>');
Parse HTML5/XML document with L<Mojo::DOM::HTML>.
Alias for L<Mojo::DOM::HTML/"parse">.
=head2 C<prepend>
Expand Down Expand Up @@ -682,7 +669,7 @@ Render DOM to XML.
my $tree = $dom->tree;
$dom = $dom->tree(['root', ['text', 'lalala']]);
Document Object Model.
Alias for L<Mojo::DOM::HTML/"tree">.
=head2 C<type>
Expand All @@ -696,8 +683,7 @@ Element type.
my $xml = $dom->xml;
$dom = $dom->xml(1);
Disable HTML5 semantics in parser and activate case sensitivity, defaults to
auto detection based on processing instructions.
Alias for L<Mojo::DOM::HTML/"xml">.
Note that this method is EXPERIMENTAL and might change without warning!
=head1 SEE ALSO
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Message.pm
Expand Up @@ -185,7 +185,7 @@ sub dom {
my $charset;
($self->headers->content_type || '') =~ /charset="?([^"\s;]+)"?/
and $charset = $1;
my $dom = $self->dom_class->new(charset => $charset)->parse($self->body);
my $dom = $self->dom_class->new->charset($charset)->parse($self->body);

# Find right away
return $dom->find(@_) if @_;
Expand Down
8 changes: 4 additions & 4 deletions t/mojo/dom.t
Expand Up @@ -211,7 +211,7 @@ is $dom->at('[id="sno\"wman"]')->text, '☃', 'right text';
my $unicode =
qq/<html><div id="☃x">Snowman<\/div><div class="x ♥">Heart<\/div><\/html>/;
encode 'UTF-8', $unicode;
$dom = Mojo::DOM->new(charset => 'UTF-8');
$dom = Mojo::DOM->new->charset('UTF-8');
$dom->parse($unicode);
is $dom->at("#\\\n\\002603x")->text, 'Snowman', 'right text';
is $dom->at('#\\2603 x')->text, 'Snowman', 'right text';
Expand Down Expand Up @@ -1639,7 +1639,7 @@ is $dom->find('table > td > tr > thead')->[2], undef, 'no result';
is $dom->find('table > td > tr > thead')->size, 2, 'right number of elements';

# Ensure XML semantics again
$dom = Mojo::DOM->new(xml => 1)->parse(<<'EOF');
$dom = Mojo::DOM->new->xml(1)->parse(<<'EOF');
<table>
<td>
<tr><thead>foo<thead></tr>
Expand Down Expand Up @@ -1707,7 +1707,7 @@ $dom->find('b')->each(
is_deeply \@results, [qw/baz yada/], 'right results';

# Autoload children in XML mode
$dom = Mojo::DOM->new(<<EOF, xml => 1);
$dom = Mojo::DOM->new->xml(1)->parse(<<EOF);
<a id="one">
<B class="two" test>
foo
Expand Down Expand Up @@ -1741,7 +1741,7 @@ is $dom->a->b->c->[2], undef, 'no result';
is $dom->a->b->c->size, 2, 'right number of elements';

# Direct hash access to attributes in XML mode
$dom = Mojo::DOM->new(<<EOF, xml => 1);
$dom = Mojo::DOM->new->xml(1)->parse(<<EOF);
<a id="one">
<B class="two" test>
foo
Expand Down

0 comments on commit e0f8e54

Please sign in to comment.