Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

last-child, etc, pseudo selectors don't work #354

Closed
paul opened this issue Oct 21, 2010 · 4 comments
Closed

last-child, etc, pseudo selectors don't work #354

paul opened this issue Oct 21, 2010 · 4 comments

Comments

@paul
Copy link

paul commented Oct 21, 2010

Example in this Gist, http://gist.github.com/639600, I'm trying to select the last DD having a certain class.

The nth-last-child(1) selector works, but last-child does not. Does nokogiri not implement the last-foo selectors?

@flavorjones
Copy link
Member

Sure we do:

puts Nokogiri::CSS.xpath_for("dd.foo:last-child")
# => //dd[contains(concat(' ', @class, ' '), ' foo ') and position() = last()]

I think the problem here is a misunderstanding of what last-child means in this context. If you use 'dd:last-child', it means "any element who is named dd and is the last child of its parent.". If you use 'dd.foo:last-child', it means "any element who is named dd and has the class 'foo' and is the last child of its parent. Your gist doesn't contain such an element, and so returns nothing. If you change the html in your gist to be:

<dl>
  <dt>Label</dd>
  <dd class=other>Other Value</dd>

  <dt>Label</dt>
  <dd class=foo>Value</dd>
  <dd class=foo>Trying to select this dd</dd>
  <dd class=foo>Other Value</dd>
</dl>

(note: the last DD now has class 'foo') you'll see that Nokogiri returns the correct element.

In the future, questions like this about CSS are better sent to the mailing list, so that people with similar questions might be able to more easily share in our conversation.

Thanks for using nokogiri!
-mike

@paul
Copy link
Author

paul commented Oct 25, 2010

Changing the class of the last dd to "foo" doesn't help, its not the one that I want to find. I'm trying to find the last dd having class foo. I understand what you're saying about :last-child, meaning "The dd with class foo that is the last child", but then why does :nth-last-child(1) work, when :last-child does not?

@flavorjones
Copy link
Member

Ah, OK, then that's a bug in nth-last-child, not a bug in last-child. I'll isolate where the problem is and will update this ticket.

(Reopening)

@flavorjones
Copy link
Member

fixing off-by-one with nth-last-child and nth-last-of-type CSS selectors when NOT using an+b notation. closed by bb3cab9.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants