Changeset 356
- Timestamp:
- 04/15/08 20:50:09 (1 month ago)
- Files:
-
- trunk/src/ingy/pQuery/Changes (modified) (1 diff)
- trunk/src/ingy/pQuery/MANIFEST (modified) (1 diff)
- trunk/src/ingy/pQuery/README (modified) (3 diffs)
- trunk/src/ingy/pQuery/lib/pQuery.pm (modified) (9 diffs)
- trunk/src/ingy/pQuery/lib/pQuery/DOM.pm (modified) (1 diff)
- trunk/src/ingy/pQuery/t/contructors.t (modified) (2 diffs)
- trunk/src/ingy/pQuery/t/document2.html (added)
- trunk/src/ingy/pQuery/t/document3.html (added)
- trunk/src/ingy/pQuery/t/eq.t (added)
- trunk/src/ingy/pQuery/t/find.t (modified) (2 diffs)
- trunk/src/ingy/pQuery/t/selectors.t (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/ingy/pQuery/Changes
r355 r356 1 --- 2 version: 0.06 3 date: Wed Apr 16 11:47:34 CST 2008 4 changes: 5 - Various changes/fixes from OSDC.tw 6 1 7 --- 2 8 version: 0.05 trunk/src/ingy/pQuery/MANIFEST
r355 r356 24 24 t/contructors.t 25 25 t/document1.html 26 t/document2.html 27 t/document3.html 26 28 t/dom.t 27 29 t/each.t 28 30 t/end.t 31 t/eq.t 29 32 t/find.t 30 33 t/html.t trunk/src/ingy/pQuery/README
r354 r356 184 184 For example: 185 185 186 pQuery('<p>I <b>like</b> pie</p>') .HTML();186 pQuery('<p>I <b>like</b> pie</p>')->toHtml; 187 187 188 188 returns: … … 192 192 while: 193 193 194 pQuery('<p>I <b>like</b> pie</p>') .html();194 pQuery('<p>I <b>like</b> pie</p>')->html(); 195 195 196 196 returns: … … 217 217 return a HTML::Response object. 218 218 219 my $html = pQuery .get("http://google.com")->content;219 my $html = pQuery->get("http://google.com")->content; 220 220 221 221 index($elem) trunk/src/ingy/pQuery/lib/pQuery.pm
r355 r356 2 2 3 3 package pQuery; 4 use 5.006001; 4 5 use strict; 5 6 use warnings; 6 use 5.006001;7 7 use pQuery::DOM; 8 use Carp; 8 9 9 10 use base 'Exporter'; 10 11 11 our $VERSION = '0.05'; 12 13 our @EXPORT = qw(pQuery PQUERY); 12 our $VERSION = '0.06'; 14 13 15 14 our $document; 15 *pQuery = \$document; 16 17 our @EXPORT = qw(pQuery $pQuery PQUERY); 16 18 17 19 my $my = {}; … … 30 32 31 33 ################################################################################ 32 # Playing around stuffs34 # New ideas / Playing around stuffs 33 35 ################################################################################ 34 36 sub url { … … 88 90 elsif ($selector =~ /^\S+\.html?$/) { 89 91 $my->{$this}{file} = $selector; 90 open FILE, $selector; 92 open FILE, $selector 93 or croak "Can't open file '$selector' for input:\n$!"; 91 94 my $html = do {local $/; <FILE>}; 92 95 close FILE; 96 $html =~ s/^\s*<!DOCTYPE\s.*?>\s*//s; 93 97 $selector = [$document = pQuery::DOM->fromHTML($html)]; 94 98 } … … 157 161 # TODO - Get/set text value 158 162 my $this = shift; 159 my $text = '';163 my @text; 160 164 161 165 $this->each(sub { 166 my $text = ''; 162 167 _to_text($_, \$text); 168 $text =~ s/\s+/ /g; 169 $text =~ s/^\s+|\s+$//g; 170 push @text, $text; 163 171 }); 164 172 165 $text =~ s/\s+/ /g; 166 $text =~ s/^\s+|\s+$//g; 167 168 return $text; 173 return wantarray ? @text : join(' ', @text); 169 174 } 170 175 … … 573 578 574 579 contains => sub { index(pQuery($_[0])->text, $_[2][3]) >= 0 }, 580 header => sub { return $_[0]->nodeName =~ /^h[1-6]$/i }, 575 581 }, 576 582 }; … … 678 684 ################################################################################ 679 685 sub _new_from_url { 686 require Encode; 680 687 my $this = shift; 681 688 my $url = shift; 682 my $response = $this-> get($url);689 my $response = $this->_web_get($url); 683 690 return $this 684 691 unless $response->is_success; 685 @$this = pQuery::DOM->fromHTML($response->content); 692 my $html = Encode::decode_utf8($response->content); 693 @$this = pQuery::DOM->fromHTML($html); 686 694 return $this; 687 695 } … … 965 973 For example: 966 974 967 pQuery('<p>I <b>like</b> pie</p>') .HTML();975 pQuery('<p>I <b>like</b> pie</p>')->toHtml; 968 976 969 977 returns: … … 973 981 while: 974 982 975 pQuery('<p>I <b>like</b> pie</p>') .html();983 pQuery('<p>I <b>like</b> pie</p>')->html(); 976 984 977 985 returns: … … 1000 1008 return a HTML::Response object. 1001 1009 1002 my $html = pQuery .get("http://google.com")->content;1010 my $html = pQuery->get("http://google.com")->content; 1003 1011 1004 1012 =head2 index($elem) trunk/src/ingy/pQuery/lib/pQuery/DOM.pm
r355 r356 98 98 my ($class, $html) = @_; 99 99 my $dom; 100 if ($html =~ /^\s*<html.*?>.*<\/html>\s*\z/ s) {100 if ($html =~ /^\s*<html.*?>.*<\/html>\s*\z/is) { 101 101 $dom = $class->_builder->parse_content($html); 102 102 return $dom; trunk/src/ingy/pQuery/t/contructors.t
r351 r356 1 use t::TestpQuery tests => 13;1 use t::TestpQuery tests => 25; 2 2 use strict; 3 3 … … 35 35 is $pq->[2], 7, 'Check value of a element'; 36 36 37 is $pQuery::document, undef, 'Global document not set yet'; 38 is $pQuery, undef, '$pQuery not set yet'; 39 $pQuery = 't/document3.html'; 40 is $pQuery::document, 't/document3.html', 'Global document is now set'; 41 is $pQuery, 't/document3.html', '$pQuery is now set'; 42 $pq = pQuery; 43 is ref($pQuery::document), 'pQuery::DOM', 'Global document is a DOM'; 44 is ref($pQuery), 'pQuery::DOM', '$pQuery is a DOM'; 45 is $pq->find('title')->text, 'Sample HTML Document 3', 'Document is correct'; 46 47 $pQuery::document = 't/document1.html'; 48 is $pQuery::document, 't/document1.html', 'Global document is now set'; 49 is $main::pQuery, 't/document1.html', '$main::pQuery is now set'; 50 $pq = pQuery; 51 is ref($pQuery::document), 'pQuery::DOM', 'Global document is a DOM'; 52 is ref($pQuery), 'pQuery::DOM', '$pQuery is a DOM'; 53 is $pq->find('title')->text, 'Sample HTML Document', 'Document is correct'; 54 55 trunk/src/ingy/pQuery/t/find.t
r355 r356 1 use t::TestpQuery tests => 6;1 use t::TestpQuery tests => 7; 2 2 3 3 use pQuery; … … 25 25 26 26 is $pquery->find('li:eq(4)')->text, 'three', ':eq works'; 27 28 is pQuery('<b>foo</b>')->find('b')->length, 0, 29 "don't find top level node"; trunk/src/ingy/pQuery/t/selectors.t
r355 r356 1 use t::TestpQuery tests => 1 2;1 use t::TestpQuery tests => 13; 2 2 3 3 use pQuery; … … 19 19 'select by class'; 20 20 21 is pQuery('td:gt(2):lt(2)')->text, 'Age Favorite Number',21 is pQuery('td:gt(2):lt(2)')->text, 'Age Favorite Number', 22 22 'select a range with gt/lt'; 23 23 24 is pQuery('td:lt(4):even')->text, 'First Name Favorite Color',24 is pQuery('td:lt(4):even')->text, 'First Name Favorite Color', 25 25 'select a with even'; 26 26 27 is pQuery('td:lt(4):odd')->text, 'Last Name Age',27 is pQuery('td:lt(4):odd')->text, 'Last Name Age', 28 28 'select a with odd'; 29 29 … … 37 37 '3 tds contain Blue'; 38 38 39 # is pQuery(' :header')->size, 2,39 # is pQuery('*:header')->size, 2, 40 40 # 'Two Headers'; 41 42 is pQuery(':header')->size, 2, 43 'Two Headers';
