File Coverage

blib/lib/Plack/Component/Tags/HTML.pm
Criterion Covered Total %
statement 88 92 95.6
branch 26 34 76.4
condition 0 6 0.0
subroutine 20 20 100.0
pod 2 2 100.0
total 136 154 88.3


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