From: John Napiorkowski Date: Mon, 28 Feb 2011 00:21:33 +0000 (-0500) Subject: moved test method to XML::Tags and modified test X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c7611f51fbc0c3d499bc4eef18093726123fe29e;p=catagits%2FHTML-Zoom.git moved test method to XML::Tags and modified test --- diff --git a/lib/HTML/Tags.pm b/lib/HTML/Tags.pm index fabaab5..e57c5e9 100644 --- a/lib/HTML/Tags.pm +++ b/lib/HTML/Tags.pm @@ -118,5 +118,6 @@ sub import { } sub to_html_string { XML::Tags::to_xml_string(@_) } +sub to_zoom_events { XML::Tags::to_zoom_events(@_) } 1; diff --git a/lib/XML/Tags.pm b/lib/XML/Tags.pm index 8b30b60..99d10c4 100644 --- a/lib/XML/Tags.pm +++ b/lib/XML/Tags.pm @@ -35,6 +35,78 @@ sub to_xml_string { } @_ } +sub to_zoom_events { + my @events; + while(my $raw = shift) { + my $normalized_raw = ref $raw ? (ref $raw eq 'SCALAR' ? $$raw: "$raw") : $raw; + my @info = ( + $normalized_raw =~m{ + ( + (?:[^<]*) < (?: + ( / )? ( [^/!<>\s"'=]+ ) + ( (?:"[^"]*"|'[^']*'|[^"'<>])+? )? + | + (!-- .*? -- | ![^\-] .*? ) + ) (\s*/\s*)? > + ) + ([^<]*) + }x + ); + + my ( + $whole, + $is_close, + $tag_name, + $attrs, + $comment_or_directive, + $in_place_close + ) = @info; + + if($comment_or_directive) { + if($events[-1] && $events[-1]->{type} eq 'SPECIAL') { + $events[-1]->{raw} .= $normalized_raw; + } else { + push @events, { type => 'SPECIAL', raw => $normalized_raw }; + } + } elsif(!scalar(@info)) { + if($events[-1] && $events[-1]->{type} eq 'TEXT') { + $events[-1]->{raw} .= $normalized_raw; + } elsif(ref $raw && ref $raw eq 'SCALAR' && $events[-1] && $events[-1]->{type} eq 'SPECIAL') { + $events[-1]->{raw} .= $normalized_raw; + } elsif(ref $raw && ref $raw eq 'SCALAR') { + push @events, { type => 'SPECIAL', raw => $normalized_raw }; + } + else { + push @events, { type => 'TEXT', raw => $normalized_raw }; + } + } else { + if($is_close) { + $tag_name =~ tr/A-Z/a-z/; + push @events, { type => 'CLOSE', name => $tag_name, raw => $normalized_raw}; + } else { + $attrs = '' if !defined($attrs) or $attrs =~ /^ +$/; + push @events, { + type => 'OPEN', + name => $tag_name, + is_in_place_close => $in_place_close, + HTML::Zoom::Parser::BuiltIn::_hacky_attribute_parser($attrs), + raw_attrs => $attrs||'', + raw => $whole, + }; + if($in_place_close) { + push @events, { + type => 'CLOSE', + name => $tag_name, + raw => '', + is_in_place_close => 1, + } + } + } + } + } + return @events; +} + sub _find_tags { shift; @_ } sub _find_target { diff --git a/t/tags_as_zoom_events.t b/t/tags_as_zoom_events.t index f70cb2f..7bbb1ca 100644 --- a/t/tags_as_zoom_events.t +++ b/t/tags_as_zoom_events.t @@ -25,7 +25,8 @@ use HTML::Zoom; sub layout { my (%data) = @_; - \'', + \'', , , , @@ -44,7 +45,7 @@ use HTML::Zoom; smiles, ; } @@ -66,63 +67,7 @@ use HTML::Zoom; sub as_events { my ($template, %data) = @_; my @content = process_templates([$template, \&layout], %data); - return [_convert_to_events(@content)]; - } - - sub _convert_to_events { - map { - my $raw = ref $_ ? (ref $_ eq 'SCALAR' ? $$_: "$_") : $_; - my @info = ( - $raw =~m{ - ( - (?:[^<]*) < (?: - ( / )? ( [^/!<>\s"'=]+ ) - ( (?:"[^"]*"|'[^']*'|[^"'<>])+? )? - | - (!-- .*? -- | ![^\-] .*? ) - ) (\s*/\s*)? > - ) - ([^<]*) - }x - ); - - my ( - $whole, - $is_close, - $tag_name, - $attrs, - $comment_or_directive, - $in_place_close - ) = @info; - - if($comment_or_directive) { - +{ type => 'SPECIAL', raw => $raw }; - } elsif(!scalar(@info)) { - +{ type => 'TEXT', raw => $raw }; - } else { - if($is_close) { - $tag_name =~ tr/A-Z/a-z/; - +{ type => 'CLOSE', name => $tag_name, raw => $raw}; - } else { - $attrs = '' if !defined($attrs) or $attrs =~ /^ +$/; - +{ - type => 'OPEN', - name => $tag_name, - is_in_place_close => $in_place_close, - HTML::Zoom::Parser::BuiltIn::_hacky_attribute_parser($attrs), - raw_attrs => $attrs||'', - raw => $whole, - }, $in_place_close ? - +{ - type => 'CLOSE', - name => $tag_name, - raw => '', - is_in_place_close => 1, - } : - +(); - } - } - } @_; + return [HTML::Tags::to_zoom_events(@content)]; } } @@ -136,6 +81,7 @@ is_deeply $zoom->to_events, $events, #use Data::Dump 'dump'; #warn dump $html; #warn dump $zoom->to_events; +#warn "======================"; #warn dump $events; done_testing();