Skip to content

Commit

Permalink
Clean up normalize_whitespace / to_regexp implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
joliss committed Jul 31, 2012
1 parent e561d24 commit 33cc2ca
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions lib/capybara/node/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

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

3 comments on commit 33cc2ca

@jnicklas
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@joliss
Copy link
Collaborator Author

@joliss joliss commented on 33cc2ca Aug 1, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@jnicklas
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.