Changeset 127

Show
Ignore:
Timestamp:
03/30/2007 11:42:29 (20 months ago)
Author:
why
Message:
  • lib/hpricot: raw_attributes and attributes, as suggested on the ml.
Location:
trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/hpricot/tag.rb

    r126 r127  
    5050    end 
    5151    def empty?; @children.empty? end 
    52     [:name, :attributes, :parent, :altered!].each do |m| 
     52    [:name, :raw_attributes, :parent, :altered!].each do |m| 
    5353      [m, "#{m}="].each { |m2| define_method(m2) { |*a| [@etag, @stag].inject { |_,t| t.send(m2, *a) if t and t.respond_to?(m2) } } } 
     54    end 
     55    def attributes 
     56      if raw_attributes 
     57        raw_attributes.inject({}) do |hsh, (k, v)| 
     58          hsh[k] = Hpricot.uxs(v) 
     59          hsh 
     60        end 
     61      end 
    5462    end 
    5563    def to_plain_text 
     
    8694    def initialize(name, attributes=nil) 
    8795      @name = name.to_s.downcase 
    88       @attributes = {} 
     96      @raw_attributes = {} 
    8997      if attributes 
    90         @attributes = attributes.inject({}) { |hsh,(k,v)| hsh[k.to_s.downcase] = v; hsh } 
    91       end 
    92     end 
    93     alterable :name, :attributes 
     98        @raw_attributes = attributes.inject({}) { |hsh,(k,v)| hsh[k.to_s.downcase] = v; hsh } 
     99      end 
     100    end 
     101    alterable :name, :raw_attributes 
    94102    def attributes_as_html 
    95       if @attributes 
    96         @attributes.map do |aname, aval| 
     103      if @raw_attributes 
     104        @raw_attributes.map do |aname, aval| 
    97105          " #{aname}" + 
    98             (aval ? "=#{html_quote(aval)}" : "") 
     106            (aval ? "=\"#{aval}\"" : "") 
    99107        end.join 
    100108      end 
  • trunk/lib/hpricot/traverse.rb

    r126 r127  
    774774  module Elem::Trav 
    775775    def has_attribute?(name) 
    776       self.attributes && self.attributes.has_key?(name.to_s) 
     776      self.raw_attributes && self.raw_attributes.has_key?(name.to_s) 
    777777    end 
    778778    def get_attribute(name) 
    779       a = self.attributes && self.attributes[name.to_s] 
     779      a = self.raw_attributes && self.raw_attributes[name.to_s] 
    780780      a = Hpricot.uxs(a) if a 
    781781      a 
     
    784784    def set_attribute(name, val) 
    785785      altered! 
    786       self.attributes ||= {} 
    787       self.attributes[name.to_s] = val 
     786      self.raw_attributes ||= {} 
     787      self.raw_attributes[name.to_s] = Hpricot.xs(val) 
    788788    end 
    789789    alias_method :[]=, :set_attribute 
     
    791791      if has_attribute? name 
    792792        altered! 
    793         self.attributes.delete(name) 
     793        self.raw_attributes.delete(name) 
    794794      end 
    795795    end 
  • trunk/test/test_parser.rb

    r116 r127  
    2222  def test_scan_text 
    2323    assert_equal 'FOO', Hpricot.make("FOO").first.content 
     24  end 
     25 
     26  def test_filter_by_attr 
     27    @boingboing = Hpricot.parse(TestFiles::BOINGBOING) 
     28 
     29    # this link is escaped in the doc 
     30    link = 'http://www.youtube.com/watch?v=TvSNXyNw26g&search=chris%20ware' 
     31    assert_equal link, @boingboing.at("a[@href='#{link}']")['href'] 
    2432  end 
    2533 
  • trunk/test/test_preserved.rb

    r126 r127  
    3939  end 
    4040 
     41  def test_escaping_of_contents 
     42    doc = Hpricot(TestFiles::BOINGBOING) 
     43    assert_equal "Fukuda\342\200\231s Automatic Door opens around your body as you pass through it. The idea is to save energy and keep the room clean.", doc.at("img[@alt='200606131240']").next.to_s.strip 
     44  end 
     45 
    4146  def test_files 
    4247    assert_roundtrip TestFiles::BASIC 
     
    4853    # ampersands in URLs 
    4954    str = %{<a href="http://google.com/search?q=hpricot&amp;l=en">Google</a>} 
    50     doc = Hpricot(str) 
    51     assert_equal "http://google.com/search?q=hpricot&l=en", doc.at(:a)['href'] 
     55    link = (doc = Hpricot(str)).at(:a) 
     56    assert_equal "http://google.com/search?q=hpricot&l=en", link['href'] 
     57    assert_equal "http://google.com/search?q=hpricot&l=en", link.attributes['href'] 
     58    assert_equal "http://google.com/search?q=hpricot&l=en", link.get_attribute('href') 
     59    assert_equal "http://google.com/search?q=hpricot&amp;l=en", link.raw_attributes['href'] 
    5260    assert_equal str, doc.to_html 
     61 
     62    # alter the url 
     63    link['href'] = "javascript:alert(\"AGGA-KA-BOO!\")" 
     64    assert_equal %{<a href="javascript:alert(&quot;AGGA-KA-BOO!&quot;)">Google</a>}, doc.to_html 
    5365  end 
    5466end