Changeset 144
- Timestamp:
- 05/27/2007 23:39:50 (18 months ago)
- Location:
- trunk
- Files:
-
- 4 modified
-
lib/hpricot.rb (modified) (1 diff)
-
lib/hpricot/elements.rb (modified) (5 diffs)
-
test/test_alter.rb (modified) (3 diffs)
-
test/test_parser.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/hpricot.rb
r123 r144 25 25 require 'hpricot/parse' 26 26 require 'hpricot/builder' 27 require 'hpricot/hpricot_functions' -
trunk/lib/hpricot/elements.rb
r143 r144 51 51 # and Hpricot::Container::Trav. 52 52 class Elements < Array 53 53 54 # Searches this list for any elements (or children of these elements) matching 54 55 # the CSS or XPath expression +expr+. Root is assumed to be the element scanned. … … 203 204 BRACK_RE = %r!(\[) *([^\]]*) *\]+!i 204 205 FUNC_RE = %r!(:)?([a-zA-Z0-9\*_-]*)\( *[\"']?([^ \)]*?)['\"]? *\)! 206 CUST_RE = %r!(:)([a-zA-Z0-9\*_-]*)()! 205 207 CATCH_RE = %r!([:\.#]*)([a-zA-Z0-9\*_-]+)! 206 208 207 209 def self.filter(nodes, expr, truth = true) 208 210 until expr.empty? 209 _, *m = *expr.match(/^(?:#{ATTR_RE}|#{BRACK_RE}|#{FUNC_RE}|#{C ATCH_RE})/)211 _, *m = *expr.match(/^(?:#{ATTR_RE}|#{BRACK_RE}|#{FUNC_RE}|#{CUST_RE}|#{CATCH_RE})/) 210 212 break unless _ 211 213 … … 222 224 if m[0] == ":" && m[1] == "not" 223 225 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])] 224 232 else 225 233 meth = "filter[#{m[0]}#{m[1]}]" unless m[0].empty? … … 352 360 filter ':nth-child' do |arg,i| 353 361 case arg 354 when 'even'; parent.containers.index(self) % 2 == 0355 when 'odd'; parent.containers.index(self) % 2 == 1356 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]) 357 365 end 358 366 end … … 401 409 html.include? arg 402 410 end 411 412 403 413 404 414 pred_procs = -
trunk/test/test_alter.rb
r132 r144 6 6 7 7 class TestAlter < Test::Unit::TestCase 8 def setup 9 @basic = Hpricot.parse(TestFiles::BASIC) 10 end 11 8 12 def test_before 9 @basic = Hpricot.parse(TestFiles::BASIC)10 13 test0 = "<link rel='stylesheet' href='test0.css' />" 11 14 @basic.at("link").before(test0) … … 14 17 15 18 def test_after 16 @basic = Hpricot.parse(TestFiles::BASIC)17 19 test_inf = "<link rel='stylesheet' href='test_inf.css' />" 18 20 @basic.search("link")[-1].after(test_inf) … … 21 23 22 24 def test_wrap 23 @basic = Hpricot.parse(TestFiles::BASIC)24 25 ohmy = (@basic/"p.ohmy").wrap("<div id='wrapper'></div>") 25 26 assert_equal 'wrapper', ohmy[0].parent['id'] 26 27 assert_equal 'ohmy', Hpricot(@basic.to_html).at("#wrapper").children[0]['class'] 27 28 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 28 48 end -
trunk/test/test_parser.rb
r143 r144 177 177 def test_alt_predicates 178 178 @boingboing = Hpricot.parse(TestFiles::BOINGBOING) 179 assert_equal 2, @boingboing.search('//table/tr:last').length179 assert_equal 1, @boingboing.search('//table/tr:last').length 180 180 181 181 @basic = Hpricot.parse(TestFiles::BASIC)
