Skip to content

Commit

Permalink
fixed attribute namespace selector bugs in Mojo::DOM::CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 27, 2012
1 parent 8cb4b9f commit e6027fb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@ This file documents the revision history for Perl extension Mojolicious.
2.92 2012-04-27
- Improved documentation.
- Improved tests.
- Fixed attribute namespace selector bugs in Mojo::DOM::CSS.

2.91 2012-04-26
- Added cookies method to Mojo::Message.
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/DOM.pm
Expand Up @@ -67,8 +67,7 @@ sub attrs {
return $attrs->{$_[0]} unless @_ > 1 || ref $_[0];

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

return $self;
}
Expand Down Expand Up @@ -584,10 +583,10 @@ selectors from L<Mojo::DOM::CSS> are supported.
Find element namespace.
# Find namespace for an element with namespace prefix
my $namespace = $dom->at('svg\:circle')->namespace;
my $namespace = $dom->at('svg > svg\:circle')->namespace;
# Find namespace for an element that may or may not have a namespace prefix
my $namespace = $dom->at('circle')->namespace;
my $namespace = $dom->at('svg > circle')->namespace;
=head2 C<parent>
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/DOM/CSS.pm
Expand Up @@ -80,7 +80,7 @@ sub _attr {
# Ignore namespace prefix
my $attrs = $current->[2];
for my $name (keys %$attrs) {
next unless $name =~ /\:?$key$/;
next unless $name =~ /(?:^|\:)$key$/;
return 1 unless defined $attrs->{$name} && defined $regex;
return 1 if $attrs->{$name} =~ $regex;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Util.pm
Expand Up @@ -28,7 +28,7 @@ my $DELIMITER = chr 0x2D;
my %ENTITIES;
{
open my $entities, '<', catfile(dirname(__FILE__), 'entities.txt');
$_ =~ /^(\S+)\s+U\+(\S+)/ and $ENTITIES{$1} = chr hex($2) for <$entities>;
/^(\S+)\s+U\+(\S+)/ and $ENTITIES{$1} = chr hex($2) for <$entities>;
}

# Reverse entities for html_escape (without "apos")
Expand Down
17 changes: 16 additions & 1 deletion t/mojo/dom.t
Expand Up @@ -2,7 +2,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 741;
use Test::More tests => 755;

# "Homer gave me a kidney: it wasn't his, I didn't need it,
# and it came postage due- but I appreciated the gesture!"
Expand Down Expand Up @@ -415,6 +415,21 @@ is $dom->at('book nons section')->namespace, undef, 'no namespace';
is $dom->at('book nons section')->text, 'Nothing', 'right text';
is $dom->at('book meta number')->namespace, 'uri:isbn-ns', 'right namespace';
is $dom->at('book meta number')->text, '978-0596000271', 'right text';
is $dom->children('bk:book')->first->{xmlns}, 'uri:default-ns',
'right attribute';
is $dom->children('k:book')->first, undef, 'no result';
is $dom->children('book')->first, undef, 'no result';
is $dom->children('ook')->first, undef, 'no result';
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'), undef, 'no attribute';
is $dom->at('[bk]')->attrs('bk'), undef, 'no attribute';
is $dom->at('[bk]')->attrs('k'), undef, 'no attribute';
is $dom->at('[s\:bk]'), undef, 'no result';
is $dom->at('[k]'), undef, 'no result';

# Yadis
$dom = Mojo::DOM->new->parse(<<'EOF');
Expand Down

0 comments on commit e6027fb

Please sign in to comment.