Created
May 26, 2012 07:14
-
-
Save timdiggins/2792715 to your computer and use it in GitHub Desktop.
Quick demo of problem with css attribute search with slash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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