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   470898 use base qw(Plack::Component);
  7         63  
  7         3735  
4 7     7   84276 use strict;
  7         17  
  7         131  
5 7     7   53 use warnings;
  7         14  
  7         176  
6              
7 7     7   3452 use CSS::Struct::Output::Raw;
  7         218183  
  7         261  
8 7     7   4374 use Encode qw(encode);
  7         103303  
  7         616  
9 7     7   61 use Error::Pure qw(err);
  7         26  
  7         467  
10 7         67 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   3236 status_code title tags);
  7         1911  
13 7     7   989 use Scalar::Util qw(blessed);
  7         17  
  7         336  
14 7     7   3677 use Tags::HTML::Page::Begin;
  7         26023  
  7         236  
15 7     7   3168 use Tags::HTML::Page::End;
  7         2012  
  7         183  
16 7     7   3836 use Tags::Output::Raw;
  7         91576  
  7         5221  
17              
18             our $VERSION = 0.13;
19              
20             sub call {
21 5     5 1 89 my ($self, $env) = @_;
22              
23             # Process actions.
24 5         38 $self->_process_actions($env);
25              
26             # PSGI application.
27 5 100       26 if ($self->psgi_app) {
28 1         9 my $app = $self->psgi_app;
29 1         6 $self->psgi_app(undef);
30 1         8 return $app;
31             }
32              
33             # Process 'CSS::Struct' for page.
34 4 100       43 if (defined $self->{'_page_begin'}) {
35 2         12 $self->{'_page_begin'}->process_css;
36             }
37 4         95 $self->_css;
38              
39             # Process 'Tags' for page.
40 4         36 $self->_tags;
41 4         15 $self->tags->finalize;
42 4         89 $self->_cleanup;
43              
44             return [
45 4         14 $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 117265 my $self = shift;
55              
56 5         39 $self->_prepare_app;
57              
58 5         15 return;
59             }
60              
61             sub _cleanup {
62 4     4   11 my $self = shift;
63              
64 4         8 return;
65             }
66              
67             sub _css {
68 4     4   20 my $self = shift;
69              
70 4         8 return;
71             }
72              
73             sub _encode {
74 4     4   414 my ($self, $string) = @_;
75              
76 4         19 return encode($self->encoding, $string);
77             }
78              
79             sub _prepare_app {
80 5     5   49 my $self = shift;
81              
82 5 50       38 if ($self->tags) {
83 0 0 0     0 if (! blessed($self->tags) || ! $self->tags->isa('Tags::Output')) {
84 0         0 err "Accessor 'tags' must be a 'Tags::Output' object.";
85             }
86             } else {
87 5         116 $self->tags(Tags::Output::Raw->new(
88             'xml' => 1,
89             'no_simple' => ['script', 'textarea'],
90             'preserved' => ['pre', 'style'],
91             ));
92             }
93              
94 5 50       974 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 5         115 $self->css(CSS::Struct::Output::Raw->new);
100             }
101              
102 5 100       644 if (! $self->encoding) {
103 4         41 $self->encoding('utf-8');
104             }
105              
106 5 100       52 if (! $self->content_type) {
107 4         30 $self->content_type('text/html; charset='.$self->encoding);
108             }
109              
110 5 100       72 if (! $self->status_code) {
111 4         30 $self->status_code(200);
112             }
113              
114 5 100       50 if (! defined $self->flag_begin) {
115 3         20 $self->flag_begin(1);
116             }
117              
118 5 100       82 if (! defined $self->flag_end) {
119 3         57 $self->flag_end(1);
120             }
121              
122 5 50       68 if (! defined $self->script_js) {
123 5         53 $self->script_js([]);
124             }
125              
126 5 50       70 if (! defined $self->script_js_src) {
127 5         59 $self->script_js_src([]);
128             }
129              
130 5 100       40 if ($self->flag_begin) {
131 3 100       36 $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 5         1455 return;
150             }
151              
152             sub _process_actions {
153 5     5   18 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   12 my $self = shift;
166              
167 4 100       34 if ($self->flag_begin) {
168 2         33 $self->{'_page_begin'}->process;
169             }
170              
171 4         3835 $self->_tags_middle;
172              
173 4 100       924 if ($self->flag_end) {
174 2         19 Tags::HTML::Page::End->new(
175             'tags' => $self->tags,
176             )->process;
177             }
178              
179 4         397 return;
180             }
181              
182             1;
183              
184             __END__