fixed test to work after migrating XML::Tags from Web::Simple
[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 $_ ? $$_ : $_;
75       my @info = ($raw =~m{
76       (
77         (?:[^<]*) < (?:
78             ( / )? ( [^/!<>\s"'=]+ )
79             ( (?:"[^"]*"|'[^']*'|[^"'<>])+? )?
80         |   
81             (!-- .*? -- | ![^\-] .*? )
82         ) (\s*/\s*)? >
83       )
84       ([^<]*)
85       }x);
86
87       my ($whole, $is_close, $tag_name, $attrs, $comment_or_directive, 
88       $in_place_close)  = @info;
89
90       if($comment_or_directive) {
91         +{ type => 'SPECIAL', raw => $raw };
92       } elsif(!scalar(@info)) {
93         +{ type => 'TEXT', raw => $raw };
94       } else {
95         if($is_close) {
96           $tag_name =~ tr/A-Z/a-z/;
97           +{ type => 'CLOSE', name => $tag_name, raw => $raw};
98         } else {
99           $attrs = '' if !defined($attrs) or $attrs =~ /^ +$/;
100           +{
101             type => 'OPEN',
102             name => $tag_name,
103             is_in_place_close => $in_place_close,
104             HTML::Zoom::Parser::BuiltIn::_hacky_attribute_parser($attrs),
105             raw_attrs => $attrs||'',
106             raw => $whole,
107           }, $in_place_close ? 
108             +{
109               type => 'CLOSE',
110               name => $tag_name, 
111               raw => '', 
112               is_in_place_close => 1,
113             } :
114             +();
115         }
116       }
117     } @_;
118   }
119 }
120
121 ok my $html = BasicPage->show_landing_html;
122 ok my $zoom = HTML::Zoom->from_html($html);
123 ok my $events = BasicPage->show_landing_events;
124
125 use Data::Dump 'dump';
126 warn dump $html;
127 warn dump $zoom->to_events;
128 warn dump $events;
129