blib/lib/HTML/TagTree.pm

filter: Statementstmt 22.1% 108 / 487 Branchbran 11.9% 31 / 260 Conditioncond 51.8% 14 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 27.7% 10 / 36 Podpod 0.0% 0 / 23 totaltotal 19.2% 163 / 849 SCAR 94.8
CC
158
Cov
20%
_quote_attribute_params
1260.0
process_select_tag
272.0
get_html_text
236.2
CRAP 13051.4
↑ Summary
1 ###############################################################################
2 ## ##
3 ## Copyright (c) 2007 - 2011 by Dan DeBrito. ##
4 ## All rights reserved. ##
5 ## ##
6 ## This package is free software; you can redistribute it ##
7 ## and/or modify it under the same terms as Perl itself. ##
8 ## ##
9 ###############################################################################
10
11
12 package HTML::TagTree;
13 1 our $AUTOLOAD;
14
15
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 1 my $DOUBLE_QUOTE = '"';
20 1 my $SINGLE_QUOTE = "'";
21 1 our $VERSION = qv('1.00');
22
23 1 my %preprocess_tag;
24 1 my %empty_tags = (
25 # Tags that should not contain content or children tags
26 br => 1,
27 input => 1,
28 );
29
30 1 my %tag_open_substitutions = (
31 ifie => '!--[if IE]',
32
33 );
34 1 my %tag_close_substitutions = (
35 ifie => '![endif]--',
36 );
37 1 my %valid_empty_tags_for_shortening = (
38 # These tags don't need a full close tag but can use abbreviated notation when empty.
39 # eg:
40 # <br /> instead of <br></br>
41
42 area => 1,
43 base => 1,
44 br => 1,
45 canvas => 1, # HTML5
46 col => 1,
47 frame => 1,
48 hr => 1,
49 input => 1,
50 img => 1,
51 link => 1,
52 meta => 1,
53 option => 1,
54 param => 1,
55
56 );
57 1 my %valid_tags = (
58 'a' => 'Defines an anchor 3.0 3.0 STF ',
59 'abbr' => 'Defines an abbreviation 6.2 STF ',
60 'acronym' => 'Defines an acronym 6.2 4.0 STF ',
61 'address' => 'Defines an address element 4.0 4.0 STF ',
62 'applet' => 'Deprecated. Defines an applet 2.0 3.0 TF ',
63 'area' => 'Defines an area inside an image map 3.0 3.0 STF ',
64 'b' => 'Defines bold text 3.0 3.0 STF ',
65 'base' => 'Defines a base URL for all the links in a page 3.0 3.0 STF ',
66 'basefont' => 'Deprecated. Defines a base font 3.0 3.0 TF ',
67 'bdo' => 'Defines the direction of text display 6.2 5.0 STF ',
68 'big' => 'Defines big text 3.0 3.0 STF ',
69 'blockquote' => 'Defines a long quotation 3.0 3.0 STF ',
70 'body' => 'Defines the body element 3.0 3.0 STF ',
71 'br' => 'Inserts a single line break 3.0 3.0 STF ',
72 'button' => 'Defines a push button 6.2 4.0 STF ',
73 'canvas' => 'HTML5',
74 'caption' => 'Defines a table caption 3.0 3.0 STF ',
75 'center' => 'Deprecated. Defines centered text 3.0 3.0 TF ',
76 'cite' => 'Defines a citation 3.0 3.0 STF ',
77 'code' => 'Defines computer code text 3.0 3.0 STF ',
78 'col' => 'Defines attributes for table columns 3.0 STF ',
79 'colgroup' => 'Defines groups of table columns 3.0 STF ',
80 'dd' => 'Defines a definition description 3.0 3.0 STF ',
81 'del' => 'Defines deleted text 6.2 4.0 STF ',
82 'dir' => 'Deprecated. Defines a directory list 3.0 3.0 TF ',
83 'div' => 'Defines a section in a document 3.0 3.0 STF ',
84 'dfn' => 'Defines a definition term 3.0 STF ',
85 'dl' => 'Defines a definition list 3.0 3.0 STF ',
86 'dt' => 'Defines a definition term 3.0 3.0 STF ',
87 'em' => 'Defines emphasized text 3.0 3.0 STF ',
88 'fieldset' => 'Defines a fieldset 6.2 4.0 STF ',
89 'font' => 'Deprecated. Defines text font, size, and color 3.0 3.0 TF ',
90 'form' => 'Defines a form 3.0 3.0 STF ',
91 'frame' => 'Defines a sub window (a frame) 3.0 3.0 F ',
92 'frameset' => 'Defines a set of frames 3.0 3.0 F ',
93 'h1' => 'Defines header 1 to header 6 3.0 3.0 STF ',
94 'h2' => 'Defines header 1 to header 6 3.0 3.0 STF ',
95 'h3' => 'Defines header 1 to header 6 3.0 3.0 STF ',
96 'h4' => 'Defines header 1 to header 6 3.0 3.0 STF ',
97 'h5' => 'Defines header 1 to header 6 3.0 3.0 STF ',
98 'h6' => 'Defines header 1 to header 6 3.0 3.0 STF ',
99 'head' => 'Defines information about the document 3.0 3.0 STF ',
100 'hr' => 'Defines a horizontal rule 3.0 3.0 STF ',
101 'html' => 'Defines an html document 3.0 3.0 STF ',
102 'i' => 'Defines italic text 3.0 3.0 STF ',
103 'ifie' => 'unigue Tag used to define Internet Explorer specific HTML ',
104 'iframe' => 'Defines an inline sub window (frame) 6.0 4.0 TF ',
105 'img' => 'Defines an image 3.0 3.0 STF ',
106 'input' => 'Defines an input field 3.0 3.0 STF ',
107 'ins' => 'Defines inserted text 6.2 4.0 STF ',
108 'isindex' => 'Deprecated. Defines a single-line input field 3.0 3.0 TF ',
109 'kbd' => 'Defines keyboard text 3.0 3.0 STF ',
110 'label' => 'Defines a label for a form control 6.2 4.0 STF ',
111 'legend' => 'Defines a title in a fieldset 6.2 4.0 STF ',
112 'li' => 'Defines a list item 3.0 3.0 STF ',
113 'link' => 'Defines a resource reference 4.0 3.0 STF ',
114 'map' => 'Defines an image map 3.0 3.0 STF ',
115 'menu' => 'Deprecated. Defines a menu list 3.0 3.0 TF ',
116 'meta' => 'Defines meta information 3.0 3.0 STF ',
117 'noframes' => 'Defines a noframe section 3.0 3.0 TF ',
118 'noscript' => 'Defines a noscript section 3.0 3.0 STF ',
119 'object' => 'Defines an embedded object 3.0 STF ',
120 'ol' => 'Defines an ordered list 3.0 3.0 STF ',
121 'optgroup' => 'Defines an option group 6.0 6.0 STF ',
122 'option' => 'Defines an option in a drop-down list 3.0 3.0 STF ',
123 'p' => 'Defines a paragraph 3.0 3.0 STF ',
124 'param' => 'Defines a parameter for an object 3.0 3.0 STF ',
125 'pre' => 'Defines preformatted text 3.0 3.0 STF ',
126 'q' => 'Defines a short quotation 6.2 STF ',
127 's' => 'Deprecated. Defines strikethrough text 3.0 3.0 TF ',
128 'samp' => 'Defines sample computer code 3.0 3.0 STF ',
129 'script' => 'Defines a script 3.0 3.0 STF ',
130 'select' => 'Defines a selectable list 3.0 3.0 STF ',
131 'small' => 'Defines small text 3.0 3.0 STF ',
132 'span' => 'Defines a section in a document 4.0 3.0 STF ',
133 'strike' => 'Deprecated. Defines strikethrough text 3.0 3.0 TF ',
134 'strong' => 'Defines strong text 3.0 3.0 STF ',
135 'style' => 'Defines a style definition 4.0 3.0 STF ',
136 'sub' => 'Defines subscripted text 3.0 3.0 STF ',
137 'sup' => 'Defines superscripted text 3.0 3.0 STF ',
138 'table' => 'Defines a table 3.0 3.0 STF ',
139 'tbody' => 'Defines a table body 4.0 STF ',
140 'td' => 'Defines a table cell 3.0 3.0 STF ',
141 'textarea' => 'Defines a text area 3.0 3.0 STF ',
142 'tfoot' => 'Defines a table footer 4.0 STF ',
143 'th' => 'Defines a table header 3.0 3.0 STF ',
144 'thead' => 'Defines a table header 4.0 STF ',
145 'title' => 'Defines the document title 3.0 3.0 STF ',
146 'tr' => 'Defines a table row 3.0 3.0 STF ',
147 'tt' => 'Defines teletype text 3.0 3.0 STF ',
148 'u' => 'Deprecated. Defines underlined text 3.0 3.0 TF ',
149 'ul' => 'Defines an unordered list 3.0 3.0 STF ',
150 'var' => 'Defines a variable 3.0 3.0 STF ',
151 'xmp' => 'Deprecated. Defines preformatted text 3.0 3.0 ',
152 );
153
154 1 my %element_types = (
155 header => 'Valid header elements: <link> <style> <title> <meta> and <script> belong in <head> ',
156 structural_block => 'Reflect hierarchy and relationship. Block elements belong in structured blocks:
157 <ol> <ul> <dl> <table> and <div> (which can be structural or terminal)',
158 terminal_block => ' <h1> <h2> <h3> <h4> <h5> <h6>
159 <p>
160 <blockquote>
161 <dt>
162 <address>
163 caption>
164 ',
165 multi_purpose_block => 'Can eithor extend or terminate structure.
166 <div> <li> <dd> <td> <th> <form> <noscript>
167 ',
168 inline => ' <em> ',
169 );
170
171
172 sub new {
173 1 ▶ my $class = shift;
Subroutine new: called CC=6 SCAR=22.3
Pod undocumented
174 1 my $tag = shift;
175 1 my $content = shift;
176 1 my $tag_attributes = shift;
177
178 1 my %hash;
179 # build class data structure
180 1 my $self = \%hash;
181
182 1 ▶ if ( (ref $class) eq 'HTML::TagTree') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class eq "HTML::TagTree") 0 1 1
183 0 my $parent_obj = $class;
184 0 $hash{parent} = $parent_obj;
185 0 $class = 'HTML::TagTree';
186 0 push @{$parent_obj->{children_objs}}, $self;
187 }
188 1 ▶ if (defined $tag_attributes && ($tag_attributes ne '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag_attributes and $tag_attributes ne "") 0 1 1
Condition33%1/3
defined $tag_attributes and $tag_attributes ne ""
!ll&&!rl&&r
100
MC/DC0%0/2
defined $tag_attributes and $tag_attributes ne ""
defined $tag_attributes $tag_attributes ne ""
Truth table33%1/3
A and B
A= defined $tag_attributesB= $tag_attributes ne ""
ABresult
0X0
100
111
189 0 $self->{attributes} = $tag_attributes;
190 }
191 1 &process_tag($self,$tag);
192 1 ▶ if (defined $content && ($content ne '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content and $content ne "") 0 1 1
Condition33%1/3
defined $content and $content ne ""
!ll&&!rl&&r
100
MC/DC0%0/2
defined $content and $content ne ""
defined $content $content ne ""
Truth table33%1/3
A and B
A= defined $contentB= $content ne ""
ABresult
0X0
100
111
193 0 &_process_content($self,$content);
194 }
195
196 1 bless $self, $class;
197 1 return $self;
198 }
199
200
201 sub release {
202 1 ▶ my $self = shift;;
Subroutine release: called CC=2 SCAR=7.6
Pod undocumented
203
204 # This method is required to release memory if HTML is used in a persistant Perl program (eg like a server).
205
206 1 ▶ if (exists $self->{parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"parent"}) 0 1 1
207 0 delete $self->{parent}; # remove ref to parent object
208 }
209 1 $self->DESTROY();
210 }
211
212 sub add_attribute {
213 0 ▶ my $self = shift;
Subroutine add_attribute: not called CC=2 SCAR=17.9
Pod undocumented
214 0 my $attribute = shift;
215 0 ▶ if (exists $self->{attributes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"attributes"}) 0 0 0
216 # pad the attributes with a space
217 0 $self->{attributes} .= ' ';
218 }
219 0 $self->{attributes} .= $attribute;
220 }
221
222 sub process_tag {
223 1 ▶ my $self = shift;
Subroutine process_tag: called CC=3 SCAR=12.3
Pod undocumented
224 1 my $tag = shift;
225
226 1 my $attributes = '';
227 1 ▶ if ($tag =~ m/^\s*(\w+)\s+(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^\s*(\w+)\s+(.*)/) 0 1 1
228 0 $tag = $1;
229 0 $attributes = $2;
230 }
231
232 1 $self->{tag} = $tag;
233 # Need to make sure all attribute set values are quoted (eg width="100%")
234 # Using .= since attributes can be passed in several ways.
235 1 ▶ if (exists $self->{attributes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"attributes"}) 0 1 1
236 # pad the attributes with a space
237 0 $self->{attributes} .= ' ';
238 }
239 1 $self->{attributes} .= $attributes;
240 }
241
242 sub _process_content {
243 0 ▶ my $self = shift;
Subroutine _process_content: not called CC=2 SCAR=17.9
244 0 my $content = shift;
245
246 0 ▶ if ( (ref $content) eq 'HTML::TagTree') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq 'HTML::TagTree') { } 0 0 0
247 0 my $child_obj = $content;
248 0 push @{$self->{children_objs}}, $child_obj;
249 # need to fix child_obj tree indent levels;
250
251 0 $child_obj->{parent} = $self;
252 }
253 else {
254 0 push @{$self->{content}}, $content;
255 }
256 }
257
258 sub print_html {
259 # Print the resulting HTML to STDOUT
260 0 ▶ my $self = shift;
Subroutine print_html: not called CC=1 SCAR=6.9
Pod undocumented
261 0 my $indent_level = shift;
262 0 my $no_whitespace_flag = shift;
263
264 0 $self->get_html_text($indent_level,$no_whitespace_flag, 1);
265 }
266
267 sub get_html_text {
268 4 ▶ my $self = shift;
Subroutine get_html_text: called CC=33 SCAR=54.6
Pod undocumented
269 4 my $indent_level = shift;
270 4 my $no_whitespace_flag = shift;
271 4 my $print = shift; # Cause to print to STDOUT immediately
272
273 4 my $content_flag = 0;
274 4 ▶ if (! $indent_level) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($indent_level) 2 2 4
275 2 $indent_level = 0;
276 }
277 4 $indent_level++;
278 4 my $nl = "\n";
279 4 my $tab = ' ' x $indent_level;
280 4 my $tab1 = $tab . ' '; # Tab is 3 spaces a Zuul intendend it to be.
281 4 ▶ if ($no_whitespace_flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_whitespace_flag) 4 0 4
282 4 $tab = '';
283 4 $tab1 = '';
284 4 $nl = '';
285 }
286 4 my $tag_open = $self->{tag};
287 4 ▶ if (exists $tag_open_substitutions{$tag_open}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tag_open_substitutions{$tag_open}) 0 4 4
288 0 $tag_open = $tag_open_substitutions{$tag_open};
289 }
290
291 4 my $html_text = $tab . "<$tag_open";
292 4 ▶ if ($print) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print) 0 4 4
293 0 print $html_text;
294 }
295 4 ▶ if ( (exists $self->{attributes}) && ($self->{attributes} ne '') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"attributes"} and $self->{"attributes"} ne "") 0 4 4
Condition66%2/3
exists $self->{"attributes"} and $self->{"attributes"} ne ""
!ll&&!rl&&r
220
MC/DC0%0/2
exists $self->{"attributes"} and $self->{"attributes"} ne ""
exists $self->{"attributes"} $self->{"attributes"} ne ""
Truth table66%2/3
A and B
A= exists $self->{"attributes"}B= $self->{"attributes"} ne ""
ABresult
0X0
100
111
296 0 ▶ if ((ref $self->{attributes}) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $self->{'attributes'} eq 'ARRAY') { } 0 0 0
elsif (ref $self->{'attributes'} eq 'CODE') { } 0 0 0
elsif (ref $self->{'attributes'} eq 'SCALAR') { } 0 0 0
297 # Check to see if any of the attributes in the array is a callback
298 # Use this callback mechanism to for doing things like minifying of
299 # javascript and css on the fly.
300 0 my $attribute_text;
301 0 foreach my $attribute (@{$self->{attributes}}) {
302 0 ▶ if ((ref $attribute) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attribute eq "CODE") 0 0 0
303 0 my $text = &$attribute();
304 0 $attribute_text .= " " . $text;
305 }
306 0 ▶ if ((ref $attribute) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attribute eq 'SCALAR') { } 0 0 0
307 0 my $text = $$attribute;
308 0 $attribute_text .= " " . $text;
309 }
310 else {
311 0 $attribute_text .= " " . $attribute;
312 }
313 }
314 # change the $self->{attributes} from an ARRAY ref to SCALAR
315 0 $self->{attributes} = $attribute_text;
316 }
317 elsif ((ref $self->{attributes}) eq 'CODE'){
318 0 $self->{attributes} = &{$self->{attributes}}();
319 }
320 elsif ((ref $self->{attributes}) eq 'SCALAR') {
321 0 $self->{attributes} = ${$self->{attributes}};
322 }
323 0 $self->{attributes} = &_quote_attribute_params($self->{attributes});
324 0 ▶ if ($print) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print) { } 0 0 0
325 0 print " $self->{attributes}";
326 }
327 else{
328 0 $html_text .= " $self->{attributes}";
329 }
330 }
331 4 ▶ if ( (!exists $self->{content})
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'content'} || exists $self->{'children_objs'} and exists $valid_empty_tags_for_shortening{$tag_open}) { } 0 4 4
Condition66%4/6
not exists $self->{"content"} || exists $self->{"children_objs"} and exists $valid_empty_tags_for_shortening{$tag_open}
!ll&&!rl&&r
130
exists $self->{'content'} || exists $self->{'children_objs'}
l!l&&r!l&&!r
013
MC/DC0%0/3
not exists $self->{"content"} || exists $self->{"children_objs"} and exists $valid_empty_tags_for_shortening{$tag_open}
exists $self->{'content'} exists $self->{'children_objs'} exists $valid_empty_tags_for_shortening{$tag_open}
Truth table50%2/4
not(A || B) and C
A= exists $self->{'content'}B= exists $self->{'children_objs'}C= exists $valid_empty_tags_for_shortening{$tag_open}
ABCresult
1XX0
01X0
0000
0011
332 && (!exists $self->{children_objs})
333 && (exists $valid_empty_tags_for_shortening{$tag_open})
334 # && 0 # disable this logic
335 ) {
336 0 ▶ if ($print) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print) { } 0 0 0
337 0 print " />$nl";
338 }
339 else{
340 0 $html_text .= " />$nl";
341 }
342 }
343 else{
344 4 ▶ if ($print) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print) { } 0 4 4
345 0 print ">$nl";
346 }
347 else{
348 4 $html_text .= ">$nl";
349 }
350 }
351 4 ▶ if (exists $self->{content}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"content"}) 0 4 4
352 0 $content_flag = 1;
353 0 foreach my $content (@{$self->{content}}) {
354 0 ▶ if ((ref $content) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $content eq 'CODE') { } 0 0 0
elsif (ref $content eq 'SCALAR') { } 0 0 0
355 # Get whats returned from the reference to a subroutine.
356 0 $content = &{$content}();
357 }
358 elsif ((ref $content) eq 'SCALAR') {
359 0 my $content = $$content;
360 }
361 0 ▶ if ($print) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print) { } 0 0 0
362 0 print $tab1 . "$content$nl";
363 }
364 else{
365 0 $html_text .= $tab1 . "$content$nl";
366 }
367 }
368 }
369 4 ▶ if (exists $self->{children_objs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"children_objs"}) 1 3 4
370 1 foreach my $child (@{$self->{children_objs}}) {
371 2 ▶ if ($print) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print) { } 0 2 2
372 0 $child->get_html_text($indent_level,$no_whitespace_flag,$print);
373 }
374 else{
375 2 $html_text .= $child->get_html_text($indent_level,$no_whitespace_flag);
376 }
377 }
378 }
379
380 # Close the tag
381 4 my $tag_close = $self->{tag};
382 4 ▶ if (exists $tag_close_substitutions{$tag_close}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tag_close_substitutions{$tag_close}) { } 0 4 4
383 0 $tag_close = $tag_close_substitutions{$tag_close};
384 0 ▶ if ($print) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print) { } 0 0 0
385 0 print "$tab<$tag_close>$nl";
386 }
387 else{
388 0 $html_text .= "$tab<$tag_close>$nl"; # No / Only print end tag if content or children
389 }
390 }
391 else{
392 4 ▶ if ( (!exists $self->{content})
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'content'} || exists $self->{'children_objs'} and exists $valid_empty_tags_for_shortening{$tag_open}) { } 0 4 4
Condition66%4/6
not exists $self->{"content"} || exists $self->{"children_objs"} and exists $valid_empty_tags_for_shortening{$tag_open}
!ll&&!rl&&r
130
exists $self->{'content'} || exists $self->{'children_objs'}
l!l&&r!l&&!r
013
MC/DC0%0/3
not exists $self->{"content"} || exists $self->{"children_objs"} and exists $valid_empty_tags_for_shortening{$tag_open}
exists $self->{'content'} exists $self->{'children_objs'} exists $valid_empty_tags_for_shortening{$tag_open}
Truth table50%2/4
not(A || B) and C
A= exists $self->{'content'}B= exists $self->{'children_objs'}C= exists $valid_empty_tags_for_shortening{$tag_open}
ABCresult
1XX0
01X0
0000
0011
393 && (!exists $self->{children_objs})
394 && (exists $valid_empty_tags_for_shortening{$tag_open})
395 ) {
396 # Do do any thing tag closed earlier via <tag />
397 }
398 else{
399 4 ▶ if ($print) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print) { } 0 4 4
400 0 print "$tab</$tag_close>$nl"; # Only print end tag if content or children
401 }
402 else{
403 4 $html_text .= "$tab</$tag_close>$nl"; # Only print end tag if content or children
404 }
405 }
406 }
407 4 return $html_text;
408 }
409
410
411 sub _quote_attribute_params {
412 0 ▶ my $attributes_string = shift;
Subroutine _quote_attribute_params: not called CC=35 SCAR=71.4
413
414 0 my $attr_key;
415 my $attr_value;
416 0 my $autoquote_quote_type;
417 0 my $autoquoting_value = '';
418 0 my $char = '';
419 0 my $prev_char = '';
420 0 my $processed_string;
421 0 my $return_attributes_string = '';
422 # <a href="http://pnwpest.org/cgi-bin/forecast/wxfc?station="KCVO"" style="text-decoration:underline; color:blue">
423 # $self->{attributes} =~ s/=([^\"\'])(\S+)/="$1$2"/gsx; # quote the arguments
424 0 my $starting_quote_type;
425 0 my $state = 'looking_for_equal_sign';
426
427 # State Machine:
428 # Note the 'looking_for_start_of_whitespace_while_autoquoting_value' state.
429 # This state is complicated because we don't know if the value contains single or double quotes.
430 # (Hopefully not both!). We'll surround the value with the proper quote type depending on content.
431 # For example:
432 # key=1"plywood
433 # converts to:
434 # key='1"plywood'
435 # The opposite occurs for input"
436 # key=8'long_stud
437 # converts to:
438 # key="8'long_stud"
439 # Default is double quoting. eg:
440 # key=value
441 # converts to:
442 # key="value"
443
444 CHAR:
445 0 ▶ while ($attributes_string =~ m/(.)/sg ){
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes_string =~ /(.)/gs) 0 0 0
446 0 $prev_char = $char;
447 0 $processed_string .= $prev_char;
448 0 $char = $1;
449
450
451 0 ▶ if ($state eq 'looking_for_equal_sign') {
Branch: 9 branches
BranchTrueFalseTotal
if ($state eq 'looking_for_equal_sign') { } 0 0 0
elsif ($state eq 'getting_char_after_equal_sign') { } 0 0 0
elsif ($state eq 'looking_for_end_single_quote') { } 0 0 0
elsif ($state eq 'looking_for_end_double_quote') { } 0 0 0
elsif ($state eq 'looking_for_end_quote') { } 0 0 0
elsif ($state eq 'looking_for_start_of_whitespace') { } 0 0 0
elsif ($state eq 'looking_for_start_of_whitespace_to_quote_before') { } 0 0 0
elsif ($state eq 'looking_for_end_of_white_space') { } 0 0 0
elsif ($state eq 'looking_for_start_of_whitespace_while_autoquoting_value') { } 0 0 0
452 0 ▶ if ($char eq '=' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($char eq '=') { } 0 0 0
elsif ($char =~ /\s/) { } 0 0 0
453 0 $state = 'getting_char_after_equal_sign';
454 }
455 elsif ($char =~ m/\s/) {
456 # Ignore whitespace
457 0 next CHAR;
458 }
459 else{
460 0 $attr_key .= $char; #Save individual attribute keys
461 }
462 0 $return_attributes_string .= $char;
463 }
464 elsif ($state eq 'getting_char_after_equal_sign'){
465 0 ▶ if ($char eq $SINGLE_QUOTE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($char eq $SINGLE_QUOTE) { } 0 0 0
elsif ($char eq $DOUBLE_QUOTE) { } 0 0 0
elsif ($char =~ /\s/) { } 0 0 0
466 0 $state = 'looking_for_end_single_quote';
467 0 $return_attributes_string .= $char;
468 }
469 elsif ($char eq $DOUBLE_QUOTE) {
470 0 $state = 'looking_for_end_double_quote';
471 0 $return_attributes_string .= $char;
472 }
473 elsif ($char =~ m/\s/) {
474 0 $return_attributes_string .= $DOUBLE_QUOTE . $DOUBLE_QUOTE . ' '; # Add two double quotes and space.
475 0 $state = 'looking_for_start_of_whitespace_to_quote_before';
476 }
477 else { # Text directly after equal sign
478 0 $state = 'looking_for_start_of_whitespace_while_autoquoting_value';
479 0 $autoquoting_value = $char;
480 0 $autoquote_quote_type = '';
481 }
482 }
483 elsif ($state eq 'looking_for_end_single_quote') {
484 0 $return_attributes_string .= $char;
485 0 ▶ if ($char eq $SINGLE_QUOTE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq $SINGLE_QUOTE) 0 0 0
486 0 $state = 'looking_for_start_of_whitespace';
487 }
488 }
489 elsif ($state eq 'looking_for_end_double_quote') {
490 0 $return_attributes_string .= $char;
491 0 ▶ if ($char eq $DOUBLE_QUOTE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq $DOUBLE_QUOTE) 0 0 0
492 0 $state = 'looking_for_start_of_whitespace';
493 }
494 }
495 elsif ($state eq 'looking_for_end_quote') {
496 0 ▶ if ($char =~ m/$starting_quote_type/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /$starting_quote_type/) 0 0 0
497 0 $state = 'looking_for_start_of_whitespace';
498 }
499 0 $return_attributes_string .= $char;
500 }
501 elsif ($state eq 'looking_for_start_of_whitespace') {
502 0 ▶ if ($char =~ m/\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /\s/) { } 0 0 0
503 0 $state = 'looking_for_end_of_white_space';
504 }
505 else {
506 0 &error_quoting($state,$char);
507 }
508 0 $return_attributes_string .= $char;
509 }
510 elsif ($state eq 'looking_for_start_of_whitespace_to_quote_before') {
511 0 ▶ if ($char =~ m/\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /\s/) { } 0 0 0
512 0 $state = 'looking_for_end_of_white_space';
513 0 $return_attributes_string .= $DOUBLE_QUOTE . $char;
514 0 next CHAR;
515 }
516 else{
517 0 $return_attributes_string .= $char;
518 0 next CHAR;
519 }
520 }
521 elsif ($state eq 'looking_for_end_of_white_space') {
522 0 ▶ if ($char =~ m/\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /\S/) 0 0 0
523 0 $state = 'looking_for_equal_sign';
524 }
525 0 $return_attributes_string .= $char;
526 0 next CHAR;
527 }
528 elsif ($state eq 'looking_for_start_of_whitespace_while_autoquoting_value') {
529 0 ▶ if ($char =~ m/\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /\s/) { } 0 0 0
530 0 ▶ if ( !$autoquote_quote_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($autoquote_quote_type) 0 0 0
531 0 $autoquote_quote_type = $DOUBLE_QUOTE; # Default to double quote.
532 }
533 0 $return_attributes_string .= $autoquote_quote_type
534 . $autoquoting_value
535 . $autoquote_quote_type
536 . $char;
537 0 $state = 'looking_for_equal_sign';
538 }
539 else{ # Non-whitespace character
540 0 $autoquoting_value .= $char;
541 0 ▶ if ($char eq $DOUBLE_QUOTE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($char eq $DOUBLE_QUOTE) { } 0 0 0
elsif ($char eq $SINGLE_QUOTE) { } 0 0 0
542 0 ▶ if (!$autoquote_quote_type) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $autoquote_quote_type) { } 0 0 0
elsif ($autoquote_quote_type eq $DOUBLE_QUOTE) { } 0 0 0
543 0 $autoquote_quote_type = $SINGLE_QUOTE;
544 }
545 elsif ($autoquote_quote_type eq $DOUBLE_QUOTE){
546 0 &error_quoting($state,$char,'Trying to quote a value with both single and double quote');
547 }
548 }
549 elsif ($char eq $SINGLE_QUOTE) {
550 0 ▶ if (!$autoquote_quote_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($autoquote_quote_type) 0 0 0
551 0 $autoquote_quote_type = $DOUBLE_QUOTE;
552 }
553 }
554 }
555 }
556 else {
557 # Error, should never get here!
558 0 &error_quoting($state, $char, "Undefined State at '$processed_string'");
559 }
560
561
562 } # end of CHAR scope
563
564 # At end of string. Clean up now.
565 0 ▶ if ($state eq 'looking_for_start_of_whitespace_to_quote_before') {
Branch: 5 branches
BranchTrueFalseTotal
if ($state eq 'looking_for_start_of_whitespace_to_quote_before') { } 0 0 0
elsif ($state eq 'getting_char_after_equal_sign') { } 0 0 0
elsif ($state eq 'looking_for_end_double_quote') { } 0 0 0
elsif ($state eq 'looking_for_end_single_quote') { } 0 0 0
elsif ($state eq 'looking_for_start_of_whitespace_while_autoquoting_value') { } 0 0 0
566 0 $return_attributes_string .= '"';
567 }
568 elsif ($state eq 'getting_char_after_equal_sign') {
569 0 $return_attributes_string .= '""';
570 }
571 elsif ($state eq 'looking_for_end_double_quote') {
572 0 &error_quoting($state, $char, "Missing double quote at end of parameter string \n '$processed_string'\n Fixing by adding double quote automatically to end.");
573 0 $return_attributes_string .= $DOUBLE_QUOTE; #Force addition of missing end quote
574 }
575 elsif ($state eq 'looking_for_end_single_quote') {
576 0 &error_quoting($state, $char, "Missing single quote at end of parameter string '$processed_string'");
577 0 $return_attributes_string .= $SINGLE_QUOTE; #Force addition of missing end quote
578 }
579 elsif ($state eq 'looking_for_start_of_whitespace_while_autoquoting_value') {
580 # Add before and after quotes.
581 0 ▶ if ( !$autoquote_quote_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($autoquote_quote_type) 0 0 0
582 0 $autoquote_quote_type = $DOUBLE_QUOTE; # Default to double quote.
583 }
584 0 $return_attributes_string .= $autoquote_quote_type
585 . $autoquoting_value
586 . $autoquote_quote_type;
587 }
588
589 0 return $return_attributes_string;
590 }
591
592
593 sub error_quoting {
594 0 ▶ my $state = shift;
Subroutine error_quoting: not called CC=3 SCAR=24.8
Pod undocumented
595 0 my $char = shift;
596 0 my $msg = shift;
597
598 0 my $total_msg = "HTML::TagTree.pm Error in quoting attribute params -- state:'$state' char:'$char'\n";
599 0 $total_msg .= "$msg\n";
600 0 Carp::cluck($total_msg);
601 0 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 0 0 0
602 0 print STDERR " $msg\n";
603 }
604 0 print STDERR "HTML::TagTree.pm Error in quoting attribute params -- state:'$state' char:'$char'\n";
605 0 ▶ if ($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 0 0 0
606 0 print STDERR " $msg\n";
607 }
608 }
609
610 sub Error {
611 0 ▶ my $self = shift;
Subroutine Error: not called CC=3 SCAR=20.6
Pod undocumented
612 0 my $msg = shift;
613
614 0 my $program_name = $0;
615 0 ▶ if (exists $self->{log_routine}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'log_routine'}) { } 0 0 0
616 0 my $log_routine = $self->{log_routine};
617 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
618 0 &log_routine($msg);
619 }
620 else {
621 0 ▶ if ($msg =! m/\n$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg = !/\n$/) 0 0 0
622 0 $msg .= "\n";
623 }
624 0 print STDERR $msg;
625 }
626 }
627
628
629 sub AUTOLOAD {
630 2 ▶ my $self = shift; # point to the new parent
Subroutine AUTOLOAD: called CC=11 SCAR=35.1
631
632 # This a autoload method catches any called method
633 # that is not defined.
634 2 my $content = shift;
635 2 my $tag_attributes = shift;
636
637 2 my $tag = $AUTOLOAD;
638 2 ▶ if ($tag =~ m/HTML::TagTree::(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /HTML::TagTree::(.+)/) 2 0 2
639 2 $tag = $1;
640 2 ▶ if ($tag eq 'add_content') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq 'add_content') { } 0 2 2
elsif (not &is_valid_html_tag($tag)) { } 0 2 2
641 0 $self->_process_content($content);
642 # *{$AUTOLOAD} = sub { $_[0]->_process_content($_[1]) };
643 0 return;
644 }
645 elsif (! &is_valid_html_tag($tag)) {
646 0 &Error("Not valid tag '$tag' attempted to be used!");
647 0 die "Not valid tag '$tag' attempted to be used!";
648 }
649 2 my %child = ();
650 2 my $child = \%child;
651 2 bless $child, 'HTML::TagTree';
652 2 $child->{tag} = $tag;
653 2 ▶ if ( (defined $tag_attributes) && ($tag_attributes ne '') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag_attributes and $tag_attributes ne "") 0 2 2
Condition33%1/3
defined $tag_attributes and $tag_attributes ne ""
!ll&&!rl&&r
200
MC/DC0%0/2
defined $tag_attributes and $tag_attributes ne ""
defined $tag_attributes $tag_attributes ne ""
Truth table33%1/3
A and B
A= defined $tag_attributesB= $tag_attributes ne ""
ABresult
0X0
100
111
654 0 ▶ if ( (ref $tag_attributes) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tag_attributes eq 'HASH') { } 0 0 0
655 # passing in some parameters
656 # The parameters are for use my this module.
657 # These parameter should not be confused with html tag attributes
658 0 $child->{parameters} = $content;
659 }
660 else {
661 0 $child->{attributes} = $tag_attributes;
662 }
663 }
664 2 ▶ if ( (defined $content) && ($content ne '') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content and $content ne "") 0 2 2
Condition33%1/3
defined $content and $content ne ""
!ll&&!rl&&r
200
MC/DC0%0/2
defined $content and $content ne ""
defined $content $content ne ""
Truth table33%1/3
A and B
A= defined $contentB= $content ne ""
ABresult
0X0
100
111
665 0 ▶ if ( (ref $content) eq 'HTML::TagTree') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq 'HTML::TagTree') { } 0 0 0
666 0 my $grand_child_obj = $content;
667 0 push @{$child->{children_objs}}, $grand_child_obj;
668 # need to fix child_obj tree indent levels;
669
670 0 $grand_child_obj->{parent} = $child;
671 }
672 else {
673 0 ▶ if (lc $tag eq 'select') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $tag eq 'select') { } 0 0 0
674 0 $child->process_select_tag($content);
675 }
676 else{
677 0 push @{$child->{content}}, $content;
678 }
679 }
680 }
681 2 $child{parent} = $self;
682 2 push @{$self->{children_objs}}, $child;
683 2 return $child;
684 }
685
686 0 return;
687 }
688
689
690 sub get_array_of_hash_keys {
691 0 ▶ my $hash_ref = shift;
Subroutine get_array_of_hash_keys: not called CC=4 SCAR=30.0
Pod undocumented
692
693 0 my @array;
694 my %hash;
695 0 ▶ if ( (ref $hash_ref) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash_ref eq "HASH") 0 0 0
696 0 %hash = %$hash_ref;
697 }
698 0 foreach my $key (keys %hash) {
699 0 push @array, $key;
700 }
701 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
702 0 return @array;
703 }
704 else {
705 0 return \@array;
706 }
707 }
708
709
710 sub add_child {
711 0 ▶ my $self = shift;
Subroutine add_child: not called CC=2 SCAR=17.9
Pod undocumented
712 0 my $content = shift;
713 0 my $tag_attributes = shift;
714
715 0 ▶ if ( (ref $content) eq 'HTML::TagTree') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "HTML::TagTree") 0 0 0
716 0 my $child_obj = $content;
717 0 push @{$self->{children_objs}}, $child_obj;
718 # need to fix child_obj tree indent levels;
719
720 0 $child_obj->{parent} = $self;
721 }
722 }
723
724
725 sub DESTROY {
726 6 ▶ my $self = shift;
Subroutine DESTROY: called CC=4 SCAR=13.9
727
728 # Need to following to prevent silly error messages like:
729 # DESTROY created new reference to dead object 'HTML::TagTree' during global destruction.
730 #
731 6 ▶ if (exists $self->{parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"parent"}) 2 4 6
732 2 delete $self->{parent}; # remove ref to parent object
733 }
734 6 ▶ if (exists $self->{children_objs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"children_objs"}) 1 5 6
735 1 foreach my $child (@{$self->{children_objs}}) {
736 2 $child->DESTROY();
737 }
738 1 delete $self->{children_objs}; # remove refs to children objects
739 }
740 }
741
742 sub is_valid_html_tag {
743 2 ▶ my $tag = shift;
Subroutine is_valid_html_tag: called CC=2 SCAR=7.9
Pod undocumented
744
745 2 my $lc_tag = lc $tag;
746 2 ▶ if (exists $valid_tags{$lc_tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $valid_tags{$lc_tag}) 2 0 2
747 2 return 1;
748 }
749 0 print STDERR "Invalid tag '$tag' supplied\n";
750 0 return 0;
751 }
752
753
754 sub get_array_ref_for_table{
755 0 ▶ my $self = shift;
Subroutine get_array_ref_for_table: not called CC=11 SCAR=48.8
Pod undocumented
756 # Turns an HTML table into a 2 dimensional array of data from the table
757
758 0 ▶ if ( (lc $self->{tag}) ne 'table') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $self->{"tag"} ne "table") 0 0 0
759 0 return;
760 }
761 0 my @children = @{$self->{children_objs}};
762 0 my @rows=();
763 0 foreach my $child_obj (@children) {
764 0 ▶ next if ( (lc $child_obj->{tag}) ne 'tr');
Branch: 1 branch
BranchTrueFalseTotal
if lc $child_obj->{"tag"} ne "tr" 0 0 0
765 0 ▶ next if (! exists $child_obj->{children_objs});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $child_obj->{"children_objs"} 0 0 0
766 0 my @possible_tds = @{$child_obj->{children_objs}};
767 0 my @row=();
768 0 foreach my $possible_td (@possible_tds) {
769 0 ▶ next if ( (lc $possible_td->{tag}) ne 'td');
Branch: 1 branch
BranchTrueFalseTotal
if lc $possible_td->{"tag"} ne "td" 0 0 0
770 0 ▶ if (! exists $possible_td->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $possible_td->{"content"}) 0 0 0
771 0 push @row, undef;
772 0 next;
773 }
774 0 my $value = $possible_td->{content};
775 0 ▶ if ( (ref $value) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "ARRAY") 0 0 0
776 0 push @row, undef;
777 0 next;
778 }
779 0 push @row, $value->[0];
780 0 my $attributes = $possible_td->{attributes};
781 0 ▶ if ($attributes =~ m/colspan=['"]?(\d+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes =~ /colspan=['"]?(\d+)/i) 0 0 0
782 0 my $colspan = $1;
783 0 ▶ for (my $i=1; $i<$colspan; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $colspan) 0 0 0
784 # fill in blanks to match colspan
785 0 push @row, undef;
786 }
787 }
788 }
789 0 push @rows, \@row;
790 }
791
792 0 return \@rows;
793 }
794
795 sub header{
796
797 # This subroutine returns the standard HTML header
798
799 0 ▶ my $header = '<?xml version="1.0" encoding="iso-8859-1"?>
Subroutine header: not called CC=1 SCAR=6.9
Pod undocumented
800 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
801 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
802 0 return $header;
803 }
804
805
806 sub process_select_tag{
807 0 ▶ my $self = shift;
Subroutine process_select_tag: not called CC=16 SCAR=56.1
Pod undocumented
808 0 my $hash = shift;
809
810 0 ▶ return undef if ((ref $hash) ne 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash ne "HASH" 0 0 0
811 0 ▶ if (exists $hash->{tag_attributes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{"tag_attributes"}) 0 0 0
812 0 $self->process_tag($hash->{tag_attributes});
813 }
814 0 ▶ if (exists $hash->{options}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{"options"}) 0 0 0
815 0 ▶ if ( (ref $hash->{options}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash->{'options'} eq 'HASH') { } 0 0 0
elsif (ref $hash->{'options'} eq 'ARRAY') { } 0 0 0
816 0 ▶ my $sort_sub = sub {lc $a cmp lc $b}; # Default sort is alphabetical ingoring case
Subroutine __ANON__: not called CC=1 SCAR=6.9
817 0 ▶ if (exists $hash->{sort_sub}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{"sort_sub"}) 0 0 0
818 0 $sort_sub = $hash->{sort_sub};
819 }
820 0 ▶ if (exists $hash->{sort_numeric}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{"sort_numeric"}) 0 0 0
821 0 ▶ $sort_sub = sub {$a <=> $b};
Subroutine __ANON__: not called CC=1 SCAR=6.9
822 }
823 0 ▶ if (exists $hash->{sort_reverse_numeric}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{"sort_reverse_numeric"}) 0 0 0
824 0 ▶ $sort_sub = sub {$b <=> $a};
Subroutine __ANON__: not called CC=1 SCAR=6.9
825 }
826 0 ▶ if (exists $hash->{sort_numeric_by_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{"sort_numeric_by_value"}) 0 0 0
827 0 ▶ $sort_sub = sub {$hash->{options}{$a} <=> $hash->{options}{$b}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
828 }
829 0 ▶ if (exists $hash->{sort_string_by_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{"sort_string_by_value"}) 0 0 0
830 0 ▶ $sort_sub = sub {$hash->{options}{$a} cmp $hash->{options}{$b}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
831 }
832 0 foreach my $option (sort $sort_sub keys %{$hash->{options}}) {
833 0 my $content = "<option ";
834 0 ▶ if ($hash->{options}{$option} eq $hash->{selected}) { # is the option value selected?
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{"options"}{$option} eq $hash->{"selected"}) 0 0 0
835 0 $content .= 'selected="selected" ';
836 }
837 0 my $value = $option;
838 0 ▶ if (not ref ($hash->{options}{$option})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hash->{"options"}{$option}) 0 0 0
839 0 $value = $hash->{options}{$option};
840 }
841 0 $content .= "value=\"$value\">$option</option>";
842 0 push @{$self->{content}}, $content;
843 }
844 }
845 elsif ( (ref $hash->{options}) eq 'ARRAY') {
846 0 foreach my $option ( @{$hash->{options}}) {
847 0 my $content = "<option";
848 0 ▶ if ($option eq $hash->{selected}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option eq $hash->{"selected"}) 0 0 0
849 0 $content .= ' selected="selected" ';
850 }
851 0 $content .= ">$option</option>";
852 0 push @{$self->{content}}, $content;
853 }
854 }
855 }
856 }
857
858
859 sub create_radio_table{
860 0 ▶ my $self = shift;
Subroutine create_radio_table: not called CC=11 SCAR=48.8
Pod undocumented
861 0 my $name = shift;
862 0 my $values_ref = shift;
863 0 my $labels_ref = shift;
864 0 my $checked = shift;
865 0 my $tag_attributes = shift;
866
867 0 my %checked;
868 0 ▶ if ( (ref $checked) eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $checked eq 'HASH') { } 0 0 0
elsif (ref $checked eq 'ARRAY') { } 0 0 0
elsif (ref $checked eq 'SCALAR') { } 0 0 0
869 0 %checked = %$checked;
870 }
871 elsif ( (ref $checked) eq 'ARRAY') {
872 0 foreach my $check (@$checked) {
873 0 $checked{$check} = 1;
874 }
875 }
876 elsif ( (ref $checked) eq 'SCALAR') {
877 0 $checked{$$checked} = 1;
878 }
879 else {
880 0 $checked{$checked} = 1;
881 }
882 0 my @values;
883 0 ▶ if ( (ref $values_ref) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $values_ref eq 'ARRAY') { } 0 0 0
elsif (ref $values_ref eq 'HASH') { } 0 0 0
884 0 @values = @{$values_ref};
885 }
886 elsif ( (ref $values_ref) eq 'HASH') {
887 0 @values = @{&get_array_of_hash_keys($values_ref)};
888 }
889
890 0 my $table;
891 0 ▶ if ($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) { } 0 0 0
892 0 $table = $self->table();
893 }
894 else {
895 0 $table = HTML::TagTree->new('table');
896 }
897 0 foreach my $value (@values) {
898 0 my $tr = $table->tr();
899 0 my $td = $tr->td();
900 0 my $label = $value;
901 0 ▶ if (exists $labels_ref->{$value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $labels_ref->{$value}) 0 0 0
902 0 $label = $labels_ref->{$value};
903 }
904 0 my $checked_string = '';
905 0 ▶ if (exists $checked{$value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $checked{$value}) 0 0 0
906 0 $checked_string='checked=checked';
907 }
908 0 $td->input($label,"type=radio name=$name id=$name value=$value $checked_string $tag_attributes");
909 }
910
911 0 return $table;
912
913 }
914
915 sub create_checkbox_table{
916 0 ▶ my $self = shift;
Subroutine create_checkbox_table: not called CC=12 SCAR=50.5
Pod undocumented
917 0 my $name = shift;
918 0 my $inputs = shift;
919 0 my $labels = shift;
920 0 my $checked = shift;
921 0 my $tag_attributes = shift;
922
923 0 my %checked;
924 0 ▶ if ( (ref $checked) eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $checked eq 'HASH') { } 0 0 0
elsif (ref $checked eq 'ARRAY') { } 0 0 0
elsif (ref $checked eq 'SCALAR') { } 0 0 0
925 0 %checked = %$checked;
926 }
927 elsif ( (ref $checked) eq 'ARRAY') {
928 0 foreach my $check (@$checked) {
929 0 $checked{$check} = 1;
930 }
931 }
932 elsif ( (ref $checked) eq 'SCALAR') {
933 0 $checked{$checked} = 1;
934 }
935 0 my @inputs;
936 0 ▶ if ( (ref $inputs) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $inputs eq 'ARRAY') { } 0 0 0
elsif (ref $inputs eq 'HASH') { } 0 0 0
937 0 @inputs = @$inputs;
938 }
939 elsif ( (ref $inputs) eq 'HASH') {
940 0 foreach my $input (sort {lc $a cmp lc $b} keys %$inputs) {
941 0 push @inputs, $input;
942 }
943 }
944 0 my $table;
945 0 ▶ if ($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) { } 0 0 0
946 0 $table = $self->table();
947 }
948 else {
949 0 $table = HTML->new('table');
950 }
951 0 foreach my $input (@inputs) {
952 0 my $tr = $table->tr();
953 0 my $td = $tr->td();
954 0 my $label = $input;
955 0 ▶ if (exists $labels->{$input}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $labels->{$input}) 0 0 0
956 0 $label = $labels->{$input};
957 }
958 0 my $checked_string = '';
959 0 ▶ if (exists $checked{$input}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $checked{$input}) 0 0 0
960 0 $checked_string='checked=checked';
961 }
962 # $td->input($label,"type=checkbox name=$name id=$name value=$input $checked_string $tag_attributes");
963 0 $td->input($label,"type=checkbox name=$name value=$input $checked_string $tag_attributes");
964 }
965
966 0 return $table;
967 }
968
969 sub make_attribute_key_values{
970 0 ▶ my $self = shift;
Subroutine make_attribute_key_values: not called CC=1 SCAR=6.9
Pod undocumented
971
972 0 my $attr_string = $self->{attributes};
973
974
975 }
976
977
978 sub add_valid_tags{
979 0 ▶ my $self = shift;
Subroutine add_valid_tags: not called CC=7 SCAR=40.3
Pod undocumented
980 0 my $tags = shift;
981
982 0 ▶ if ((ref $tags) eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $tags eq 'ARRAY') { } 0 0 0
elsif (ref $tags eq 'HASH') { } 0 0 0
elsif (ref $tags eq 'SCALAR') { } 0 0 0
elsif (ref $tags eq '') { } 0 0 0
983 0 foreach my $tag (@$tags) {
984 0 $valid_tags{$tag} = 1;
985 }
986 }
987 elsif ((ref $tags) eq 'HASH') {
988 0 foreach my $tag (keys %$tags) {
989 0 $valid_tags{$tag} = 1;
990 }
991 }
992 elsif ((ref $tags) eq 'SCALAR') {
993 0 $valid_tags{$$tags} = 1;
994 }
995 elsif ((ref $tags) eq '') {
996 0 $valid_tags{$tags} = 1;
997 }
998 }
999
1000 sub set_valid_tags{
1001 0 ▶ my $self = shift;
Subroutine set_valid_tags: not called CC=7 SCAR=40.3
Pod undocumented
1002 0 my $tags = shift;
1003
1004 0 %valid_tags = ();
1005 0 ▶ if ((ref $tags) eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $tags eq 'ARRAY') { } 0 0 0
elsif (ref $tags eq 'HASH') { } 0 0 0
elsif (ref $tags eq 'SCALAR') { } 0 0 0
elsif (ref $tags eq '') { } 0 0 0
1006 0 foreach my $tag (@$tags) {
1007 0 $valid_tags{$tag} = 1;
1008 }
1009 }
1010 elsif ((ref $tags) eq 'HASH') {
1011 0 foreach my $tag (keys %$tags) {
1012 0 $valid_tags{$tag} = 1;
1013 }
1014 }
1015 elsif ((ref $tags) eq 'SCALAR') {
1016 0 $valid_tags{$$tags} = 1;
1017 }
1018 elsif ((ref $tags) eq '') {
1019 0 $valid_tags{$tags} = 1;
1020 }
1021 }
1022
1023
1024 %preprocess_tag = (
1025 'html' => sub {
1026 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1027 0 my $tag = shift;
1028
1029 0 my $tag_parameters = $self->{parameters};
1030 0 ▶ if ($tag_parameters !~ m/lang=/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $tag_parameters =~ /lang=/) 0 0 0
1031 0 $self->{parameters} .= ' lang=en-US';
1032 }
1033 }
1034 1 );
1035
1036 sub get_default_head_meta_attributes{
1037 0 ▶ my $attributes = 'http-equiv="content-type" content="text/html; charset=UTF-8"';
Subroutine get_default_head_meta_attributes: not called CC=1 SCAR=6.9
Pod undocumented
1038
1039 0 return $attributes;
1040 }
1041
1042 sub get_http_header {
1043 0 ▶ my $return = "Content-type: text/html\n";
Subroutine get_http_header: not called CC=1 SCAR=6.9
Pod undocumented
1044 0 $return .= "Status: 200 OK\n\n";
1045 }
1046
1047
1048 sub get_doctype {
1049 0 ▶ my $return = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"';
Subroutine get_doctype: not called CC=1 SCAR=6.9
Pod undocumented
1050 0 $return .= ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
1051 0 $return .= "\n\n";
1052
1053 0 return $return;
1054 }
1055
1056 sub version {
1057 0 ▶ return $VERSION;
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
1058 }
1059
1060 1 return 1;
1061
1062
1063 __END__
↑ Summary