From: Matt S Trout Date: Thu, 1 Jul 2010 06:12:28 +0000 (+0100) Subject: make then() work on streams and fix up replace_content on in_place_close elements X-Git-Tag: release_0.009004~44 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FHTML-Zoom.git;a=commitdiff_plain;h=a88c1c57d4d91cc32c4cdf779a1b737482244322 make then() work on streams and fix up replace_content on in_place_close elements --- diff --git a/lib/HTML/Zoom/FilterBuilder.pm b/lib/HTML/Zoom/FilterBuilder.pm index 6b50bbb..32ffe11 100644 --- a/lib/HTML/Zoom/FilterBuilder.pm +++ b/lib/HTML/Zoom/FilterBuilder.pm @@ -193,6 +193,20 @@ sub replace { my ($evt, $stream) = @_; my $emit = $self->_stream_from_proto($replace_with); my $coll = &$coll_proto; + # if we're replacing the contents of an in place close + # then we need to handle that here + if ($options->{content} + && ref($coll) eq 'HASH' + && delete $coll->{is_in_place_close} + ) { + delete $coll->{raw}; + my $close = $stream->next; + delete @{$close}{qw(is_in_place_close raw)}; + $emit = $self->_stream_concat( + $emit, + $self->_stream_from_array($close), + ); + } # For a straightforward replace operation we can, in fact, do the emit # -before- the collect, and my first cut did so. However in order to # use the captured content in generating the new content, we need @@ -201,9 +215,11 @@ sub replace { # for the difference to be noticeable return ($coll - ? (ref $coll eq 'ARRAY' + ? (ref $coll eq 'ARRAY' # [ event, stream ] ? [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ] - : $self->_stream_concat($coll, $emit) + : (ref $coll eq 'HASH' # event or stream? + ? [ $coll, $emit ] + : $self->_stream_concat($coll, $emit)) ) : $emit ); diff --git a/lib/HTML/Zoom/StreamBase.pm b/lib/HTML/Zoom/StreamBase.pm index f81089f..e9bee1c 100644 --- a/lib/HTML/Zoom/StreamBase.pm +++ b/lib/HTML/Zoom/StreamBase.pm @@ -68,6 +68,12 @@ sub select { }); } +sub then { + my ($self) = @_; + # see notes in HTML/Zoom.pm for why this needs to be fixed + $self->select($self->transform->selector); +} + sub apply { my ($self, $code) = @_; local $_ = $self; diff --git a/lib/HTML/Zoom/Transform.pm b/lib/HTML/Zoom/Transform.pm index 627e52a..e43474c 100644 --- a/lib/HTML/Zoom/Transform.pm +++ b/lib/HTML/Zoom/Transform.pm @@ -3,6 +3,7 @@ package HTML::Zoom::Transform; use strict; use warnings FATAL => 'all'; use base qw(HTML::Zoom::SubObject); +use HTML::Zoom::TransformedStream; sub new { my ($class, $args) = @_; @@ -36,10 +37,9 @@ sub match { sub apply_to_stream { my ($self, $stream) = @_; - HTML::Zoom::FilterStream->new({ + HTML::Zoom::TransformedStream->new({ stream => $stream, - match => $self->match, - filters => $self->filters, + transform => $self, zconfig => $self->_zconfig, }); } diff --git a/lib/HTML/Zoom/TransformBuilder.pm b/lib/HTML/Zoom/TransformBuilder.pm index 122c66c..8381021 100644 --- a/lib/HTML/Zoom/TransformBuilder.pm +++ b/lib/HTML/Zoom/TransformBuilder.pm @@ -3,6 +3,7 @@ package HTML::Zoom::TransformBuilder; use strict; use warnings FATAL => 'all'; use base qw(HTML::Zoom::SubObject); +use HTML::Zoom::Transform; sub new { my ($class, $args) = @_; diff --git a/lib/HTML/Zoom/TransformedStream.pm b/lib/HTML/Zoom/TransformedStream.pm new file mode 100644 index 0000000..5abd021 --- /dev/null +++ b/lib/HTML/Zoom/TransformedStream.pm @@ -0,0 +1,19 @@ +package HTML::Zoom::TransformedStream; + +use strict; +use warnings FATAL => 'all'; +use base qw(HTML::Zoom::FilterStream); + +sub new { + my ($class, $args) = @_; + $args->{selector} = $args->{transform}->selector; + $args->{match} = $args->{transform}->match; + $args->{filters} = $args->{transform}->filters; + my $new = $class->SUPER::new($args); + $new->{transform} = $args->{transform}; + $new +} + +sub transform { shift->{transform} } + +1; diff --git a/t/todo-forms.t b/t/todo-forms.t index 29cb575..ab8dcd7 100644 --- a/t/todo-forms.t +++ b/t/todo-forms.t @@ -28,5 +28,7 @@ my $h = $zoom->select('.myform')->repeat_content([ } } @fields ])->to_html; +warn $h; + ok 1; done_testing; diff --git a/t/todo-span.t b/t/todo-span.t index b8aaafc..887ba40 100644 --- a/t/todo-span.t +++ b/t/todo-span.t @@ -3,4 +3,4 @@ use Test::More tests => 1; my $zoom = HTML::Zoom->from_html('

Hello my name is

'); my $html = $zoom->select('#name')->replace_content('Foo foo')->to_html; -is($html, '

Hello my name is Foo foo

'); +is($html, '

Hello my name is Foo foo

');