Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Make Node#text work for svg elements #438

Closed
wants to merge 1 commit into from

3 participants

@betelgeuse

svg elements don't return to the non standard innerText attribute so
fall back on the standard textContent when innerText is null.

Fixes #437.

@betelgeuse

@mhoran any problems with this as it wasn't included in the latest release?

@mhoran
Collaborator

Sorry, I'd looked into this but failed to follow up. I'd prefer we use the standards compliant method in all cases to have consistent behavior, however when I tried changing that, the Capybara integration tests failed. I focused on Capybara 2.0 compatibility for this release, but we can get this out in the next feature release.

@betelgeuse

The string coming out of innerText is in a different format than textContext so I did it this way in order to not break backwards compatibility. If changing behavior is ok then we can just call textContext. I don't any more remember how the return values differed but I do remember that innerText looked a little better.

@mhoran
Collaborator

Instead of ||, could we detect an SVG element as we do for textareas: http://git.io/_Ts-tQ?

@betelgeuse

It could be done assuming we have access to the namespace information. Do you mean this just as a stylistic issue? innertText is always null for svg elements.

@mhoran
Collaborator

Partly stylistic, but also to ensure that there is no case where innerText was returning nil and now returns something because of textContent.

@betelgeuse betelgeuse Make Node#text work for svg elements
svg elements don't return to the non standard innerText attribute so
fall back on the standard textContent when innerText is null.

Fixes #437.
a01bea5
@betelgeuse

@mhoran I pushed a version that detects if the elements are SVG so for html elements nothing will change. I am not especially fond of this solution but it will solve the problem for svg without any worriers for backwards compatibility.

@betelgeuse

Also I rebased against the latest master to verify it works there.

@mhoran
Collaborator

What about if (type == "svg")?

type == "text" for for example the element in the spec

@mhoran
Collaborator

In that case, perhaps we should go with the original implementation of innerText || textContent. I don't like it, but I don't see a better solution. @jferris, thoughts?

@jferris
Admin

I'm fine with that as a solution. Does that mean we won't need to check the element type anymore? I think I prefer that anyway.

@betelgeuse

@jferris yes no other properties of the node need to be checked. The original commit is at 8ba1696.

@jferris
Admin

Yeah, I think 8ba1696 will be fine.

@mhoran
Collaborator

Merged 8ba1696. Thanks!

@mhoran mhoran closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 21, 2013
  1. @betelgeuse

    Make Node#text work for svg elements

    betelgeuse authored
    svg elements don't return to the non standard innerText attribute so
    fall back on the standard textContent when innerText is null.
    
    Fixes #437.
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 1 deletion.
  1. +20 −0 spec/driver_spec.rb
  2. +4 −1 src/capybara.js
View
20 spec/driver_spec.rb
@@ -472,6 +472,26 @@ def visit(url, driver=driver)
end
end
+ context "svg app" do
+ let(:driver) do
+ driver_for_html(<<-HTML)
+ <html>
+ <body>
+ <svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="100">
+ <text x="10" y="25" fill="navy" font-size="15" id="navy_text">In the navy!</text>
+ </svg>
+ </body>
+ </html>
+ HTML
+ end
+
+ before { visit("/") }
+
+ it "should handle text for svg elements" do
+ driver.find("//*[@id='navy_text']").first.text.should == "In the navy!"
+ end
+ end
+
context "console messages app" do
let(:driver) do
driver_for_html(<<-HTML)
View
5 src/capybara.js
@@ -38,7 +38,10 @@ Capybara = {
text: function (index) {
var node = this.nodes[index];
var type = (node.type || node.tagName).toLowerCase();
- if (type == "textarea") {
+ // SVG elements do not have the non standardized innerText attribute
+ if (node.namespaceURI == 'http://www.w3.org/2000/svg') {
+ return node.textContent;
+ } else if (type == "textarea") {
return node.innerHTML;
} else {
return node.innerText;
Something went wrong with that request. Please try again.