moved test method to XML::Tags and modified test
John Napiorkowski [Mon, 28 Feb 2011 00:21:33 +0000 (19:21 -0500)]
lib/HTML/Tags.pm
lib/XML/Tags.pm
t/tags_as_zoom_events.t

index fabaab5..e57c5e9 100644 (file)
@@ -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;
index 8b30b60..99d10c4 100644 (file)
@@ -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 {
index f70cb2f..7bbb1ca 100644 (file)
@@ -25,7 +25,8 @@ use HTML::Zoom;
 
   sub layout {
     my (%data) = @_;
-    \'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
+    \'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ',
+    \'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
     <html>,
       <head>,
         <!-- here is a comment -->,
@@ -44,7 +45,7 @@ use HTML::Zoom;
     <img src="smilyface.png" alt="smiles" />,
     <ul>,
       <li>, <a href="/user">, "My Users", </li>,
-      <li>, <a href="/user/$data{new_user_link}">, "Create New User", </li>,
+      <li>, <a href="/user/$data{new_user_link}">, "Create", "New User", </li>,
     </ul>;
   }
 
@@ -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();