Permalink
Browse files

Clean up normalize_whitespace / to_regexp implementation

  • Loading branch information...
1 parent e561d24 commit 33cc2cade1f640cae4593cd0f7aab916a2ebc4d8 @joliss joliss committed Jul 31, 2012
Showing with 5 additions and 9 deletions.
  1. +5 −9 lib/capybara/node/matchers.rb
@@ -200,10 +200,8 @@ def has_no_css?(path, options={})
# @return [Boolean] Whether it exists
#
def has_text?(content)
- normalized_content = normalize_whitespace(content)
-
synchronize do
- normalize_whitespace(text).match(escape_regexp(normalized_content)) or
+ normalize_whitespace(text).match(to_regexp(content)) or
raise ExpectationNotMet
end
return true
@@ -224,10 +222,8 @@ def has_text?(content)
# @return [Boolean] Whether it exists
#
def has_no_text?(content)
- normalized_content = normalize_whitespace(content)
-
synchronize do
- !normalize_whitespace(text).match(escape_regexp(normalized_content)) or
+ !normalize_whitespace(text).match(to_regexp(content)) or
raise ExpectationNotMet
end
return true
@@ -472,7 +468,7 @@ def ==(other)
# @return [String] Normalized text
#
def normalize_whitespace(text)
- text.is_a?(Regexp) ? text : text.to_s.gsub(/\s+/, ' ').strip
+ text.to_s.gsub(/\s+/, ' ').strip
end
##
@@ -483,8 +479,8 @@ def normalize_whitespace(text)
# @param [String] text Text to escape
# @return [String] Escaped text
#
- def escape_regexp(text)
- text.is_a?(Regexp) ? text : Regexp.escape(text)
+ def to_regexp(text)
+ text.is_a?(Regexp) ? text : Regexp.escape(normalize_whitespace(text))
end
end
end

3 comments on commit 33cc2ca

Collaborator

jnicklas replied Jul 31, 2012

Seems like this should fix #767, right? Should we maybe add a test case for the failing behaviour?

Collaborator

joliss replied Aug 1, 2012

No it doesn't, it's just preliminary cleanup. We need to use [\s\u00a0] in several places because Ruby is stupid. I have a test and half a fix in my repo, but I need to move normalize_whitespace to a place where it's accessible by other methods, notably RackTest::Node#text. Any ideas? Should I create a Capybara::Helpers module?

Collaborator

jnicklas replied Aug 1, 2012

That might be the best option. Though really, why doesn't Ruby do this out of the box? Sigh.

Please sign in to comment.