Fix color not being applied

This commit is contained in:
Shadowfacts 2023-12-24 11:52:36 -05:00
parent 38b1d2949b
commit 5a7b5860dd
2 changed files with 30 additions and 5 deletions

View File

@ -281,6 +281,7 @@ public struct AttributedStringConverter<Callbacks: HTMLConversionCallbacks> {
}
attributes[.font] = getFont(traits: currentFontTraits)
attributes[.foregroundColor] = configuration.color
if !attributes.keys.contains(.paragraphStyle) {
attributes[.paragraphStyle] = configuration.paragraphStyle

View File

@ -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<Callbacks>(configuration: config)
@ -56,10 +58,12 @@ final class AttributedStringConverterTests: XCTestCase {
XCTAssertEqual(convert("a<br>b"), NSAttributedString(string: "a\nb", attributes: [
.font: font,
.paragraphStyle: NSParagraphStyle.default,
.foregroundColor: color,
]))
XCTAssertEqual(convert("a<br />b"), NSAttributedString(string: "a\nb", attributes: [
.font: font,
.paragraphStyle: NSParagraphStyle.default,
.foregroundColor: color,
]))
}
@ -67,11 +71,13 @@ final class AttributedStringConverterTests: XCTestCase {
XCTAssertEqual(convert("<a href='https://example.com'>link</a>"), NSAttributedString(string: "link", attributes: [
.font: font,
.paragraphStyle: NSParagraphStyle.default,
.foregroundColor: color,
.link: URL(string: "https://example.com")!,
]))
XCTAssertEqual(convert("<a>link</a>"), NSAttributedString(string: "link", attributes: [
.font: font,
.paragraphStyle: NSParagraphStyle.default,
.foregroundColor: color,
]))
}
@ -79,6 +85,7 @@ final class AttributedStringConverterTests: XCTestCase {
XCTAssertEqual(convert("<p>a</p><p>b</p>"), NSAttributedString(string: "a\n\nb", attributes: [
.font: font,
.paragraphStyle: NSParagraphStyle.default,
.foregroundColor: color,
]))
}
@ -86,10 +93,12 @@ final class AttributedStringConverterTests: XCTestCase {
XCTAssertEqual(convert("<em>hello</em>"), NSAttributedString(string: "hello", attributes: [
.font: italicFont,
.paragraphStyle: NSParagraphStyle.default,
.foregroundColor: color,
]))
XCTAssertEqual(convert("<i>hello</i>"), NSAttributedString(string: "hello", attributes: [
.font: italicFont,
.paragraphStyle: NSParagraphStyle.default,
.foregroundColor: color,
]))
}
@ -97,10 +106,12 @@ final class AttributedStringConverterTests: XCTestCase {
XCTAssertEqual(convert("<strong>hello</strong>"), NSAttributedString(string: "hello", attributes: [
.font: boldFont,
.paragraphStyle: NSParagraphStyle.default,
.foregroundColor: color,
]))
XCTAssertEqual(convert("<b>hello</b>"), NSAttributedString(string: "hello", attributes: [
.font: boldFont,
.paragraphStyle: NSParagraphStyle.default,
.foregroundColor: color,
]))
}
@ -108,6 +119,7 @@ final class AttributedStringConverterTests: XCTestCase {
XCTAssertEqual(convert("<strong><em>hello</em></strong>"), 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("<strong>bold <em>both</strong> italic</em>"), 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("<a href='https://example.com'>hello <b>world</a></b>"), result)
}
@ -145,6 +157,7 @@ final class AttributedStringConverterTests: XCTestCase {
XCTAssertEqual(convert("<del>blah</del>"), 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("<code>wee</code>"), NSAttributedString(string: "wee", attributes: [
.font: monospaceFont,
.paragraphStyle: NSParagraphStyle.default,
.foregroundColor: color,
]))
}
@ -160,6 +174,7 @@ final class AttributedStringConverterTests: XCTestCase {
XCTAssertEqual(convert("<pre>wee</pre>"), NSAttributedString(string: "wee", attributes: [
.font: monospaceFont,
.paragraphStyle: NSParagraphStyle.default,
.foregroundColor: color,
]))
}
@ -167,10 +182,12 @@ final class AttributedStringConverterTests: XCTestCase {
XCTAssertEqual(convert("<blockquote>hello</blockquote>"), NSAttributedString(string: "hello", attributes: [
.font: italicFont,
.paragraphStyle: blockquoteParagraphStyle,
.foregroundColor: color,
]))
XCTAssertEqual(convert("<blockquote><b>hello</b></blockquote>"), 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("<blockquote>wee</blockquote>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("<blockquote>a</blockquote><blockquote>b</blockquote>"), result)
}
@ -208,6 +227,7 @@ final class AttributedStringConverterTests: XCTestCase {
XCTAssertEqual(convert("<b />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,
]))
}