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