blib/lib/Plack/Middleware/Debug/W3CValidate.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Plack::Middleware::Debug::W3CValidate;
2  
3 our $VERSION = '0.04';
4 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use parent qw(Plack::Middleware::Debug::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶use XML::XPath;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use WebService::Validator::HTML::W3C;
11 use Plack::Util::Accessor qw(validator_uri);
12  
13 my $table_template = __PACKAGE__->build_template(<<'TABLETMPL');
14 % my $errs = shift @_;
15 <style>
16 #validation_errors td strong {
17 color: red;
18 }
19 </style>
20 <table id="validation_errors">
21 <thead>
22 <tr>
23 <th>Line</th>
24 <th>Column</th>
25 <th>Message</th>
26 <th>Source</th>
27 </tr>
28 </thead>
29 <tbody>
30 % my $i = 0;
31 % for my $error (@$errs) {
32 <tr class="<%= ++$i % 2 ? 'plDebugOdd' : 'plDebugEven' %>">
33 <td><%= $error->{line} %></td>
34 <td><%= $error->{col} %></td>
35 <td><%= $error->{msg} %></td>
36 <td><%= Text::MicroTemplate::encoded_string($error->{source}) %></td>
37 </tr>
38 % }
39 </tbody>
40 </table>
41 TABLETMPL
42  
43 sub get_validator {
44 my $self = shift @_;
45 my %opts = ($self->validator_uri ? (validator_uri=>$self->validator_uri) : ());
46 return WebService::Validator::HTML::W3C->new(detailed=>1, %opts);
47 }
48  
49 sub flatten_body {
50 my ($self, $res) = @_;
51 my $body = $res->[2];
52 if(ref $body eq 'ARRAY') {
53 return join "", @$body;
54 } elsif(defined $body) {
55 my $slurped;
56 while (defined(my $line = $body->getline)) {
57 $slurped .= $line if length $line;
58 }
59 return $slurped;
60 }
61 }
62  
63 ## Until (and if) WebService::Validator::HTML::W3C parses for source
64 sub parse_for_errors {
65 my ($self, $xml) = @_;
66 my $xp = XML::XPath->new(xml => $xml);
67 my @messages = $xp->findnodes( '/env:Envelope/env:Body/m:markupvalidationresponse/m:errors/m:errorlist/m:error' );
68  
69 my @errs;
70 foreach my $msg ( @messages ) {
71 my $err = {
72 line => $xp->find( './m:line', $msg )->get_node(1)->getChildNode(1)->getValue,
73 col => $xp->find( './m:col', $msg )->get_node(1)->getChildNode(1)->getValue,
74 msg => $xp->find( './m:message', $msg )->get_node(1)->getChildNode(1)->getValue,
75 source => $xp->find( './m:source', $msg )->get_node(1)->getChildNode(1)->getValue,
76 };
77 push @errs, $err;
78 }
79 return @errs;
80 }
81  
82 sub run {
83 my($self, $env, $panel) = @_;
84 $panel->title("W3C Validation");
85 $panel->nav_title("W3C Validation");
86 return sub {
87 my $res = shift @_;
88 my $v = $self->get_validator;
89 my $slurped_body = $self->flatten_body($res);
90 if($v->validate_markup($slurped_body)) {
91 if ( $v->is_valid ) {
92 $panel->nav_subtitle("Page validated.");
93 $panel->content(sub { "<h3>No Errors on Page</h3>" });
94 } else {
95 $panel->nav_subtitle('Not valid. Error Count: '.$v->num_errors);
96 my @errs = $self->parse_for_errors($v->_content());
97 $panel->content(sub {
98 "<h3>Errors</h3>".
99 $self->render($table_template, \@errs);
100 });
101 }
102 } else {
103 $panel->content(sub {
104 $self->render_lines([
105 "Failed to validate the website: ". $v->validator_error,
106 ]);
107 });
108 }
109 }
110 }
111  
112 1;
113  
114 =head1 NAME
115  
116 Plack::Middleware::Debug::W3CValidate - Validate your Response Content
117  
118 =head2 SYNOPSIS
119  
120 use Plack::Builder;
121  
122 my $app = ...; ## Build your Plack App
123  
124 builder {
125 enable 'Debug', panels =>['W3CValidate'];
126 $app;
127 };
128  
129 =head1 DESCRIPTION
130  
131 Adds a debug panel that runs your response body through the W3C validator and
132 returns a list of errors.
133  
134 =head1 OPTIONS
135  
136 This debug panel defines the following options.
137  
138 =head2 validator_uri
139  
140 Takes the url of the W3C validator. Defaults to the common validator, but if
141 you plan to pound this it would be polite to setup your own and point to that
142 instead. Please see L<WebService::Validator::HTML::W3C> for more.
143  
144 Since this panel needs to read and submit the response body to a POST service
145 it will definitely increase the time it takes to load the page.
146  
147 =head1 SEE ALSO
148  
149 L<Plack::Middleware::Debug>
150  
151 =head1 AUTHOR
152  
153 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
154  
155 =head1 COPYRIGHT & LICENSE
156  
157 This program is free software; you can redistribute it and/or modify
158 it under the same terms as Perl itself.
159  
160 =cut
161  
↑ Summary