From: Simon Elliott Date: Fri, 20 May 2011 19:44:16 +0000 (+0100) Subject: support for hashref in set_attribute + add_to_attribute X-Git-Tag: allocate~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FHTML-Zoom.git;a=commitdiff_plain;h=3c53c43950487cb2a92d0f49a8932c37867a6ba0 support for hashref in set_attribute + add_to_attribute --- 3c53c43950487cb2a92d0f49a8932c37867a6ba0 diff --cc lib/HTML/Zoom/FilterBuilder.pm index 6577fd9,6040288..74b7c14 --- a/lib/HTML/Zoom/FilterBuilder.pm +++ b/lib/HTML/Zoom/FilterBuilder.pm @@@ -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 --cc t/actions.t index 6e1541b,4e3315b..dbfda0b --- a/t/actions.t +++ b/t/actions.t @@@ -9,7 -9,7 +9,7 @@@ use HTML::Zoom::FilterStream my $tmpl = < --
++
Bob Builder
@@@ -55,6 -55,6 +55,14 @@@ is '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( @@@ -207,7 -207,7 +215,7 @@@ is is( HTML::Zoom::Producer::BuiltIn->html_from_events(\@ev), -- '
++ '
Bob Builder
@@@ -220,7 -220,7 +228,7 @@@ is( run_for { $_->collect({ into => \@ev, content => 1 }) }, ' --
++
', 'collect w/content removes correctly' @@@ -239,7 -239,7 +247,7 @@@ is is( run_for { $_->replace($ohai, { content => 1 }) }, ' --
O HAI
++
O HAI
', 'replace w/content' @@@ -273,11 -273,11 +281,11 @@@ is ) }, q{ --
++
mst Chainsaw Wielder
--
++
mdk Adminion
@@@ -305,7 -305,7 +313,7 @@@ is ) }, q{ --
++
mst Chainsaw Wielder
@@@ -345,7 -345,7 +353,7 @@@ is ) }, q{ --
++
mst Chainsaw Wielder
@@@ -378,7 -378,7 +386,7 @@@ is ) }, q{ --
++
mst Chainsaw Wielder