File Coverage

blib/lib/Plack/Component/Tags/HTML.pm
Criterion Covered Total %
statement 90 92 97.8
branch 29 34 85.2
condition 3 6 50.0
subroutine 20 20 100.0
pod 2 2 100.0
total 144 154 93.5


line stmt bran cond sub pod time code
1             package Plack::Component::Tags::HTML;
2              
3 8     8   603577 use base qw(Plack::Component);
  8         73  
  8         4239  
4 8     8   97120 use strict;
  8         18  
  8         153  
5 8     8   38 use warnings;
  8         18  
  8         203  
6              
7 8     8   4053 use CSS::Struct::Output::Raw;
  8         240770  
  8         295  
8 8     8   4025 use Encode qw(encode);
  8         103755  
  8         615  
9 8     8   70 use Error::Pure qw(err);
  8         18  
  8         406  
10 8         70 use Plack::Util::Accessor qw(author content_type css css_init encoding
11             favicon flag_begin flag_end generator psgi_app script_js script_js_src
12 8     8   3707 status_code title tags);
  8         2225  
13 8     8   1130 use Scalar::Util qw(blessed);
  8         17  
  8         366  
14 8     8   4216 use Tags::HTML::Page::Begin;
  8         29845  
  8         282  
15 8     8   3757 use Tags::HTML::Page::End;
  8         2323  
  8         218  
16 8     8   3600 use Tags::Output::Raw;
  8         93504  
  8         6335  
17              
18             our $VERSION = 0.15;
19              
20             sub call {
21 6     6 1 93 my ($self, $env) = @_;
22              
23             # Process actions.
24 6         39 $self->_process_actions($env);
25              
26             # PSGI application.
27 6 100       29 if ($self->psgi_app) {
28 1         9 my $app = $self->psgi_app;
29 1         6 $self->psgi_app(undef);
30 1         9 return $app;
31             }
32              
33             # Process 'CSS::Struct' for page.
34 5 100       67 if (defined $self->{'_page_begin'}) {
35 2         15 $self->{'_page_begin'}->process_css;
36             }
37 5         160 $self->_css($env);
38              
39             # Process 'Tags' for page.
40 5         45 $self->_tags($env);
41 5         24 $self->tags->finalize;
42 5         122 $self->_cleanup($env);
43              
44             return [
45 5         23 $self->status_code,
46             [
47             'content-type' => $self->content_type,
48             ],
49             [$self->_encode($self->tags->flush(1))],
50             ];
51             }
52              
53             sub prepare_app {
54 8     8 1 154165 my $self = shift;
55              
56 8         46 $self->_prepare_app;
57              
58 6         16 return;
59             }
60              
61             sub _cleanup {
62 5     5   17 my ($self, $env) = @_;
63              
64 5         11 return;
65             }
66              
67             sub _css {
68 5     5   21 my ($self, $env) = @_;
69              
70 5         11 return;
71             }
72              
73             sub _encode {
74 5     5   546 my ($self, $string) = @_;
75              
76 5         25 return encode($self->encoding, $string);
77             }
78              
79             sub _prepare_app {
80 8     8   69 my $self = shift;
81              
82 8 100       58 if ($self->tags) {
83 3 100 100     32 if (! blessed($self->tags) || ! $self->tags->isa('Tags::Output')) {
84 2         69 err "Accessor 'tags' must be a 'Tags::Output' object.";
85             }
86             } else {
87 5         100 $self->tags(Tags::Output::Raw->new(
88             'xml' => 1,
89             'no_simple' => ['script', 'textarea'],
90             'preserved' => ['pre', 'style'],
91             ));
92             }
93              
94 6 50       1087 if ($self->css) {
95 0 0 0     0 if (! blessed($self->css) || ! $self->css->isa('CSS::Struct::Output')) {
96 0         0 err "Accessor 'css' must be a 'CSS::Struct::Output' object.";
97             }
98             } else {
99 6         123 $self->css(CSS::Struct::Output::Raw->new);
100             }
101              
102 6 100       722 if (! $self->encoding) {
103 5         38 $self->encoding('utf-8');
104             }
105              
106 6 100       72 if (! $self->content_type) {
107 5         41 $self->content_type('text/html; charset='.$self->encoding);
108             }
109              
110 6 100       144 if (! $self->status_code) {
111 5         49 $self->status_code(200);
112             }
113              
114 6 100       54 if (! defined $self->flag_begin) {
115 3         55 $self->flag_begin(1);
116             }
117              
118 6 100       80 if (! defined $self->flag_end) {
119 3         22 $self->flag_end(1);
120             }
121              
122 6 50       68 if (! defined $self->script_js) {
123 6         66 $self->script_js([]);
124             }
125              
126 6 50       72 if (! defined $self->script_js_src) {
127 6         58 $self->script_js_src([]);
128             }
129              
130 6 100       41 if ($self->flag_begin) {
131 3 100       29 $self->{'_page_begin'} = Tags::HTML::Page::Begin->new(
132             'author' => $self->author,
133             'css' => $self->css,
134             defined $self->css_init ? (
135             'css_init' => $self->css_init,
136             ) : (),
137             'charset' => $self->encoding,
138             'favicon' => $self->favicon,
139             'generator' => $self->generator,
140             'lang' => {
141             'title' => $self->title,
142             },
143             'script_js' => $self->script_js,
144             'script_js_src' => $self->script_js_src,
145             'tags' => $self->tags,
146             );
147             }
148              
149 6         1505 return;
150             }
151              
152             sub _process_actions {
153 6     6   21 my ($self, $env) = @_;
154              
155 6         13 return;
156             }
157              
158             sub _tags_middle {
159 2     2   5 my ($self, $env) = @_;
160              
161 2         5 return;
162             }
163              
164             sub _tags {
165 5     5   27 my ($self, $env)= @_;
166              
167 5 100       25 if ($self->flag_begin) {
168 2         43 $self->{'_page_begin'}->process;
169             }
170              
171 5         4065 $self->_tags_middle($env);
172              
173 5 100       938 if ($self->flag_end) {
174 2         23 Tags::HTML::Page::End->new(
175             'tags' => $self->tags,
176             )->process;
177             }
178              
179 5         407 return;
180             }
181              
182             1;
183              
184             __END__