Changeset 145

Show
Ignore:
Timestamp:
05/31/2007 01:51:43 (18 months ago)
Author:
why
Message:
  • lib/hpricot/: wrapped up the rest of wycats' patch. no [] syntax for now and changed everything to use get_attribute, set_attribute rather than raw_attributes.
Location:
trunk
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/hpricot.rb

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

    r140 r145  
    7676      childs = [] 
    7777      attrs = args.grep(Hash) 
    78       childs.concat((args - attrs).map { |x| Text.new(Hpricot.xs(x)) if x }) 
     78      childs.concat((args - attrs).map do |x| 
     79        if x.respond_to? :to_html 
     80          Hpricot.make(x.to_html) 
     81        elsif x 
     82          Text.new(Hpricot.xs(x)) 
     83        end 
     84      end.flatten) 
    7985      attrs = attrs.inject({}) do |hsh, ath| 
    8086        ath.each do |k, v| 
    81           hsh[k] = Hpricot.xs(v) 
     87          hsh[k] = Hpricot.xs(v.to_s) if v 
    8288        end 
    8389        hsh 
     
    183189      end 
    184190 
    185       unless args.empty? 
    186         if args.last.respond_to? :to_hash 
    187           @attrs.merge! args.pop.to_hash 
    188         end 
    189       end 
    190        
    191191      if block or args.any? 
    192192        args.push(@attrs) 
  • trunk/lib/hpricot/elements.rb

    r144 r145  
    173173    end 
    174174 
    175     # Access a property on the first matched element. 
    176     def attr(k) 
    177       node = first 
    178       node.get_attribute(k) if node 
     175    def attr key, value = nil 
     176      if value 
     177        each do |el| 
     178          el.set_attribute(key, value) 
     179        end 
     180        return self       
     181      end     
     182      if key.is_a? Hash 
     183        key.each { |k,v| self.attr(k,v) } 
     184        return self 
     185      else 
     186        return self[0].get_attribute(key) 
     187      end 
     188    end 
     189   
     190    def add_class class_name 
     191      each do |el| 
     192        next unless el.respond_to? :get_attribute 
     193        classes = el.get_attribute('class').to_s.split(" ") 
     194        el.set_attribute('class', classes.push(class_name).uniq.join(" ")) 
     195      end 
     196      self 
    179197    end 
    180198 
  • trunk/lib/hpricot/traverse.rb

    r141 r145  
    811811    alias_method :[]=, :set_attribute 
    812812    def remove_attribute(name) 
     813      name = name.to_s 
    813814      if has_attribute? name 
    814815        altered! 
  • trunk/lib/hpricot/xchar.rb

    r139 r145  
    5252      34 => '"', # quotation mark 
    5353      38 => '&',  # ampersand 
    54       39 => ''', # apostrophe 
    5554      60 => '<',   # left angle bracket 
    5655      62 => '>'    # right angle bracket 
     
    8079    # XML escaped version of to_s 
    8180    def xs(str) 
    82       str.unpack('U*').map {|n| xchr(n)}.join # ASCII, UTF-8 
     81      str.to_s.unpack('U*').map {|n| xchr(n)}.join # ASCII, UTF-8 
    8382    rescue 
    84       str.unpack('C*').map {|n| xchr(n)}.join # ISO-8859-1, WIN-1252 
     83      str.to_s.unpack('C*').map {|n| xchr(n)}.join # ISO-8859-1, WIN-1252 
    8584    end 
    8685 
  • trunk/test/test_alter.rb

    r144 r145  
    2929   
    3030  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") 
     31    first_p = (@basic/"p:first").add_class("testing123") 
     32    assert first_p[0].get_attribute("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") 
    3535  end 
    3636   
    3737  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") 
     38    all_ps = (@basic/"p").attr("title", "Some Title") 
     39    all_as = (@basic/"a").attr("href", "http://my_new_href.com") 
    4040    assert_changed(@basic, "p", all_ps) {|p| p.attributes["title"] == "Some Title"} 
    4141    assert_changed(@basic, "a", all_as) {|a| a.attributes["href"] == "http://my_new_href.com"} 
     
    4343   
    4444  def assert_changed original, selector, set, &block 
    45     assert set.all? &block 
    46     assert Hpricot(original.to_html)[selector].all? &block 
     45    assert set.all?(&block) 
     46    assert Hpricot(original.to_html).search(selector).all?(&block) 
    4747  end 
    4848end