File Coverage

blib/lib/Mojolicious/Plugin/HTMLLint.pm
Criterion Covered Total %
statement 24 26 92.3
branch 5 10 50.0
condition 3 7 42.8
subroutine 4 5 80.0
pod 1 1 100.0
total 37 49 75.5


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::HTMLLint;
2 1     1   1116 use Mojo::Base 'Mojolicious::Plugin';
  1         4  
  1         10  
3              
4 1     1   1169 use HTML::Lint;
  1         23258  
  1         530  
5              
6             our $VERSION = '0.04';
7              
8             sub register {
9 1     1 1 59 my ( $self, $app, $conf ) = @_;
10 1   50     7 $conf ||= {};
11            
12 1   50     9 my $skip = delete $conf->{skip} // [];
13              
14             # On error callback
15 1         3 my $on_error;
16 1 50 33     10 if ( $conf->{on_error} && ref($conf->{on_error}) eq 'CODE' ) {
17 1         3 $on_error = delete $conf->{on_error};
18             } else {
19 0     0   0 $on_error = sub { $app->log->warn($_[1]) };
  0         0  
20             }
21              
22            
23            
24             $app->hook(
25             'after_dispatch' => sub {
26 2     2   72084 my ( $c ) = @_;
27 2         9 my $res = $c->res;
28              
29             # Only successful response
30 2 50       234 return if $res->code !~ m/^2/;
31            
32             ## Only html response
33 2 50       29 return unless $res->headers->content_type;
34 2 50       124 return if $res->headers->content_type !~ /html/;
35            
36 2         139 my $lint = HTML::Lint->new(%$conf);
37 2         44 $lint->parse($res->body);
38            
39 2         4508 foreach my $error ( $lint->errors ) {
40 3         46 my $err_msg = $error->as_string();
41 3 50       94 next if $err_msg ~~ $skip;
42              
43 3         9 $on_error->( $c, "HTMLLint:" . $error->as_string );
44             }
45 1         13 } );
46             }
47              
48             1;
49             __END__