Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick demo of problem with css attribute search with slash
require 'test/unit'
require 'nokogiri'
class AttributeWithSlashCssTest < Test::Unit::TestCase
def setup
@doc = Nokogiri.parse(
<<-END
<html><body>
<a href=\"/my/path\">My link text</a>
<a href=\"/my-other-path\">
</body></html>
END
)
end
#these ones work fine
def test_1_should_be_able_to_retrieve_all_all_href_with_ending1
nodes = @doc.css('a[href$=my-other-path]')
assert_equal(1, nodes.length)
end
#these don't
#but I think they should?
# see http://www.w3.org/TR/CSS2/selector.html#matching-attrs
def test_2_should_be_able_to_retrieve_all_all_href_with_starting_slash
nodes = @doc.css('a[href=/my-other-path]')
assert_equal(1, nodes.length)
end
def test_3_should_be_able_to_retrieve_all_all_href_with_ending_containing_slash
nodes = @doc.css('a[href$=my/path]')
assert_equal(1, nodes.length)
end
#but actually these do, so it's not such a big problem after all
def test_4_should_be_able_to_retrieve_all_all_href_with_starting_slash_with_double_quotes
nodes = @doc.css('a[href="/my-other-path"]')
assert_equal(1, nodes.length)
end
def test_5_should_be_able_to_retrieve_all_all_href_with_starting_slash_with_quotes
nodes = @doc.css("a[href='/my-other-path']")
assert_equal(1, nodes.length)
end
end
@timdiggins

This comment has been minimized.

Copy link
Owner Author

@timdiggins timdiggins commented May 26, 2012

apologies to the Prague judges... just noticed (since posting to nokogiri-discuss) that actually it attr selection works with quotes or dquotes, so no big deal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.