merge
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / FilterBuilder.pm
index 6713841..ed7fef1 100644 (file)
@@ -28,15 +28,13 @@ sub set_attr { shift->set_attribute(@_); }
 
 sub set_attribute {
   my $self = shift;
-  my ($name, $value) = $self->_parse_attribute_args(@_);
+  my $attr = $self->_parse_attribute_args(@_);
   sub {
     my $a = (my $evt = $_[0])->{attrs};
-    my $e = exists $a->{$name};
+    my @kadd = grep {!exists $a->{$_}} keys %$attr;
     +{ %$evt, raw => undef, raw_attrs => undef,
-       attrs => { %$a, $name => $value },
-      ($e # add to name list if not present
-        ? ()
-        : (attr_names => [ @{$evt->{attr_names}}, $name ]))
+       attrs => { %$a, %$attr },
+       @kadd ? (attr_names => [ @{$evt->{attr_names}}, @kadd ]) : ()
      }
    };
 }
@@ -46,8 +44,10 @@ sub _parse_attribute_args {
 
   die "Long form arg (name => 'class', value => 'x') is no longer supported"
     if(@_ == 1 && $_[0]->{'name'} && $_[0]->{'value'});
-  my ($name, $value) = @_ > 1 ? @_ : @{$_[0]}{qw(name value)};
-  return ($name, $self->_zconfig->parser->html_escape($value));
+
+  my $opts = ref($_[0]) eq 'HASH' ? $_[0] : {$_[0] => $_[1]};
+  for (values %{$opts}) { $self->_zconfig->parser->html_escape($_); }
+  return $opts;
 }
 
 sub add_attribute {
@@ -64,18 +64,17 @@ sub set_id { shift->set_attribute('id',@_) }
 
 sub add_to_attribute {
   my $self = shift;
-  my ($name, $value) = $self->_parse_attribute_args(@_);
+  my $attr = $self->_parse_attribute_args(@_);
   sub {
     my $a = (my $evt = $_[0])->{attrs};
-    my $e = exists $a->{$name};
+    my @kadd = grep {!exists $a->{$_}} keys %$attr;
     +{ %$evt, raw => undef, raw_attrs => undef,
        attrs => {
          %$a,
-         $name => join(' ', ($e ? $a->{$name} : ()), $value)
+         map {$_ => join(' ', (exists $a->{$_} ? $a->{$_} : ()), $attr->{$_}) }
+          keys %$attr
       },
-      ($e # add to name list if not present
-        ? ()
-        : (attr_names => [ @{$evt->{attr_names}}, $name ]))
+      @kadd ? (attr_names => [ @{$evt->{attr_names}}, @kadd ]) : ()
     }
   };
 }
@@ -89,7 +88,7 @@ sub remove_from_attribute {
        attrs => {
          %$a,
          #TODO needs to support multiple removes
-         map { my $tar = $_; $_ => join ' ', 
+         map { my $tar = $_; $_ => join ' ',
           map {$attr->{$tar} ne $_} split ' ', $a->{$_} }
             grep {exists $a->{$_}} keys %$attr
       },
@@ -678,9 +677,9 @@ Similar to add_before, but adds the content to the match.
     ->select('p')
     ->prepend_content("Hello ")
     ->to_html
-    
+
   ## <p>Hello World</p>
-  
+
 Acceptable values are strings, scalar refs and L<HTML::Zoom> objects
 
 =head2 append_content
@@ -692,7 +691,7 @@ Similar to add_after, but adds the content to the match.
     ->select('p')
     ->prepend_content("World")
     ->to_html
-    
+
   ## <p>Hello World</p>
 
 Acceptable values are strings, scalar refs and L<HTML::Zoom> objects
@@ -729,7 +728,7 @@ pre-generated)
         }
       } @list
     ]);
-    
+
 Subroutines would be run with $_ localized to result of L<HTML::Zoom/select> (of
 collected elements), and with said result passed as parameter to subroutine.