Debian Codegraph @

t/HTML-TreeBuilder-XPath.t

raw symbols SCIP
1 # Before `make install' is performed this script should be runnable with
2 # `make test'. After `make install' it should work as `perl HTML-TreeBuilder-XPath.t'
3
4 #########################
5
6 use Test::More tests => 31;
7 BEGIN { use_ok('HTML::TreeBuilder::XPath') };
8 use HTML::TreeBuilder::LibXML;
9 HTML::TreeBuilder::LibXML->replace_original;
10
11 #########################
12
13 my $doc='<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
14 <html>
15 <head><title>Example</title></head>
16 <body><h1>Example header</h1>
17 <div class="intro"><p>Intro p1</p><p>Intro p2</p><p>Intro p3 with <b>bold</b> text</p></div>
18 <p id="toto">para including <a href="http://foo.com/">links</a>, <a href="/bar/">more links</a>,
19 and even <span id="foo" class="myspan">spans</span>, <span class="myspan" id="bar">several</span>,
20 and that is <b>all</b> folks.</p>
21 <!-- a commented line break --><br>
22 <blockquote id="bq" bgcolor="0">0</blockquote>
23 </body>
24 </html>
25 ';
26
27 my $html= HTML::TreeBuilder::XPath->new_from_content( $doc);
28
29
30 is( $html->findvalue( '//p[@id]/@id'), 'toto', 'attribute value');
31 is( $html->findvalue( '//title'), 'Example', 'element text');
32 is( $html->findvalue( '//span[1]'), 'spans', '[1]');
33 is( $html->findvalue( '/html/body//p[@id="toto"]/*[@id="bar"]/@class'), 'myspan', 'attribute');
34 is( $html->findvalue( '//p[@id="toto"]/text()[2]'), ', ', 'text node');
35
36 # test sorting
37 TODO: {
38 local $TODO = "I don't know, this order is required for xpath spec, or not??";
39 is( $html->findvalue( '//*[@id="foo"]/@*'), 'myspanfoo', '2 atts on same element');
40 is( $html->findvalue( '//*[@id="foo"]/@id|//*[@id="foo"]/@class'), 'myspanfoo', '2 atts on same element');
41 is( $html->findvalue( '//*[@id="foo"]/@class|//*[@id="foo"]/@id'), 'myspanfoo', '2 atts on same element (unsorted)');
42
43 is( $html->findvalue( '//b'), 'boldall', '2 texts');
44 is( $html->findvalue( '//p[@id="toto"]/a'), 'linksmore links', '2 siblings');
45 is( $html->findvalue( '//p[@id="toto"]/a[1]|//p[@id="toto"]/a[2]'), 'linksmore links', '2 siblings');
46
47 is( $html->findvalue( '//@id[.="toto"]|//*[@id="bar"]|/html/body/h1|//@id[.="toto"]/../a[1]|//*[@id="foo"]'), 'Example headertotolinksspansseveral',
48 'query on various types of nodes');
49 };
50
51 is( $html->findvalue( './/*[@bgcolor="0"]'),'0', 'one child has a value of "0"');
52
53 {
54 my $p= $html->findnodes( '//p[@id="toto"]')->[0];
55 is( $p->findvalue( './a'), 'linksmore links', 'query on siblings of an element');
56 is( $p->findvalue( './a[1]|./a[2]'), 'linksmore links', 'query on siblings of an element (ordered)');
57 is( $p->findvalue( './a[2]|./a[1]'), 'linksmore links', 'query on siblings of an element (not ordered)');
58
59 is( $html->findvalue('id("foo")'), 'spans', 'id function');
60 is( $html->findvalue('id("foo")/@id'), 'foo', 'id function (attribute)');
61 }
62
63 is( $html->exists( '//p[@id]/@id' ) , 1 , 'does exist');
64 is( $html->exists( '//p[@id]/@id2' ) , 0 , 'does not exist' );
65
66 {
67 my @p = $html->find( 'p' );
68 is( scalar @p , 4 , '4 <p>');
69 is( $p[0]->getValue , 'Intro p1' , '<p>1');
70
71 my( $bq ) = $html->find( 'blockquote' );
72 is( $bq->getValue , 0 , 'bq' );
73 }
74
75 {
76 my @val = $html->findvalues('//span[@class="myspan"]');
77 is_deeply( \@val , [ qw/ spans several / ] , 'findvalues on multi element');
78
79 my( $val2 ) = $html->findvalues('//*[@id="bq"]');
80 is( $val2 , 0 , 'findvalues on unique element')
81 }
82
83 {
84 my @children = $html->findnodes( '//p[@id="toto"]')->[0]->childNodes;
85 is( scalar @children , 11 , 'child count');
86 is( $children[1]->getValue , 'links' , 'first child');
87 }
88
89 {
90 my @filter_nodes = $html->findnodes_filter( '//p' , sub { shift->getValue =~ /Intro / } );
91 is( scalar @filter_nodes , 3 , '3 <p>' );
92 }
93
94 {
95 my ($div ) = $html->findnodes( '//div[@class="intro"]' );
96 my $left = $div->left;
97 is( $left->tag , 'h1' );
98 my $right = $div->right;
99 is( $right->tag , 'p' );
100 }
101
102 __END__
103 /html/body/h1 1 Example header
104 //@id[.="toto"] 2 toto
105 //@id[.="toto"]/../a[1] 3 links
106 //*[@id="foo"] 4 spans
107 //*[@id="bar"] 5 several