Skip to content

Commit

Permalink
Merge branch 'Krish-sysadmin-issue242'
Browse files Browse the repository at this point in the history
  • Loading branch information
tid-kijyun committed Jul 16, 2021
2 parents 9732b7b + 82695dc commit 48e0a76
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Sources/Kanna/libxmlHTMLDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ final class libxmlHTMLDocument: HTMLDocument {
throw ParseError.EncodingMismatch
}

rootNode = libxmlHTMLNode(document: self, docPtr: docPtr)
rootNode = try libxmlHTMLNode(document: self, docPtr: docPtr)
}

deinit {
Expand Down Expand Up @@ -299,7 +299,7 @@ final class libxmlXMLDocument: XMLDocument {
}
let url: String = ""
docPtr = cur.withUnsafeBytes { xmlReadDoc($0.bindMemory(to: xmlChar.self).baseAddress!, url, charsetName, CInt(option)) }
rootNode = libxmlHTMLNode(document: self, docPtr: docPtr!)
rootNode = try libxmlHTMLNode(document: self, docPtr: docPtr!)
}

deinit {
Expand Down
9 changes: 7 additions & 2 deletions Sources/Kanna/libxmlHTMLNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,15 @@ final class libxmlHTMLNode: XMLElement {
}
}

init(document: XMLDocument?, docPtr: xmlDocPtr) {
init(document: XMLDocument?, docPtr: xmlDocPtr) throws {
self.weakDocument = document
self.docPtr = docPtr
self.nodePtr = xmlDocGetRootElement(docPtr)
guard let nodePtr = xmlDocGetRootElement(docPtr) else {
// Error handling is omitted, and will be added if necessary in the future.
// e.g: if let error = xmlGetLastError(), error.pointee.code == XML_ERR_DOCUMENT_EMPTY.rawValue
throw ParseError.Empty
}
self.nodePtr = nodePtr
}

init(document: XMLDocument?, docPtr: xmlDocPtr, node: xmlNodePtr) {
Expand Down
10 changes: 10 additions & 0 deletions Tests/KannaTests/KannaHTMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ class KannaHTMLTests: XCTestCase {
XCTAssert(previous.text == "first")
}

func testEmptyHTML() {
XCTAssertThrowsError(try HTML(html: "", encoding: .utf8)) { error in
XCTAssertEqual(error as? ParseError, ParseError.Empty)
}

XCTAssertThrowsError(try HTML(html: " ", encoding: .utf8)) { error in
XCTAssertEqual(error as? ParseError, ParseError.Empty)
}
}

func testEscapeId() {
let html = "<body><div id='my.id'>target</div><div>second</div><div>third</div></body>"
guard let doc = try? HTML(html: html, encoding: .utf8),
Expand Down
8 changes: 7 additions & 1 deletion Tests/KannaTests/KannaXMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ class KannaXMLTests: XCTestCase {
}

func testXmlThrows() {
XCTAssertThrowsError(try XML(xml: "", encoding: .utf8))
XCTAssertThrowsError(try XML(xml: "", encoding: .utf8)) { error in
XCTAssertEqual(error as? ParseError, ParseError.Empty)
}

XCTAssertThrowsError(try XML(xml: " ", encoding: .utf8)) { error in
XCTAssertEqual(error as? ParseError, ParseError.Empty)
}
}

func testNamespaces() {
Expand Down

0 comments on commit 48e0a76

Please sign in to comment.