scinfu / SwiftSoup

SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS)
https://scinfu.github.io/SwiftSoup/
MIT License
4.49k stars 340 forks source link

`.first()` returns wrong results #274

Open clementinelove opened 2 months ago

clementinelove commented 2 months ago

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 commented 3 weeks ago

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