blib/lib/GetWeb/FormatText.pm untested

filter: Statementstmt 0.0% 0 / 212 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 31 totaltotal 0.0% 0 / 363 CC 32 SCAR 31.5
CC
32
Cov
0%
form_start
72.0
select_start
72.0
out
30.0
CRAP 23.3
1 require HTML::FormatText;
2 use URI::URL;
3  
4 require 'dumpvar.pl';
5  
6 package GetWeb::FormatText;
7 @ISA = qw( HTML::FormatText );
8  
9 use strict;
10  
11 my $DEFAULT_COLS = 20;
12 my $DEFAULT_SIZE = $DEFAULT_COLS;
13  
14 # sub new
15 # {
16 # my $type = shift;
17 # my $self = $type -> SUPER::new(@_);
18 # $$self{option_seen} = {};
19 # $self;
20 # }
21  
22 sub table_start
23 {
24 shift -> vspace(1);
25 1;
26 }
27  
28 # jfj solve radio-link problem of extra <P> after <INPUT>
29  
30 sub table_end
31 {
32 shift -> vspace(1);
33 1;
34 }
35  
36 sub out
37 {
38 my $self = shift;
39 my $text = shift;
40  
41 if ($self -> {input})
42 {
43 $text = "[$text]";
44 }
45  
46 if ($text =~ /^\s+$/)
47 {
48 $self -> {pending_space} = 1;
49 return;
50 }
51  
52 if ($self->{curpos} + length($text) > $self->{rm})
53 {
54 # line is too long, break it
55 if ($self -> {curpos} > $self -> {lm} + 5)
56 {
57 $text =~ s/^\s+//;
58 $self -> vspace(0);
59 }
60 }
61  
62 $self -> SUPER::out($text);
63 }
64  
65 sub begin
66 {
67 my $self = shift;
68 my $retVal = $self->HTML::FormatText::begin (@_);
69  
70 # $self->{lm} = 3;
71 $self->{lm} = 0; # left margin
72 # $self->{rm} = 70;
73 $self->{rm} = 68; # right margin
74  
75  
76 $retVal;
77 }
78  
79 sub form_start
80 {
81 my ($self, $elem) = @_;
82  
83 $$elem{option_seen} = {};
84 $$self{current_form} = $elem;
85
86 my $phphpaInput = { 'radio' => {},
87 'checkbox' => {} };
88  
89 $elem ->
90 traverse(sub
91 {
92 my ($node, $start, $depth) = @_;
93 return 1 unless $start eq 1;
94
95 my $tag = $node -> tag;
96 return 1 unless $tag eq 'input';
97
98 my $type = $node -> attr('type');
99  
100 my $phpaInput = $$phphpaInput{$type};
101 return 1 unless defined $phpaInput;
102  
103 my $name = $node -> attr('name');
104 my $input_list = $$phpaInput{$name};
105 if (! defined $input_list)
106 {
107 $$phpaInput{$name} = [];
108 $input_list = $$phpaInput{$name};
109 }
110  
111 push(@$input_list,$node);
112 1;
113 },
114 1);
115  
116 #$::gDump = $phphpaInput;
117 #&::dumpvar('');
118  
119 my $phpaInput = $$phphpaInput{'radio'};
120 my $paInput;
121 foreach $paInput (values %$phpaInput)
122 {
123 my $oneChecked = 0;
124 my $input;
125 foreach $input (@$paInput)
126 {
127 my $checked = $input -> attr('checked');
128 if ($checked)
129 {
130 if ($oneChecked++)
131 {
132 $input -> attr('checked',0);
133 }
134 }
135 }
136 unless ($oneChecked)
137 {
138 # check the first radio button
139 $paInput -> [0] -> attr('checked',1);
140 }
141 }
142  
143 # in case another programmer wants to use this structure
144 $elem -> {phphpaInput} = $phphpaInput;
145  
146 1;
147 }
148  
149 sub form_end
150 {
151 delete shift -> {current_form};
152 }
153  
154 sub input_start
155 {
156 my $self = shift;
157 my ($elem) = @_;
158  
159 if (! defined $elem -> attr('type'))
160 {
161 $elem -> attr('type','text');
162 }
163 $self -> input_route("start",@_);
164 }
165  
166 sub input_end
167 {
168 shift -> input_route("end",@_);
169 }
170  
171 sub input_route
172 {
173 my $self = shift;
174 my $direction = shift;
175 my ($elem) = @_;
176  
177 my $type = $elem -> attr('type');
178 $type = lc $type;
179 # defined $type or $type = 'text';
180  
181 my $func = "input_${type}_$direction";
182 my $retval = eval { $self -> $func(@_) };
183 $@ ? 1 : $retval;
184 }
185  
186 sub input_text_out
187 {
188 my ($self,$size,$text) = @_;
189  
190 $text =~ s^[\[\]\\_]^\\$&^g;
191  
192 my $out;
193  
194 if (length($text) + 2 > $size)
195 {
196 $out = "_${text}_";
197 }
198 else
199 {
200 $out = '_' x $size;
201 substr($out,1,length($text)) = $text;
202 }
203 #$out =~ /_$/ or $out .= '_';
204 $self -> {input}++;
205 $self -> out("$out");
206 $self -> {input}--;
207 }
208  
209 sub input_text_start
210 {
211 my ($self, $elem) = @_;
212  
213 my $size = $elem -> attr('size') || $DEFAULT_SIZE;
214 my $value = $elem -> attr('value');
215  
216 $self -> input_text_out($size,$value);
217 1;
218 }
219  
220 sub textarea_start
221 {
222 my ($self, $elem) = @_;
223  
224 my $rows = $elem -> attr('rows');
225 my $cols = $elem -> attr('cols') || $DEFAULT_COLS;
226  
227 my $content_line;
228 my $content = $elem -> content;
229 while ($content_line = shift @$content
230 or $rows > 0)
231 {
232 next if ref($content_line);
233 $self -> vspace(0);
234 $rows--;
235 $self -> input_text_out($cols,$content_line);
236 }
237  
238 if ($rows < 0)
239 {
240 # expand row count
241 my $newRows = $elem -> attr('rows') - $rows;
242 $elem -> attr('rows',$newRows);
243 }
244  
245 0;
246 }
247  
248 sub input_password_start
249 {
250 shift -> input_text_start(@_);
251 }
252  
253 sub checkbox_out
254 {
255 my ($self, $filled) = @_;
256  
257 my $text = $filled?"X":" ";
258 $self -> {input}++;
259 $self -> out("$text");
260 $self -> {input}--;
261 }
262  
263 sub input_checkbox_start
264 {
265 my ($self, $elem) = @_;
266  
267 my $checked = $elem -> attr('checked');
268 $self -> checkbox_out($checked);
269  
270 1;
271 }
272  
273 sub input_radio_start
274 {
275 my $self = shift;
276 my ($elem) = @_;
277 # my $name = $elem -> attr('name');
278  
279 # my $radio_seen = $$self{radio_seen};
280 # if ($$radio_seen{$name}++)
281 # {
282 # $self -> out(" OR ");
283 # }
284  
285 $self -> out("(");
286 $self -> input_checkbox_start(@_);
287 $self -> out(")");
288 }
289  
290 sub vspace
291 {
292 my $self = shift;
293 #my ($package, $filename, $line) = caller;
294 #$self -> collect("v $package $filename $line \n");
295 #my ($package, $filename, $line) = caller(1);
296 #$self -> collect("v $package $filename $line \n");
297 #my ($package, $filename, $line) = caller(2);
298 #$self -> collect("vspacing from $package $filename $line \n");
299 $self -> SUPER::vspace(@_);
300 }
301  
302 sub input_image_start
303 {
304 my $self = shift;
305  
306 $self -> out(" [IMAGE] -");
307 $self -> input_submit_start(@_);
308 }
309  
310 sub button_out
311 {
312 my ($self,$default,$text) = @_;
313 $text = $default unless defined $text;
314  
315 $self -> out(" ");
316 $self -> {input}++;
317 $self -> out("$text");
318 $self -> {input}--;
319 $self -> out(" ");
320 }
321  
322 sub input_submit_start
323 {
324 my ($self, $elem) = @_;
325 my $text = $elem -> attr('value');
326 $self -> button_out("submit",$text);
327 1;
328 }
329  
330 sub input_reset_start
331 {
332 my ($self, $elem) = @_;
333 my $text = $elem -> attr('value');
334 $self -> button_out("reset",$text);
335 1;
336 }
337  
338 # jfjf handle file upload
339  
340 sub option_start
341 {
342 my $self = shift;
343 my ($elem) = @_;
344  
345 defined $self -> {current_form} or
346 die "no form defined";
347  
348 my $name = $self -> {select_name};
349 my $option_seen = $self -> {current_form} -> {option_seen};
350  
351 $self -> vspace(0);
352  
353 my $conjunction = $$self{conj};
354  
355 my $need_conj = $$option_seen{$name}++;
356  
357 if ($need_conj)
358 {
359 $self -> out($conjunction);
360 }
361 else
362 {
363 $self -> adjust_lm(length($conjunction));
364 }
365  
366 $self -> option_out(@_);
367 $self -> {pending_space} = 1;
368  
369 $self -> adjust_lm(-length($conjunction))
370 unless $need_conj;
371  
372 1;
373 }
374  
375 sub option_out
376 {
377 my ($self, $elem) = @_;
378  
379 my $multiple = $elem -> attr('multiple');
380 # my $type = $multiple?'checkbox':'radio';
381 # my $name = $self -> {select_name};
382 my $selected = $elem -> attr('selected');
383  
384 # my $value = $elem -> attr('value');
385 # if (! defined $value)
386 # {
387 # my $content = $elem -> content;
388 # $value = join('',@$content);
389 # }
390  
391 $self -> checkbox_out($selected);
392  
393 # my $input = new HTML::Element ('input',
394 # 'type' => $type,
395 # 'name' => $name,
396 # 'value' => $value);
397  
398 # $input -> attr('checked',1)
399 # if $selected;
400  
401 # $self -> input_start($input);
402 # $self -> input_end($input);
403 }
404  
405 sub select_start
406 {
407 my ($self, $elem) = @_;
408  
409 $self -> vspace(1);
410 $self->adjust_lm( +2 );
411 $self->adjust_rm( -2 );
412  
413 $$self{select_name} = $elem -> attr('name');
414 $$self{current_select} = $elem;
415  
416 my $multiple = $elem -> attr('multiple');
417 $$self{conj} = $multiple?'AND/OR ':'OR ';
418 return 1 if $multiple;
419  
420 my $select = $elem;
421  
422 # select first radio button if no others selected
423 $elem ->
424 traverse(sub
425 {
426 my ($node, $start, $depth) = @_;
427 return 1 unless $start eq 1;
428  
429 my $tag = $node -> tag;
430 return 1 unless $tag eq 'option';
431
432 defined $$select{first_option} or
433 $$select{first_option} = \$node;
434  
435 if (defined $node -> attr('selected'))
436 {
437 # avoid duplicates, protect from bad HTML
438 if ($$select{one_selected})
439 {
440 #print "deselecting\n";
441 $node -> attr('selected',0);
442 }
443 else
444 {
445 #print "noting\n";
446 $$select{one_selected} = 1;
447 }
448 }
449 1;
450 },
451 1);
452 if (! defined $$elem{one_selected})
453 {
454 ${$elem -> {first_option}} -> attr('selected',1);
455 }
456  
457 1;
458 }
459  
460 sub select_end
461 {
462 my ($self, $elem) = @_;
463 $self -> vspace(1);
464 $self->adjust_lm( -2 );
465 $self->adjust_rm( +2 );
466 }
467  
468 sub tr_start
469 {
470 shift -> vspace(0);
471 1;
472 }
473  
474 sub tr_end
475 {
476 shift -> vspace(0);
477 1;
478 }
479  
480 sub li_start
481 {
482 my $self = shift;
483  
484 # jf reduce spacing of recursive lists
485  
486 # make list spacing more regular
487 $self->{pending_space} = 0;
488  
489 $self-> HTML::FormatText::li_start (@_);
490 }
491  
492 sub li_end
493 {
494 my $self = shift;
495  
496 #self -> vspace(1);
497 # vspace is 1 in FormatText, single-spaced lists are more standard, so:
498 $self->vspace(0);
499
500 $self->adjust_lm(-2);
501 my $markers = $self->{markers};
502 if ($markers->[-1] =~ /^\d+/) {
503 # increment ordered markers
504 $markers->[-1]++;
505 }
506 }
507  
508 sub font_start
509 {
510 my($self, $elem) = @_;
511 my $size = $elem->attr('size');
512 return 1 unless defined $size;
513 if ($size =~ /^\s*[+\-]/) {
514 my $base = $self->{basefont_size}[-1];
515 $size = $base + $size;
516 }
517 push(@{$self->{font_size}}, $size);
518 1;
519 }
520  
521 sub basefont_start
522 {
523 my($self, $elem) = @_;
524 my $size = $elem->attr('size');
525 return 1 unless defined $size;
526 push(@{$self->{basefont_size}}, $size);
527 1;
528 }
529  
530 1;