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

79 lines
2.6 KiB
Ruby
Raw Normal View History

2016-03-07 19:52:19 -05:00
# frozen_string_literal: true
module Capybara
# @api private
module Helpers
2013-03-17 10:48:04 -04:00
extend self
2013-03-17 10:48:04 -04:00
##
# @deprecated
2013-03-17 10:48:04 -04:00
# 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)
warn "DEPRECATED: Capybara::Helpers::normalize_whitespace is deprecated, please update your driver"
2013-03-17 10:48:04 -04:00
text.to_s.gsub(/[[:space:]]+/, ' ').strip
end
2013-02-19 17:57:34 -05:00
2013-03-17 10:48:04 -04:00
##
#
# Escapes any characters that would have special meaning in a regexp
# if text is not a regexp
#
# @param [String] text Text to escape
2018-02-27 18:46:13 -05:00
# @param [Boolean] exact (false) Whether or not this should be an exact text match
# @param [Fixnum, Boolean, nil] options Options passed to Regexp.new when creating the Regexp
# @return [Regexp] Regexp to match the passed in text and options
2013-03-17 10:48:04 -04:00
#
def to_regexp(text, exact: false, all_whitespace: false, options: nil)
2018-01-13 16:06:03 -05:00
return text if text.is_a?(Regexp)
escaped = Regexp.escape(text)
escaped = escaped.gsub("\\ ", "[[:blank:]]") if all_whitespace
2018-01-13 16:06:03 -05:00
escaped = "\\A#{escaped}\\z" if exact
2018-02-27 18:46:13 -05:00
Regexp.new(escaped, options)
2013-03-17 10:48:04 -04:00
end
##
#
# Injects a `<base>` tag into the given HTML code, pointing to
# `Capybara.asset_host`.
#
# @param [String] html HTML code to inject into
2018-02-27 18:46:13 -05:00
# @param [URL] host (Capybara.asset_host) The host from which assets should be loaded
2013-03-28 22:15:07 -04:00
# @return [String] The modified HTML code
2013-03-17 10:48:04 -04:00
#
2018-02-27 18:46:13 -05:00
def inject_asset_host(html, host: Capybara.asset_host)
if host && Nokogiri::HTML(html).css("base").empty?
match = html.match(/<head[^<]*?>/)
2018-02-27 18:46:13 -05:00
return html.clone.insert match.end(0), "<base href='#{host}' />" if match
2013-02-19 17:57:34 -05:00
end
html
end
2013-03-03 18:04:23 -05:00
2013-03-17 10:48:04 -04:00
##
#
# A poor man's `pluralize`. Given two declensions, one singular and one
# plural, as well as a count, this will pick the correct declension. This
2014-03-19 19:28:26 -04:00
# way we can generate grammatically correct error message.
2013-03-17 10:48:04 -04:00
#
# @param [String] singular The singular form of the word
# @param [String] plural The plural form of the word
# @param [Integer] count The number of items
#
def declension(singular, plural, count)
2018-01-13 16:06:03 -05:00
count == 1 ? singular : plural
2013-03-03 18:04:23 -05:00
end
if defined?(Process::CLOCK_MONOTONIC)
2018-02-27 18:46:13 -05:00
def monotonic_time; Process.clock_gettime Process::CLOCK_MONOTONIC; end
else
2018-02-27 18:46:13 -05:00
def monotonic_time; Time.now.to_f; end
end
2013-03-03 18:04:23 -05:00
end
end