From: Simon Elliott Date: Fri, 20 May 2011 17:48:12 +0000 (+0100) Subject: some simple shorthand helpers. X-Git-Tag: allocate~4^2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FHTML-Zoom.git;a=commitdiff_plain;h=bf07288fe4550dffb8aa7f9a887fa7b00701e104 some simple shorthand helpers. add_class remove_class set_class set_id --- diff --git a/lib/HTML/Zoom/FilterBuilder.pm b/lib/HTML/Zoom/FilterBuilder.pm index 6577fd9..5c8943a 100644 --- a/lib/HTML/Zoom/FilterBuilder.pm +++ b/lib/HTML/Zoom/FilterBuilder.pm @@ -24,6 +24,8 @@ 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(@_); @@ -51,6 +53,14 @@ 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(@_); diff --git a/t/actions.t b/t/actions.t index 6e1541b..c2df1df 100644 --- a/t/actions.t +++ b/t/actions.t @@ -55,6 +55,14 @@ is( 'set attribute on existing attribute' ); +($expect = $tmpl) =~ s/class="main"/class="foo"/; + +is( + run_for { $_->set_attr({ name => 'class', value => 'foo' }) }, + $expect, + 'set attribute on existing attribute (shorthand)' +); + ($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/; is( @@ -71,6 +79,22 @@ is( 'add attribute on existing attribute' ); +($expect = $tmpl) =~ s/class="main"/class="main foo"/; + +is( + run_for { $_->add_class('foo') }, + $expect, + 'add attribute on existing attribute (shorthand)' +); + +($expect = $tmpl) =~ s/class="main"/class="main" id="foo"/; + +is( + run_for { $_->set_id('foo') }, + $expect, + 'set_id (shorthand)' +); + ($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/; is(