File Coverage

blib/lib/HTML/HTML5/Parser/Error.pm
Criterion Covered Total %
statement 13 41 31.7
branch 0 14 0.0
condition 0 9 0.0
subroutine 5 11 45.4
pod 7 7 100.0
total 25 82 30.4


line stmt bran cond sub pod time code
1             package HTML::HTML5::Parser::Error;
2              
3 11     11   190 use 5.008001;
  11         52  
4 11     11   71 use strict;
  11         21  
  11         295  
5 11     11   68 use warnings;
  11         23  
  11         710  
6              
7             our $VERSION = '0.992';
8              
9 11     11   11779 use overload '""' => \&to_string;
  11         9636  
  11         90  
10              
11             sub new
12             {
13 2147     2147 1 12652 my ($class, %args) = @_;
14 2147         7344 bless \%args, $class;
15             }
16              
17             sub level
18             {
19 0     0 1   my $self = shift;
20             return {
21             m => 'MUST',
22             s => 'SHOULD',
23             w => 'WARN',
24             i => 'INFO',
25             u => undef,
26 0   0       }->{$self->{level}} || undef;
27             }
28              
29             sub layer
30             {
31 0     0 1   my $self = shift;
32 0   0       return $self->{layer} || undef;
33             }
34              
35             sub type
36             {
37 0     0 1   my $self = shift;
38 0   0       return $self->{type}||undef;
39             }
40              
41             sub tag_name
42             {
43 0     0 1   my $self = shift;
44 0 0 0       return undef unless $self->{token} && exists $self->{token}{tag_name};
45 0           return $self->{token}{tag_name};
46             }
47              
48             sub source_line
49             {
50 0     0 1   my $self = shift;
51            
52 0 0         if (wantarray)
53             {
54 0           return ($self->{line}, $self->{column});
55             }
56             else
57             {
58 0           return $self->{line};
59             }
60             }
61              
62             sub to_string
63             {
64 0     0 1   my $self = shift;
65            
66 0           my $msg = $self->type;
67 0           my $level = $self->level;
68 0           my $tag = $self->tag_name;
69 0           my ($l, $c) = $self->source_line;
70              
71 0           my @details;
72 0 0         push @details, sprintf('complicance: %s', $level) if defined $level;
73 0 0         push @details, sprintf('line: %d', $l) if defined $l;
74 0 0         push @details, sprintf('column: %d', $c) if defined $c;
75 0 0         push @details, sprintf('tag: %s', $tag) if defined $tag;
76              
77 0 0         if (@details)
78             {
79 0           $msg .= " [";
80 0           $msg .= join '; ', @details;
81 0           $msg .= "]";
82             }
83            
84 0           return $msg;
85             }
86              
87             1;
88              
89             =head1 NAME
90              
91             HTML::HTML5::Parser::Error - an error that occurred during parsing
92              
93             =head1 DESCRIPTION
94              
95             The C and C methods of C generate
96             C objects.
97              
98             C overloads stringification, so can be printed,
99             matched against regular expressions, etc.
100              
101             Note that L is not a validation tool, and there are many
102             classes of error that it does not care about, so will not raise.
103              
104             =head2 Constructor
105              
106             =over
107              
108             =item C<< new(level=>$level, type=>$type, token=>$token, ...) >>
109              
110             Constructs a new C object.
111              
112             =back
113              
114             =head2 Methods
115              
116             =over
117              
118             =item C
119              
120             Returns the level of error. ('MUST', 'SHOULD', 'WARN', 'INFO' or undef.)
121              
122             =item C
123              
124             Returns the parsing layer involved, often undef. e.g. 'encode'.
125              
126             =item C
127              
128             Returns the type of error as a string.
129              
130             =item C
131              
132             Returns the tag name (if any).
133              
134             =item C
135              
136             ($line, $col) = $error->source_line();
137             $line = $error->source_line;
138            
139             In scalar context, C returns the line number of the
140             source code that triggered the error.
141              
142             In list context, returns a line/column pair. (Tab characters count as
143             one column, not eight.)
144              
145             =item C
146              
147             Returns a friendly error string.
148              
149             =back
150              
151             =head1 SEE ALSO
152              
153             L.
154              
155             =head1 AUTHOR
156              
157             Toby Inkster, Etobyink@cpan.orgE
158              
159             =head1 COPYRIGHT AND LICENSE
160              
161             Copyright (C) 2011-2012 by Toby Inkster
162              
163             This library is free software; you can redistribute it and/or modify
164             it under the same terms as Perl itself.
165              
166             =head1 DISCLAIMER OF WARRANTIES
167              
168             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
169             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
170             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
171