Changeset 360
- Timestamp:
- 04/30/08 23:43:44 (2 weeks ago)
- Files:
-
- trunk/src/ingy/pQuery/lib/pQuery.pm (modified) (4 diffs)
- trunk/src/ingy/pQuery/lib/pQuery/DOM.pm (modified) (2 diffs)
- trunk/src/ingy/pQuery/t/selectors.t (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/ingy/pQuery/lib/pQuery.pm
r359 r360 171 171 }); 172 172 173 return wantarray ? @text : join(' ', @text);173 return wantarray ? @text : join(' ', grep /\S/, @text); 174 174 } 175 175 … … 569 569 my $expr = { 570 570 ":" => { 571 # Position Checks 571 572 lt => sub { return $_[1] < $_[2][3] }, 572 573 gt => sub { return $_[1] > $_[2][3] }, 574 nth => sub { return $_[2][3] == $_[1] }, 573 575 eq => sub { return $_[2][3] == $_[1] }, 574 576 first => sub { return $_[1] == 0 }, … … 577 579 odd => sub { return $_[1] % 2 }, 578 580 579 contains => sub { index(pQuery($_[0])->text, $_[2][3]) >= 0 }, 581 # Child Checks 582 "first-child" => sub { 583 return $_[0]->parentNode->getElementsByTagName("*")->[0] == $_[0]; 584 }, 585 "last-child" => sub { 586 return pQuery->_nth( 587 $_[0]->parentNode->lastChildRef, 588 1, 589 "previousSiblingRef" 590 ) == $_[0]; 591 }, 592 "only-child" => sub { 593 return ! pQuery->_nth( 594 $_[0]->parentNode->lastChildRef, 595 2, 596 "previousSiblingRef" 597 ); 598 }, 599 600 # Text Check 601 contains => sub { return index(pQuery($_[0])->text, $_[2][3]) >= 0 }, 602 603 # :header 580 604 header => sub { return $_[0]->nodeName =~ /^h[1-6]$/i }, 605 581 606 }, 582 607 }; … … 661 686 } 662 687 688 sub _nth { 689 my ($this, $cur, $result, $dir, $elem) = @_; 690 $result ||= 1; 691 my $num = 0; 692 693 for (; $cur; $cur = $cur->$dir) { 694 last if (ref($cur) and $cur->nodeType == 1 and ++$num == $result); 695 } 696 697 return $cur; 698 } 699 663 700 ################################################################################ 664 701 # These methods need to go down here because they are Perl builtins. trunk/src/ingy/pQuery/lib/pQuery/DOM.pm
r358 r360 238 238 } 239 239 240 sub firstChildRef { 241 my $content = $_[0]->{_content} or return; 242 for (my $i = 0; $i < @$content; $i++) { 243 return $content->[$i] if ref $content->[$i]; 244 } 245 return; 246 } 247 248 sub lastChildRef { 249 my $content = $_[0]->{_content} or return; 250 for (my $i = @$content - 1; $i >= 0; $i--) { 251 return $content->[$i] if ref $content->[$i]; 252 } 253 return; 254 } 255 240 256 sub appendChild { 241 257 my ($self, $elem) = @_; … … 248 264 sub previousSibling { 249 265 die "pQuery::DOM does not support the previousSibling method"; 266 } 267 268 sub nextSiblingRef { 269 my $content = $_[0]->parentNode->{_content} or return; 270 my $found = 0; 271 for (my $i = 0; $i < @$content; $i++) { 272 return $content->[$i] if $found and ref $content->[$i]; 273 $found = 1 if ref($content->[$i]) and $content->[$i] == $_[0]; 274 } 275 return; 276 } 277 278 sub previousSiblingRef { 279 my $content = $_[0]->parentNode->{_content} or return; 280 my $found = 0; 281 for (my $i = @$content - 1; $i >= 0; $i--) { 282 return $content->[$i] if $found and ref $content->[$i]; 283 $found = 1 if ref($content->[$i]) and $content->[$i] == $_[0]; 284 } 285 return; 250 286 } 251 287 trunk/src/ingy/pQuery/t/selectors.t
r359 r360 1 use t::TestpQuery tests => 1 5;1 use t::TestpQuery tests => 19; 2 2 3 3 use pQuery; … … 20 20 'select by class'; 21 21 22 is pQuery('td:nth(4)')->text, 'Favorite Number', 23 'select nth'; 24 22 25 is pQuery('td:gt(2):lt(2)')->text, 'Age Favorite Number', 23 26 'select a range with gt/lt'; … … 35 38 'select last'; 36 39 40 is pQuery('td:first-child')->text, 41 'First Name Alyssa Bennie Chester Delilah Eldridge Fennel', 42 'select :first-child'; 43 44 is pQuery('td:last-child')->text, 45 'Favorite Number 13 7 144 54 21 138 377 62.83', 46 'select :last-child'; 47 48 is pQuery('ul li:only-child')->text, 49 'Subversion: http://svn.spreadily.com/repo/trunk/', 50 'select :only-child'; 51 37 52 is pQuery('td:contains(Blue)')->size, 3, 38 53 '3 tds contain Blue'; … … 43 58 is pQuery(':header')->size, 2, 44 59 'Two Headers'; 60
