Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / Presenter / Tag.pm
index 1896445..a140fe4 100644 (file)
@@ -10,7 +10,7 @@ use Exporter qw(import);
 our @EXPORT_OK = qw(
   html_tag input_tag hidden_tag javascript man_days_tag name_to_id select_tag
   checkbox_tag button_tag submit_tag ajax_submit_tag input_number_tag
-  stringify_attributes restricted_html textarea_tag link_tag date_tag
+  stringify_attributes textarea_tag link_tag date_tag
   div_tag radio_button_tag img_tag);
 our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
 
@@ -46,6 +46,15 @@ sub _J {
   return $string;
 }
 
+sub join_values {
+  my ($name, $value) = @_;
+  my $spacer = $name eq 'class' ? ' ' : ''; # join classes with spaces, everything else as is
+
+  ref $value && 'ARRAY' eq ref $value
+  ? join $spacer, map { join_values($name, $_) } @$value
+  : $value
+}
+
 sub stringify_attributes {
   my (%params) = @_;
 
@@ -54,6 +63,7 @@ sub stringify_attributes {
     next unless $name;
     next if $_valueless_attributes{$name} && !$value;
     $value = '' if !defined($value);
+    $value = join_values($name, $value) if ref $value && 'ARRAY' eq ref $value;
     push @result, $_valueless_attributes{$name} ? escape($name) : escape($name) . '="' . escape($value) . '"';
   }
 
@@ -237,6 +247,9 @@ sub select_tag {
 sub checkbox_tag {
   my ($name, %attributes) = @_;
 
+  my %label_attributes = map { (substr($_, 6) => $attributes{$_}) } grep { m{^label_} } keys %attributes;
+  delete @attributes{grep { m{^label_} } keys %attributes};
+
   _set_id_attribute(\%attributes, $name);
 
   $attributes{value}   = 1 unless defined $attributes{value};
@@ -253,7 +266,7 @@ sub checkbox_tag {
   my $code  = '';
   $code    .= hidden_tag($name, 0, %attributes, id => $attributes{id} . '_hidden') if $for_submit;
   $code    .= html_tag('input', undef,  %attributes, name => $name, type => 'checkbox');
-  $code    .= html_tag('label', $label, for => $attributes{id}) if $label;
+  $code    .= html_tag('label', $label, for => $attributes{id}, %label_attributes) if $label;
   $code    .= javascript(qq|\$('#$attributes{id}').checkall('$checkall');|) if $checkall;
 
   return $code;
@@ -262,6 +275,9 @@ sub checkbox_tag {
 sub radio_button_tag {
   my ($name, %attributes) = @_;
 
+  my %label_attributes = map { (substr($_, 6) => $attributes{$_}) } grep { m{^label_} } keys %attributes;
+  delete @attributes{grep { m{^label_} } keys %attributes};
+
   $attributes{value}   = 1 unless exists $attributes{value};
 
   _set_id_attribute(\%attributes, $name, 1);
@@ -276,7 +292,7 @@ sub radio_button_tag {
   }
 
   my $code  = html_tag('input', undef,  %attributes, name => $name, type => 'radio');
-  $code    .= html_tag('label', $label, for => $attributes{id}) if $label;
+  $code    .= html_tag('label', $label, for => $attributes{id}, %label_attributes) if $label;
 
   return $code;
 }
@@ -289,7 +305,7 @@ sub button_tag {
 
   $onclick = 'if (!confirm("'. _J(delete($attributes{confirm})) .'")) return false; ' . $onclick if $attributes{confirm};
 
-  html_tag('input', undef, %attributes, value => $value, onclick => $onclick);
+  html_tag('input', undef, %attributes, value => $value, (onclick => $onclick)x!!$onclick);
 }
 
 sub submit_tag {
@@ -353,13 +369,6 @@ sub _set_id_attribute {
 
 my $html_restricter;
 
-sub restricted_html {
-  my ($value) = @_;
-
-  $html_restricter ||= SL::HTML::Restrict->create;
-  return $html_restricter->process($value);
-}
-
 sub textarea_tag {
   my ($name, $content, %attributes) = @_;
 
@@ -484,10 +493,6 @@ Creates a string from all elements in C<%items> suitable for usage as
 HTML tag attributes. Keys and values are HTML escaped even though keys
 must not contain non-ASCII characters for browsers to accept them.
 
-=item C<restricted_html $html>
-
-Returns HTML stripped of unknown tags. See L<SL::HTML::Restrict>.
-
 =back
 
 =head2 HIGH-LEVEL FUNCTIONS
@@ -564,7 +569,10 @@ C<name_to_id($name)>. The tag's C<value> defaults to C<1>.
 
 If C<%attributes> contains a key C<label> then a HTML 'label' tag is
 created with said C<label>. No attribute named C<label> is created in
-that case.
+that case. Furthermore, all attributes whose names start with
+C<label_> become attributes on the label tag without the C<label_>
+prefix. For example, C<label_style='#ff0000'> will be turned into
+C<style='#ff0000'> on the label tag, causing the text to become red.
 
 If C<%attributes> contains a key C<checkall> then the value is taken as a
 JQuery selector and clicking this checkbox will also toggle all checkboxes
@@ -578,7 +586,10 @@ C<1>. The tag's C<id> defaults to C<name_to_id($name . "_" . $value)>.
 
 If C<%attributes> contains a key C<label> then a HTML 'label' tag is
 created with said C<label>. No attribute named C<label> is created in
-that case.
+that case. Furthermore, all attributes whose names start with
+C<label_> become attributes on the label tag without the C<label_>
+prefix. For example, C<label_style='#ff0000'> will be turned into
+C<style='#ff0000'> on the label tag, causing the text to become red.
 
 =item C<select_tag $name, \@collection, %attributes>