move normalize_ws behavior to node#text methods

This commit is contained in:
Thomas Walpole 2018-08-03 09:12:06 -07:00
parent 82bbef80f2
commit 4a06bb9729
4 changed files with 17 additions and 17 deletions

View File

@ -20,8 +20,8 @@ module Capybara
#
# @return [String] The text of the document
#
def text(type = nil)
find(:xpath, '/html').text(type)
def text(type = nil, normalize_ws: false)
find(:xpath, '/html').text(type, normalize_ws: normalize_ws)
end
##

View File

@ -53,9 +53,10 @@ module Capybara
# @param type [:all, :visible] Whether to return only visible or all text
# @return [String] The text of the element
#
def text(type = nil)
def text(type = nil, normalize_ws: false)
type ||= :all unless session_options.ignore_hidden_elements || session_options.visible_text_only
synchronize { type == :all ? base.all_text : base.visible_text }
t = synchronize { type == :all ? base.all_text : base.visible_text }
normalize_ws ? t.gsub(/[[:space:]]+/, ' ').strip : t
end
##

View File

@ -28,8 +28,9 @@ module Capybara
#
# @return [String] The text of the element
#
def text(_type = nil)
native.text
def text(_type = nil, normalize_ws: false)
t = native.text
normalize_ws ? t.gsub(/[[:space:]]+/, ' ').strip : t
end
##

View File

@ -5,12 +5,7 @@ module Capybara
module Queries
class TextQuery < BaseQuery
def initialize(type = nil, expected_text, session_options:, **options) # rubocop:disable Style/OptionalArguments
@type = if type.nil?
Capybara.ignore_hidden_elements || Capybara.visible_text_only ? :visible : :all
else
type
end
@type = type.nil? ? default_type : type
@expected_text = expected_text.is_a?(Regexp) ? expected_text : expected_text.to_s
@options = options
super(@options)
@ -23,8 +18,7 @@ module Capybara
def resolve_for(node)
@node = node
@actual_text = text(node, @type)
@actual_text.gsub!(/[[:space:]]+/, ' ').strip! if options[:normalize_ws]
@actual_text = text
@count = @actual_text.scan(@search_regexp).size
end
@ -74,7 +68,7 @@ module Capybara
end
def invisible_message
invisible_text = text(@node, :all)
invisible_text = text(query_type: :all)
invisible_count = invisible_text.scan(@search_regexp).size
return if invisible_count == @count
"it was found #{invisible_count} #{Capybara::Helpers.declension('time', 'times', invisible_count)} including non-visible text"
@ -95,8 +89,12 @@ module Capybara
!@expected_text.is_a?(Regexp)
end
def text(node, query_type)
node.text(query_type)
def text(node: @node, query_type: @type)
node.text(query_type, normalize_ws: options[:normalize_ws])
end
def default_type
Capybara.ignore_hidden_elements || Capybara.visible_text_only ? :visible : :all
end
end
end