HTML-IDs aus Attributnamen nur mit für IDs gültigen Zeichen bilden
authorMoritz Bunkus <m.bunkus@linet-services.de>
Fri, 8 Jan 2010 13:56:36 +0000 (14:56 +0100)
committerSven Schöling <s.schoeling@linet-services.de>
Mon, 28 Jun 2010 11:07:48 +0000 (13:07 +0200)
SL/Template/Plugin/L.pm

index f044fc1..93de09b 100644 (file)
@@ -17,6 +17,16 @@ sub new {
   return bless { }, $class;
 }
 
   return bless { }, $class;
 }
 
+sub name_to_id {
+  my $self =  shift;
+  my $name =  shift;
+
+  $name    =~ s/[^\w_]/_/g;
+  $name    =~ s/_+/_/g;
+
+  return $name;
+}
+
 sub attributes {
   my $self    = shift;
   my $options = shift || {};
 sub attributes {
   my $self    = shift;
   my $options = shift || {};
@@ -48,7 +58,7 @@ sub select_tag {
   my $attributes        = shift || {};
 
   $attributes->{name}   = $name;
   my $attributes        = shift || {};
 
   $attributes->{name}   = $name;
-  $attributes->{id}   ||= $name;
+  $attributes->{id}   ||= $self->name_to_id($name);
 
   return $self->html_tag('select', $options_str, $attributes);
 }
 
   return $self->html_tag('select', $options_str, $attributes);
 }
@@ -106,8 +116,14 @@ functions that create HTML tags from various kinds of data sources.
 
 =head1 FUNCTIONS
 
 
 =head1 FUNCTIONS
 
+=head2 LOW-LEVEL FUNCTIONS
+
 =over 4
 
 =over 4
 
+=item C<name_to_id $name>
+
+Converts a name to a HTML id by replacing various characters.
+
 =item C<attributes \%items>
 
 Creates a string from all elements in C<\%items> suitable for usage as
 =item C<attributes \%items>
 
 Creates a string from all elements in C<\%items> suitable for usage as
@@ -121,6 +137,29 @@ C<$content_string> between the two. If C<$content_string> is undefined
 or empty then only a E<lt>tag/E<gt> tag will be created. Attributes
 are key/value pairs added to the opening tag.
 
 or empty then only a E<lt>tag/E<gt> tag will be created. Attributes
 are key/value pairs added to the opening tag.
 
+C<$content_string> is not HTML escaped.
+
+=back
+
+=head2 HIGH-LEVEL FUNCTIONS
+
+=over 4
+
+=item C<select_tag $name, $options_string, \%attributes>
+
+Creates a HTML 'select' tag named $name with the contents
+$options_string and with arbitrary HTML attributes from
+C<\%attributes>. The tag's C<id> defaults to C<name_to_id($name)>.
+
+The $options_string is usually created by the C<options_for_select>
+function.
+
+=back
+
+=head2 CONVERSION FUNCTIONS
+
+=over 4
+
 =item C<options_for_select \@collection, \%options>
 
 Creates a string suitable for a HTML 'select' tag consisting of one
 =item C<options_for_select \@collection, \%options>
 
 Creates a string suitable for a HTML 'select' tag consisting of one
@@ -149,15 +188,6 @@ respectively.
 For cases 3 and 4 C<$options{value}> defaults to C<id> and
 C<$options{title}> defaults to C<$options{value}>.
 
 For cases 3 and 4 C<$options{value}> defaults to C<id> and
 C<$options{title}> defaults to C<$options{value}>.
 
-=item C<select_tag $name, $options_string, \%attributes>
-
-Creates a HTML 'select' tag named $name with the contents
-$options_string and with arbitrary HTML attributes from
-C<\%attributes>. The tag's C<id> defaults to C<$name>.
-
-The $options_string is usually created by the C<options_for_select>
-function.
-
 =back
 
 =head1 MODULE AUTHORS
 =back
 
 =head1 MODULE AUTHORS