Skip to content

Commit

Permalink
Refactoring XPath manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Feb 24, 2009
1 parent 29c40bd commit 18c60f4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
4 changes: 3 additions & 1 deletion lib/webrat/core/matchers/have_selector.rb
Expand Up @@ -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
Expand Down
38 changes: 18 additions & 20 deletions lib/webrat/core/matchers/have_xpath.rb
Expand Up @@ -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)

Expand All @@ -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|
Expand All @@ -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
Expand Down

0 comments on commit 18c60f4

Please sign in to comment.