Navigation Menu

Skip to content

Commit

Permalink
deprecated Mojo::DOM::attrs in favor of Mojo::DOM::attr
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 28, 2013
1 parent 4b38124 commit 1bf35c3
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 70 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

4.20 2013-07-27
4.20 2013-07-28
- Deprecated Mojo::DOM::attrs in favor of Mojo::DOM::attr.
- Improved Mojo::UserAgent connection management to be fork-safe.

4.19 2013-07-21
Expand Down
23 changes: 15 additions & 8 deletions lib/Mojo/DOM.pm
@@ -1,7 +1,7 @@
package Mojo::DOM;
use Mojo::Base -base;
use overload
'%{}' => sub { shift->attrs },
'%{}' => sub { shift->attr },
'bool' => sub {1},
'""' => sub { shift->to_xml },
fallback => 1;
Expand All @@ -12,7 +12,7 @@ use Carp 'croak';
use Mojo::Collection;
use Mojo::DOM::CSS;
use Mojo::DOM::HTML;
use Mojo::Util 'squish';
use Mojo::Util qw(deprecated squish);
use Scalar::Util qw(blessed weaken);

sub AUTOLOAD {
Expand Down Expand Up @@ -52,7 +52,7 @@ sub append_content {

sub at { shift->find(@_)->[0] }

sub attrs {
sub attr {
my $self = shift;

# Hash
Expand All @@ -69,6 +69,12 @@ sub attrs {
return $self;
}

# DEPRECATED in Top Hat!
sub attrs {
deprecated 'Mojo::DOM::attrs is DEPRECATED in favor of Mojo::DOM::attr';
shift->attr(@_);
}

sub children {
my ($self, $type) = @_;

Expand Down Expand Up @@ -478,12 +484,12 @@ L<Mojo::DOM::CSS> are supported.
# Find first element with "svg" namespace definition
my $namespace = $dom->at('[xmlns\:svg]')->{'xmlns:svg'};
=head2 attrs
=head2 attr
my $attrs = $dom->attrs;
my $foo = $dom->attrs('foo');
$dom = $dom->attrs({foo => 'bar'});
$dom = $dom->attrs(foo => 'bar');
my $attrs = $dom->attr;
my $foo = $dom->attr('foo');
$dom = $dom->attr({foo => 'bar'});
$dom = $dom->attr(foo => 'bar');
Element attributes.
Expand Down Expand Up @@ -520,6 +526,7 @@ L<Mojo::DOM::CSS> are supported.
# Extract information from multiple elements
my @headers = $dom->find('h1, h2, h3')->pluck('text')->each;
my @links = $dom->find('a[href]')->pluck(attr => 'href')->each;
=head2 namespace
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/get.pm
Expand Up @@ -135,7 +135,7 @@ sub _select {
# Attribute
elsif ($command eq 'attr') {
next unless my $name = shift @args;
_say($_->attrs->{$name}) for @$results;
_say($_->attr->{$name}) for @$results;
}

# Unknown
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -51,7 +51,7 @@ sub _perldoc {
my $perldoc = $self->url_for('/perldoc/');
$dom->find('a[href]')->each(
sub {
my $attrs = shift->attrs;
my $attrs = shift->attr;
$attrs->{href} =~ s!%3A%3A!/!gi
if $attrs->{href} =~ s!^http://search\.cpan\.org/perldoc\?!$perldoc!;
}
Expand All @@ -62,7 +62,7 @@ sub _perldoc {
sub {
my $e = shift;
return if $e->all_text =~ /^\s*\$\s+/m;
my $attrs = $e->attrs;
my $attrs = $e->attr;
my $class = $attrs->{class};
$attrs->{class} = defined $class ? "$class prettyprint" : 'prettyprint';
}
Expand Down
114 changes: 57 additions & 57 deletions t/mojo/dom.t
Expand Up @@ -13,8 +13,8 @@ is_deeply \@div, [qw(A B)], 'found all div elements with id';
@div = ();
$dom->find('div[id]')->each(sub { push @div, $_->text });
is_deeply \@div, [qw(A B)], 'found all div elements with id';
is $dom->at('#a')->attrs('foo'), 0, 'right attribute';
is $dom->at('#a')->attrs->{foo}, 0, 'right attribute';
is $dom->at('#a')->attr('foo'), 0, 'right attribute';
is $dom->at('#a')->attr->{foo}, 0, 'right attribute';
is "$dom", '<div><div foo="0" id="a">A</div><div id="b">B</div></div>',
'right result';

Expand Down Expand Up @@ -85,8 +85,8 @@ $dom = Mojo::DOM->new->parse(<<EOF);
EOF
ok !$dom->xml, 'XML mode not detected';
is $dom->type, '', 'no type';
is $dom->attrs('foo'), '', 'no attribute';
is $dom->attrs(foo => 'bar')->attrs('foo'), '', 'no attribute';
is $dom->attr('foo'), '', 'no attribute';
is $dom->attr(foo => 'bar')->attr('foo'), '', 'no attribute';
is $dom->tree->[1][0], 'doctype', 'right element';
is $dom->tree->[1][1], ' foo', 'right doctype';
is "$dom", <<EOF, 'right result';
Expand All @@ -109,17 +109,17 @@ my $simple = $dom->at('foo simple.working[class^="wor"]');
is $simple->parent->all_text,
'test easy works well yada yada < very broken more text', 'right text';
is $simple->type, 'simple', 'right type';
is $simple->attrs('class'), 'working', 'right class attribute';
is $simple->attr('class'), 'working', 'right class attribute';
is $simple->text, 'easy', 'right text';
is $simple->parent->type, 'foo', 'right parent type';
is $simple->parent->attrs->{bar}, 'ba<z', 'right parent attribute';
is $simple->parent->attr->{bar}, 'ba<z', 'right parent attribute';
is $simple->parent->children->[1]->type, 'test', 'right sibling';
is $simple->to_xml, '<simple class="working">easy</simple>',
'stringified right';
$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';
$simple->parent->attr(bar => 'baz')->attr({this => 'works', too => 'yea'});
is $simple->parent->attr('bar'), 'baz', 'right parent attribute';
is $simple->parent->attr('this'), 'works', 'right parent attribute';
is $simple->parent->attr('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';
Expand Down Expand Up @@ -162,13 +162,13 @@ $dom = Mojo::DOM->new->parse(<<EOF);
</html>
EOF
my $p = $dom->find('body > #container > div p[id]');
is $p->[0]->attrs('id'), 'foo', 'right id attribute';
is $p->[0]->attr('id'), 'foo', 'right id attribute';
is $p->[1], undef, 'no second result';
is $p->size, 1, 'right number of elements';
my @p;
@div = ();
$dom->find('div')->each(sub { push @div, $_->attrs('id') });
$dom->find('p')->each(sub { push @p, $_->attrs('id') });
$dom->find('div')->each(sub { push @div, $_->attr('id') });
$dom->find('p')->each(sub { push @p, $_->attr('id') });
is_deeply \@p, [qw(foo bar)], 'found all p elements';
my $ids = [qw(container header logo buttons buttons content)];
is_deeply \@div, $ids, 'found all div elements';
Expand Down Expand Up @@ -402,8 +402,8 @@ $dom = Mojo::DOM->new->parse(<<EOF);
</rss>
EOF
ok $dom->xml, 'XML mode detected';
is $dom->find('rss')->[0]->attrs('version'), '2.0', 'right version';
is $dom->at('extension')->attrs('foo:id'), 'works', 'right id';
is $dom->find('rss')->[0]->attr('version'), '2.0', 'right version';
is $dom->at('extension')->attr('foo:id'), 'works', 'right id';
like $dom->at('#works')->text, qr/\[awesome\]\]/, 'right text';
like $dom->at('[id="works"]')->text, qr/\[awesome\]\]/, 'right text';
is $dom->find('description')->[1]->text, '<p>trololololo>', 'right text';
Expand Down Expand Up @@ -450,10 +450,10 @@ is $dom->at('k\:book'), undef, 'no result';
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]')->attrs('xmlns:bk'), 'uri:book-ns', 'right attribute';
is $dom->at('[bk]')->attrs('s:bk'), '', 'no attribute';
is $dom->at('[bk]')->attrs('bk'), '', 'no attribute';
is $dom->at('[bk]')->attrs('k'), '', 'no 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('[s\:bk]'), undef, 'no result';
is $dom->at('[k]'), undef, 'no result';

Expand Down Expand Up @@ -562,20 +562,20 @@ is_deeply \@numbers, [1, 1, 2, 2, 3, 3], 'right order';

# Attributes on multiple lines
$dom = Mojo::DOM->new->parse("<div test=23 id='a' \n class='x' foo=bar />");
is $dom->at('div.x')->attrs('test'), 23, 'right attribute';
is $dom->at('[foo="bar"]')->attrs('class'), 'x', 'right attribute';
is $dom->at('div.x')->attr('test'), 23, 'right attribute';
is $dom->at('[foo="bar"]')->attr('class'), 'x', 'right attribute';

# Markup characters in attribute values
$dom = Mojo::DOM->new->parse(
qq{<div id="<a>" \n test='='>Test<div id='><' /></div>});
is $dom->at('div[id="<a>"]')->attrs->{test}, '=', 'right attribute';
is $dom->at('div[id="<a>"]')->attr->{test}, '=', 'right attribute';
is $dom->at('[id="<a>"]')->text, 'Test', 'right text';
is $dom->at('[id="><"]')->attrs->{id}, '><', 'right attribute';
is $dom->at('[id="><"]')->attr->{id}, '><', 'right attribute';

# Empty attributes
$dom = Mojo::DOM->new->parse(qq{<div test="" test2='' />});
is $dom->at('div')->attrs->{test}, '', 'empty attribute value';
is $dom->at('div')->attrs->{test2}, '', 'empty attribute value';
is $dom->at('div')->attr->{test}, '', 'empty attribute value';
is $dom->at('div')->attr->{test2}, '', 'empty attribute value';

# Whitespaces before closing bracket
$dom = Mojo::DOM->new->parse(qq{<div >content</div>});
Expand Down Expand Up @@ -677,14 +677,14 @@ is $dom->find(':root')->[0]->type, 'form', 'right type';
is $dom->find('*:root')->[0]->type, 'form', 'right type';
is $dom->find('form:root')->[0]->type, 'form', 'right type';
is $dom->find(':root')->[1], undef, 'no result';
is $dom->find(':checked')->[0]->attrs->{name}, 'groovy', 'right name';
is $dom->find('option:checked')->[0]->attrs->{value}, 'e', 'right value';
is $dom->find(':checked')->[0]->attr->{name}, 'groovy', 'right name';
is $dom->find('option:checked')->[0]->attr->{value}, 'e', 'right value';
is $dom->find(':checked')->[1]->text, 'E', 'right text';
is $dom->find('*:checked')->[1]->text, 'E', 'right text';
is $dom->find(':checked')->[2]->text, 'H', 'right name';
is $dom->find(':checked')->[3]->attrs->{name}, 'I', 'right name';
is $dom->find(':checked')->[3]->attr->{name}, 'I', 'right name';
is $dom->find(':checked')->[4], undef, 'no result';
is $dom->find('option[selected]')->[0]->attrs->{value}, 'e', 'right value';
is $dom->find('option[selected]')->[0]->attr->{value}, 'e', 'right value';
is $dom->find('option[selected]')->[1]->text, 'H', 'right text';
is $dom->find('option[selected]')->[2], undef, 'no result';
is $dom->find(':checked[value="e"]')->[0]->text, 'E', 'right text';
Expand All @@ -697,9 +697,9 @@ is $dom->at('optgroup > :checked[value="e"]')->text, 'E', 'right text';
is $dom->at('select *:checked[value="e"]')->text, 'E', 'right text';
is $dom->at('optgroup > *:checked[value="e"]')->text, 'E', 'right text';
is $dom->find(':checked[value="e"]')->[1], undef, 'no result';
is $dom->find(':empty')->[0]->attrs->{name}, 'user', 'right name';
is $dom->find('input:empty')->[0]->attrs->{name}, 'user', 'right name';
is $dom->at(':empty[type^="ch"]')->attrs->{name}, 'groovy', 'right name';
is $dom->find(':empty')->[0]->attr->{name}, 'user', 'right name';
is $dom->find('input:empty')->[0]->attr->{name}, 'user', 'right name';
is $dom->at(':empty[type^="ch"]')->attr->{name}, 'groovy', 'right name';

# More pseudo classes
$dom = Mojo::DOM->new->parse(<<EOF);
Expand Down Expand Up @@ -1132,7 +1132,7 @@ is $dom->find('div > p')->[4]->text, 'E', 'right text';
is $dom->find('div > p')->[5]->text, 'F G', 'right text';
is $dom->find('div > p')->[6]->text, 'H', 'right text';
is $dom->find('div > p > p')->[0], undef, 'no results';
is $dom->at('div > p > img')->attrs->{src}, 'foo.png', 'right attribute';
is $dom->at('div > p > img')->attr->{src}, 'foo.png', 'right attribute';
is $dom->at('div > div')->text, 'X', 'right text';

# Optional "dt" and "dd" tags
Expand Down Expand Up @@ -1205,13 +1205,13 @@ $dom = Mojo::DOM->new->parse(<<EOF);
<col id=bar>
</table>
EOF
is $dom->find('table > col')->[0]->attrs->{id}, 'morefail', 'right attribute';
is $dom->find('table > col')->[1]->attrs->{id}, 'fail', 'right attribute';
is $dom->find('table > colgroup > col')->[0]->attrs->{id}, 'foo',
is $dom->find('table > col')->[0]->attr->{id}, 'morefail', 'right attribute';
is $dom->find('table > col')->[1]->attr->{id}, 'fail', 'right attribute';
is $dom->find('table > colgroup > col')->[0]->attr->{id}, 'foo',
'right attribute';
is $dom->find('table > colgroup > col')->[1]->attrs->{class}, 'foo',
is $dom->find('table > colgroup > col')->[1]->attr->{class}, 'foo',
'right attribute';
is $dom->find('table > colgroup > col')->[2]->attrs->{id}, 'bar',
is $dom->find('table > colgroup > col')->[2]->attr->{id}, 'bar',
'right attribute';

# Optional "thead", "tbody", "tfoot", "tr", "th" and "td" tags
Expand Down Expand Up @@ -1254,13 +1254,13 @@ $dom = Mojo::DOM->new->parse(<<EOF);
<td>B
</table>
EOF
is $dom->find('table > col')->[0]->attrs->{id}, 'morefail', 'right attribute';
is $dom->find('table > col')->[1]->attrs->{id}, 'fail', 'right attribute';
is $dom->find('table > colgroup > col')->[0]->attrs->{id}, 'foo',
is $dom->find('table > col')->[0]->attr->{id}, 'morefail', 'right attribute';
is $dom->find('table > col')->[1]->attr->{id}, 'fail', 'right attribute';
is $dom->find('table > colgroup > col')->[0]->attr->{id}, 'foo',
'right attribute';
is $dom->find('table > colgroup > col')->[1]->attrs->{class}, 'foo',
is $dom->find('table > colgroup > col')->[1]->attr->{class}, 'foo',
'right attribute';
is $dom->find('table > colgroup > col')->[2]->attrs->{id}, 'bar',
is $dom->find('table > colgroup > col')->[2]->attr->{id}, 'bar',
'right attribute';
is $dom->at('table > thead > tr > th')->text, 'A', 'right text';
is $dom->find('table > thead > tr > th')->[1]->text, 'D', 'right text';
Expand All @@ -1280,11 +1280,11 @@ $dom = Mojo::DOM->new->parse(<<EOF);
<td>B
</table>
EOF
is $dom->find('table > colgroup > col')->[0]->attrs->{id}, 'foo',
is $dom->find('table > colgroup > col')->[0]->attr->{id}, 'foo',
'right attribute';
is $dom->find('table > colgroup > col')->[1]->attrs->{class}, 'foo',
is $dom->find('table > colgroup > col')->[1]->attr->{class}, 'foo',
'right attribute';
is $dom->find('table > colgroup > col')->[2]->attrs->{id}, 'bar',
is $dom->find('table > colgroup > col')->[2]->attr->{id}, 'bar',
'right attribute';
is $dom->at('table > tbody > tr > td')->text, 'B', 'right text';

Expand Down Expand Up @@ -1436,11 +1436,11 @@ $dom = Mojo::DOM->new->parse(<<EOF);
</html>
EOF
is $dom->at('title')->text, 'Foo', 'right text';
is $dom->find('html > head > script')->[0]->attrs('src'), '/js/one.js',
is $dom->find('html > head > script')->[0]->attr('src'), '/js/one.js',
'right attribute';
is $dom->find('html > head > script')->[1]->attrs('src'), '/js/two.js',
is $dom->find('html > head > script')->[1]->attr('src'), '/js/two.js',
'right attribute';
is $dom->find('html > head > script')->[2]->attrs('src'), '/js/three.js',
is $dom->find('html > head > script')->[2]->attr('src'), '/js/three.js',
'right attribute';
is $dom->find('html > head > script')->[2]->text, '', 'no text';
is $dom->at('html > body')->text, 'Bar', 'right text';
Expand All @@ -1459,11 +1459,11 @@ $dom = Mojo::DOM->new->parse(<<EOF);
</html>
EOF
is $dom->at('title')->text, 'Foo', 'right text';
is $dom->find('html > head > script')->[0]->attrs('src'), '/js/one.js',
is $dom->find('html > head > script')->[0]->attr('src'), '/js/one.js',
'right attribute';
is $dom->find('html > head > script')->[1]->attrs('src'), '/js/two.js',
is $dom->find('html > head > script')->[1]->attr('src'), '/js/two.js',
'right attribute';
is $dom->find('html > head > script')->[2]->attrs('src'), '/js/three.js',
is $dom->find('html > head > script')->[2]->attr('src'), '/js/three.js',
'right attribute';
is $dom->find('html > head > script')->[2]->text, '', 'no text';
is $dom->at('html > body')->text, 'Bar', 'right text';
Expand All @@ -1481,7 +1481,7 @@ $dom = Mojo::DOM->new->parse(<<EOF);
</root>
EOF
ok $dom->xml, 'XML mode detected';
is $dom->at('root')->attrs('att'), 'test', 'right attribute';
is $dom->at('root')->attr('att'), 'test', 'right attribute';
is $dom->tree->[5][1], ' root [
<!ELEMENT root (#PCDATA)>
<!ATTLIST root att CDATA #REQUIRED>
Expand Down Expand Up @@ -1519,7 +1519,7 @@ is $dom->tree->[3][1], ' foo [
<!ENTITY % e SYSTEM "myentities.ent">
%myentities;
] ', 'right doctype';
is $dom->at('foo')->attrs->{'xml:lang'}, 'de', 'right attribute';
is $dom->at('foo')->attr->{'xml:lang'}, 'de', 'right attribute';
is $dom->at('foo')->text, 'Check!', 'right text';
$dom = Mojo::DOM->new->parse(<<EOF);
<!DOCTYPE TESTSUITE PUBLIC "my.dtd" 'mhhh' [
Expand All @@ -1541,7 +1541,7 @@ is $dom->tree->[1][1], ' TESTSUITE PUBLIC "my.dtd" \'mhhh\' [
<!-- This is a comment -->
<!NOTATION hmmm SYSTEM "hmmm">
] ', 'right doctype';
is $dom->at('foo')->attrs('bar'), 'false', 'right attribute';
is $dom->at('foo')->attr('bar'), 'false', 'right attribute';

# Broken "font" block and useless end tags
$dom = Mojo::DOM->new->parse(<<EOF);
Expand Down Expand Up @@ -1672,9 +1672,9 @@ $dom = Mojo::DOM->new->parse(<<'EOF');
</html>
EOF
is $dom->at('#screw-up > b')->text, '>la<>la<<>>la<', 'right text';
is $dom->at('#screw-up .ewww > a > img')->attrs('src'), '/test.png',
is $dom->at('#screw-up .ewww > a > img')->attr('src'), '/test.png',
'right attribute';
is $dom->find('#screw-up .ewww > a > img')->[1]->attrs('src'), '/test2.png',
is $dom->find('#screw-up .ewww > a > img')->[1]->attr('src'), '/test2.png',
'right attribute';
is $dom->find('#screw-up .ewww > a > img')->[2], undef, 'no result';
is $dom->find('#screw-up .ewww > a > img')->size, 2,
Expand Down
5 changes: 4 additions & 1 deletion t/pod_coverage.t
Expand Up @@ -7,5 +7,8 @@ plan skip_all => 'set TEST_POD to enable this test (developer only!)'
plan skip_all => 'Test::Pod::Coverage 1.04 required for this test!'
unless eval 'use Test::Pod::Coverage 1.04; 1';

# DEPRECATED in Top Hat!
my @tophat = qw(attrs);

# False positive constants
all_pod_coverage_ok({also_private => [qw(IPV6 TLS)]});
all_pod_coverage_ok({also_private => [qw(IPV6 TLS), @tophat]});

0 comments on commit 1bf35c3

Please sign in to comment.