Changeset 144

Show
Ignore:
Timestamp:
05/27/2007 23:39:50 (18 months ago)
Author:
lwu
Message:

wycats's selector patches for jQuery compatibility (see ticket #85 for details)

Location:
trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/hpricot.rb

    r123 r144  
    2525require 'hpricot/parse' 
    2626require 'hpricot/builder' 
     27require 'hpricot/hpricot_functions' 
  • trunk/lib/hpricot/elements.rb

    r143 r144  
    5151# and Hpricot::Container::Trav. 
    5252  class Elements < Array 
     53   
    5354    # Searches this list for any elements (or children of these elements) matching 
    5455    # the CSS or XPath expression +expr+.  Root is assumed to be the element scanned. 
     
    203204    BRACK_RE = %r!(\[) *([^\]]*) *\]+!i 
    204205    FUNC_RE = %r!(:)?([a-zA-Z0-9\*_-]*)\( *[\"']?([^ \)]*?)['\"]? *\)! 
     206    CUST_RE = %r!(:)([a-zA-Z0-9\*_-]*)()! 
    205207    CATCH_RE = %r!([:\.#]*)([a-zA-Z0-9\*_-]+)! 
    206208 
    207209    def self.filter(nodes, expr, truth = true) 
    208210        until expr.empty? 
    209             _, *m = *expr.match(/^(?:#{ATTR_RE}|#{BRACK_RE}|#{FUNC_RE}|#{CATCH_RE})/) 
     211            _, *m = *expr.match(/^(?:#{ATTR_RE}|#{BRACK_RE}|#{FUNC_RE}|#{CUST_RE}|#{CATCH_RE})/) 
    210212            break unless _ 
    211213 
     
    222224            if m[0] == ":" && m[1] == "not" 
    223225                nodes, = Elements.filter(nodes, m[2], false) 
     226            elsif "#{m[0]}#{m[1]}" =~ /^(:even|:odd)$/ 
     227                new_nodes = [] 
     228                nodes.each_with_index {|n,i| new_nodes.push(n) if (i % 2 == (m[1] == "even" ? 0 : 1)) } 
     229                nodes = new_nodes 
     230            elsif "#{m[0]}#{m[1]}" =~ /^(:first|:last)$/ 
     231                nodes = [nodes.send(m[1])] 
    224232            else 
    225233                meth = "filter[#{m[0]}#{m[1]}]" unless m[0].empty? 
     
    352360    filter ':nth-child' do |arg,i| 
    353361      case arg  
    354       when 'even'; parent.containers.index(self) % 2 == 0 
    355       when 'odd';  parent.containers.index(self) % 2 == 1 
    356       else         self == parent.containers[arg.to_i] 
     362      when 'even'; (parent.containers.index(self) + 1) % 2 == 0 
     363      when 'odd';  (parent.containers.index(self) + 1) % 2 == 1 
     364      else         self == (parent.containers[arg.to_i + 1]) 
    357365      end 
    358366    end 
     
    401409      html.include? arg 
    402410    end 
     411     
     412     
    403413 
    404414    pred_procs = 
  • trunk/test/test_alter.rb

    r132 r144  
    66 
    77class TestAlter < Test::Unit::TestCase 
     8  def setup 
     9    @basic = Hpricot.parse(TestFiles::BASIC) 
     10  end 
     11   
    812  def test_before 
    9     @basic = Hpricot.parse(TestFiles::BASIC) 
    1013    test0 = "<link rel='stylesheet' href='test0.css' />" 
    1114    @basic.at("link").before(test0) 
     
    1417 
    1518  def test_after 
    16     @basic = Hpricot.parse(TestFiles::BASIC) 
    1719    test_inf = "<link rel='stylesheet' href='test_inf.css' />" 
    1820    @basic.search("link")[-1].after(test_inf) 
     
    2123 
    2224  def test_wrap 
    23     @basic = Hpricot.parse(TestFiles::BASIC) 
    2425    ohmy = (@basic/"p.ohmy").wrap("<div id='wrapper'></div>") 
    2526    assert_equal 'wrapper', ohmy[0].parent['id'] 
    2627    assert_equal 'ohmy', Hpricot(@basic.to_html).at("#wrapper").children[0]['class'] 
    2728  end 
     29   
     30  def test_add_class 
     31    first_p = @basic["p:first"].add_class("testing123") 
     32    assert first_p[0].attributes["class"].split(" ").include?("testing123") 
     33    assert Hpricot(@basic.to_html)["p:first"][0].attributes["class"].split(" ").include?("testing123") 
     34    assert !Hpricot(@basic.to_html)["p:gt(0)"][0].attributes["class"].split(" ").include?("testing123") 
     35  end 
     36   
     37  def test_change_attributes 
     38    all_ps = @basic["p"].attr("title", "Some Title") 
     39    all_as = @basic["a"].attr("href", "http://my_new_href.com") 
     40    assert_changed(@basic, "p", all_ps) {|p| p.attributes["title"] == "Some Title"} 
     41    assert_changed(@basic, "a", all_as) {|a| a.attributes["href"] == "http://my_new_href.com"} 
     42  end 
     43   
     44  def assert_changed original, selector, set, &block 
     45    assert set.all? &block 
     46    assert Hpricot(original.to_html)[selector].all? &block 
     47  end 
    2848end 
  • trunk/test/test_parser.rb

    r143 r144  
    177177  def test_alt_predicates 
    178178    @boingboing = Hpricot.parse(TestFiles::BOINGBOING) 
    179     assert_equal 2, @boingboing.search('//table/tr:last').length 
     179    assert_equal 1, @boingboing.search('//table/tr:last').length 
    180180 
    181181    @basic = Hpricot.parse(TestFiles::BASIC)