Fix returning invisible text on a hidden page

Commit 2f3832fa15 introduced a bug wherein
if the entire page has hidden content, the entire page text is returned
as visible text. The bug was introduced to support SVG text handling.
The svg element doesn't have an innerText property, but does have
textContent property, so the patch was written to fallback on
textContent. The thing is, innerText will return an empty string when
there is no visible text on the screen, which is a falsey condition, so
we fall back to textContent. Thankfully, calling innerText on an svg
element returns undefined, so now we can confidently return the empty
string when it exists before falling back to textContent.

The patch could also be written as:

    return visible_text == undefined ? node.textContent : visible_text;

(double equals should work fine in this case)
This commit is contained in:
Sandro Turriate 2015-01-20 15:50:32 -08:00
parent 0b67f67d0f
commit aab340ae8d
2 changed files with 20 additions and 1 deletions

View File

@ -561,6 +561,24 @@ describe Capybara::Webkit::Driver do
end
end
context "hidden text app" do
let(:driver) do
driver_for_html(<<-HTML)
<html>
<body>
<h1 style="display: none">Hello</h1>
</body>
</html>
HTML
end
before { visit("/") }
it "has no visible text" do
driver.find_xpath("/html").first.text.should be_empty
end
end
context "console messages app" do
let(:driver) do
driver_for_html(<<-HTML)

View File

@ -71,7 +71,8 @@ Capybara = {
} else if (type == "textarea") {
return node.innerHTML;
} else {
return node.innerText || node.textContent;
visible_text = node.innerText;
return typeof visible_text === "string" ? visible_text : node.textContent;
}
},