first pass simple cases working
[catagits/HTML-Zoom.git] / t / tags_as_zoom_events.t
CommitLineData
d7f82e1b 1use strict;
2use warnings FATAL => 'all';
3use Test::More;
490df899 4use HTML::Zoom;
d7f82e1b 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) = @_;
c931318d 42 <p>, "Hi, I'm version: $data{site_version}", </p>,
d7f82e1b 43 <p>, "Here's some interesting things about me", </p>,
44 <img src="smilyface.png" alt="smiles" />,
45 <ul>,
c931318d 46 <li>, <a href="/user">, "My Users", </li>,
47 <li>, <a href="/user/$data{new_user_link}">, "Create New User", </li>,
d7f82e1b 48 </ul>;
49 }
50
51 sub process_templates {
52 my ($templates, %data) = @_;
53 for my $template(@$templates) {
c931318d 54 my @processed = $template->(%data);
55 $data{content} = \@processed;
d7f82e1b 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 {
c931318d 74 ## TODO: This seems quite fragile
75 my $raw = ref $_ ? (ref $_ eq 'SCALAR' ? $$_: "$_") : $_;
d7f82e1b 76 my @info = ($raw =~m{
77 (
78 (?:[^<]*) < (?:
79 ( / )? ( [^/!<>\s"'=]+ )
80 ( (?:"[^"]*"|'[^']*'|[^"'<>])+? )?
81 |
82 (!-- .*? -- | ![^\-] .*? )
83 ) (\s*/\s*)? >
84 )
85 ([^<]*)
86 }x);
87
88 my ($whole, $is_close, $tag_name, $attrs, $comment_or_directive,
89 $in_place_close) = @info;
90
91 if($comment_or_directive) {
92 +{ type => 'SPECIAL', raw => $raw };
93 } elsif(!scalar(@info)) {
94 +{ type => 'TEXT', raw => $raw };
95 } else {
96 if($is_close) {
97 $tag_name =~ tr/A-Z/a-z/;
98 +{ type => 'CLOSE', name => $tag_name, raw => $raw};
99 } else {
100 $attrs = '' if !defined($attrs) or $attrs =~ /^ +$/;
101 +{
102 type => 'OPEN',
103 name => $tag_name,
104 is_in_place_close => $in_place_close,
105 HTML::Zoom::Parser::BuiltIn::_hacky_attribute_parser($attrs),
106 raw_attrs => $attrs||'',
107 raw => $whole,
108 }, $in_place_close ?
109 +{
110 type => 'CLOSE',
111 name => $tag_name,
112 raw => '',
113 is_in_place_close => 1,
114 } :
115 +();
116 }
117 }
118 } @_;
119 }
120}
121
122ok my $html = BasicPage->show_landing_html;
123ok my $zoom = HTML::Zoom->from_html($html);
124ok my $events = BasicPage->show_landing_events;
125
c931318d 126is_deeply $zoom->to_events, $events,
127 'Made HZoom events from XMLTags';
d7f82e1b 128
c931318d 129#use Data::Dump 'dump';
130#warn dump $html;
131#warn dump $zoom->to_events;
132#warn dump $events;
133
134done_testing();