2023-11-25 02:59:30 +00:00
|
|
|
//
|
|
|
|
// AttributedStringConverterTests.swift
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 11/24/23.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
@testable import HTMLStreamer
|
|
|
|
|
|
|
|
final class AttributedStringConverterTests: XCTestCase {
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
private let font = UIFont.systemFont(ofSize: 13)
|
|
|
|
private let monospaceFont = UIFont.monospacedSystemFont(ofSize: 13, weight: .regular)
|
|
|
|
#elseif os(macOS)
|
|
|
|
private let font = NSFont.systemFont(ofSize: 13)
|
|
|
|
private lazy var italicFont = NSFont(descriptor: font.fontDescriptor.withSymbolicTraits(.italic), size: 13)!
|
|
|
|
private lazy var boldFont = NSFont(descriptor: font.fontDescriptor.withSymbolicTraits(.bold), size: 13)!
|
|
|
|
private lazy var boldItalicFont = NSFont(descriptor: font.fontDescriptor.withSymbolicTraits([.bold, .italic]), size: 13)!
|
|
|
|
private let monospaceFont = NSFont.monospacedSystemFont(ofSize: 13, weight: .regular)
|
|
|
|
#endif
|
|
|
|
private let blockquoteParagraphStyle: NSParagraphStyle = {
|
|
|
|
let style = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
|
|
|
|
style.headIndent = 32
|
|
|
|
style.firstLineHeadIndent = 32
|
|
|
|
return style
|
|
|
|
}()
|
2023-11-25 14:44:53 +00:00
|
|
|
private let listParagraphStyle: NSParagraphStyle = {
|
|
|
|
let style = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
|
|
|
|
style.headIndent = 32
|
|
|
|
style.firstLineHeadIndent = 0
|
|
|
|
style.tabStops = [NSTextTab(textAlignment: .right, location: 28), NSTextTab(textAlignment: .natural, location: 32)]
|
|
|
|
return style
|
|
|
|
}()
|
2023-11-25 02:59:30 +00:00
|
|
|
|
|
|
|
private func convert(_ html: String) -> NSAttributedString {
|
|
|
|
convert(html, callbacks: DefaultCallbacks.self)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func convert<Callbacks: AttributedStringCallbacks>(_ html: String, callbacks _: Callbacks.Type = Callbacks.self) -> NSAttributedString {
|
|
|
|
let config = AttributedStringConverterConfiguration(
|
|
|
|
font: font,
|
|
|
|
monospaceFont: monospaceFont,
|
|
|
|
color: .black,
|
|
|
|
paragraphStyle: .default
|
|
|
|
)
|
2023-11-26 23:53:59 +00:00
|
|
|
var converter = AttributedStringConverter<Callbacks>(configuration: config)
|
|
|
|
return converter.convert(html: html)
|
2023-11-25 02:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testConvertBR() {
|
|
|
|
XCTAssertEqual(convert("a<br>b"), NSAttributedString(string: "a\nb", attributes: [
|
|
|
|
.font: font,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConvertA() {
|
|
|
|
XCTAssertEqual(convert("<a href='https://example.com'>link</a>"), NSAttributedString(string: "link", attributes: [
|
|
|
|
.font: font,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
.link: URL(string: "https://example.com")!,
|
|
|
|
]))
|
|
|
|
XCTAssertEqual(convert("<a>link</a>"), NSAttributedString(string: "link", attributes: [
|
|
|
|
.font: font,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConvertP() {
|
|
|
|
XCTAssertEqual(convert("<p>a</p><p>b</p>"), NSAttributedString(string: "a\n\nb", attributes: [
|
|
|
|
.font: font,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConvertEm() {
|
|
|
|
XCTAssertEqual(convert("<em>hello</em>"), NSAttributedString(string: "hello", attributes: [
|
|
|
|
.font: italicFont,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
]))
|
|
|
|
XCTAssertEqual(convert("<i>hello</i>"), NSAttributedString(string: "hello", attributes: [
|
|
|
|
.font: italicFont,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConvertStrong() {
|
|
|
|
XCTAssertEqual(convert("<strong>hello</strong>"), NSAttributedString(string: "hello", attributes: [
|
|
|
|
.font: boldFont,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
]))
|
|
|
|
XCTAssertEqual(convert("<b>hello</b>"), NSAttributedString(string: "hello", attributes: [
|
|
|
|
.font: boldFont,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConvertBoldItalic() {
|
|
|
|
XCTAssertEqual(convert("<strong><em>hello</em></strong>"), NSAttributedString(string: "hello", attributes: [
|
|
|
|
.font: boldItalicFont,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIncorrectNesting() {
|
|
|
|
let result = NSMutableAttributedString()
|
|
|
|
result.append(NSAttributedString(string: "bold ", attributes: [
|
|
|
|
.font: boldFont,
|
|
|
|
]))
|
|
|
|
result.append(NSAttributedString(string: "both", attributes: [
|
|
|
|
.font: boldItalicFont,
|
|
|
|
]))
|
|
|
|
result.append(NSAttributedString(string: " italic", attributes: [
|
|
|
|
.font: italicFont,
|
|
|
|
]))
|
|
|
|
result.addAttribute(.paragraphStyle, value: NSParagraphStyle.default, range: NSRange(location: 0, length: result.length))
|
|
|
|
XCTAssertEqual(convert("<strong>bold <em>both</strong> italic</em>"), result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDel() {
|
|
|
|
XCTAssertEqual(convert("<del>blah</del>"), NSAttributedString(string: "blah", attributes: [
|
|
|
|
.font: font,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
.strikethroughStyle: NSUnderlineStyle.single.rawValue,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCode() {
|
|
|
|
XCTAssertEqual(convert("<code>wee</code>"), NSAttributedString(string: "wee", attributes: [
|
|
|
|
.font: monospaceFont,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPre() {
|
|
|
|
XCTAssertEqual(convert("<pre>wee</pre>"), NSAttributedString(string: "wee", attributes: [
|
|
|
|
.font: monospaceFont,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBlockquote() {
|
|
|
|
XCTAssertEqual(convert("<blockquote>hello</blockquote>"), NSAttributedString(string: "hello", attributes: [
|
|
|
|
.font: italicFont,
|
|
|
|
.paragraphStyle: blockquoteParagraphStyle,
|
|
|
|
]))
|
|
|
|
XCTAssertEqual(convert("<blockquote><b>hello</b></blockquote>"), NSAttributedString(string: "hello", attributes: [
|
|
|
|
.font: boldItalicFont,
|
|
|
|
.paragraphStyle: blockquoteParagraphStyle,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSelfClosing() {
|
|
|
|
XCTAssertEqual(convert("<b />asdf"), NSAttributedString(string: "asdf", attributes: [
|
|
|
|
.font: font,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMakeURLCallback() {
|
|
|
|
struct Callbacks: AttributedStringCallbacks {
|
|
|
|
static func makeURL(string: String) -> URL? {
|
|
|
|
URL(string: "https://apple.com")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let result = convert("<a href='https://example.com'>test</a>", callbacks: Callbacks.self)
|
|
|
|
XCTAssertEqual(result, NSAttributedString(string: "test", attributes: [
|
|
|
|
.font: font,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
.link: URL(string: "https://apple.com")!,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testElementActionCallback() {
|
|
|
|
struct Callbacks: AttributedStringCallbacks {
|
2023-11-27 03:35:10 +00:00
|
|
|
static func elementAction(name: String, attributes: [Attribute]) -> ElementAction {
|
2023-11-25 02:59:30 +00:00
|
|
|
let clazz = attributes.attributeValue(for: "class")
|
|
|
|
if clazz == "invisible" {
|
|
|
|
return .skip
|
|
|
|
} else if clazz == "ellipsis" {
|
|
|
|
return .replace("…")
|
|
|
|
} else {
|
|
|
|
return .default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let skipped = convert("<span class='invisible'>test</span>", callbacks: Callbacks.self)
|
|
|
|
XCTAssertEqual(skipped, NSAttributedString())
|
|
|
|
let skipNestped = convert("<span class='invisible'><b>test</b></span>", callbacks: Callbacks.self)
|
|
|
|
XCTAssertEqual(skipNestped, NSAttributedString())
|
|
|
|
let skipNestped2 = convert("<b><span class='invisible'>test</span></b>", callbacks: Callbacks.self)
|
|
|
|
XCTAssertEqual(skipNestped2, NSAttributedString())
|
|
|
|
let replaced = convert("<span class='ellipsis'>test</span>", callbacks: Callbacks.self)
|
|
|
|
XCTAssertEqual(replaced, NSAttributedString(string: "…", attributes: [
|
|
|
|
.font: font,
|
|
|
|
.paragraphStyle: NSParagraphStyle.default,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
2023-11-25 14:44:53 +00:00
|
|
|
func testOrderedList() {
|
|
|
|
let result = convert("<ol><li>a</li><li>b</li></ol>")
|
|
|
|
XCTAssertEqual(result, NSAttributedString(string: "\t1.\ta\n\t2.\tb", attributes: [
|
|
|
|
.font: font,
|
|
|
|
.paragraphStyle: listParagraphStyle,
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
2023-11-25 02:59:30 +00:00
|
|
|
}
|