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

:nth-last-child selector incorrectly starts at 0 instead of 1 #46

Closed
calebj0seph opened this issue Dec 19, 2014 · 3 comments
Closed

:nth-last-child selector incorrectly starts at 0 instead of 1 #46

calebj0seph opened this issue Dec 19, 2014 · 3 comments

Comments

@calebj0seph
Copy link

Using cssselect 0.9.1, the :nth-last-child selector starts from 0 instead of 1, that is, :nth-last-child(0) selects the last element when it should instead select nothing.

Here is an example using lxml:

import lxml.html

html_fragment = lxml.html.fromstring("""
<div>
    <p>First</p>
    <p>Second</p>
    <p>Second Last</p>
    <p>Last</p>
</div>
""")

for element in html_fragment.cssselect("div > p:nth-last-child(1)"):
    print(element.text_content())

print()

for element in html_fragment.cssselect("div > p:nth-last-child(2n)"):
    print(element.text_content())

Output:

Second Last

Second
Last

Expected output:

Last

First
Second Last
@SimonSapin
Copy link
Contributor

Thanks for the report! To be honest I’m unlikely to fix this myself. I believe the entire approach of translating Selectors to XPath is flawed (see #12 (comment) for example), and so I don’t really want to do more work on cssselect. I’ve started cssselect2 that implements Selectors "for real", but it’s blocked on some non-obvious design decisions: Kozea/cssselect2#1

I’d review a pull request, though.

@redapple
Copy link
Contributor

@Dobz , #42 also fixes your example. It doesn't fix #12 though

@redapple
Copy link
Contributor

redapple commented Sep 9, 2016

Fixed in #60

@redapple redapple closed this as completed Sep 9, 2016
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

3 participants