From: Simon Elliott Date: Sat, 4 Jun 2011 12:24:43 +0000 (+0100) Subject: merge helpers X-Git-Tag: allocate~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FHTML-Zoom.git;a=commitdiff_plain;h=c84b68f396fb5b0e81dbae8927519b2e06138734 merge helpers add remove_from_attribute to make remove_class actually do what it's supposed to. --- diff --git a/lib/HTML/Zoom/FilterBuilder.pm b/lib/HTML/Zoom/FilterBuilder.pm index fdfcb28..3c44a7c 100644 --- a/lib/HTML/Zoom/FilterBuilder.pm +++ b/lib/HTML/Zoom/FilterBuilder.pm @@ -56,7 +56,7 @@ sub add_attribute { sub add_class { shift->add_to_attribute('class',@_) } -sub remove_class { shift->remove_attribute('class',@_) } +sub remove_class { shift->remove_from_attribute('class',@_) } sub set_class { shift->set_attribute('class',@_) } @@ -79,6 +79,22 @@ sub add_to_attribute { }; } +sub remove_from_attribute { + my $self = shift; + my $attr = $self->_parse_attribute_args(@_); + sub { + my $a = (my $evt = $_[0])->{attrs}; + +{ %$evt, raw => undef, raw_attrs => undef, + attrs => { + %$a, + #TODO needs to support multiple removes + map { my $tar = $_; $_ => join ' ', + map {$attr->{$tar} ne $_} split ' ', $a->{$_} } keys %$attr + }, + } + }; +} + sub remove_attribute { my ($self, $args) = @_; my $name = (ref($args) eq 'HASH') ? $args->{name} : $args; @@ -489,8 +505,26 @@ Removes an attribute and all its values. ->then ->remove_attribute('class'); +=head2 remove_from_attribute + +Removes a value from existing attribute + + $html_zoom + ->select('p') + ->set_attribute(class=>'paragraph lead') + ->then + ->remove_from_attribute('class' => 'lead'); + Removes attributes from the original stream or events already added. +=head2 add_class + +Add to a class attribute + +=head2 remove_class + +Remove from a class attribute + =head2 transform_attribute Transforms (or creates or deletes) an attribute by running the passed diff --git a/t/actions.t b/t/actions.t index 7647fd7..1b8b1fb 100644 --- a/t/actions.t +++ b/t/actions.t @@ -58,9 +58,9 @@ is( ($expect = $tmpl) =~ s/name="cow" class="main"/name="bar" class="foo"/; is( - run_for { $_->set_attribute({ 'class' => 'foo', 'name' => 'bar'}) }, + run_for { $_->set_attr({ 'class' => 'foo', 'name' => 'bar'}) }, $expect, - 'set attributes using hashref form' + 'set attributes using hashref form (shorthand)' ); ($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/; @@ -117,6 +117,14 @@ is( 'remove attribute on non existing attribute' ); +($expect = $tmpl) =~ s/class="main"/class=""/; + +is( + run_for { $_->remove_from_attribute({ class => 'main' }) }, + $expect, + 'remove name from attribute' +); + ($expect = $tmpl) =~ s/ class="main"//; is(