Changeset 127
- Timestamp:
- 03/30/2007 11:42:29 (20 months ago)
- Location:
- trunk
- Files:
-
- 4 modified
-
lib/hpricot/tag.rb (modified) (2 diffs)
-
lib/hpricot/traverse.rb (modified) (3 diffs)
-
test/test_parser.rb (modified) (1 diff)
-
test/test_preserved.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/hpricot/tag.rb
r126 r127 50 50 end 51 51 def empty?; @children.empty? end 52 [:name, : attributes, :parent, :altered!].each do |m|52 [:name, :raw_attributes, :parent, :altered!].each do |m| 53 53 [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 54 62 end 55 63 def to_plain_text … … 86 94 def initialize(name, attributes=nil) 87 95 @name = name.to_s.downcase 88 @ attributes = {}96 @raw_attributes = {} 89 97 if attributes 90 @ attributes = attributes.inject({}) { |hsh,(k,v)| hsh[k.to_s.downcase] = v; hsh }91 end 92 end 93 alterable :name, : attributes98 @raw_attributes = attributes.inject({}) { |hsh,(k,v)| hsh[k.to_s.downcase] = v; hsh } 99 end 100 end 101 alterable :name, :raw_attributes 94 102 def attributes_as_html 95 if @ attributes96 @ attributes.map do |aname, aval|103 if @raw_attributes 104 @raw_attributes.map do |aname, aval| 97 105 " #{aname}" + 98 (aval ? "= #{html_quote(aval)}" : "")106 (aval ? "=\"#{aval}\"" : "") 99 107 end.join 100 108 end -
trunk/lib/hpricot/traverse.rb
r126 r127 774 774 module Elem::Trav 775 775 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) 777 777 end 778 778 def get_attribute(name) 779 a = self. attributes && self.attributes[name.to_s]779 a = self.raw_attributes && self.raw_attributes[name.to_s] 780 780 a = Hpricot.uxs(a) if a 781 781 a … … 784 784 def set_attribute(name, val) 785 785 altered! 786 self. attributes ||= {}787 self. attributes[name.to_s] = val786 self.raw_attributes ||= {} 787 self.raw_attributes[name.to_s] = Hpricot.xs(val) 788 788 end 789 789 alias_method :[]=, :set_attribute … … 791 791 if has_attribute? name 792 792 altered! 793 self. attributes.delete(name)793 self.raw_attributes.delete(name) 794 794 end 795 795 end -
trunk/test/test_parser.rb
r116 r127 22 22 def test_scan_text 23 23 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'] 24 32 end 25 33 -
trunk/test/test_preserved.rb
r126 r127 39 39 end 40 40 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 41 46 def test_files 42 47 assert_roundtrip TestFiles::BASIC … … 48 53 # ampersands in URLs 49 54 str = %{<a href="http://google.com/search?q=hpricot&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&l=en", link.raw_attributes['href'] 52 60 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("AGGA-KA-BOO!")">Google</a>}, doc.to_html 53 65 end 54 66 end
