cezheng / Fuzi

A fast & lightweight XML & HTML parser in Swift with XPath & CSS support
MIT License
1.06k stars 151 forks source link

Printing fails after outgoing a return value, and the content of the value is missing #126

Open baobao9999 opened 6 months ago

baobao9999 commented 6 months ago

Description:

After calling this method, the array of XMLElement is obtained, and it can be printed normally in the current method, but after the return value is obtained, when the getElements method is called, an error will be reported when arr is printed

Fuzi/Node.swift:122: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value Fuzi/Node.swift:122: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

ele.attr("href") === ""

ele.stringValue == ""

private func getResult(_ xPath: String) -> Array { let node : Fuzi.HTMLDocument = htmlNode

        let nodeset = node.xpath(xPath)

         let array = nodeset.map { $0 }

         return array

}

public func getElements(_ xPath: String) -> Array<XMLElement>? {
    if xPath.isEmpty { return [] }
    do {
        var jxNodes = Array<XMLElement>()
        let ruleAnalyzes = RuleAnalyzer(xPath)
        let rules = try ruleAnalyzes.splitRule("&&", "||", "%%")

        if (rules.count == 1) {

            if let arr = getResult(rules[0]){

                return arr
            }}

    }
}

ele.stringValue == "test 1"

ele.attr("href") === ""

ele.stringValue == ""

Environment

How to reproduce:

xpath