support for hashref in set_attribute + add_to_attribute
Simon Elliott [Fri, 20 May 2011 19:44:16 +0000 (20:44 +0100)]
1  2 
lib/HTML/Zoom/FilterBuilder.pm
t/actions.t
t/dwim-autoload.t

@@@ -24,47 -24,60 +24,57 @@@ sub _flatten_stream_of_streams 
    shift->_zconfig->stream_utils->flatten_stream_of_streams(@_)
  }
  
+ 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 ]) : ()
       }
     };
  }
  
  sub _parse_attribute_args {
    my $self = shift;
--  # allow ->add_to_attribute(name => 'value')
--  #    or ->add_to_attribute({ name => 'name', value => 'value' })
-   my ($name, $value) = @_ > 1 ? @_ : @{$_[0]}{qw(name value)};
-   return ($name, $self->_zconfig->parser->html_escape($value));
 -  die "WARNING: Long form arg (name => 'class', value => 'x') is deprecated"
++  warn "WARNING: Long form arg (name => 'class', value => 'x') is deprecated. This may not do what you originally intended..."
+     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 {
      die "renamed to add_to_attribute. killing this entirely for 1.0";
  }
  
+ sub add_class { shift->add_to_attribute('class',@_) }
+ sub remove_class { shift->remove_attribute('class',@_) }
+ sub set_class { shift->set_attribute('class',@_) }
+ 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 ]) : ()
      }
    };
  }
diff --combined t/actions.t
@@@ -9,7 -9,7 +9,7 @@@ use HTML::Zoom::FilterStream
  
  my $tmpl = <<END;
  <body>
--  <div class="main">
++  <div name="cow" class="main">
      <span class="hilight name">Bob</span>
      <span class="career">Builder</span>
      <hr />
@@@ -50,15 -50,15 +50,23 @@@ my ($expect, @ev)
  ($expect = $tmpl) =~ s/class="main"/class="foo"/;
  
  is(
-   run_for { $_->set_attribute({ name => 'class', value => 'foo' }) },
+   run_for { $_->set_attribute( 'class' => 'foo' ) },
    $expect,
    'set attribute on existing attribute'
  );
  
++($expect = $tmpl) =~ s/name="cow" class="main"/name="bar" class="foo"/;
++
++is(
++  run_for { $_->set_attribute({ 'class' => 'foo', 'name' => 'bar'}) },
++  $expect,
++  'set attributes using hashref form'
++);
++
  ($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/;
  
  is(
-   run_for { $_->set_attribute({ name => 'foo', value => 'bar' }) },
+   run_for { $_->set_attribute( 'foo' => 'bar' ) },
    $expect,
    'set attribute on non existing attribute'
  );
@@@ -66,7 -66,7 +74,7 @@@
  ($expect = $tmpl) =~ s/class="main"/class="main foo"/;
  
  is(
-   run_for { $_->add_to_attribute({ name => 'class', value => 'foo' }) },
+   run_for { $_->add_to_attribute( 'class' => 'foo' ) },
    $expect,
    'add attribute on existing attribute'
  );
@@@ -74,7 -74,7 +82,7 @@@
  ($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/;
  
  is(
-   run_for { $_->add_to_attribute({ name => 'foo', value => 'bar' }) },
+   run_for { $_->add_to_attribute( 'foo' => 'bar' ) },
    $expect,
    'add attribute on non existing attribute'
  );
@@@ -207,7 -207,7 +215,7 @@@ is
  
  is(
    HTML::Zoom::Producer::BuiltIn->html_from_events(\@ev),
--  '<div class="main">
++  '<div name="cow" class="main">
      <span class="hilight name">Bob</span>
      <span class="career">Builder</span>
      <hr />
  is(
    run_for { $_->collect({ into => \@ev, content => 1 }) },
    '<body>
--  <div class="main"></div>
++  <div name="cow" class="main"></div>
  </body>
  ',
    'collect w/content removes correctly'
@@@ -239,7 -239,7 +247,7 @@@ is
  is(
    run_for { $_->replace($ohai, { content => 1 }) },
    '<body>
--  <div class="main">O HAI</div>
++  <div name="cow" class="main">O HAI</div>
  </body>
  ',
    'replace w/content'
@@@ -273,11 -273,11 +281,11 @@@ is
      )
    },
    q{<body>
--  <div class="main">
++  <div name="cow" class="main">
      <span class="hilight name">mst</span>
      <span class="career">Chainsaw Wielder</span>
      <hr />
--  </div><div class="main">
++  </div><div name="cow" class="main">
      <span class="hilight name">mdk</span>
      <span class="career">Adminion</span>
      <hr />
@@@ -305,7 -305,7 +313,7 @@@ is
      )
    },
    q{<body>
--  <div class="main">
++  <div name="cow" class="main">
      <span class="hilight name">mst</span>
      <span class="career">Chainsaw Wielder</span>
      <hr />
@@@ -345,7 -345,7 +353,7 @@@ is
      )
    },
    q{<body>
--  <div class="main">
++  <div name="cow" class="main">
      <span class="hilight name">mst</span>
      <span class="career">Chainsaw Wielder</span>
      <hr />
@@@ -378,7 -378,7 +386,7 @@@ is
      )
    },
    q{<body>
--  <div class="main">
++  <div name="cow" class="main">
      <span class="hilight name">mst</span>
      <span class="career">Chainsaw Wielder</span>
      <hr />
diff --combined t/dwim-autoload.t
@@@ -100,15 -100,6 +100,6 @@@ like
  like(
    HTML::Zoom
      ->from_html(q[<p class="first">Hi!</p>])
-     ->add_to_attribute('p', {name => 'class', value => 'para'})
-     ->to_html,
-   qr/first para/,
-   'Got correct from add_to_attribute'
- );
- like(
-   HTML::Zoom
-     ->from_html(q[<p class="first">Hi!</p>])
      ->add_to_attribute('p', class => 'para')
      ->to_html,
    qr/first para/,
@@@ -124,15 -115,6 +115,6 @@@ like
    'Got correct from set_attribute'
  );
  
- like(
-   HTML::Zoom
-     ->from_html(q[<p class="first">Hi!</p>])
-     ->set_attribute('p', {name => 'class', value => 'para'})
-     ->to_html,
-   qr/class="para"/,
-   'Got correct from set_attribute'
- );
  is(
    HTML::Zoom
      ->from_html(q[<p class="first">Hi!</p>])
@@@ -322,42 -304,4 +304,42 @@@ sub code_stream (&) 
      'Got correct from repeat_content';
  }
  
 +{
 +  ok my $dwim = HTML::Zoom
 +    ->from_html(q[<ul><li class="foo"></li><li class="bar"></li></ul>])
 +    ->replace_content({
 +      'li.foo' => ['foo'],
 +      'li.bar' => ['bar'],
 +    })->to_html;
 +  is $dwim, '<ul><li class="foo">foo</li><li class="bar">bar</li></ul>',
 +    'Hashref selectors (via replace_content)';
 +}
 +
 +{
 +  ok my $dwim = HTML::Zoom
 +  ->from_html(q[<ul><li class="foo"></li><li class="bar"></li></ul>])
 +    ->set_attribute({
 +      'li.foo' => [ class => 'baz' ],
 +      'li.bar' => [ class => 'qux' ],
 +    })->to_html;
 +  is $dwim, '<ul><li class="baz"></li><li class="qux"></li></ul>',
 +    'Hashref selectors (via set_attribute)';
 +}
 +
 +{
 +  ok my $dwim = HTML::Zoom
 +  ->from_html(q[<ul><li class="foo"></li><li class="bar"></li></ul>])
 +  ->select('ul')->collect({ 
 +  passthrough => 1,
 +  filter => sub {
 +      $_->set_attribute({
 +        'li.foo' => [ class => 'baz' ],
 +        'li.bar' => [ class => 'qux' ],
 +      });
 +    }
 +  })->to_html;
 +  is $dwim, '<ul><li class="baz"></li><li class="qux"></li></ul>',
 +    'Hashref selectors on codestream';
 +}
 +
  done_testing;