diff --git a/Sources/HTMLStreamer/AttributedStringConverter.swift b/Sources/HTMLStreamer/AttributedStringConverter.swift index 29b0626..4664e53 100644 --- a/Sources/HTMLStreamer/AttributedStringConverter.swift +++ b/Sources/HTMLStreamer/AttributedStringConverter.swift @@ -281,6 +281,7 @@ public struct AttributedStringConverter { } attributes[.font] = getFont(traits: currentFontTraits) + attributes[.foregroundColor] = configuration.color if !attributes.keys.contains(.paragraphStyle) { attributes[.paragraphStyle] = configuration.paragraphStyle diff --git a/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift b/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift index 53e00e0..3cbc26b 100644 --- a/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift +++ b/Tests/HTMLStreamerTests/AttributedStringConverterTests.swift @@ -12,12 +12,14 @@ final class AttributedStringConverterTests: XCTestCase { #if os(iOS) private let font = UIFont.systemFont(ofSize: 13) + private let color = UIColor.black private lazy var italicFont = UIFont(descriptor: font.fontDescriptor.withSymbolicTraits([.traitItalic])!, size: 13) private lazy var boldFont = UIFont(descriptor: font.fontDescriptor.withSymbolicTraits([.traitBold])!, size: 13) private lazy var boldItalicFont = UIFont(descriptor: font.fontDescriptor.withSymbolicTraits([.traitBold, .traitItalic])!, size: 13) private let monospaceFont = UIFont.monospacedSystemFont(ofSize: 13, weight: .regular) #elseif os(macOS) private let font = NSFont.systemFont(ofSize: 13) + private let color = NSColor.black 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)! @@ -45,7 +47,7 @@ final class AttributedStringConverterTests: XCTestCase { let config = AttributedStringConverterConfiguration( font: font, monospaceFont: monospaceFont, - color: .black, + color: color, paragraphStyle: .default ) var converter = AttributedStringConverter(configuration: config) @@ -56,10 +58,12 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(convert("a
b"), NSAttributedString(string: "a\nb", attributes: [ .font: font, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) XCTAssertEqual(convert("a
b"), NSAttributedString(string: "a\nb", attributes: [ .font: font, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) } @@ -67,11 +71,13 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(convert("link"), NSAttributedString(string: "link", attributes: [ .font: font, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, .link: URL(string: "https://example.com")!, ])) XCTAssertEqual(convert("link"), NSAttributedString(string: "link", attributes: [ .font: font, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) } @@ -79,6 +85,7 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(convert("

a

b

"), NSAttributedString(string: "a\n\nb", attributes: [ .font: font, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) } @@ -86,10 +93,12 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(convert("hello"), NSAttributedString(string: "hello", attributes: [ .font: italicFont, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) XCTAssertEqual(convert("hello"), NSAttributedString(string: "hello", attributes: [ .font: italicFont, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) } @@ -97,10 +106,12 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(convert("hello"), NSAttributedString(string: "hello", attributes: [ .font: boldFont, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) XCTAssertEqual(convert("hello"), NSAttributedString(string: "hello", attributes: [ .font: boldFont, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) } @@ -108,6 +119,7 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(convert("hello"), NSAttributedString(string: "hello", attributes: [ .font: boldItalicFont, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) } @@ -123,21 +135,21 @@ final class AttributedStringConverterTests: XCTestCase { .font: italicFont, ])) result.addAttribute(.paragraphStyle, value: NSParagraphStyle.default, range: NSRange(location: 0, length: result.length)) + result.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: result.length)) XCTAssertEqual(convert("bold both italic"), result) } func testMisnestedLink() { let result = NSMutableAttributedString() result.append(NSAttributedString(string: "hello ", attributes: [ - .link: URL(string: "https://example.com")!, .font: font, - .paragraphStyle: NSParagraphStyle.default, ])) result.append(NSAttributedString(string: "world", attributes: [ - .link: URL(string: "https://example.com")!, .font: boldFont, - .paragraphStyle: NSParagraphStyle.default, ])) + result.addAttribute(.link, value: URL(string: "https://example.com")!, range: NSRange(location: 0, length: result.length)) + result.addAttribute(.paragraphStyle, value: NSParagraphStyle.default, range: NSRange(location: 0, length: result.length)) + result.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: result.length)) XCTAssertEqual(convert("hello world"), result) } @@ -145,6 +157,7 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(convert("blah"), NSAttributedString(string: "blah", attributes: [ .font: font, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, .strikethroughStyle: NSUnderlineStyle.single.rawValue, ])) } @@ -153,6 +166,7 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(convert("wee"), NSAttributedString(string: "wee", attributes: [ .font: monospaceFont, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) } @@ -160,6 +174,7 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(convert("
wee
"), NSAttributedString(string: "wee", attributes: [ .font: monospaceFont, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) } @@ -167,10 +182,12 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(convert("
hello
"), NSAttributedString(string: "hello", attributes: [ .font: italicFont, .paragraphStyle: blockquoteParagraphStyle, + .foregroundColor: color, ])) XCTAssertEqual(convert("
hello
"), NSAttributedString(string: "hello", attributes: [ .font: boldItalicFont, .paragraphStyle: blockquoteParagraphStyle, + .foregroundColor: color, ])) } @@ -184,6 +201,7 @@ final class AttributedStringConverterTests: XCTestCase { .font: font, .paragraphStyle: NSParagraphStyle.default, ])) + result.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: result.length)) XCTAssertEqual(convert("
wee
after"), result) } @@ -201,6 +219,7 @@ final class AttributedStringConverterTests: XCTestCase { .font: italicFont, .paragraphStyle: blockquoteParagraphStyle, ])) + result.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: result.length)) XCTAssertEqual(convert("
a
b
"), result) } @@ -208,6 +227,7 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(convert("asdf"), NSAttributedString(string: "asdf", attributes: [ .font: font, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) } @@ -222,6 +242,7 @@ final class AttributedStringConverterTests: XCTestCase { .font: font, .paragraphStyle: NSParagraphStyle.default, .link: URL(string: "https://apple.com")!, + .foregroundColor: color, ])) } @@ -248,6 +269,7 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(replaced, NSAttributedString(string: "…", attributes: [ .font: font, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) } @@ -256,6 +278,7 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(result, NSAttributedString(string: "\t1.\ta\n\t2.\tb", attributes: [ .font: font, .paragraphStyle: listParagraphStyle, + .foregroundColor: color, ])) } @@ -263,6 +286,7 @@ final class AttributedStringConverterTests: XCTestCase { XCTAssertEqual(convert("🇺🇸"), NSAttributedString(string: "🇺🇸", attributes: [ .font: font, .paragraphStyle: NSParagraphStyle.default, + .foregroundColor: color, ])) }