// // NSAttributedStringHTMLTests.swift // NetNewsWireTests // // Created by Brent Simmons on 4/21/26. // Copyright © 2026 Ranchero Software, LLC. All rights reserved. // import Foundation import Testing #if canImport(AppKit) import AppKit #else import UIKit #endif @testable import NetNewsWire @MainActor struct NSAttributedStringHTMLTests { // MARK: - Plain text / basic tags @Test func plainText() { let s = NSAttributedString(simpleHTML: "Hello World") #expect(s.string == "Hello World") } @Test func boldTag() { let s = NSAttributedString(simpleHTML: "Use bold here") #expect(s.string == "Use bold here") } @Test func italicTag() { let s = NSAttributedString(simpleHTML: "Use italic here") #expect(s.string == "Use italic here") } @Test func multipleTags() { let s = NSAttributedString(simpleHTML: "Bold and italic together") #expect(s.string == "Bold and italic together") } // MARK: - Entities @Test func ampersandEntity() { let s = NSAttributedString(simpleHTML: "Tom & Jerry") #expect(s.string == "Tom & Jerry") } @Test func mixedEntitiesAndTags() { let s = NSAttributedString(simpleHTML: "Tom & Jerry") #expect(s.string == "Tom & Jerry") } // MARK: - tags (locale-dependent quotes) @Test func qTagProducesQuotes() { // Use en_US locale for deterministic output — else this depends // on the test-runner locale. let s = NSAttributedString(simpleHTML: "hi", locale: Locale(identifier: "en_US")) #expect(s.string.contains("hi")) // en_US uses curly quotes — confirm something was added. #expect(s.string.count > 2) } // MARK: - Attribute application // Bold text should have a bold-weighted font. @Test func boldFontIsApplied() throws { let s = NSAttributedString(simpleHTML: "Bold") let font = try #require(s.attribute(.font, at: 0, effectiveRange: nil) as? RSFont) let traits = font.fontDescriptor.symbolicTraits #if canImport(AppKit) #expect(traits.contains(.bold)) #else #expect(traits.contains(.traitBold)) #endif } // Underline should carry the underline attribute. @Test func underlineIsApplied() { let s = NSAttributedString(simpleHTML: "Underlined") let underline = s.attribute(.underlineStyle, at: 0, effectiveRange: nil) as? Int #expect(underline == NSUnderlineStyle.single.rawValue) } // MARK: - Unicode @Test func unicodeText() { let s = NSAttributedString(simpleHTML: "Café résumé 🎉") #expect(s.string == "Café résumé 🎉") } @Test func unicodeInsideTag() { let s = NSAttributedString(simpleHTML: "Café résumé") #expect(s.string == "Café résumé") } // MARK: - Timeline title end-to-end (issue #3325) // The timeline title is `simpleHTML(sanitizedTitle(forHTML: true))`. // A feed title with `` must show as plain text, not // raw markup. @Test func abbrTitleRendersAsPlainText() throws { let sanitized = try #require(ArticleStringFormatter.sanitizedTitle("[NSFW]", forHTML: true)) let s = NSAttributedString(simpleHTML: sanitized) #expect(s.string == "[NSFW]") } // #4742: a title with a literal `<` followed by non-tag text must // render in full in the timeline — not truncated at the `<`, and with // no spurious trailing `>`. @Test func literalLessThanTitleIsNotTruncated() throws { let input = "No place in children's hands: <16s in UK to be banned" let sanitized = try #require(ArticleStringFormatter.sanitizedTitle(input, forHTML: true)) let s = NSAttributedString(simpleHTML: sanitized) #expect(s.string == "No place in children's hands: <16s in UK to be banned") } }