diff --git a/Packages/Models/Sources/Models/Alias/HTMLString.swift b/Packages/Models/Sources/Models/Alias/HTMLString.swift index be59d634..7f922ac5 100644 --- a/Packages/Models/Sources/Models/Alias/HTMLString.swift +++ b/Packages/Models/Sources/Models/Alias/HTMLString.swift @@ -11,7 +11,7 @@ public struct HTMLString: Codable, Equatable, Hashable, @unchecked Sendable { public var asMarkdown: String = "" public var asRawText: String = "" public var statusesURLs = [URL]() - public private(set) var links = [Link]() + private(set) public var links = [Link]() public var asSafeMarkdownAttributedString: AttributedString = .init() private var main_regex: NSRegularExpression? @@ -151,14 +151,25 @@ public struct HTMLString: Codable, Equatable, Hashable, @unchecked Sendable { handleNode(node: nn) } let finish = asMarkdown.endIndex + + var linkRef = href + + // Try creating a URL from the string. If it fails, try URL encoding + // the string first. + var url = URL(string: href) + if url == nil { + url = URL(string: href, encodePath: true) + } + if let linkUrl = url { + linkRef = linkUrl.absoluteString + let displayString = asMarkdown[start..This is a test

\"" + var htmlString = try decoder.decode(HTMLString.self, from: Data(basicContent.utf8)) + XCTAssertEqual("This is a test", htmlString.asRawText) + XCTAssertEqual("

This is a test

", htmlString.htmlValue) + XCTAssertEqual("This is a test", htmlString.asMarkdown) + XCTAssertEqual(0, htmlString.statusesURLs.count) + XCTAssertEqual(0, htmlString.links.count) + + let basicLink = "\"

This is a test

\"" + htmlString = try decoder.decode(HTMLString.self, from: Data(basicLink.utf8)) + XCTAssertEqual("This is a test", htmlString.asRawText) + XCTAssertEqual("

This is a test

", htmlString.htmlValue) + XCTAssertEqual("This is a [test](https://test.com)", htmlString.asMarkdown) + XCTAssertEqual(0, htmlString.statusesURLs.count) + XCTAssertEqual(1, htmlString.links.count) + XCTAssertEqual("https://test.com", htmlString.links[0].url.absoluteString) + XCTAssertEqual("test", htmlString.links[0].displayString) + + let extendedCharLink = "\"

This is a test

\"" + htmlString = try decoder.decode(HTMLString.self, from: Data(extendedCharLink.utf8)) + XCTAssertEqual("This is a test", htmlString.asRawText) + XCTAssertEqual("

This is a test

", htmlString.htmlValue) + XCTAssertEqual("This is a [test](https://test.com/go%C3%9F%C3%AB%C3%B1a)", htmlString.asMarkdown) + XCTAssertEqual(0, htmlString.statusesURLs.count) + XCTAssertEqual(1, htmlString.links.count) + XCTAssertEqual("https://test.com/go%C3%9F%C3%AB%C3%B1a", htmlString.links[0].url.absoluteString) + XCTAssertEqual("test", htmlString.links[0].displayString) + + let alreadyEncodedLink = "\"

This is a test

\"" + htmlString = try decoder.decode(HTMLString.self, from: Data(alreadyEncodedLink.utf8)) + XCTAssertEqual("This is a test", htmlString.asRawText) + XCTAssertEqual("

This is a test

", htmlString.htmlValue) + XCTAssertEqual("This is a [test](https://test.com/go%C3%9F%C3%AB%C3%B1a)", htmlString.asMarkdown) + XCTAssertEqual(0, htmlString.statusesURLs.count) + XCTAssertEqual(1, htmlString.links.count) + XCTAssertEqual("https://test.com/go%C3%9F%C3%AB%C3%B1a", htmlString.links[0].url.absoluteString) + XCTAssertEqual("test", htmlString.links[0].displayString) + } +} diff --git a/Packages/Models/Tests/ModelsTests/ModelsTests.swift b/Packages/Models/Tests/ModelsTests/ModelsTests.swift deleted file mode 100644 index 11428a63..00000000 --- a/Packages/Models/Tests/ModelsTests/ModelsTests.swift +++ /dev/null @@ -1,11 +0,0 @@ -@testable import Models -import XCTest - -final class ModelsTests: XCTestCase { - func testExample() throws { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(Models().text, "Hello, World!") - } -}