Changeset 360

Show
Ignore:
Timestamp:
04/30/08 23:43:44 (2 weeks ago)
Author:
ingy
Message:
more porting of selectors
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/ingy/pQuery/lib/pQuery.pm

    r359 r360  
    171171    }); 
    172172 
    173     return wantarray ? @text : join(' ', @text); 
     173    return wantarray ? @text : join(' ', grep /\S/, @text); 
    174174} 
    175175 
     
    569569my $expr = { 
    570570    ":" => { 
     571        # Position Checks 
    571572        lt => sub { return $_[1] < $_[2][3] }, 
    572573        gt => sub { return $_[1] > $_[2][3] }, 
     574        nth => sub { return $_[2][3] == $_[1] }, 
    573575        eq => sub { return $_[2][3] == $_[1] }, 
    574576        first => sub { return $_[1] == 0 }, 
     
    577579        odd => sub { return $_[1] % 2 }, 
    578580 
    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 
    580604        header => sub { return $_[0]->nodeName =~ /^h[1-6]$/i }, 
     605 
    581606    }, 
    582607}; 
     
    661686} 
    662687 
     688sub _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 
    663700################################################################################ 
    664701# These methods need to go down here because they are Perl builtins. 
  • trunk/src/ingy/pQuery/lib/pQuery/DOM.pm

    r358 r360  
    238238} 
    239239 
     240sub 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 
     248sub 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 
    240256sub appendChild { 
    241257    my ($self, $elem) = @_; 
     
    248264sub previousSibling { 
    249265    die "pQuery::DOM does not support the previousSibling method"; 
     266} 
     267 
     268sub 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 
     278sub 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; 
    250286} 
    251287 
  • trunk/src/ingy/pQuery/t/selectors.t

    r359 r360  
    1 use t::TestpQuery tests => 15
     1use t::TestpQuery tests => 19
    22 
    33use pQuery; 
     
    2020    'select by class'; 
    2121 
     22is pQuery('td:nth(4)')->text, 'Favorite Number', 
     23    'select nth'; 
     24 
    2225is pQuery('td:gt(2):lt(2)')->text, 'Age Favorite Number', 
    2326    'select a range with gt/lt'; 
     
    3538    'select last'; 
    3639 
     40is pQuery('td:first-child')->text,  
     41    'First Name Alyssa Bennie Chester Delilah Eldridge Fennel', 
     42    'select :first-child'; 
     43 
     44is pQuery('td:last-child')->text,  
     45    'Favorite Number 13 7 144 54 21 138 377 62.83', 
     46    'select :last-child'; 
     47 
     48is pQuery('ul li:only-child')->text,  
     49    'Subversion: http://svn.spreadily.com/repo/trunk/', 
     50    'select :only-child'; 
     51 
    3752is pQuery('td:contains(Blue)')->size, 3, 
    3853    '3 tds contain Blue'; 
     
    4358is pQuery(':header')->size, 2, 
    4459    'Two Headers'; 
     60