Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improved RFC 3986 compliance of Mojo::Parameters
  • Loading branch information
kraih committed Jul 23, 2012
1 parent 6f18adf commit aab2f1f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 34 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

3.13 2012-07-23
- Added remove method to Mojo::DOM.
- Improved RFC 3986 compliance of Mojo::Parameters.
- Improved Mojolicious::Plugin::Config log messages. (jberger)
- Improved documentation.
- Improved tests.
Expand Down
48 changes: 24 additions & 24 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -293,27 +293,6 @@ sub _pc {
return;
}

sub _sibling {
my ($self, $selectors, $current, $tree, $immediate) = @_;

# Find preceding elements
my $parent = $current->[3];
my $found;
my $start = $parent->[0] eq 'root' ? 1 : 4;
for my $e (@$parent[$start .. $#$parent]) {
return $found if $e eq $current;
next unless $e->[0] eq 'tag';

# "+" (immediately preceding sibling)
if ($immediate) { $found = $self->_combinator($selectors, $e, $tree) }

# "~" (preceding sibling)
else { return 1 if $self->_combinator($selectors, $e, $tree) }
}

return;
}

sub _regex {
my ($self, $op, $value) = @_;
return unless defined $value;
Expand All @@ -335,9 +314,6 @@ sub _regex {
return qr/^$value$/;
}

# "All right, brain.
# You don't like me and I don't like you,
# but let's just do this and I can get back to killing you with beer."
sub _selector {
my ($self, $selector, $current) = @_;

Expand All @@ -363,6 +339,30 @@ sub _selector {
return 1;
}

sub _sibling {
my ($self, $selectors, $current, $tree, $immediate) = @_;

# Find preceding elements
my $parent = $current->[3];
my $found;
my $start = $parent->[0] eq 'root' ? 1 : 4;
for my $e (@$parent[$start .. $#$parent]) {
return $found if $e eq $current;
next unless $e->[0] eq 'tag';

# "+" (immediately preceding sibling)
if ($immediate) { $found = $self->_combinator($selectors, $e, $tree) }

# "~" (preceding sibling)
else { return 1 if $self->_combinator($selectors, $e, $tree) }
}

return;
}

# "All right, brain.
# You don't like me and I don't like you,
# but let's just do this and I can get back to killing you with beer."
sub _unescape {
my ($self, $value) = @_;

Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Parameters.pm
Expand Up @@ -168,7 +168,7 @@ sub to_string {
my $charset = $self->charset;
if (defined(my $string = $self->{string})) {
$string = encode $charset, $string if $charset;
return url_escape $string, "^$Mojo::URL::UNRESERVED&;=+%";
return url_escape $string, "^$Mojo::URL::UNRESERVED!\$&'()*+,;=%:@/?";
}

# Build pairs
Expand All @@ -180,11 +180,11 @@ sub to_string {

# Escape and replace whitespace with "+"
$name = encode $charset, $name if $charset;
$name = url_escape $name, "^$Mojo::URL::UNRESERVED";
$name = url_escape $name, "^$Mojo::URL::UNRESERVED!\$'()*,%:@/?";
$name =~ s/\%20/\+/g;
if ($value) {
$value = encode $charset, $value if $charset;
$value = url_escape $value, "^$Mojo::URL::UNRESERVED";
$value = url_escape $value, "^$Mojo::URL::UNRESERVED!\$'()*,%:@/?";
$value =~ s/\%20/\+/g;
}

Expand Down
16 changes: 11 additions & 5 deletions t/mojo/parameters.t
Expand Up @@ -2,7 +2,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 92;
use Test::More tests => 95;

# "Now that's a wave of destruction that's easy on the eyes."
use Mojo::Parameters;
Expand Down Expand Up @@ -184,7 +184,13 @@ $p->parse('foo=bar&baz=23');
is "$p", 'foo=bar&baz=23', 'right result';

# Query string
$p = Mojo::Parameters->new('%AZaz09-._~&;=+');
is "$p", '%AZaz09-._~&;=+', 'right result';
$p = Mojo::Parameters->new('foo?bar');
is "$p", 'foo%3Fbar', 'right result';
$p = Mojo::Parameters->new('%AZaz09-._~&;=+!$\'()*,%:@/?');
is "$p", '%AZaz09-._~&;=+!$\'()*,%:@/?', 'right result';
$p = Mojo::Parameters->new('foo{}bar');
is "$p", 'foo%7B%7Dbar', 'right result';

# Special characters
$p = Mojo::Parameters->new('foo=!$\'()*,%:@/?&bar=23');
is $p->param('foo'), '!$\'()*,%:@/?', 'right value';
is $p->param('bar'), 23, 'right value';
is "$p", 'foo=!$\'()*,%:@/?&bar=23', 'right result';
2 changes: 1 addition & 1 deletion t/mojo/request.t
Expand Up @@ -2010,7 +2010,7 @@ is $req->method, 'GET', 'right method';
is $req->version, '1.1', 'right version';
ok $req->at_least_version('1.0'), 'at least version 1.0';
ok !$req->at_least_version('1.2'), 'not version 1.2';
is $req->url, '/perldoc?Mojo%3A%3AMessage%3A%3ARequest', 'right URL';
is $req->url, '/perldoc?Mojo::Message::Request', 'right URL';
is $req->url->query->params->[0], 'Mojo::Message::Request', 'right value';

# Parse lots of special characters in URL
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/url.t
Expand Up @@ -644,7 +644,7 @@ is $url->to_abs($base), 'http://a/b/c/d?y', 'right absolute version';
$url = Mojo::URL->new('g?y');
is $url->to_abs($base), 'http://a/b/c/g?y', 'right absolute version';
$url = Mojo::URL->new('g?y/./x');
is $url->to_abs($base), 'http://a/b/c/g?y%2F.%2Fx', 'right absolute version';
is $url->to_abs($base), 'http://a/b/c/g?y/./x', 'right absolute version';
$url = Mojo::URL->new('#s');
is $url->to_abs($base), 'http://a/b/c/d?q#s', 'right absolute version';
$url = Mojo::URL->new('g#s');
Expand Down

0 comments on commit aab2f1f

Please sign in to comment.