1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/capybara/helpers.rb
Jo Liss b2767a8413 Make whitespace normalization Unicode-aware. Fixes #767
Also move normalize_whitespace and to_regexp into Capybara::Helpers, and
improve method documentation.

I'm not adding tests for the helpers. I think they're so simple that we
really only care whether they work in conjunction with other methods,
like Node#text.
2012-08-01 13:29:50 +02:00

33 lines
1 KiB
Ruby

module Capybara
module Helpers
class << self
##
#
# Normalizes whitespace space by stripping leading and trailing
# whitespace and replacing sequences of whitespace characters
# with a single space.
#
# @param [String] text Text to normalize
# @return [String] Normalized text
#
def normalize_whitespace(text)
# http://en.wikipedia.org/wiki/Whitespace_character#Unicode
# We should have a better reference.
# See also http://stackoverflow.com/a/11758133/525872
text.to_s.gsub(/[\s\u0085\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]+/, ' ').strip
end
##
#
# Escapes any characters that would have special meaning in a regexp
# if text is not a regexp
#
# @param [String] text Text to escape
# @return [String] Escaped text
#
def to_regexp(text)
text.is_a?(Regexp) ? text : Regexp.escape(normalize_whitespace(text))
end
end
end
end