File: 12html.t

package info (click to toggle)
libxml-libxml-perl 2.0116%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,420 kB
  • sloc: perl: 6,139; ansic: 3,752; xml: 182; sh: 64; makefile: 8
file content (350 lines) | stat: -rw-r--r-- 9,151 bytes parent folder | download | duplicates (7)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350

use strict;
use warnings;

# should be 43.
use Test::More tests => 43;

use XML::LibXML;
use IO::File;

# TEST
ok(1, ' TODO : Add test name');

my $html = "example/test.html";

my $parser = XML::LibXML->new();
{
    my $doc = $parser->parse_html_file($html);
    # TEST
    ok($doc, ' TODO : Add test name');
}

my $fh;

open $fh, '<', $html
    or die "Can't open '$html': $!";

my $string;
{
    local $/;
    $string = <$fh>;
}

seek($fh, 0, 0);

# TEST

ok($string, ' TODO : Add test name');

my $doc = $parser->parse_html_string($string);

# TEST

ok($doc, ' TODO : Add test name');

undef $doc;

$doc = $parser->parse_html_fh($fh);

# TEST

ok($doc, ' TODO : Add test name');

$fh->close();

# parsing HTML's CGI calling links

my $strhref = <<EOHTML;

<html>
    <body>
        <a href="http:/foo.bar/foobar.pl?foo=bar&bar=foo">
            foo
        </a>
        <p>test
    </body>
</html>
EOHTML

my $htmldoc;

$parser->recover(1);
eval {
    local $SIG{'__WARN__'} = sub { };
    $htmldoc = $parser->parse_html_string( $strhref );
};

# ok( not $@ );
# TEST
ok( $htmldoc, ' TODO : Add test name' );

# parse_html_string with encoding
# encodings
SKIP:
{
    if (! eval { require Encode; })
    {
        skip("Encoding related tests require Encode", 14);
    }
    use utf8;

    my $utf_str = "ěščř";

    # w/o 'meta' charset
    $strhref = <<EOHTML;
<html>
  <body>
    <p>$utf_str</p>
  </body>
</html>
EOHTML

    # TEST

    ok( Encode::is_utf8($strhref), ' TODO : Add test name' );
    $htmldoc = $parser->parse_html_string( $strhref );
    # TEST
    ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
    # TEST
    is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');

    $htmldoc = $parser->parse_html_string( $strhref,
        {
            encoding => 'UTF-8'
        }
    );
    # TEST
    ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
    # TEST
    is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');


    my $iso_str = Encode::encode('iso-8859-2', $strhref);
    $htmldoc = $parser->parse_html_string( $iso_str,
        {
            encoding => 'iso-8859-2'
        }
    );
    # TEST
    ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
    # TEST
    is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');

    # w/ 'meta' charset
    $strhref = <<EOHTML;
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;
      charset=iso-8859-2">
  </head>
  <body>
    <p>$utf_str</p>
  </body>
</html>
EOHTML

    $htmldoc = $parser->parse_html_string( $strhref, { encoding => 'UTF-8' });
    # TEST
    ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
    # TEST
    is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');

    $iso_str = Encode::encode('iso-8859-2', $strhref);
    $htmldoc = $parser->parse_html_string( $iso_str );
    # TEST
    ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
    # TEST
    is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');

    $htmldoc = $parser->parse_html_string( $iso_str, { encoding => 'iso-8859-2',
            URI => 'foo'
        } );
    # TEST
    ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
    # TEST
    is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');
    # TEST
    is($htmldoc->URI, 'foo', ' TODO : Add test name');
}

# parse example/enc_latin2.html
# w/ 'meta' charset
{
    use utf8;
    my $utf_str = "ěščř";
    my $test_file = 'example/enc_latin2.html';
    my $fh;

    $htmldoc = $parser->parse_html_file( $test_file );
    # TEST
    ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
    # TEST
    is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');

    $htmldoc = $parser->parse_html_file( $test_file, { encoding => 'iso-8859-2',
            URI => 'foo'
        });
    # TEST
    ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
    # TEST
    is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');
    # TEST
    is($htmldoc->URI, 'foo', ' TODO : Add test name');

    open $fh, '<', $test_file
        or die "Cannot open '$test_file' for reading - $!";
    $htmldoc = $parser->parse_html_fh( $fh );
    close $fh;
    # TEST
    ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
    # TEST
    is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');

    open $fh, '<', $test_file
        or die "Cannot open '$test_file' for reading - $!";
    $htmldoc = $parser->parse_html_fh( $fh, { encoding => 'iso-8859-2',
            URI => 'foo',
        });
    close $fh;
    # TEST
    ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
    # TEST
    is($htmldoc->URI, 'foo', ' TODO : Add test name');
    # TEST
    is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');

    SKIP:
    {
        my $num_tests = 2;

        # LibXML_read_perl doesn't play well with encoding layers. Skip
        # unconditionally for now.
        skip("skipping until LibXML_read_perl is fixed", $num_tests);

        if (1000*$] < 5008)
        {
            skip("skipping for Perl < 5.8", $num_tests);
        }
        elsif (20627 > XML::LibXML::LIBXML_VERSION)
        {
            skip("skipping for libxml2 < 2.6.27", $num_tests);
        }
        # translate to UTF8 on perl-side
        open $fh, '<:encoding(iso-8859-2)', $test_file
            or die "Cannot open '$test_file' for reading - $!";
        $htmldoc = $parser->parse_html_fh( $fh, { encoding => 'UTF-8' });
        close $fh;
        # TEST
        ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
        # TEST
        is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');
    }
}

# parse example/enc2_latin2.html
# w/o 'meta' charset
{
    use utf8;
    my $utf_str = "ěščř";
    my $test_file = 'example/enc2_latin2.html';
    my $fh;

    $htmldoc = $parser->parse_html_file( $test_file, { encoding => 'iso-8859-2' });
    # TEST
    ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
    # TEST
    is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');

    open $fh, '<', $test_file
        or die "Cannot open '$test_file' for reading - $!";
    $htmldoc = $parser->parse_html_fh( $fh, { encoding => 'iso-8859-2' });
    close $fh;
    # TEST
    ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
    # TEST
    is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');

    SKIP:
    {
        my $num_tests = 2;

        # LibXML_read_perl doesn't play well with encoding layers. Skip
        # unconditionally for now.
        skip("skipping until LibXML_read_perl is fixed", $num_tests);

        if (1000*$] < 5008)
        {
            skip("skipping for Perl < 5.8", $num_tests);
        }
        # translate to UTF8 on perl-side
        open my $fh, '<:encoding(iso-8859-2)', $test_file
            or die "Cannot open '$test_file' for reading - $!";
        $htmldoc = $parser->parse_html_fh( $fh, { encoding => 'UTF-8' } );
        close $fh;
        # TEST
        ok( $htmldoc && $htmldoc->getDocumentElement, ' TODO : Add test name' );
        # TEST
        is($htmldoc->findvalue('//p/text()'), $utf_str, ' TODO : Add test name');
    }
}


{
  # 44715

  my $html = <<'EOF';
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test &amp; Test some more</title>
</head>
<body>
<p>Meet you at the caf&eacute;?</p>
<p>How about <a href="http://example.com?mode=cafe&id=1&ref=foo">this one</a>?
</p>
<input class="wibble" id="foo" value="working" />
</body>
</html>
EOF
  my $parser = XML::LibXML->new;
  eval {
    $doc = $parser->parse_html_string(
      $html => { recover => 1, suppress_errors => 1 }
     );
  };
  # TEST
  ok (!$@, 'No exception was thrown.');
  # TEST
  ok ($doc, ' Parsing was successful.');
  my $root = $doc && $doc->documentElement;
  my $val = $root && $root->findvalue('//input[@id="foo"]/@value');
  # TEST
  is ($val, 'working', 'XPath');
}


{
    # 70878
    # HTML_PARSE_NODEFDTD

    SKIP: {
        skip("LibXML version is below 20708", 2) unless ( XML::LibXML::LIBXML_VERSION >= 20708 );

        my $html = q(<body bgcolor='#ffffff' style="overflow: hidden;" leftmargin=0 MARGINWIDTH=0 CLASS="text">);
        my $p = XML::LibXML->new;

        # TEST
        like( $p->parse_html_string( $html, {
                    recover => 2,
                    no_defdtd => 1,
                    encoding => 'UTF-8' } )->toStringHTML, qr/^\Q<html>\E/, 'do not add a default DOCTYPE' );

        # TEST
        like ( $p->parse_html_string( $html, {
                    recover => 2,
                    encoding => 'UTF-8' } )->toStringHTML, qr/^\Q<!DOCTYPE html\E/, 'add a default DOCTYPE' );
    }
}