From 18c60f4e51568c9a92fafe4b3c3d2ec0bdde14db Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 23 Feb 2009 20:52:10 -0500 Subject: [PATCH] Refactoring XPath manipulation --- lib/webrat/core/matchers/have_selector.rb | 4 ++- lib/webrat/core/matchers/have_xpath.rb | 38 +++++++++++------------ 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/webrat/core/matchers/have_selector.rb b/lib/webrat/core/matchers/have_selector.rb index d02dceb2..9b38ead9 100644 --- a/lib/webrat/core/matchers/have_selector.rb +++ b/lib/webrat/core/matchers/have_selector.rb @@ -36,7 +36,9 @@ def tag_inspect end def query - Nokogiri::CSS::Parser.parse(@expected.to_s).map { |ast| ast.to_xpath }.first + Nokogiri::CSS::Parser.parse(@expected.to_s).map do |ast| + ast.to_xpath + end.first end end diff --git a/lib/webrat/core/matchers/have_xpath.rb b/lib/webrat/core/matchers/have_xpath.rb index 75bf698a..2ddad4b1 100644 --- a/lib/webrat/core/matchers/have_xpath.rb +++ b/lib/webrat/core/matchers/have_xpath.rb @@ -37,20 +37,7 @@ def rexml_matches(stringlike) @query = query end - attribute_conditions = [] - - @options.each do |key, value| - next if [:content, :count].include?(key) - attribute_conditions << "@#{key} = #{xpath_escape(value)}" - end - - if attribute_conditions.any? - @query.first << "[#{attribute_conditions.join(' and ')}]" - end - - if @options[:content] - @query.first << "[contains(., #{xpath_escape(@options[:content])})]" - end + add_options_conditions_to(@query.first) @document = Webrat.rexml_document(stringlike) @@ -70,6 +57,18 @@ def nokogiri_matches(stringlike) @query = query end + add_options_conditions_to(@query.first) + + @document = Webrat::XML.document(stringlike) + @document.xpath(*@query) + end + + def add_options_conditions_to(query) + add_attributes_conditions_to(query) + add_content_condition_to(query) + end + + def add_attributes_conditions_to(query) attribute_conditions = [] @options.each do |key, value| @@ -78,15 +77,14 @@ def nokogiri_matches(stringlike) end if attribute_conditions.any? - @query.first << "[#{attribute_conditions.join(' and ')}]" + query << "[#{attribute_conditions.join(' and ')}]" end - + end + + def add_content_condition_to(query) if @options[:content] - @query.first << "[contains(., #{xpath_escape(@options[:content])})]" + query << "[contains(., #{xpath_escape(@options[:content])})]" end - - @document = Webrat::XML.document(stringlike) - @document.xpath(*@query) end def query