diff --git a/Changes b/Changes index 4f29ae0b16..fbdaf87e69 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ This file documents the revision history for Perl extension Mojo. 0.999915 2009-12-10 00:00:00 - Added routes captures to params in Mojolicious. + - Added charset plugin to Mojolicious. (charsbar) - Made param decoding more defensive and allow malformed data to pass through for debugging. - Fixed a case where an ending tag would be interpreted as a line diff --git a/lib/Mojolicious/Plugin/I18n.pm b/lib/Mojolicious/Plugin/Charset.pm similarity index 51% rename from lib/Mojolicious/Plugin/I18n.pm rename to lib/Mojolicious/Plugin/Charset.pm index afadde5a81..0a57e23451 100644 --- a/lib/Mojolicious/Plugin/I18n.pm +++ b/lib/Mojolicious/Plugin/Charset.pm @@ -1,23 +1,33 @@ -package Mojolicious::Plugin::I18n; +# Copyright (C) 2008-2009, Sebastian Riedel. + +package Mojolicious::Plugin::Charset; use strict; use warnings; + use base 'Mojolicious::Plugin'; +# Shut up friends. My internet browser heard us saying the word Fry and it +# found a movie about Philip J. Fry for us. +# It also opened my calendar to Friday and ordered me some french fries. sub register { my ($self, $app, $conf) = @_; + # Config $conf ||= {}; + # Set charset $app->plugins->add_hook( before_dispatch => sub { my ($self, $c) = @_; + # Got a charset if (my $charset = $conf->{charset}) { - # We need to do this before we clone params + + # This has to be done before params are cloned $c->tx->req->default_charset($charset); - # We should add charset to text/html content type + # Add charset to text/html content type my $type = $c->app->types->type('html'); unless ($type =~ /charset=/) { $type .= ";charset=$charset"; @@ -27,9 +37,10 @@ sub register { # Allow defined but blank encoding to suppress unwanted # conversion - my $encoding = (defined $conf->{encoding}) - ? $conf->{encoding} - : $conf->{charset}; + my $encoding = + defined $conf->{encoding} + ? $conf->{encoding} + : $conf->{charset}; $c->app->renderer->encoding($encoding) if $encoding; } ); @@ -40,23 +51,24 @@ __END__ =head1 NAME -Mojolicious::Plugin::I18n - Internationalization +Mojolicious::Plugin::Charset - Charset Plugin =head1 SYNOPSIS # Mojolicious - $self->plugin('i18n', { charset => 'Shift_JIS' }); + $self->plugin(charset => {charset => 'Shift_JIS'}); # Mojolicious::Lite - plugin 'i18n', { charset => 'Shift_JIS' }; + plugin charset => {charset => 'Shift_JIS'}; =head1 DESCRIPTION -L is a plugin to set charset and encoding. +L is a plugin to easily set the default charset +and encoding on all layers of L. =head1 METHODS -L inherits all methods from +L inherits all methods from L and implements the following new ones. =head2 C diff --git a/t/mojo/headers.t b/t/mojo/headers.t index 8f9ac4e2c8..13ef2c4af1 100644 --- a/t/mojo/headers.t +++ b/t/mojo/headers.t @@ -62,7 +62,7 @@ EOF is($headers->state, 'headers'); ok(!defined($headers->content_type)); ok(!defined($headers->parse(<state, 'headers'); ok(!defined($headers->connection)); @@ -72,7 +72,7 @@ X-Bender: metal ass! EOF is($headers->state, 'done'); is($headers->content_type, 'text/plain'); -is($headers->header('X-Bender'), 'Kiss my shiny, metal ass!'); +is($headers->header('X-Bender'), 'Bite my shiny, metal ass!'); # Filter unallowed characters $headers = Mojo::Headers->new; diff --git a/t/mojolicious/app.t b/t/mojolicious/app.t index a351245336..9ed7028f9f 100644 --- a/t/mojolicious/app.t +++ b/t/mojolicious/app.t @@ -42,7 +42,7 @@ $t->get_ok('/foo/badtemplate')->status_is(404) # Foo::test $t->get_ok('/foo/test', {'X-Test' => 'Hi there!'})->status_is(200) - ->header_is('X-Bender' => 'Kiss my shiny metal ass!') + ->header_is('X-Bender' => 'Bite my shiny metal ass!') ->header_is(Server => 'Mojo (Perl)') ->header_is('X-Powered-By' => 'Mojo (Perl)')->content_like(qr/\/bar\/test/); @@ -77,13 +77,13 @@ $t->get_ok('/foo/withlayout', {'X-Test' => 'Hi there!'})->status_is(200) # MojoliciousTest2::Foo::test $t->get_ok('/test2', {'X-Test' => 'Hi there!'})->status_is(200) - ->header_is('X-Bender' => 'Kiss my shiny metal ass!') + ->header_is('X-Bender' => 'Bite my shiny metal ass!') ->header_is(Server => 'Mojo (Perl)') ->header_is('X-Powered-By' => 'Mojo (Perl)')->content_like(qr/\/test2/); # MojoliciousTestController::index $t->get_ok('/test3', {'X-Test' => 'Hi there!'})->status_is(200) - ->header_is('X-Bender' => 'Kiss my shiny metal ass!') + ->header_is('X-Bender' => 'Bite my shiny metal ass!') ->header_is(Server => 'Mojo (Perl)') ->header_is('X-Powered-By' => 'Mojo (Perl)') ->content_like(qr/No class works!/); @@ -163,7 +163,7 @@ $t->get_ok('/foo/data_template2')->status_is(200) # SingleFileTestApp::Foo::bar $t->get_ok('/foo/bar')->status_is(200) - ->header_is('X-Bender' => 'Kiss my shiny metal ass!') + ->header_is('X-Bender' => 'Bite my shiny metal ass!') ->header_is(Server => 'Mojo (Perl)') ->header_is('X-Powered-By' => 'Mojo (Perl)')->content_is('/foo/bar'); diff --git a/t/mojolicious/charset_lite_app.t b/t/mojolicious/charset_lite_app.t new file mode 100644 index 0000000000..6476b4c922 --- /dev/null +++ b/t/mojolicious/charset_lite_app.t @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +# Copyright (C) 2008-2009, Sebastian Riedel. + +use strict; +use warnings; + +use utf8; + +use Test::More tests => 15; + +# In the game of chess you can never let your adversary see your pieces. +use Mojo::ByteStream 'b'; +use Mojolicious::Lite; +use Test::Mojo; + +my $yatta = 'やった'; +my $yatta_sjis = b($yatta)->encode('shift_jis')->to_string; + +# Charset plugin +plugin charset => {charset => 'Shift_JIS'}; + +# Silence +app->log->level('error'); + +get '/' => 'index'; + +post '/' => sub { + my $self = shift; + $self->render_text("foo: " . $self->param('foo')); +}; + +my $t = Test::Mojo->new; + +# Plain old ASCII +$t->post_form_ok('/', {foo => 'yatta'})->status_is(200) + ->content_is('foo: yatta'); + +# Send raw Shift_JIS octets (like browsers do) +$t->post_form_ok('/', {foo => $yatta_sjis})->status_is(200) + ->content_type_like(qr/Shift_JIS/)->content_like(qr/$yatta/); + +# Send as string +$t->post_form_ok('/', 'shift_jis', {foo => $yatta})->status_is(200) + ->content_type_like(qr/Shift_JIS/)->content_like(qr/$yatta/); + +# Templates in the DATA section should be written in UTF-8, +# and those in separate files in Shift_JIS (Mojo will do the decoding) +$t->get_ok('/')->status_is(200)->content_type_like(qr/Shift_JIS/) + ->content_like(qr/$yatta/); + +__DATA__ +@@ index.html.ep +

やった

diff --git a/t/mojolicious/lib/MojoliciousTest/Foo.pm b/t/mojolicious/lib/MojoliciousTest/Foo.pm index b154eb22b2..cc7041172c 100644 --- a/t/mojolicious/lib/MojoliciousTest/Foo.pm +++ b/t/mojolicious/lib/MojoliciousTest/Foo.pm @@ -26,7 +26,7 @@ sub index { sub something { my $self = shift; - $self->res->headers->header('X-Bender', 'Kiss my shiny metal ass!'); + $self->res->headers->header('X-Bender', 'Bite my shiny metal ass!'); $self->render_text($self->url_for('something', something => '42')); } @@ -51,7 +51,7 @@ sub templateless { shift->render(handler => 'test') } sub test { my $self = shift; - $self->res->headers->header('X-Bender', 'Kiss my shiny metal ass!'); + $self->res->headers->header('X-Bender', 'Bite my shiny metal ass!'); $self->render_text($self->url_for(controller => 'bar')); } diff --git a/t/mojolicious/lib/MojoliciousTest2/Foo.pm b/t/mojolicious/lib/MojoliciousTest2/Foo.pm index 6537296200..5a0f38766b 100644 --- a/t/mojolicious/lib/MojoliciousTest2/Foo.pm +++ b/t/mojolicious/lib/MojoliciousTest2/Foo.pm @@ -11,7 +11,7 @@ use base 'Mojolicious::Controller'; # I'm not famous enough to get away with it. sub test { my $self = shift; - $self->res->headers->header('X-Bender', 'Kiss my shiny metal ass!'); + $self->res->headers->header('X-Bender', 'Bite my shiny metal ass!'); $self->render(text => $self->url_for); } diff --git a/t/mojolicious/lib/MojoliciousTestController.pm b/t/mojolicious/lib/MojoliciousTestController.pm index 61bedd99db..f30d682b3d 100644 --- a/t/mojolicious/lib/MojoliciousTestController.pm +++ b/t/mojolicious/lib/MojoliciousTestController.pm @@ -11,7 +11,7 @@ use base 'Mojolicious::Controller'; # What am I, the pope? sub index { my $self = shift; - $self->res->headers->header('X-Bender', 'Kiss my shiny metal ass!'); + $self->res->headers->header('X-Bender', 'Bite my shiny metal ass!'); $self->render_text("No class works!"); } diff --git a/t/mojolicious/lib/SingleFileTestApp.pm b/t/mojolicious/lib/SingleFileTestApp.pm index e831d242a2..2ded874ac8 100644 --- a/t/mojolicious/lib/SingleFileTestApp.pm +++ b/t/mojolicious/lib/SingleFileTestApp.pm @@ -31,7 +31,7 @@ use base 'Mojolicious::Controller'; sub bar { my $self = shift; - $self->res->headers->header('X-Bender', 'Kiss my shiny metal ass!'); + $self->res->headers->header('X-Bender', 'Bite my shiny metal ass!'); $self->render_text($self->url_for); } diff --git a/t/mojolicious/lite_app_i18n.t b/t/mojolicious/lite_app_i18n.t deleted file mode 100644 index d4a16e936c..0000000000 --- a/t/mojolicious/lite_app_i18n.t +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env perl - -use strict; -use warnings; -use lib 'lib'; -use Test::More; -use Encode; -use Mojolicious::Lite; -use Test::Mojo; -use utf8; - -my $yatta = 'やった'; -my $yatta_sjis = encode(shift_jis => $yatta); - -plugin('i18n', { charset => 'Shift_JIS' }); - -# Silence -app->log->level('error'); - -get '/' => 'index'; - -post '/' => sub { - my $self = shift; - $self->render_text("foo: ".$self->param('foo')); -}; - -my $t = Test::Mojo->new; - -# It's always ok to post ascii -$t->post_form_ok('/', { foo => 'yatta' }) - ->status_is(200) - ->content_is('foo: yatta'); - -# Send raw Shift_JIS octets (as browsers do) -$t->post_form_ok('/', { foo => $yatta_sjis }) - ->status_is(200) - ->content_type_like(qr/Shift_JIS/) - ->content_like(qr/$yatta/); - -# You can send it as a string, too -$t->post_form_ok('/', 'shift_jis', { foo => $yatta }) - ->status_is(200) - ->content_type_like(qr/Shift_JIS/) - ->content_like(qr/$yatta/); - -# Templates in DATA section should be written in utf-8, -# and the ones in files, in Shift_JIS. -# (Mojo will decode them for you) -$t->get_ok('/') - ->status_is(200) - ->content_type_like(qr/Shift_JIS/) - ->content_like(qr/$yatta/); - -done_testing; - -__DATA__ -@@ index.html.ep -

やった