Changeset 361

Show
Ignore:
Timestamp:
05/01/08 03:33:30 (2 weeks ago)
Author:
ingy
Message:
doc, tests, refactoring.
Files:

Legend:

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

    r360 r361  
    388388my $quickId = qr/^($chars+)(#)($chars+)/; 
    389389my $quickClass = qr/^(([#.]?)($chars*))/; 
     390 
     391my $expr = { 
     392    ":" => { 
     393        # Position Checks 
     394        lt => sub { return $_[1] < $_[2][3] }, 
     395        gt => sub { return $_[1] > $_[2][3] }, 
     396        nth => sub { return $_[2][3] == $_[1] }, 
     397        eq => sub { return $_[2][3] == $_[1] }, 
     398        first => sub { return $_[1] == 0 }, 
     399        last => sub { return $_[1] == $#{$_[3]} }, 
     400        even => sub { return $_[1] % 2 == 0 }, 
     401        odd => sub { return $_[1] % 2 }, 
     402 
     403        # Child Checks 
     404        "first-child" => sub { 
     405            return $_[0]->parentNode->getElementsByTagName("*")->[0] == $_[0]; 
     406        }, 
     407        "last-child" => sub { 
     408            return pQuery->_nth( 
     409                $_[0]->parentNode->lastChildRef, 
     410                1, 
     411                "previousSiblingRef" 
     412            ) == $_[0]; 
     413        }, 
     414        "only-child" => sub { 
     415            return ! pQuery->_nth( 
     416                $_[0]->parentNode->lastChildRef, 
     417                2, 
     418                "previousSiblingRef" 
     419            ); 
     420        }, 
     421 
     422        # Parent Checks 
     423        parent => sub { return $_[0]->firstChild ? 1 : 0 }, 
     424        empty  => sub { return $_[0]->firstChild ? 0 : 1 }, 
     425 
     426        # Text Check 
     427        contains => sub { return index(pQuery($_[0])->text, $_[2][3]) >= 0 }, 
     428 
     429#             // Visibility 
     430#             visible: function(a){return "hidden"!=a.type&&jQuery.css(a,"display")!="none"&&jQuery.css(a,"visibility")!="hidden";}, 
     431#             hidden: function(a){return "hidden"==a.type||jQuery.css(a,"display")=="none"||jQuery.css(a,"visibility")=="hidden";}, 
     432#  
     433#             // Form attributes 
     434#             enabled: function(a){return !a.disabled;}, 
     435#             disabled: function(a){return a.disabled;}, 
     436#             checked: function(a){return a.checked;}, 
     437#             selected: function(a){return a.selected||jQuery.attr(a,"selected");}, 
     438#  
     439#             // Form elements 
     440#             text: function(a){return "text"==a.type;}, 
     441#             radio: function(a){return "radio"==a.type;}, 
     442#             checkbox: function(a){return "checkbox"==a.type;}, 
     443#             file: function(a){return "file"==a.type;}, 
     444#             password: function(a){return "password"==a.type;}, 
     445#             submit: function(a){return "submit"==a.type;}, 
     446#             image: function(a){return "image"==a.type;}, 
     447#             reset: function(a){return "reset"==a.type;}, 
     448#             button: function(a){return "button"==a.type||jQuery.nodeName(a,"button");}, 
     449#             input: function(a){return /input|select|textarea|button/i.test(a.nodeName);}, 
     450 
     451 
     452        # :has() 
     453# XXX - The first form should work. Indicates that context is messed up. 
     454#         has => sub { return pQuery->find($_[2][3], $_[0])->length ? 1 : 0 }, 
     455        has => sub { return pQuery($_[0])->find($_[2][3])->length ? 1 : 0 }, 
     456 
     457        # :header 
     458        header => sub { return $_[0]->nodeName =~ /^h[1-6]$/i }, 
     459    }, 
     460}; 
     461 
     462# The regular expressions that power the parsing engine 
     463my $parse = [ 
     464    # Match: [@value='test'], [@foo] 
     465    qr/^(\[) *@?([\w-]+) *([!*$^~=]*) *('?"?)(.*?)\4 *\]/, 
     466 
     467    # Match: :contains('foo') 
     468    qr/^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/, 
     469 
     470    # Match: :even, :last-chlid, #id, .class 
     471    qr/^([:.#]*)($chars+)/, 
     472]; 
     473 
     474sub _multiFilter { 
     475    # XXX - Port me. 
     476} 
     477 
    390478sub _find { 
    391479    my ($this, $t, $context) = @_; 
     
    555643} 
    556644 
    557 # The regular expressions that power the parsing engine 
    558 my $parse = [ 
    559     # Match: [@value='test'], [@foo] 
    560     qr/^(\[) *@?([\w-]+) *([!*$^~=]*) *('?"?)(.*?)\4 *\]/, 
    561  
    562     # Match: :contains('foo') 
    563     qr/^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/, 
    564  
    565     # Match: :even, :last-chlid, #id, .class 
    566     qr/^([:.#]*)($chars+)/, 
    567 ]; 
    568  
    569 my $expr = { 
    570     ":" => { 
    571         # Position Checks 
    572         lt => sub { return $_[1] < $_[2][3] }, 
    573         gt => sub { return $_[1] > $_[2][3] }, 
    574         nth => sub { return $_[2][3] == $_[1] }, 
    575         eq => sub { return $_[2][3] == $_[1] }, 
    576         first => sub { return $_[1] == 0 }, 
    577         last => sub { return $_[1] == $#{$_[3]} }, 
    578         even => sub { return $_[1] % 2 == 0 }, 
    579         odd => sub { return $_[1] % 2 }, 
    580  
    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 
    604         header => sub { return $_[0]->nodeName =~ /^h[1-6]$/i }, 
    605  
    606     }, 
    607 }; 
    608  
    609645sub _filter { 
    610646    my ($this, $t, $r, $not) = @_; 
     
    686722} 
    687723 
     724sub _dir { 
     725    # XXX - Port me. 
     726} 
     727 
    688728sub _nth { 
    689729    my ($this, $cur, $result, $dir, $elem) = @_; 
     
    696736 
    697737    return $cur; 
     738} 
     739 
     740sub _sibling { 
     741    # XXX - Port me. 
    698742} 
    699743 
  • trunk/src/ingy/pQuery/lib/pQuery/DOM.pm

    r360 r361  
    366366 
    367367This implies that the DOM methods previousSibling and nextSibling 
    368 wouldn't really work correctly. Therefore they are not implemented. 
     368wouldn't really work correctly. Therefore they are not 
     369implemented. However, previousSiblingRef and nextSiblingRef are 
     370implemented. See below. 
    369371 
    370372To deal with children, use the childNodes method which returns a list 
     
    498500=back 
    499501 
     502=head2 Non-standard Object Methods 
     503 
     504These methods are variants of standard methods, but guarantee that the 
     505result, if found, is another pQuery::DOM node. 
     506 
     507=over 
     508 
     509=item firstChildRef() 
     510 
     511Returns the first child node which is actually a pQuery::DOM node and 
     512not a string. 
     513 
     514=item lastChildRef() 
     515 
     516Returns the last child node which is actually a pQuery::DOM node and 
     517not a string. 
     518 
     519=item previousSiblingRef() 
     520 
     521Returns the previous sibling node which is actually a pQuery::DOM node 
     522and not a string. 
     523 
     524=item nextSiblingRef() 
     525 
     526Returns the next sibling node which is actually a pQuery::DOM node and 
     527not a string. 
     528 
     529=back 
     530 
    500531=head1 AUTHOR 
    501532 
  • trunk/src/ingy/pQuery/t/selectors.t

    r360 r361  
    1 use t::TestpQuery tests => 19
     1use t::TestpQuery tests => 22
    22 
    33use pQuery; 
     
    5050    'select :only-child'; 
    5151 
     52is pQuery('tr:last:parent')->text, 'Average 43.83 62.83', 'select :parent'; 
     53is pQuery('br:empty')->size, 3, 'select :empty'; 
     54 
    5255is pQuery('td:contains(Blue)')->size, 3, 
    5356    '3 tds contain Blue'; 
     57 
     58like pQuery('p:has(u)')->text, qr/^Then you can/, 
     59    ':has()'; 
    5460 
    5561is pQuery('*:header')->size, 2,