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;hp=701459a45dfa55feb465de0201bc93778439c804 support for hashref in set_attribute + add_to_attribute --- diff --git a/lib/HTML/Zoom/FilterBuilder.pm b/lib/HTML/Zoom/FilterBuilder.pm index 6577fd9..74b7c14 100644 --- a/lib/HTML/Zoom/FilterBuilder.pm +++ b/lib/HTML/Zoom/FilterBuilder.pm @@ -24,47 +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)); + + 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 $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 --git a/t/actions.t b/t/actions.t index 6e1541b..dbfda0b 100644 --- a/t/actions.t +++ b/t/actions.t @@ -9,7 +9,7 @@ use HTML::Zoom::FilterStream; my $tmpl = < -
+
Bob Builder
@@ -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 +74,7 @@ is( ($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 +82,7 @@ is( ($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 +215,7 @@ is( is( HTML::Zoom::Producer::BuiltIn->html_from_events(\@ev), - '
+ '
Bob Builder
@@ -220,7 +228,7 @@ is( is( run_for { $_->collect({ into => \@ev, content => 1 }) }, ' -
+
', 'collect w/content removes correctly' @@ -239,7 +247,7 @@ is( is( run_for { $_->replace($ohai, { content => 1 }) }, ' -
O HAI
+
O HAI
', 'replace w/content' @@ -273,11 +281,11 @@ is( ) }, q{ -
+
mst Chainsaw Wielder
-
+
mdk Adminion
@@ -305,7 +313,7 @@ is( ) }, q{ -
+
mst Chainsaw Wielder
@@ -345,7 +353,7 @@ is( ) }, q{ -
+
mst Chainsaw Wielder
@@ -378,7 +386,7 @@ is( ) }, q{ -
+
mst Chainsaw Wielder
diff --git a/t/dwim-autoload.t b/t/dwim-autoload.t index edf7f38..2fc73de 100644 --- a/t/dwim-autoload.t +++ b/t/dwim-autoload.t @@ -100,15 +100,6 @@ like( like( HTML::Zoom ->from_html(q[

Hi!

]) - ->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[

Hi!

]) ->add_to_attribute('p', class => 'para') ->to_html, qr/first para/, @@ -124,15 +115,6 @@ like( 'Got correct from set_attribute' ); -like( - HTML::Zoom - ->from_html(q[

Hi!

]) - ->set_attribute('p', {name => 'class', value => 'para'}) - ->to_html, - qr/class="para"/, - 'Got correct from set_attribute' -); - is( HTML::Zoom ->from_html(q[

Hi!

])