2023-12-23 01:30:29 +00:00
|
|
|
//
|
|
|
|
// TextConverterTests.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 12/22/23.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
@testable import HTMLStreamer
|
|
|
|
|
|
|
|
final class TextConverterTests: XCTestCase {
|
|
|
|
|
|
|
|
private func convert(_ html: String, configuration: TextConverterConfiguration = .init()) -> String {
|
|
|
|
convert(html, configuration: configuration, callbacks: DefaultCallbacks.self)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func convert<Callbacks: HTMLConversionCallbacks>(_ html: String, configuration: TextConverterConfiguration = .init(), callbacks _: Callbacks.Type = Callbacks.self) -> String {
|
|
|
|
var converter = TextConverter<Callbacks>(configuration: configuration)
|
|
|
|
return converter.convert(html: html)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConvertBR() {
|
|
|
|
XCTAssertEqual(convert("a<br>b"), "a\nb")
|
|
|
|
XCTAssertEqual(convert("a<br />b"), "a\nb")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConvertA() {
|
|
|
|
XCTAssertEqual(convert("<a href='https://example.com'>link</a>"), "link")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIncorrectNesting() {
|
|
|
|
XCTAssertEqual(convert("<strong>bold <em>both</strong> italic</em>"), "bold both italic")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testTextAfterBlockElement() {
|
|
|
|
XCTAssertEqual(convert("<blockquote>wee</blockquote>after"), "wee\n\nafter")
|
2023-12-23 15:45:11 +00:00
|
|
|
XCTAssertEqual(convert("<blockquote>wee</blockquote>after", configuration: .init(insertNewlines: false)), "wee after")
|
2023-12-23 01:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testMultipleBlockElements() {
|
|
|
|
XCTAssertEqual(convert("<blockquote>a</blockquote><blockquote>b</blockquote>"), "a\n\nb")
|
2023-12-23 15:45:11 +00:00
|
|
|
XCTAssertEqual(convert("<blockquote>a</blockquote><blockquote>b</blockquote>", configuration: .init(insertNewlines: false)), "a b")
|
2023-12-23 01:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testElementActionCallback() {
|
|
|
|
struct Callbacks: HTMLConversionCallbacks {
|
|
|
|
static func elementAction(name: String, attributes: [Attribute]) -> ElementAction {
|
|
|
|
let clazz = attributes.attributeValue(for: "class")
|
2024-01-17 00:15:56 +00:00
|
|
|
if clazz == "skip" {
|
2023-12-23 01:30:29 +00:00
|
|
|
return .skip
|
2024-01-17 00:15:56 +00:00
|
|
|
} else if clazz == "replace" {
|
2023-12-23 01:30:29 +00:00
|
|
|
return .replace("…")
|
2024-01-17 00:15:56 +00:00
|
|
|
} else if clazz == "append" {
|
|
|
|
return .append("…")
|
2023-12-23 01:30:29 +00:00
|
|
|
} else {
|
|
|
|
return .default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 00:15:56 +00:00
|
|
|
let skipped = convert("<span class='skip'>test</span>", callbacks: Callbacks.self)
|
2023-12-23 01:30:29 +00:00
|
|
|
XCTAssertEqual(skipped, "")
|
2024-01-17 00:15:56 +00:00
|
|
|
let skipNested = convert("<span class='skip'><b>test</b></span>", callbacks: Callbacks.self)
|
2023-12-23 01:30:29 +00:00
|
|
|
XCTAssertEqual(skipNested, "")
|
2024-01-17 00:15:56 +00:00
|
|
|
let replaced = convert("<span class='replace'>test</span>", callbacks: Callbacks.self)
|
2023-12-23 01:30:29 +00:00
|
|
|
XCTAssertEqual(replaced, "…")
|
2024-01-17 00:15:56 +00:00
|
|
|
let appended = convert("<span class='append'>test</span>", callbacks: Callbacks.self)
|
|
|
|
XCTAssertEqual(appended, "test…")
|
|
|
|
let appended2 = convert("<span class='append'>test <span>blah</span></span>", callbacks: Callbacks.self)
|
|
|
|
XCTAssertEqual(appended2, "test blah…")
|
|
|
|
|
2023-12-23 01:30:29 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 19:29:40 +00:00
|
|
|
func testEmptyBlockElements() {
|
|
|
|
XCTAssertEqual(convert("<p></p><blockquote><span>inside<br>quote</span></blockquote><span>after</span><p></p>"), "inside\nquote\n\nafter")
|
|
|
|
}
|
|
|
|
|
2024-02-04 20:09:08 +00:00
|
|
|
func testSkipElementActionFollowingUnfinishedRun() {
|
|
|
|
struct Callbacks: HTMLConversionCallbacks {
|
|
|
|
static func elementAction(name: String, attributes: [Attribute]) -> ElementAction {
|
|
|
|
attributes.attributeValue(for: "class") == "invisible" ? .skip : .default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XCTAssertEqual(convert(#"<a href="https://example.com"><span class="invisible">https://</span><span>example.com</span><span class="invisible"></span></a>"#, callbacks: Callbacks.self), "example.com")
|
|
|
|
}
|
|
|
|
|
2024-02-06 23:53:47 +00:00
|
|
|
func testMalformedOnlyClosingTag() {
|
|
|
|
XCTAssertEqual(convert("</span>"), "")
|
|
|
|
}
|
|
|
|
|
2023-12-23 01:30:29 +00:00
|
|
|
}
|