diff --git a/godtools/App/Share/Data/WebArchiveQueue/HTMLDocumentWrapper.swift b/godtools/App/Share/Data/WebArchiveQueue/HTMLDocumentWrapper.swift index cbcb9cf83..d127df7c6 100644 --- a/godtools/App/Share/Data/WebArchiveQueue/HTMLDocumentWrapper.swift +++ b/godtools/App/Share/Data/WebArchiveQueue/HTMLDocumentWrapper.swift @@ -11,22 +11,28 @@ import Fuzi import libxml2 // NOTE: Wrapper for addressing bad access starting in Xcode 16.2 Fuzi version 3.1.3. (https://github.com/cezheng/Fuzi/issues/130) +// Will fix by implementing change from open PR (https://github.com/cezheng/Fuzi/pull/131). +// TODO: GT-2492 Once merged we can remove this wrapper. ~Levi class HTMLDocumentWrapper { let htmlDocument: HTMLDocument init(string: String, encoding: String.Encoding = String.Encoding.utf8) throws { - + guard let cChars = string.cString(using: encoding) else { throw XMLError.invalidData } - let buffer = cChars.withUnsafeBufferPointer { buffer in - UnsafeBufferPointer(rebasing: buffer[0...allocate(capacity: cChars.count) + _ = mutablebuffer.initialize(from: cChars) + + defer { + mutablebuffer.deallocate() } - - // NOTE: Seems to solve bad access at line 130 in Document.swift Fuzi version 3.1.3. "guard let document = type(of: self).parse(buffer: buffer, options: options)" ~Levi + + let buffer = UnsafeBufferPointer(mutablebuffer) + let htmlDocument = try HTMLDocument(buffer: buffer) self.htmlDocument = htmlDocument diff --git a/godtools/App/Share/Data/WebArchiveQueue/WebArchiveOperation.swift b/godtools/App/Share/Data/WebArchiveQueue/WebArchiveOperation.swift index 6acaf4840..53ac10782 100644 --- a/godtools/App/Share/Data/WebArchiveQueue/WebArchiveOperation.swift +++ b/godtools/App/Share/Data/WebArchiveQueue/WebArchiveOperation.swift @@ -225,9 +225,12 @@ class WebArchiveOperation: Operation, @unchecked Sendable { do { let webArchiveResource: WebArchiveResource = WebArchiveResource(url: url, data: data, mimeType: mimeType) let mainResource: WebArchiveMainResource = WebArchiveMainResource(baseResource: webArchiveResource) - //let htmlDocument: HTMLDocument = try HTMLDocument(string: htmlString, encoding: .utf8) // NOTE: Was getting a bad access starting in Xcode 16.2 (https://github.com/cezheng/Fuzi/issues/130). ~Levi - let htmlDocumentWrapper = try HTMLDocumentWrapper(string: htmlString, encoding: .utf8) - let resourceUrls: [String] = htmlDocumentWrapper.htmlDocument.getHTMLReferences(host: host, includeJavascript: includeJavascript) + + // NOTE: Using HTMLDocumentWrapper for now with open PR fix until merged and Fuzi is updated. ~Levi + // TODO: GT-2492 Remove HTMLDocumentWrapper once PR is merged. ~Levi + //let htmlDocument: HTMLDocument = try HTMLDocument(string: htmlString, encoding: .utf8) + let htmlDocument: HTMLDocument = try HTMLDocumentWrapper(string: htmlString, encoding: .utf8).htmlDocument + let resourceUrls: [String] = htmlDocument.getHTMLReferences(host: host, includeJavascript: includeJavascript) complete(.success(HTMLDocumentData(mainResource: mainResource, resourceUrls: resourceUrls))) } catch let parseHtmlDocumentError {