a bit of code reformatting
[catagits/HTML-Zoom.git] / t / tags_as_zoom_events.t
1 use strict;
2 use warnings FATAL => 'all';
3 use Test::More;
4 use HTML::Zoom;
5
6 {
7   package BasicPage;
8   use HTML::Tags;
9
10   sub show_landing_html {
11     as_html(\&landing, (
12       title => "Welcome to the Demo Home",
13       site_version => 10,
14       new_user_link => 'create_user.html',
15     ));
16   }
17
18   sub show_landing_events {
19     as_events(\&landing, (
20       title => "Welcome to the Demo Home",
21       site_version => 10,
22       new_user_link => 'create_user.html',
23     ));
24   }
25
26   sub layout {
27     my (%data) = @_;
28     \'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
29     <html>,
30       <head>,
31         <!-- here is a comment -->,
32         <title>, ($data{title} || 'Hello World'), </title>,
33       </head>,
34       <body>,
35         @{$data{content}},
36       </body>,
37     </html>;
38   }
39
40   sub landing {
41     my (%data) = @_;
42     <p>, "Hi, I'm version: $data{site_version}", </p>,
43     <p>, "Here's some interesting things about me", </p>,
44     <img src="smilyface.png" alt="smiles" />,
45     <ul>,
46       <li>, <a href="/user">, "My Users", </li>,
47       <li>, <a href="/user/$data{new_user_link}">, "Create New User", </li>,
48     </ul>;
49   }
50
51   sub process_templates {
52     my ($templates, %data) = @_;
53     for my $template(@$templates) {
54       my @processed = $template->(%data);
55       $data{content} = \@processed;
56     }
57     return @{$data{content}};
58   }
59
60   sub as_html {
61     my ($template, %data) = @_;
62     my @content = process_templates([$template, \&layout], %data);
63     return join '', HTML::Tags::to_html_string(@content);
64   }
65
66   sub as_events {
67     my ($template, %data) = @_;
68     my @content = process_templates([$template, \&layout], %data);
69     return [_convert_to_events(@content)];
70   }
71
72   sub _convert_to_events {
73     map {
74       my $raw = ref $_ ? (ref $_ eq 'SCALAR' ? $$_: "$_") : $_;
75       my @info = (
76         $raw =~m{
77           (
78             (?:[^<]*) < (?:
79                 ( / )? ( [^/!<>\s"'=]+ )
80                 ( (?:"[^"]*"|'[^']*'|[^"'<>])+? )?
81             |   
82                 (!-- .*? -- | ![^\-] .*? )
83             ) (\s*/\s*)? >
84           )
85           ([^<]*)
86         }x
87       );
88
89       my (
90         $whole, 
91         $is_close,
92         $tag_name,
93         $attrs, 
94         $comment_or_directive, 
95         $in_place_close
96       ) = @info;
97
98       if($comment_or_directive) {
99         +{ type => 'SPECIAL', raw => $raw };
100       } elsif(!scalar(@info)) {
101         +{ type => 'TEXT', raw => $raw };
102       } else {
103         if($is_close) {
104           $tag_name =~ tr/A-Z/a-z/;
105           +{ type => 'CLOSE', name => $tag_name, raw => $raw};
106         } else {
107           $attrs = '' if !defined($attrs) or $attrs =~ /^ +$/;
108           +{
109             type => 'OPEN',
110             name => $tag_name,
111             is_in_place_close => $in_place_close,
112             HTML::Zoom::Parser::BuiltIn::_hacky_attribute_parser($attrs),
113             raw_attrs => $attrs||'',
114             raw => $whole,
115           }, $in_place_close ? 
116             +{
117               type => 'CLOSE',
118               name => $tag_name, 
119               raw => '', 
120               is_in_place_close => 1,
121             } :
122             +();
123         }
124       }
125     } @_;
126   }
127 }
128
129 ok my $html = BasicPage->show_landing_html;
130 ok my $zoom = HTML::Zoom->from_html($html);
131 ok my $events = BasicPage->show_landing_events;
132
133 is_deeply $zoom->to_events, $events,
134   'Made HZoom events from XMLTags';
135
136 #use Data::Dump 'dump';
137 #warn dump $html;
138 #warn dump $zoom->to_events;
139 #warn dump $events;
140
141 done_testing();