comparison Ikariam.pm @ 146:00a21f73e906

we don't use DOM API of HTML::TagParser
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sat, 01 Nov 2008 00:14:28 +0800
parents bfa78302cf09
children 7f9109a50679
comparison
equal deleted inserted replaced
144:9fb18f3d6e26 146:00a21f73e906
93 use strict; 93 use strict;
94 use Data::Dumper; 94 use Data::Dumper;
95 use LWP; 95 use LWP;
96 use HTTP::Cookies; 96 use HTTP::Cookies;
97 use WWW::Mechanize; 97 use WWW::Mechanize;
98 use HTML::TagParser;
99 use XML::LibXML qw(:encoding); 98 use XML::LibXML qw(:encoding);
100 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 99 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
101 use POSIX; 100 use POSIX;
102 use utf8; 101 use utf8;
103 102
911 foreach(@cities) { 910 foreach(@cities) {
912 $self->{'cities'}->{$_} = {}; 911 $self->{'cities'}->{$_} = {};
913 } 912 }
914 } 913 }
915 914
916 sub getElementsByTagName {
917 my $element = shift;
918 my $tagname = lc(shift);
919 my ( $flat, $cur ) = @$element;
920
921 my $out = [];
922 for( ; $cur <= $#$flat ; $cur++ ) {
923 last if ($flat->[ $cur + 1 ]->[001] eq $element->tagName() );
924 next if ($flat->[$cur]->[001] ne $tagname );
925 next if $flat->[$cur]->[000]; # close
926
927 my $elem = HTML::TagParser::Element->new( $flat, $cur );
928 return $elem unless wantarray;
929 push( @$out, $elem );
930 }
931 return unless wantarray;
932 @$out;
933 }
934
935 sub getElementsByAttribute {
936 my $element = shift;
937 my $key = lc(shift);
938 my $val = shift;
939 my ( $flat, $cur ) = @$element;
940
941 my $out = [];
942 for ( ; $cur <= $#$flat ; $cur++ ) {
943 next if $flat->[$cur]->[000]; # close
944 my $elem = HTML::TagParser::Element->new( $flat, $cur );
945 my $attr = $elem->attributes();
946 next unless exists $attr->{$key};
947 next if ( $attr->{$key} ne $val );
948 return $elem unless wantarray;
949 push( @$out, $elem );
950 }
951 return unless wantarray;
952 @$out;
953 }
954 915
955 1; 916 1;