Skip to content

Commit

Permalink
Merge pull request teamcapybara#1309 from twalpole/visible_optimization
Browse files Browse the repository at this point in the history
Optimization of Capybara::RackTest::Node#unnormalized_text while checking visibility
  • Loading branch information
twalpole committed May 21, 2014
2 parents f57a30b + 41f2feb commit 3c5dd37
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
11 changes: 9 additions & 2 deletions lib/capybara/node/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,17 @@ def value
# Whether or not the element is visible. Does not support CSS, so
# the result may be inaccurate.
#
# @param [Boolean] check_ancestors Whether to inherit visibility from ancestors
# @return [Boolean] Whether the element is visible
#
def visible?
native.xpath("./ancestor-or-self::*[contains(@style, 'display:none') or contains(@style, 'display: none') or @hidden or name()='script' or name()='head']").size == 0
def visible?(check_ancestors = true)
if check_ancestors
#check size because oldest supported nokogiri doesnt support xpath boolean() function
native.xpath("./ancestor-or-self::*[contains(@style, 'display:none') or contains(@style, 'display: none') or @hidden or name()='script' or name()='head']").size() == 0
else
#no need for an xpath if only checking the current element
!(native.has_attribute?('hidden') || (native[:style] =~ /display:\s?none/) || %w(script head).include?(tag_name))
end
end

##
Expand Down
6 changes: 3 additions & 3 deletions lib/capybara/rack_test/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ def ==(other)

protected

def unnormalized_text
if !visible?
def unnormalized_text(check_ancestor_visibility = true)
if !string_node.visible?(check_ancestor_visibility)
''
elsif native.text?
native.text
elsif native.element?
native.children.map do |child|
Capybara::RackTest::Node.new(driver, child).unnormalized_text
Capybara::RackTest::Node.new(driver, child).unnormalized_text(false)
end.join
else
''
Expand Down
5 changes: 5 additions & 0 deletions lib/capybara/spec/session/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
Capybara.ignore_hidden_elements = false
expect(@session.find(:id, "hidden-text").text).to eq('Some of this text is')
end

it "ignores invisible text if ancestor is invisible" do
@session.visit('/with_html')
expect(@session.find(:id, "hidden_via_ancestor", visible: false).text).to eq('')
end

context "with css as default selector" do
before { Capybara.default_selector = :css }
Expand Down

0 comments on commit 3c5dd37

Please sign in to comment.