Skip to content

Instantly share code, notes, and snippets.

@timdiggins
Created May 26, 2012 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timdiggins/2792715 to your computer and use it in GitHub Desktop.
Save timdiggins/2792715 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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