📄 test.pl
字号:
use HTML::Tree;*HTML_FILE =\'../../../test.html';$test = 0;sub ok { ++$test; print "ok $test\n";}######################### We start with some black magic to print on failure.BEGIN { $| = 1; print "1..16\n"; }END { print "not ok 1\n" unless $loaded; }$loaded = 1;ok();################################################################################# Test #2## Test the visitor function and HTML::Tree->from_file().################################################################################@data = ( { name => 'Santa Claus', address => 'North Pole', phone => '000-555-1212', }, { name => 'Paul J. Lucas', address => 'Silicon Valley', phone => '650-555-1212', },);package Record;sub new { my $that = shift; my $class = ref( $that ) || $that; my $this = { }; return bless $this, $class;}package main;sub fetchrow { my( $object, $node, $class, $is_end_tag ) = @_; return 1 if $is_end_tag; ## # Pretend each element of @data is a tuple from a result of a database # query. Then each shift is equivalent to getting the next tuple. If # there's no more data, we're done. ## return !!( $object->{ data } = shift @data );}sub sub_val { my( $object, $node, $class, $is_end_tag ) = @_; return 0 if $is_end_tag; ## # Substitute the text of the text_node child of us, a <TD> element. ## $node->children()->[0]->text( $object->{ data }->{ $class } ); return 1;}%class_map = ( loop => \&fetchrow, name => \&sub_val, address => \&sub_val, phone => \&sub_val,);sub visitor { my( $object, $node, $depth, $is_end_tag ) = @_; if ( $node->is_text() ) { $out .= ( " " x $depth ) . $node->text() . "\n"; return 1; } my $result = 0; for ( split( /\s+/, $node->att( 'class' ) ) ) { if ( my $func = $class_map{ $_ } ) { $result = $func->( $object, $node, $_, $is_end_tag ); return 0 unless $is_end_tag || $result; last; } } $out .= " " x $depth; if ( $is_end_tag ) { $out .= "</" . $node->name() . ">\n"; return $result; } $out .= '<' . $node->name(); my $atts = $node->atts(); while ( my( $k, $v ) = each %{ $atts } ) { $out .= " $k=\"$v\""; } $out .= ">\n"; return 1;}$html = HTML::Tree->from_file( $HTML_FILE, { Include_Comments => 1 } );$record = Record->new();$html->visit( $record, \&visitor );$out =~ s/^\s*\n//gm;print 'not ' if $out ne '<!-- this is a comment --><html lang="en-US"> <head> <title> Sample </title> </head> <body bgcolor="#FFFFFF"> <img src="banner.jpg"> <table> <tr> <th> Name </th> <th> Address </th> <th> Phone </th> </tr> <tr class="loop"> <td align="left" class="name"> Santa Claus </td> <td class="address"> North Pole </td> <td class="phone"> 000-555-1212 </td> </tr> <tr class="loop"> <td align="left" class="name"> Paul J. Lucas </td> <td class="address"> Silicon Valley </td> <td class="phone"> 650-555-1212 </td> </tr> </table> </body></html>';ok();################################################################################# Test #3## Test HTML::Tree->from_string().################################################################################$html_string = '<html><head><title>Hello, world!</title></head></html>';$html = HTML::Tree->from_string( $html_string );print 'not ' unless $html->as_string() eq $html_string;ok();################################################################################# Test #4## Test $html->as_array() to see if we can generate a Perl data structure.################################################################################$html = HTML::Tree->from_file( $HTML_FILE, { Include_Comments => 1 } );$array_ref = $html->as_array( { Include_Comments => 1 } );print 'not ' unless $array_ref;ok();################################################################################# Test #5## Test HTML::Tree->from_array() to see if it can build a tree from a Perl# data structure.################################################################################$html2 = HTML::Tree->from_array( $array_ref );print 'not ' unless $html2;ok();################################################################################# Test #6## Test $html->as_string() to see that an HTML::Tree can be made into a# string correctly.################################################################################$html_string = $html->as_string();open( FILE, $HTML_FILE );$html_text = lc( join( '', <FILE> ) );close( FILE );print 'not ' if $html_text ne lc( $html_string );ok();################################################################################# Test #7## Test that the Perl data structure generated in test 4 and the 2nd HTML# tree generated from it in test 5 are both correct by comparing the# string generated from the 2nd tree matches the string generated from# the 1st tree.################################################################################$html_string = $html ->as_string( { Pretty_Print => 0 } );$html_string2 = $html2->as_string( { Pretty_Print => 0 } );print 'not ' if $html_string ne $html_string2;ok();################################################################################# Test #8## Test $html->write() to see that an HTML::Tree can be written to disk# correctly.################################################################################$tmp_file_name = "/tmp/HTML_Tree-test.$$";$html->write( $tmp_file_name );open( FILE, $tmp_file_name );$html_text_new = lc( join( '', <FILE> ) );close( FILE );print 'not ' if $html_text ne $html_text_new;ok();################################################################################# Test #9## Test that find_name() works.################################################################################$head_node = $html->find_name( 'head' );print 'not ' if $head_node->name() ne 'head';ok();################################################################################# Test #10## Test that find_name() returns undef for a node not found.################################################################################print 'not ' if $html->find_name( 'foo' );ok();################################################################################# Test #11## Test that find_if() works.################################################################################sub make_predicate { my( $att, $val ) = @_; return sub { my $node = shift; return $node->is_element() && $node->att( $att ) eq $val; }}$tr_node = $html->find_if( make_predicate( 'class', 'loop' ) );print 'not ' if $tr_node->name() ne 'tr';ok();################################################################################# Test #12## Test that a shift on children() works.################################################################################$text_node = shift @{ $tr_node->children() };$td_node = shift @{ $tr_node->children() };print 'not ' if $td_node->name() ne 'td';ok();################################################################################# Test #13## Test that a push on children() with an HTML::Tree reference works.################################################################################push( @{ $tr_node->children() }, $td_node );$name_node = $html->find_if( make_predicate( 'class', 'name' ) );print 'not ' if $name_node->name() ne 'td';ok();################################################################################# Test #14## Test that a pop on children() works.################################################################################$td_node = pop( @{ $tr_node->children() } );print 'not ' if $html->find_if( make_predicate( 'class', 'name' ) );ok();################################################################################# Test #15## Test that an unshift on children() works.################################################################################unshift( @{ $tr_node->children() }, $td_node );print 'not ' unless $html->find_if( make_predicate( 'class', 'name' ) );ok();################################################################################# Test #16## Test that replacing a child with an HTML string that is to be parsed# works.################################################################################$tr_node->children()->[0] = '<td class="name" align="right">Elwood Blues</td>';print 'not ' unless $html->find_if( make_predicate( 'align', 'right' ) );ok();END { unlink( $tmp_file_name ) if $tmp_file_name;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -