1 package Catalyst::View::ContentNegotiation::XHTML;
4 use MooseX::Types::Moose qw/Num Str ArrayRef/;
5 use MooseX::Types::Structured qw/Tuple/;
6 use HTTP::Negotiate qw/choose/;
8 use namespace::clean -except => 'meta';
10 # Remember to bump $VERSION in View::TT::XHTML also.
11 our $VERSION = '1.100';
17 isa => ArrayRef[Tuple[Str, Num, Str]],
19 builder => '_build_variants',
24 [qw| xhtml 1.000 application/xhtml+xml |],
25 [qw| html 0.900 text/html |],
29 after process => sub {
31 if ($c->request->header('Accept') && $c->response->headers->{'content-type'} =~ m|text/html|) {
32 $self->pragmatic_accept($c);
33 my $var = choose($self->variants, $c->request->headers);
34 if ($var eq 'xhtml') {
35 $c->response->headers->{'content-type'} =~ s|text/html|application/xhtml+xml|;
40 sub pragmatic_accept {
42 my $accept = $c->request->header('Accept');
43 if ($accept =~ m|text/html|) {
44 $accept =~ s!\*/\*\s*([,]+|$)!*/*;q=0.5$1!;
46 $accept =~ s!\*/\*\s*([,]+|$)!text/html,*/*;q=0.5$1!;
48 $c->request->header('Accept' => $accept);
57 Catalyst::View::ContentNegotiation::XHTML - A Moose Role to apply to
58 Catalyst views adjusts the response Content-Type header to
59 application/xhtml+xml content if the browser accepts it.
63 package Catalyst::View::TT;
66 use namespace::clean -except => 'meta';
68 extends qw/Catalyst::View::TT/;
69 with qw/Catalyst::View::ContentNegotiation::XHTML/;
75 This is a very simple Role which uses a method modifier to run after the
76 C<process> method, and sets the response C<Content-Type> to be
77 C<application/xhtml+xml> if the users browser sends an C<Accept> header
78 indicating that it is willing to process that MIME type.
80 Changing the C<Content-Type> causes browsers to interpret the page as
81 XML, meaning that the markup must be well formed.
83 This is useful when you're developing your application, as you know that
84 all pages you view are parsed as XML, so any errors caused by your markup
85 not being well-formed will show up at once.
87 =head1 METHOD MODIFIERS
91 Changes the response C<Content-Type> if appropriate (from the requests C<Accept> header).
95 =head2 pragmatic_accept
97 Some browsers (such as Internet Explorer) have a nasty way of sending
98 Accept */* and this claiming to support XHTML just as well as HTML.
99 Saving to a file on disk or opening with another application does
100 count as accepting, but it really should have a lower q value then
101 text/html. This sub takes a pragmatic approach and corrects this mistake
102 by modifying the Accept header before passing it to content negotiation.
108 Returns an array ref of 3 part arrays, comprising name, priority, output
109 mime-type, which is used for the content negotiation algorithm.
111 =head1 PRIVATE METHODS
113 =head2 _build_variants
115 Returns the default variant attribute contents.
121 =item L<Catalyst::View::TT::XHTML> - Trivial Catalyst TT view using this role.
123 =item L<http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html> - Content negotiation RFC.
129 Will only work with Views which implement a process method.
131 Should be split into a base ContentNegotiation role which is consumed by ContentNegotiation::XHTML.
135 Tomas Doran (t0m) C<< <bobtfish@bobtfish.net> >>
141 =item David Dorward - test patches and */* pragmatism.
143 =item Florian Ragwitz (rafl) C<< <rafl@debian.org> >> - Conversion into a Moose Role
149 This module itself is copyright (c) 2008 Tomas Doran and is licensed under the same terms as Perl itself.