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

.first() returns wrong results #274

Open
clementinelove opened this issue Jul 11, 2024 · 1 comment
Open

.first() returns wrong results #274

clementinelove opened this issue Jul 11, 2024 · 1 comment

Comments

@clementinelove
Copy link

clementinelove commented Jul 11, 2024

Please see the example below:

    func testExample() throws {
        let html = """
<div class="models">
<a class="model" href="https://cat.com">
<img title="" data-original-title="NAME-A">
</a>
<a class="model" href="https://duck.com">
<span title="" data-original-title="NAME-B">
</a>
</div>
"""
        let doc: Document = try SwiftSoup.parse(html)
        let creatives = try doc.select(".models .model")
        for creative in creatives {
            let name = try creative.select(":first-child")
            print(try name.attr("data-original-title"))
        }
        
    }

The code above works correctly and prints

NAME-A
NAME-B

However, if the creative.select(":first-child") part was replaced by creative.select(":first-child").first()!, then the first child will return the parent element a:

...
        for creative in creatives {
            let name = try creative.select(":first-child").first()!
            print(try name.attr("data-original-title"))
        }
...

prints an empty string and NAME-B


NAME-B
@aehlke
Copy link
Collaborator

aehlke commented Aug 19, 2024

I can't fix this for you right now but if you want to submit a PR with test coverage, I'll merge it, thanks

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

No branches or pull requests

2 participants