diff --git a/Source/libxml/libxmlHTMLNode.swift b/Source/libxml/libxmlHTMLNode.swift index af710bf..ff726a2 100644 --- a/Source/libxml/libxmlHTMLNode.swift +++ b/Source/libxml/libxmlHTMLNode.swift @@ -43,11 +43,12 @@ internal final class libxmlHTMLNode: XMLElement { } var innerHTML: String? { - var html: String = "" - html += libxmlGetNodeText(nodePtr.memory.children) ?? "" - html += self.xpath(".//*").first?.toHTML ?? "" - html += libxmlGetNodeText(xmlGetLastChild(nodePtr)) ?? "" - return html + if let html = self.toHTML { + let inner = html.stringByReplacingOccurrencesOfString("$", withString: "", options: .RegularExpressionSearch, range: nil) + .stringByReplacingOccurrencesOfString("^<.*>", withString: "", options: .RegularExpressionSearch, range: nil) + return inner + } + return nil } var className: String? { @@ -162,16 +163,6 @@ internal final class libxmlHTMLNode: XMLElement { } } -private func libxmlGetNodeText(nodePtr: xmlNodePtr) -> String? { - if nodePtr != nil { - let type = nodePtr.memory.type - if type.value == XML_TEXT_NODE.value { - return libxmlGetNodeText(nodePtr) - } - } - return nil -} - private func libxmlGetNodeContent(nodePtr: xmlNodePtr) -> String? { let content = xmlNodeGetContent(nodePtr) if let result = String.fromCString(UnsafePointer(content)) { diff --git a/Tests/KannaTests.swift b/Tests/KannaTests.swift index 2860e8d..e6e627c 100644 --- a/Tests/KannaTests.swift +++ b/Tests/KannaTests.swift @@ -173,4 +173,15 @@ class KannaTests: XCTestCase { XCTAssert(false, "File not found. name: (\(filename))") } } + + func testInnerHTML() { + let filename = "test_HTML4" + if let path = NSBundle(forClass:self.classForCoder).pathForResource(filename, ofType:"html"), + html = NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil) as? String, + doc = HTML(html: html, encoding: NSUTF8StringEncoding) { + + XCTAssert(doc.at_css("div#inner")!.innerHTML == "\n abc