Changeset 356

Show
Ignore:
Timestamp:
04/15/08 20:50:09 (1 month ago)
Author:
ingy
Message:
v 0.06
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/ingy/pQuery/Changes

    r355 r356  
     1--- 
     2version: 0.06 
     3date:    Wed Apr 16 11:47:34 CST 2008 
     4changes: 
     5- Various changes/fixes from OSDC.tw 
     6 
    17--- 
    28version: 0.05 
  • trunk/src/ingy/pQuery/MANIFEST

    r355 r356  
    2424t/contructors.t 
    2525t/document1.html 
     26t/document2.html 
     27t/document3.html 
    2628t/dom.t 
    2729t/each.t 
    2830t/end.t 
     31t/eq.t 
    2932t/find.t 
    3033t/html.t 
  • trunk/src/ingy/pQuery/README

    r354 r356  
    184184    For example: 
    185185 
    186         pQuery('<p>I <b>like</b> pie</p>').HTML()
     186        pQuery('<p>I <b>like</b> pie</p>')->toHtml
    187187 
    188188    returns: 
     
    192192    while: 
    193193 
    194         pQuery('<p>I <b>like</b> pie</p>').html(); 
     194        pQuery('<p>I <b>like</b> pie</p>')->html(); 
    195195 
    196196    returns: 
     
    217217    return a HTML::Response object. 
    218218 
    219         my $html = pQuery.get("http://google.com")->content; 
     219        my $html = pQuery->get("http://google.com")->content; 
    220220 
    221221  index($elem) 
  • trunk/src/ingy/pQuery/lib/pQuery.pm

    r355 r356  
    22 
    33package pQuery; 
     4use 5.006001; 
    45use strict; 
    56use warnings; 
    6 use 5.006001; 
    77use pQuery::DOM; 
     8use Carp; 
    89 
    910use base 'Exporter'; 
    1011 
    11 our $VERSION = '0.05'; 
    12  
    13 our @EXPORT = qw(pQuery PQUERY); 
     12our $VERSION = '0.06'; 
    1413 
    1514our $document; 
     15*pQuery = \$document; 
     16 
     17our @EXPORT = qw(pQuery $pQuery PQUERY); 
    1618 
    1719my $my = {}; 
     
    3032 
    3133################################################################################ 
    32 # Playing around stuffs 
     34# New ideas / Playing around stuffs 
    3335################################################################################ 
    3436sub url { 
     
    8890            elsif ($selector =~ /^\S+\.html?$/) { 
    8991                $my->{$this}{file} = $selector; 
    90                 open FILE, $selector; 
     92                open FILE, $selector 
     93                    or croak "Can't open file '$selector' for input:\n$!"; 
    9194                my $html = do {local $/; <FILE>}; 
    9295                close FILE; 
     96                $html =~ s/^\s*<!DOCTYPE\s.*?>\s*//s; 
    9397                $selector = [$document = pQuery::DOM->fromHTML($html)]; 
    9498            } 
     
    157161    # TODO - Get/set text value 
    158162    my $this = shift; 
    159     my $text = ''
     163    my @text
    160164 
    161165    $this->each(sub { 
     166        my $text = ''; 
    162167        _to_text($_, \$text); 
     168        $text =~ s/\s+/ /g; 
     169        $text =~ s/^\s+|\s+$//g; 
     170        push @text, $text; 
    163171    }); 
    164172 
    165     $text =~ s/\s+/ /g; 
    166     $text =~ s/^\s+|\s+$//g; 
    167  
    168     return $text; 
     173    return wantarray ? @text : join(' ', @text); 
    169174} 
    170175 
     
    573578 
    574579        contains => sub { index(pQuery($_[0])->text, $_[2][3]) >= 0 }, 
     580        header => sub { return $_[0]->nodeName =~ /^h[1-6]$/i }, 
    575581    }, 
    576582}; 
     
    678684################################################################################ 
    679685sub _new_from_url { 
     686    require Encode; 
    680687    my $this = shift; 
    681688    my $url = shift; 
    682     my $response = $this->get($url); 
     689    my $response = $this->_web_get($url); 
    683690    return $this 
    684691        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); 
    686694    return $this; 
    687695} 
     
    965973For example: 
    966974 
    967     pQuery('<p>I <b>like</b> pie</p>').HTML()
     975    pQuery('<p>I <b>like</b> pie</p>')->toHtml
    968976 
    969977returns: 
     
    973981while: 
    974982 
    975     pQuery('<p>I <b>like</b> pie</p>').html(); 
     983    pQuery('<p>I <b>like</b> pie</p>')->html(); 
    976984 
    977985returns: 
     
    10001008return a HTML::Response object. 
    10011009 
    1002     my $html = pQuery.get("http://google.com")->content; 
     1010    my $html = pQuery->get("http://google.com")->content; 
    10031011 
    10041012=head2 index($elem) 
  • trunk/src/ingy/pQuery/lib/pQuery/DOM.pm

    r355 r356  
    9898    my ($class, $html) = @_; 
    9999    my $dom; 
    100     if ($html =~ /^\s*<html.*?>.*<\/html>\s*\z/s) { 
     100    if ($html =~ /^\s*<html.*?>.*<\/html>\s*\z/is) { 
    101101        $dom = $class->_builder->parse_content($html); 
    102102        return $dom; 
  • trunk/src/ingy/pQuery/t/contructors.t

    r351 r356  
    1 use t::TestpQuery tests => 13
     1use t::TestpQuery tests => 25
    22use strict; 
    33 
     
    3535is $pq->[2], 7, 'Check value of a element'; 
    3636 
     37is $pQuery::document, undef, 'Global document not set yet'; 
     38is $pQuery, undef, '$pQuery not set yet'; 
     39$pQuery = 't/document3.html'; 
     40is $pQuery::document, 't/document3.html', 'Global document is now set'; 
     41is $pQuery, 't/document3.html', '$pQuery is now set'; 
     42$pq = pQuery; 
     43is ref($pQuery::document), 'pQuery::DOM', 'Global document is a DOM'; 
     44is ref($pQuery), 'pQuery::DOM', '$pQuery is a DOM'; 
     45is $pq->find('title')->text, 'Sample HTML Document 3', 'Document is correct'; 
     46 
     47$pQuery::document = 't/document1.html'; 
     48is $pQuery::document, 't/document1.html', 'Global document is now set'; 
     49is $main::pQuery, 't/document1.html', '$main::pQuery is now set'; 
     50$pq = pQuery; 
     51is ref($pQuery::document), 'pQuery::DOM', 'Global document is a DOM'; 
     52is ref($pQuery), 'pQuery::DOM', '$pQuery is a DOM'; 
     53is $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
     1use t::TestpQuery tests => 7
    22 
    33use pQuery; 
     
    2525 
    2626is $pquery->find('li:eq(4)')->text, 'three', ':eq works'; 
     27 
     28is 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 => 12
     1use t::TestpQuery tests => 13
    22 
    33use pQuery; 
     
    1919    'select by class'; 
    2020 
    21 is pQuery('td:gt(2):lt(2)')->text, 'AgeFavorite Number', 
     21is pQuery('td:gt(2):lt(2)')->text, 'Age Favorite Number', 
    2222    'select a range with gt/lt'; 
    2323 
    24 is pQuery('td:lt(4):even')->text, 'First NameFavorite Color', 
     24is pQuery('td:lt(4):even')->text, 'First Name Favorite Color', 
    2525    'select a with even'; 
    2626 
    27 is pQuery('td:lt(4):odd')->text, 'Last NameAge', 
     27is pQuery('td:lt(4):odd')->text, 'Last Name Age', 
    2828    'select a with odd'; 
    2929 
     
    3737    '3 tds contain Blue'; 
    3838 
    39 # is pQuery(':header')->size, 2, 
     39# is pQuery('*:header')->size, 2, 
    4040#     'Two Headers'; 
     41 
     42is pQuery(':header')->size, 2, 
     43    'Two Headers';