Skip to content

.first() returns wrong results  #274

Open
@clementinelove

Description

@clementinelove

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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions