File: test_3_45.t

package info (click to toggle)
libxml-twig-perl 1%3A3.52-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,952 kB
  • sloc: perl: 21,221; xml: 423; makefile: 9
file content (103 lines) | stat: -rwxr-xr-x 3,603 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;
use Test::More tests => 16;

is( XML::Twig->new( keep_encoding => 1)->parse( q{<d a='"foo'/>})->sprint, q{<d a="&quot;foo"/>}, "quote in att with keep_encoding");

# test CDATA sections in HTML escaping https://rt.cpan.org/Ticket/Display.html?id=86773
my $html = <<'EOF';
<div id="body">body</div>
<script>
//<![CDATA[
if ( this.value && ( !request.term || matcher.test(text) ) && 1 > 0 && 0 < 1 )
//]]>
</script>
EOF

               # module              =>  XML::Twig->new options
my %html_conv= ( 'HTML::TreeBuilder' =>  {},
                 'HTML::Tidy'        =>  { use_tidy => 1 },
               );
foreach my $module ( sort keys  %html_conv)
  { SKIP: 
      { eval "use $module";
        skip "$module not available", 3 if $@ ;

        my $parser= XML::Twig->new( %{$html_conv{$module}});
        my $xml = $parser->safe_parse_html($html);
        print $@ if $@;

        my @cdata = $xml->get_xpath('//#CDATA');
        ok(@cdata == 1, "1 CDATA section found (using $module)");

        ok(((index $xml->sprint, "//]]>") >= 0), "end of cdata ok in doc (using $module)");
        #diag "\n", $xml->sprint, "\n";

        my @elts = $xml->get_xpath('//script');

        foreach my $el (@elts) 
          { #diag $el->sprint;
            ok(((index $el->sprint, "//]]>") >= 0), "end of cdata ok in script element (using $module)");
          }
      }
  }

# test & in HTML (RT #86633)
my $html_with_amp='<h1>Marco&amp;company</h1>';
my $expected_body= '<body><h1>Marco&amp;company</h1></body>';

SKIP: 
{ eval "use HTML::Tidy";
  skip "HTML::Tidy not available", 1 if $@ ;
  my $parsert = XML::Twig->new();
  my $html_tidy = $parsert->safe_parse_html( { use_tidy => 1 }, "<h1>Marco&amp;company</h1>");
  diag $@ if $@;
  is( $html_tidy->first_elt( 'body')->sprint, $expected_body, "&amp; in text, converting html with use_tidy");
}

SKIP:
{ eval "use HTML::TreeBuilder";
  skip "HTML::TreeBuilder not available", 1 if $@ ;
  my $parserh= XML::Twig->new();
  my $html = $parserh->safe_parse_html("<h1>Marco&amp;company</h1>");
  diag $@ if $@;
  is( $html->first_elt( 'body')->sprint , $expected_body, "&amp; in text, converting html with treebuilder");
}

is( XML::Twig::_unescape_cdata( '&lt;tag att="foo&amp;bar&amp;baz"&gt;&gt;&gt;&lt;/tag&gt;'), '<tag att="foo&bar&baz">>></tag>', '_unescape_cdata');

SKIP:
{ skip "safe_print_to_file method does not work on Windows", 6 if $^O =~ m{win}i;
  # testing safe_print_to_file
  my $tmp= "safe_print_to_file.xml";
  my $doc= "<doc>foo</doc>";
  unlink( $tmp); # no check, it could not be there
  my $t1= XML::Twig->nparse( $doc)->safe_print_to_file( $tmp);
  ok( -f $tmp, "safe_print_to_file created document");
  my $t2= XML::Twig->nparse( $tmp);
  is( $t2->sprint, $t1->sprint, "generated document identical to original document");
  unlink( $tmp); 

  my $e1=  XML::Twig->parse( '<d><a>foo</a><b>bar</b></d>')->first_elt( 'b')->safe_print_to_file( $tmp);
  ok( -f $tmp, "safe_print_to_file on elt created document");
  $t2= XML::Twig->nparse( $tmp);
  is( $t2->sprint, '<b>bar</b>', "generated sub-document identical to original sub-document");
  unlink( $tmp); 

  # failure modes
  eval { XML::Twig->nparse( $tmp); };
  like( $@, qr/Couldn't open $tmp:/, 'parse a non-existent file');
  my $non_existent="safe_non_existent_I_hope_01/tmp";
  while( -f $non_existent) { $non_existent++; } # most likely unnecessary ;--)
  eval { $t1->safe_print_to_file( $non_existent); };
  like( $@, qr/(does not exist|is not a directory)/, 'safe_print_to_file in non-existent dir');
} 

exit;