Flute/HTML.pm untested

filter: Statementstmt 0.0% 0 / 450 Branchbran 0.0% 0 / 214 Conditioncond 0.0% 0 / 79 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 803 CC 108 SCAR 56.0
CC
108
Cov
0%
_elt_handler
702.0
_elt_indicate_replacements
462.0
_set_selected
380.0
CRAP 269.7
1 package Template::Flute::HTML;
2  
3 use strict;
4 use warnings;
5  
6 use Encode;
7 use Path::Tiny ();
8 use XML::Twig;
9 use HTML::Entities;
10  
11 use Template::Flute::Increment;
12 use Template::Flute::Container;
13 use Template::Flute::List;
14 use Template::Flute::Form;
15 use Template::Flute::UriAdjust;
16  
17 use Scalar::Util qw/blessed/;
18  
19 =head1 NAME
20  
21 Template::Flute::HTML - HTML Template Parser
22  
23 =head1 SYNOPSIS
24  
25 $html_object = new Template::Flute::HTML;
26  
27 $html_object->parse('<div class="example">Hello world</div>');
28 $html_object->parse_file($html_file, $spec);
29  
30 =head1 CONSTRUCTOR
31  
32 =head2 new
33  
34 Create a Template::Flute::HTML object.
35  
36 =cut
37  
38 # constructor
39  
40 sub new {
41 my ($class, $self);
42  
43 $class = shift;
44  
45 my %args = @_;
46  
47 $self = {%args, containers => {}, lists => {}, pagings => {}, forms => {},
48 params => {}, values => {}, query => {}, file => undef};
49
50 bless $self, $class;
51 }
52  
53 =head1 METHODS
54  
55 =head2 containers
56  
57 Returns list of L<Template::Flute::Container> objects for this template.
58  
59 =cut
60  
61 # containers method - return list of Template::Flute::Container objects for this# template
62  
63 sub containers {
64 my ($self) = @_;
65  
66 return values %{$self->{containers}};
67 }
68  
69 =head2 container NAME
70  
71 Returns container object named NAME.
72  
73 =cut
74  
75 sub container {
76 my ($self, $name) = @_;
77  
78 if (exists $self->{containers}->{$name}) {
79 return $self->{containers}->{$name};
80 }
81 }
82  
83 =head2 lists
84  
85 Returns list of L<Template::Flute::List> objects for this template.
86  
87 =cut
88  
89 sub lists {
90 my ($self) = @_;
91  
92 return values %{$self->{lists}};
93 }
94  
95 =head2 list NAME
96  
97 Returns list object named NAME.
98  
99 =cut
100  
101 # list method - returns specific list object
102 sub list {
103 my ($self, $name) = @_;
104  
105 if (exists $self->{lists}->{$name}) {
106 return $self->{lists}->{$name};
107 }
108 }
109  
110 =head2 forms
111  
112 Returns list of L<Template::Flute::Form> objects for this template.
113  
114 =cut
115  
116 sub forms {
117 my ($self) = @_;
118  
119 return values %{$self->{forms}};
120 }
121  
122 =head2 form NAME
123  
124 Returns form object named NAME.
125  
126 =cut
127  
128 # form method - returns specific form object
129 sub form {
130 my ($self, $name) = @_;
131  
132 if (exists $self->{forms}->{$name}) {
133 return $self->{forms}->{$name};
134 }
135 }
136  
137 =head2 values
138  
139 Returns list of values for this template.
140  
141 =cut
142  
143 sub values {
144 my ($self) = @_;
145  
146 return values %{$self->{values}};
147 }
148  
149 =head2 iterators
150  
151 Returns hash with iterator names as keys and iterator objects
152 as values.
153  
154 =cut
155  
156 sub iterators {
157 my ($self) = @_;
158 my (%iterators, $name, $object);
159  
160 for my $list (CORE::values %{$self->{lists}}) {
161 $name = $list->iterator('name');
162 next unless $name;
163 $iterators{$name} = $list->iterator();
164 }
165  
166 wantarray ? %iterators : \%iterators;
167 }
168  
169 =head2 root
170  
171 Returns root of HTML/XML tree.
172  
173 =cut
174  
175 # root method - returns root of HTML/XML tree
176 sub root {
177 my ($self) = @_;
178  
179 return $self->{xml}->root();
180 }
181  
182 =head2 translate I18NOBJECT
183  
184 Localizes static text inside the HTML template through
185 the I18NOBJECT.
186  
187 =cut
188  
189 sub _translate_string {
190 my ($self, $i18n, $text) = @_;
191  
192 # remove surrounding whitespace before passing
193 # to translation function
194 my ($ws_before, $ws_after);
195 if ($text =~ s/^(\s+)//s) {
196 $ws_before = $1;
197 }
198 else {
199 $ws_before = '';
200 }
201  
202 if ($text =~ s/(\s+)$//s) {
203 $ws_after = $1;
204 }
205 else {
206 $ws_after = '';
207 }
208 # return undef if no text is left
209 return unless length($text);
210  
211 # collapse the whitespace inside, discarding it for the
212 # purpose of localization.
213 my $original = $text;
214 $text =~ s/\s+/ /g;
215 my $translated = $i18n->localize($text);
216 if ($translated eq $text) {
217 $text = $original;
218 }
219 else {
220 $text = $translated;
221 }
222 # translate and restore spaces
223 return $ws_before . $text . $ws_after;
224 }
225  
226 sub _translate_attribute {
227 my ($self, $i18n, $elt, $attribute) = @_;
228 die unless ($i18n && $elt && $attribute);
229 # here the i18n-key doesn't make sense, because we can't know
230 # exactly which attribute has to be translated.
231  
232 if (my $string = $elt->att($attribute)) {
233 my $translated = $self->_translate_string($i18n, $string);
234 if (defined $translated) {
235 $elt->set_att($attribute, $translated);
236 }
237 }
238 }
239  
240  
241 sub translate {
242 my ($self, $i18n, @translate_attributes) = @_;
243 my ($root, @text_elts, $i18n_ret, $parent_gi, $parent_i18n,
244 %parents, $text, $ws_before, $ws_after);
245  
246 $root = $self->root();
247  
248 @text_elts = $root->descendants('#TEXT');
249  
250 for my $elt (@text_elts) {
251 $parent_gi = $elt->parent->gi();
252 my %exclude = (
253 style => 1,
254 script => 1,
255 textarea => 1,
256 );
257 next if $exclude{$parent_gi};
258
259 $parent_i18n = $elt->parent->att('i18n-key');
260
261 if ($parent_i18n) {
262 $i18n_ret = $i18n->localize($parent_i18n);
263 }
264 else {
265 $text = $elt->text;
266 $i18n_ret = $self->_translate_string($i18n, $text);
267 next unless defined $i18n_ret;
268 }
269  
270 $elt->set_text($i18n_ret);
271 }
272  
273 foreach my $attr (@translate_attributes) {
274 if ($attr =~ m/\./) {
275 my ($tag, $el_att, %conditions) = split(/\./, $attr);
276 if ($tag && $el_att) {
277 my $xpath = $tag . '[@' . $el_att . ']';
278 my @elements = $root->descendants($xpath);
279 TRX_ATT_EL:
280 foreach my $el (@elements) {
281 foreach my $att_cond (keys %conditions) {
282 my $cond_val = $conditions{$att_cond};
283 die "undefined condition on translate attribute $attr"
284 unless defined $cond_val;
285 my $el_val = $el->att($att_cond);
286 next TRX_ATT_EL unless defined $el_val;
287 next TRX_ATT_EL unless $el_val eq $cond_val;
288 }
289 # still here? good.
290 $self->_translate_attribute($i18n, $el, $el_att);
291 }
292 }
293 else {
294 die "Invalid translate_attributes configuration: $attr";
295 }
296 }
297 else {
298 my @elements = $root->descendants('*[@'. $attr . ']');
299 foreach my $el (@elements) {
300 $self->_translate_attribute($i18n, $el, $attr);
301 }
302 }
303 }
304  
305 # cleanup
306 if ($self->{_i18n_key_elts}) {
307 for my $elt (@{$self->{_i18n_key_elts}}) {
308 $elt->del_att('i18n-key');
309 }
310  
311 delete $self->{_i18n_key_elts};
312 }
313  
314 return;
315 }
316  
317 =head2 file
318  
319 Returns name of template file.
320  
321 =cut
322  
323 sub file {
324 my $self = shift;
325
326 return $self->{file};
327 }
328  
329 =head2 parse [ STRING | SCALARREF ] SPECOBJECT
330  
331 Parses HTML template from STRING or SCALARREF with the help
332 of a L<Template::Flute::Specification> object SPECOBJECT.
333  
334 =cut
335  
336 sub parse {
337 my ($self, $template, $spec_object, $snippet) = @_;
338 my ($object);
339
340 if (ref($template) eq 'SCALAR') {
341 $object = $self->_parse_template($template, $spec_object, $snippet);
342 }
343 else {
344 $object = $self->_parse_template(\$template, $spec_object, $snippet);
345 }
346  
347 return $object;
348 }
349  
350 =head2 parse_file FILENAME SPECOBJECT
351  
352 Parses HTML template from file FILENAME with the help
353 of a L<Template::Flute::Specification> object SPECOBJECT.
354  
355 =cut
356
357 sub parse_file {
358 my ($self, $template_file, $spec_object, $snippet) = @_;
359  
360 return $self->_parse_template($template_file, $spec_object, $snippet);
361 }
362  
363 sub _parse_template {
364 my ($self, $template, $spec_object, $snippet) = @_;
365 my ($twig, %twig_args, $xml, $object, $list, $html_content, $encoding);
366  
367 $object = {specs => {}, lists => {}, forms => {}, params => {}};
368
369 %twig_args = (twig_handlers => {_all_ => sub {$self->_parse_handler($_[1], $spec_object)}});
370  
371 if ($XML::Twig::VERSION > 3.39) {
372 $twig_args{output_html_doctype} = 1;
373 }
374
375 $twig = new XML::Twig (%twig_args);
376  
377 if (ref($template) eq 'SCALAR') {
378 $self->{file} = '';
379 $html_content = decode_entities($$template);
380 }
381 else {
382 $self->{file} = $template;
383 $encoding = $spec_object->encoding();
384 $html_content = Path::Tiny::path($template)->slurp({binmode => ":encoding($encoding)"});
385 my $first_char = substr($html_content, 0, 1);
386 if ($first_char eq "\x{feff}") {
387 substr($html_content, 0, 1, '');
388 }
389 unless ($encoding eq 'utf8') {
390 $html_content = encode('utf8', $html_content);
391 }
392 }
393 $xml = $snippet ? $twig->safe_parse($html_content) : $twig->safe_parse_html($html_content);
394  
395 unless ($xml) {
396 my $failure = '';
397 if ($@ =~ /, byte ([0-9]+) at/) {
398 $failure = '...' . substr($html_content, $1, 50) . '...';
399 }
400 die "Invalid HTML template: $html_content: $@ $failure\n";
401 }
402
403 _fix_script_tags($xml);
404  
405 $self->{xml} = $object->{xml} = $xml;
406  
407 return $object;
408 }
409  
410 # parse_handler - Callback for HTML elements
411  
412 sub _parse_handler {
413 my ($self, $elt, $spec_object) = @_;
414 my ($gi, @classes, @static_classes, $class_names, $id, $elt_name, $name, $sob, $sob_ref);
415  
416 $gi = $elt->gi();
417 $class_names = $elt->class();
418 $id = $elt->id();
419 $elt_name = $elt->att('name');
420  
421 if ($self->{uri}) {
422 my %targets = (a => {link_att => 'href'},
423 base => {link_att => 'href'},
424 img => {link_att => 'src'},
425 link => {link_att => 'href'},
426 script => {link_att => 'src'},
427 form => {link_att => 'action'},
428 );
429  
430 # adjust links to static files
431 if (exists $targets{$gi}) {
432 my $link_att = $targets{$gi}->{link_att};
433 my $link_value = $elt->att($link_att);
434 my $uri_adjust;
435  
436 if (defined $link_value) {
437 $uri_adjust = Template::Flute::UriAdjust->new(uri => $link_value,
438 adjust => $self->{uri},
439 );
440  
441 if (my $result = $uri_adjust->result) {
442 $elt->set_att($link_att, $result);
443 }
444 }
445 }
446 }
447 # don't act on elements without class, id or name attribute
448 return unless $class_names || $id || $elt_name;
449
450 # weed out "static" classes
451 if ($class_names) {
452 for my $class (split(/\s+/, $class_names)) {
453 if ($spec_object->elements_by_class($class)) {
454 push @classes, $class;
455 }
456 else {
457 push @static_classes, $class;
458 }
459 }
460 }
461
462 if ($id) {
463 $sob_ref = $spec_object->elements_by_id($id);
464 for my $sob (@$sob_ref) {
465 $name = $sob->{name} || $id;
466 $self->_elt_handler($sob, $elt, $gi, $spec_object, $name);
467 }
468 }
469  
470 if ($elt_name) {
471 $sob_ref = $spec_object->elements_by_name($elt_name);
472
473 for my $sob (@$sob_ref) {
474 $name = $sob->{name} || $elt_name;
475 $self->_elt_handler($sob, $elt, $gi, $spec_object, $name);
476 }
477 }
478
479 for my $class (@classes) {
480 $sob_ref = $spec_object->elements_by_class($class);
481 for my $sob (@$sob_ref) {
482 $name = $sob->{name} || $class;
483 $self->_elt_handler($sob, $elt, $gi, $spec_object, $name, \@static_classes);
484 }
485 }
486  
487 return $self;
488 }
489  
490 sub _elt_handler {
491 my ($self, $sob, $elt, $gi, $spec_object, $name, $static_classes) = @_;
492  
493 if ($sob->{type} eq 'container') {
494 if (exists $self->{containers}->{$name}) {
495 push @{$self->{containers}->{$name}->{sob}->{elts}}, $elt;
496 }
497 else {
498 $sob->{elts} = [$elt];
499 $self->{containers}->{$name} = new Template::Flute::Container ($sob, $spec_object, $name);
500 }
501  
502 return $self;
503 }
504
505 if ($sob->{type} eq 'list') {
506 my $iter;
507
508 if (exists $self->{lists}->{$name}) {
509 # record static classes
510 my ($list, $first_static, $first_classes);
511
512 $list = $self->{lists}->{$name};
513  
514 if ($first_static = $list->static_class(0)) {
515 # remove static class from initial list element
516 $first_classes = $list->elt->att('class');
517 #$first_classes =~ s/\s*\b$first_static\b//;
518 $list->elt->set_att('class', $first_classes);
519 }
520  
521 $list->set_static_class(@$static_classes);
522
523 # discard repeated lists
524 $elt->cut();
525 return;
526 }
527
528 $sob->{elts} = [$elt];
529  
530 # weed out parameters which aren't descendants of list element
531 for my $p (@{$self->{params}->{$name}->{array}}) {
532 my @p_new;
533
534 for my $p_elt (@{$p->{elts}}) {
535 for my $a ($p_elt->ancestors()) {
536 if ($a eq $elt) {
537 push (@p_new, $p_elt);
538 last;
539 }
540 }
541 }
542  
543 $p->{elts} = \@p_new;
544 }
545
546 $self->{lists}->{$name} = new Template::Flute::List ($sob, [join(' ', @$static_classes)], $spec_object, $name);
547 $self->{lists}->{$name}->params_add($self->{params}->{$name}->{array});
548 $self->{lists}->{$name}->paging_add($self->{paging}->{$name});
549 $self->{lists}->{$name}->separators_add($self->{separators}->{$name}->{array});
550 $self->{lists}->{$name}->increments_add($self->{increments}->{$name}->{array});
551
552 if (exists $sob->{iterator}) {
553 if ($iter = $spec_object->iterator($sob->{iterator})) {
554 $self->{lists}->{$name}->set_iterator($iter);
555 }
556 }
557  
558 if (exists $sob->{filter}) {
559 $self->{lists}->{$name}->set_filter($sob->{filter});
560 }
561
562 return $self;
563 }
564  
565 if ($sob->{type} eq 'separator') {
566 push (@{$sob->{elts}}, $elt);
567 $self->_elt_indicate_replacements($sob, $elt, $gi, $name, $spec_object);
568  
569 if (exists $self->{lists}->{$sob->{list}}) {
570 $self->{lists}->{$sob->{list}}->separators_add([$sob]);
571 }
572 else {
573 $self->{separators}->{$sob->{list}}->{hash}->{$name} = $sob;
574 push(@{$self->{separators}->{$sob->{list}}->{array}}, $sob);
575 }
576 }
577 elsif ($sob->{type} eq 'paging') {
578 # go through paging elements and record corresponding HTML elements
579 for my $element_ref (CORE::values %{$sob->{elements}}) {
580 if (exists $self->{paging_elements}->{$name}->{$element_ref->{type}}) {
581 $element_ref->{elts} = $self->{paging_elements}->{$name}->{$element_ref->{type}}->{elts};
582 }
583 }
584  
585 push (@{$sob->{elts}}, $elt);
586 $self->_elt_indicate_replacements($sob, $elt, $gi, $name, $spec_object);
587  
588 if (exists $self->{lists}->{$sob->{list}}) {
589 $self->{lists}->{$sob->{list}}->paging_add($sob);
590 }
591 else {
592 $self->{paging}->{$sob->{list}} = $sob;
593 }
594 }
595
596 if ($sob->{type} eq 'form') {
597 # only HTML <form> elements can be tied to 'form'
598 return $self if $elt->tag ne 'form';
599  
600 $sob->{elts} = [$elt];
601  
602 $self->{forms}->{$name} = new Template::Flute::Form ($sob);
603  
604 $self->{forms}->{$name}->fields_add($self->{fields}->{$name}->{array});
605 $self->{forms}->{$name}->params_add($self->{params}->{$name}->{array});
606
607 $self->{forms}->{$name}->inputs_add($spec_object->form_inputs($name));
608
609 return $self;
610 }
611
612 if ($sob->{type} eq 'param') {
613 push (@{$sob->{elts}}, $elt);
614  
615 $self->_elt_indicate_replacements($sob, $elt, $gi, $name, $spec_object);
616  
617 if ($sob->{increment}) {
618 # create increment object and record it for increment updates
619 my $inc = new Template::Flute::Increment (increment => $sob->{increment});
620
621 $sob->{increment} = $inc;
622 push(@{$self->{increments}->{$sob->{list}}->{array}}, $inc);
623 }
624  
625 $self->{params}->{$sob->{list} || $sob->{form}}->{hash}->{$name} = $sob;
626 push(@{$self->{params}->{$sob->{list} || $sob->{form}}->{array}}, $sob);
627 } elsif ($sob->{type} eq 'element') {
628 push (@{$sob->{elts}}, $elt);
629 $self->_elt_indicate_replacements($sob, $elt, $gi, $name, $spec_object);
630 $self->{paging_elements}->{$sob->{paging}}->{$sob->{element_type}} = $sob;
631 } elsif ($sob->{type} eq 'value') {
632 push (@{$sob->{elts}}, $elt);
633  
634 $self->_elt_indicate_replacements($sob, $elt, $gi, $name, $spec_object);
635
636 $self->{values}->{$name} = $sob;
637 } elsif ($sob->{type} eq 'field') {
638 # HTML <form> elements can't be tied to 'field'
639 return $self if $elt->tag eq 'form';
640
641 # match for form field found in HTML
642 push (@{$sob->{elts}}, $elt);
643  
644 if ($gi eq 'select') {
645 if ($sob->{iterator}) {
646 $elt->{"flute_$name"}->{rep_sub} = sub {
647 _set_selected($_[0], $_[1],
648 $spec_object->resolve_iterator($sob->{iterator}),
649 $sob,
650 );
651 };
652 }
653 else {
654 $elt->{"flute_$name"}->{rep_sub} = \&_set_selected;
655 }
656 }
657 push(@{$self->{fields}->{$sob->{form}}->{array}}, $sob);
658 } elsif ($sob->{type} eq 'i18n') {
659  
660 $elt->set_att('i18n-key', $sob->{'key'});
661 push(@{$self->{_i18n_key_elts}}, $elt);
662 } else {
663 return $self;
664 }
665 }
666  
667 # _elt_indicate_replacements - indicate location of replacements
668  
669 sub _elt_indicate_replacements {
670 my ($self, $sob, $elt, $gi, $name, $spec_object) = @_;
671 my ($elt_text, $att_orig);
672
673 if (exists $sob->{op}) {
674 if ($sob->{op} eq 'hook') {
675 $elt->{"flute_$name"}->{rep_sub} = \&hook_html;
676 return;
677 }
678 elsif (($sob->{op} eq 'prepend' || $sob->{op} eq 'append') && ! $sob->{target}) {
679 $elt->{"flute_$name"}->{rep_text_orig} = $elt->text_only;
680 my $joiner = '';
681 if (exists $sob->{joiner}) {
682 $joiner = $sob->{joiner};
683 }
684 if ($sob->{op} eq 'append') {
685 $elt->{"flute_$name"}->{rep_sub} = sub {
686 my ($elt, $str) = @_;
687 $str ||= '';
688 if (! $joiner || $str =~ /\S/) {
689 $elt->set_text($elt->{"flute_$name"}->{rep_text_orig} . $joiner . $str);
690 }
691 };
692 }
693 else {
694 $elt->{"flute_$name"}->{rep_sub} = sub {
695 my ($elt, $str) = @_;
696 $str ||= '';
697 if (! $joiner || $str =~ /\S/) {
698 $elt->set_text($str . $joiner . $elt->{"flute_$name"}->{rep_text_orig});
699 }
700 };
701 };
702 }
703 elsif ($sob->{op} eq 'toggle' && exists $sob->{args}
704 && $sob->{args} eq 'tree') {
705 $elt->{"flute_$name"}->{rep_sub} = sub {
706 my ($elt, $value) = @_;
707 unless (defined $value && $value =~ /\S/) {
708 $elt->cut;
709 }
710  
711 return;
712 };
713 }
714 elsif ($sob->{op} eq 'keep') {
715 $elt->{"flute_$name"}->{rep_sub} = \&_keep;
716 }
717 }
718  
719 if ($sob->{target}) {
720 if (exists $sob->{op}) {
721 if ($sob->{op} eq 'append' || $sob->{op} eq 'prepend') {
722 # keep original values around. The target could be a
723 # wildcard.
724 foreach my $attribute (keys %{ $elt->atts }) {
725 my $att_orig = $elt->att($attribute);
726 unless (defined $att_orig) {
727 $att_orig = '';
728 }
729 $elt->{"flute_$name"}->{rep_att_orig}->{$attribute} = $att_orig;
730 }
731 }
732 }
733
734 $elt->{"flute_$name"}->{rep_att} = $sob->{target};
735 } elsif ($gi eq 'img') {
736 # replace src attribute instead of text
737 $elt->{"flute_$name"}->{rep_att} = 'src';
738 } elsif ($gi eq 'input') {
739 my $type = $elt->att('type');
740 # replace value attribute instead of text
741 $elt->{"flute_$name"}->{rep_att} = 'value';
742
743 } elsif ($gi eq 'select') {
744 if ($sob->{iterator}) {
745 $elt->{"flute_$name"}->{rep_sub} = sub {
746 _set_selected($_[0], $_[1],
747 $spec_object->resolve_iterator($sob->{iterator}),
748 $sob,
749 );
750 };
751 } else {
752 $elt->{"flute_$name"}->{rep_sub} = \&_set_selected;
753 }
754 } elsif (! $elt->contains_only_text()) {
755 # contains real elements, so we have to be careful with
756 # set text and apply it only to the first PCDATA element
757 if ($elt_text = $elt->first_descendant('#PCDATA')) {
758 $elt->{"flute_$name"}->{rep_elt} = $elt_text;
759 }
760 }
761 }
762  
763 # _set_selected - Set selected value in a dropdown menu
764  
765 sub _set_selected {
766 my ($elt, $value, $iter, $sob) = @_;
767 my (@children, $eltval, $optref, $cond);
768  
769 @children = $elt->children('option');
770
771 if ($iter) {
772 # remove existing children
773 if (exists $sob->{keep} && $sob->{keep} eq 'empty_value') {
774 $cond = 'option[@value=~/\S/]';
775 }
776 else {
777 $cond = '';
778 }
779
780 $elt->cut_children($cond);
781  
782 if (! $value) {
783 # check whether there is a default in the specification
784 if (exists $sob->{iterator_default} && $sob->{iterator_default}) {
785 $value = $sob->{iterator_default};
786 }
787 }
788  
789 # determine where to look for labels and values in the iterator
790 my $value_k = "value";
791 my $label_k = "label";
792 if (exists $sob->{iterator_value_key} && $sob->{iterator_value_key}) {
793 $value_k = $sob->{iterator_value_key};
794 }
795 if (exists $sob->{iterator_name_key} && $sob->{iterator_name_key}) {
796 $label_k = $sob->{iterator_name_key};
797 }
798  
799 # get options from iterator
800 $iter->reset();
801 while ($optref = $iter->next()) {
802  
803 # check the record if is an object
804 my $is_an_object = blessed($optref);
805  
806 my (%att, $text);
807 my ($record_value, $record_label);
808  
809 if ($is_an_object) {
810 # here we could also peek inside the object, but hey,
811 # if it's an object the correct practise is not to
812 # look inside it.
813 if ($optref->can("$value_k")) {
814 $record_value = $optref->$value_k;
815 }
816 if ($optref->can("$label_k")) {
817 $record_label = $optref->$label_k;
818 }
819 }
820 else {
821 if (exists $optref->{$value_k}) {
822 $record_value = $optref->{$value_k};
823 }
824 if (exists $optref->{$label_k}) {
825 $record_label = $optref->{$label_k};
826 }
827 }
828  
829 if (defined $record_label) {
830 $text = $record_label;
831 $att{value} = $record_value;
832 }
833 else {
834 $text = $record_value;
835 }
836 if (defined $value and
837 defined $record_value) {
838 if (ref($value) eq 'ARRAY') {
839 $att{selected} = 'selected' if grep { $record_value eq $_ } @$value;
840 }
841 elsif ($record_value eq $value) {
842 $att{selected} = 'selected';
843 }
844 }
845
846 $elt->insert_new_elt('last_child', 'option',
847 \%att, $text);
848 }
849  
850 # reset iterator in case we use it multiple times
851 $iter->reset;
852 }
853 else {
854 for my $node (@children) {
855 $eltval = $node->att('value');
856  
857 unless (length($eltval)) {
858 $eltval = $node->text();
859 }
860
861 if ($eltval eq $value) {
862 $node->set_att('selected', 'selected');
863 }
864 else {
865 $node->del_att('selected', '');
866 }
867 }
868 }
869 }
870  
871 =head2 hook_html ELT VALUE
872  
873 Parse HTML provided by VALUE and replace any children of ELT
874 with the result.
875  
876 =cut
877
878 sub hook_html {
879 my ($elt, $value) = @_;
880 my ($parser, $html, $body, @children, @ret, $elt_hook);
881  
882 unless (defined $value && $value =~ /\S/) {
883 $elt->cut_children;
884 return '';
885 }
886
887 $parser = new XML::Twig ();
888 unless ($html = $parser->safe_parse_html($value)) {
889 my $failure = '';
890 if ($@ =~ /, byte ([0-9]+) at/) {
891 $failure = '...' . substr($value, $1, 40)
892 . '...';
893 }
894 die "Failed to parse HTML snippet: $@. $failure\n";
895 }
896 _fix_script_tags($html);
897  
898 $elt->cut_children();
899  
900 if (my $head_elt = $html->root->first_child('head')) {
901 # preserve useful elements like <script> inside HTML snippets (GH #99).
902 @children = $head_elt->cut_children;
903 for my $h_elt (@children) {
904 $h_elt->paste(last_child => $elt);
905 }
906 }
907  
908 my $fc = $html->root()->first_child('body');
909  
910 if ($elt->gi eq 'select' && $fc->first_child->gi eq 'select') {
911 $fc = $fc->first_child;
912 }
913  
914 @children = $fc->cut_children();
915  
916 for my $elt_hook (@children) {
917 $elt_hook->paste(last_child => $elt);
918 }
919
920 return;
921 }
922  
923 sub _keep {
924 my ($elt, $value) = @_;
925  
926 if ($value) {
927 $elt->set_text($value);
928 }
929  
930 return;
931 }
932  
933 sub _fix_script_tags {
934 my $parsed = shift;
935 # script tags should not be escaped. Please note that this should
936 # be safe. It affects only the *content* of the <script> tags. If
937 # your values contains injected JS, having the & escaped as &amp;
938 # or > as &gt; will not save you.
939 my @elts = $parsed->get_xpath('//script');
940 foreach my $el (@elts) {
941 $el->set_asis;
942 }
943 }
944  
945 =head1 AUTHOR
946  
947 Stefan Hornburg (Racke), <racke@linuxia.de>
948  
949 =head1 LICENSE AND COPYRIGHT
950  
951 Copyright 2010-2019 Stefan Hornburg (Racke) <racke@linuxia.de>.
952  
953 This program is free software; you can redistribute it and/or modify it
954 under the terms of either: the GNU General Public License as published
955 by the Free Software Foundation; or the Artistic License.
956  
957 See http://dev.perl.org/licenses/ for more information.
958  
959 =cut
960  
961 1;
962