Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Gumbo initialization options, output status #2

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Sources/SwiftGumbo/SwiftGumbo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ public class SwiftGumbo {
private let htmlBuffer: [CChar]
let gumboOutput: UnsafeMutablePointer<GumboOutput>

public init(html: String) {
public let status: GumboOutputStatus

public init(html: String, options: GumboOptions = kGumboDefaultOptions) {
// CGumboParser and the types returned by SwiftGumbo all share the same buffer in memory.
// The class holds it strongly to ensure it has the same lifetime as the parser.
let htmlBuffer = html.cString(using: .utf8) ?? []
let outputResult = withUnsafePointer(to: kGumboDefaultOptions) { (optionsPointer) -> Result<UnsafeMutablePointer<GumboOutput>, Never> in
.success(gumbo_parse_with_options(optionsPointer, htmlBuffer, htmlBuffer.count))
let outputResult = withUnsafePointer(to: options) { (optionsPointer) -> UnsafeMutablePointer<GumboOutput> in
gumbo_parse_with_options(optionsPointer, htmlBuffer, htmlBuffer.count)
}

self.htmlBuffer = htmlBuffer

// Safe to force try here since the outputResult only returns success.
self.gumboOutput = try! outputResult.get()
self.gumboOutput = outputResult
Comment on lines -17 to +26
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gshahbazian wrapping with Result seemed unnecessary here, but happy to revert that change if there's a good reason for it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see any either


self.status = outputResult.pointee.status
}

deinit {
Expand Down
12 changes: 11 additions & 1 deletion Tests/SwiftGumboTests/SwiftGumboTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// Created by Gabe Shahbazian 2020
//

import CGumboParser
import SwiftGumbo
import XCTest
@testable import SwiftGumbo

final class SwiftGumboTests: XCTestCase {
func testParsingDom() {
Expand Down Expand Up @@ -199,6 +200,14 @@ final class SwiftGumboTests: XCTestCase {
XCTAssertEqual(match.count, 4)
}

func testMaxTreeDepthOption() {
var options = kGumboDefaultOptions
options.max_tree_depth = 2
let g = SwiftGumbo(html: #"<html><body><h1>node too deep</h1></body></html>"#, options: options)

XCTAssertEqual(g.status, GUMBO_STATUS_TREE_TOO_DEEP)
}

static var allTests = [
("testParsingDom", testParsingDom),
("testTextContent", testTextContent),
Expand All @@ -218,5 +227,6 @@ final class SwiftGumboTests: XCTestCase {
("testAttributePrefixSelector", testAttributePrefixSelector),
("testGlobalSelector", testGlobalSelector),
("testNotSelector", testNotSelector),
("testMaxTreeDepthOption", testMaxTreeDepthOption),
]
}
Loading