Refactoring XPath manipulation

This commit is contained in:
Bryan Helmkamp 2009-02-23 20:52:10 -05:00
parent 29c40bd73c
commit 18c60f4e51
2 changed files with 21 additions and 21 deletions

View File

@ -36,7 +36,9 @@ module Webrat
end end
def query 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
end end

View File

@ -37,20 +37,7 @@ module Webrat
@query = query @query = query
end end
attribute_conditions = [] add_options_conditions_to(@query.first)
@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
@document = Webrat.rexml_document(stringlike) @document = Webrat.rexml_document(stringlike)
@ -70,6 +57,18 @@ module Webrat
@query = query @query = query
end 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 = [] attribute_conditions = []
@options.each do |key, value| @options.each do |key, value|
@ -78,15 +77,14 @@ module Webrat
end end
if attribute_conditions.any? if attribute_conditions.any?
@query.first << "[#{attribute_conditions.join(' and ')}]" query << "[#{attribute_conditions.join(' and ')}]"
end
end end
def add_content_condition_to(query)
if @options[:content] if @options[:content]
@query.first << "[contains(., #{xpath_escape(@options[:content])})]" query << "[contains(., #{xpath_escape(@options[:content])})]"
end end
@document = Webrat::XML.document(stringlike)
@document.xpath(*@query)
end end
def query def query