Changeset 145
- Timestamp:
- 05/31/2007 01:51:43 (18 months ago)
- Location:
- trunk
- Files:
-
- 6 modified
-
lib/hpricot.rb (modified) (1 diff)
-
lib/hpricot/builder.rb (modified) (2 diffs)
-
lib/hpricot/elements.rb (modified) (1 diff)
-
lib/hpricot/traverse.rb (modified) (1 diff)
-
lib/hpricot/xchar.rb (modified) (2 diffs)
-
test/test_alter.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/hpricot.rb
r144 r145 25 25 require 'hpricot/parse' 26 26 require 'hpricot/builder' 27 require 'hpricot/hpricot_functions' -
trunk/lib/hpricot/builder.rb
r140 r145 76 76 childs = [] 77 77 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) 79 85 attrs = attrs.inject({}) do |hsh, ath| 80 86 ath.each do |k, v| 81 hsh[k] = Hpricot.xs(v )87 hsh[k] = Hpricot.xs(v.to_s) if v 82 88 end 83 89 hsh … … 183 189 end 184 190 185 unless args.empty?186 if args.last.respond_to? :to_hash187 @attrs.merge! args.pop.to_hash188 end189 end190 191 191 if block or args.any? 192 192 args.push(@attrs) -
trunk/lib/hpricot/elements.rb
r144 r145 173 173 end 174 174 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 179 197 end 180 198 -
trunk/lib/hpricot/traverse.rb
r141 r145 811 811 alias_method :[]=, :set_attribute 812 812 def remove_attribute(name) 813 name = name.to_s 813 814 if has_attribute? name 814 815 altered! -
trunk/lib/hpricot/xchar.rb
r139 r145 52 52 34 => '"', # quotation mark 53 53 38 => '&', # ampersand 54 39 => ''', # apostrophe55 54 60 => '<', # left angle bracket 56 55 62 => '>' # right angle bracket … … 80 79 # XML escaped version of to_s 81 80 def xs(str) 82 str. unpack('U*').map {|n| xchr(n)}.join # ASCII, UTF-881 str.to_s.unpack('U*').map {|n| xchr(n)}.join # ASCII, UTF-8 83 82 rescue 84 str. unpack('C*').map {|n| xchr(n)}.join # ISO-8859-1, WIN-125283 str.to_s.unpack('C*').map {|n| xchr(n)}.join # ISO-8859-1, WIN-1252 85 84 end 86 85 -
trunk/test/test_alter.rb
r144 r145 29 29 30 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")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") 35 35 end 36 36 37 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")38 all_ps = (@basic/"p").attr("title", "Some Title") 39 all_as = (@basic/"a").attr("href", "http://my_new_href.com") 40 40 assert_changed(@basic, "p", all_ps) {|p| p.attributes["title"] == "Some Title"} 41 41 assert_changed(@basic, "a", all_as) {|a| a.attributes["href"] == "http://my_new_href.com"} … … 43 43 44 44 def assert_changed original, selector, set, &block 45 assert set.all? &block46 assert Hpricot(original.to_html) [selector].all? &block45 assert set.all?(&block) 46 assert Hpricot(original.to_html).search(selector).all?(&block) 47 47 end 48 48 end
