rails/rails-dom-testing

assert_select_encoded can't handle escaped characters within XML

dnrce opened this issue · 4 comments

Escaped characters within XML (as required by the spec) aren't handled properly by assert_select_encoded. For example, no p element is found within the following:

<element>&lt;p&gt;Paragraph text&lt;/p&gt;</element>

The same content using CDATA has no issues.

See https://gist.github.com/dnrce/9e48173bda64ea1ee6ed to reproduce.

This looks like escaped content and not encoded CDATA content. As far as I know, assert_select_encoded is only supposed to work on CDATA elements — see the tests here:

def test_feed_item_encoded
render_xml <<-EOF
<rss version="2.0">
<channel>
<item>
<description>
<![CDATA[
<p>Test 1</p>
]]>
</description>
</item>
<item>
<description>
<![CDATA[
<p>Test 2</p>
]]>
</description>
</item>
</channel>
</rss>
EOF
assert_select "channel item description" do
assert_select_encoded do
assert_select "p", :count=>2, :text=>/Test/
end
# Test individually.
assert_select "description" do |elements|
assert_select_encoded elements[0] do
assert_select "p", "Test 1"
end
assert_select_encoded elements[1] do
assert_select "p", "Test 2"
end
end
end
# Test that we only un-encode element itself.
assert_select "channel item" do
assert_select_encoded do
assert_select "p", 0
end
end
end
.

Escaped content is the default output of the XML builder used by Action View, so it seems like a miss to be unable to test that easily. I'll see if I can write a test case that uses the builder directly.

@kaspth, I updated my gist to include tests based on builder output.

I should also mention that I ran into this during a Rails 4.2 upgrade -- the version of the method built into Action Pack 4.1 has no trouble with this because it uses CGI.unescapeHTML. So although rails-dom-testing is new in the 4.2 ecosystem, in a way this is a regression.

https://github.com/rails/rails/blob/13dfc8fbdc273e11f28dffc2e3aca10cb43d3a82/actionpack/lib/action_dispatch/testing/assertions/selector.rb#L382