cdfc5b0ca0da70b69d5c9629beca564de2f3d407
[dh-make-perl] / dev / arm / libhtml-parser-perl / libhtml-parser-perl-3.56 / t / xml-mode.t
1 use strict;
2 use Test::More tests => 8;
3
4 use HTML::Parser ();
5 my $p = HTML::Parser->new(xml_mode => 1,
6                          );
7
8 my $text = "";
9 $p->handler(start =>
10             sub {
11                  my($tag, $attr) = @_;
12                  $text .= "S[$tag";
13                  for my $k (sort keys %$attr) {
14                      my $v =  $attr->{$k};
15                      $text .= " $k=$v";
16                  }
17                  $text .= "]";
18              }, "tagname,attr");
19 $p->handler(end =>
20              sub {
21                  $text .= "E[" . shift() . "]";
22              }, "tagname");
23 $p->handler(process => 
24              sub {
25                  $text .= "PI[" . shift() . "]";
26              }, "token0");
27 $p->handler(text =>
28              sub {
29                  $text .= shift;
30              }, "text");
31
32 my $xml = <<'EOT';
33 <?xml version="1.0"?>
34 <?IS10744:arch name="html"?><!-- comment -->
35 <DOC>
36 <title html="h1">My first architectual document</title>
37 <author html="address">Geir Ove Gronmo, grove@infotek.no</author>
38 <para>This is the first paragraph in this document</para>
39 <para html="p">This is the second paragraph</para>
40 <para/>
41 <xmp><foo></foo></xmp>
42 </DOC>
43 EOT
44
45 $p->parse($xml)->eof;
46
47 is($text, <<'EOT');
48 PI[xml version="1.0"]
49 PI[IS10744:arch name="html"]
50 S[DOC]
51 S[title html=h1]My first architectual documentE[title]
52 S[author html=address]Geir Ove Gronmo, grove@infotek.noE[author]
53 S[para]This is the first paragraph in this documentE[para]
54 S[para html=p]This is the second paragraphE[para]
55 S[para]E[para]
56 S[xmp]S[foo]E[foo]E[xmp]
57 E[DOC]
58 EOT
59
60 $text = "";
61 $p->xml_mode(0);
62 $p->parse($xml)->eof;
63
64 is($text, <<'EOT');
65 PI[xml version="1.0"?]
66 PI[IS10744:arch name="html"?]
67 S[doc]
68 S[title html=h1]My first architectual documentE[title]
69 S[author html=address]Geir Ove Gronmo, grove@infotek.noE[author]
70 S[para]This is the first paragraph in this documentE[para]
71 S[para html=p]This is the second paragraphE[para]
72 S[para/]
73 S[xmp]<foo></foo>E[xmp]
74 E[doc]
75 EOT
76
77 # Test that we get an empty tag back
78 $p = HTML::Parser->new(api_version => 3,
79                        xml_mode => 1);
80
81 $p->handler("end" =>
82             sub {
83                 my($tagname, $text) = @_;
84                 is($tagname, "Xyzzy");
85                 ok(!length($text));
86             }, "tagname,text");
87 $p->parse("<Xyzzy foo=bar/>and some more")->eof;
88
89 # Test that we get an empty tag back
90 $p = HTML::Parser->new(api_version => 3,
91                        empty_element_tags => 1);
92
93 $p->handler("end" =>
94             sub {
95                 my($tagname, $text) = @_;
96                 is($tagname, "xyzzy");
97                 ok(!length($text));
98             }, "tagname,text");
99 $p->parse("<Xyzzy foo=bar/>and some more")->eof;
100
101 $p = HTML::Parser->new(
102     api_version => 3,
103     xml_pic => 1,
104 );
105
106 $p->handler(
107     "process" => sub {
108         my($text, $t0) = @_;
109         is($text, "<?foo > bar?>");
110         is($t0, "foo > bar");
111     }, "text, token0");
112 $p->parse("<?foo > bar?> and then")->eof;