mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-05 19:57:31 +00:00
120 lines
3.6 KiB
Swift
120 lines
3.6 KiB
Swift
//
|
|
// 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 <b>bold</b> here")
|
|
#expect(s.string == "Use bold here")
|
|
}
|
|
|
|
@Test func italicTag() {
|
|
let s = NSAttributedString(simpleHTML: "Use <i>italic</i> here")
|
|
#expect(s.string == "Use italic here")
|
|
}
|
|
|
|
@Test func multipleTags() {
|
|
let s = NSAttributedString(simpleHTML: "<b>Bold</b> and <i>italic</i> 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: "<b>Tom</b> & <i>Jerry</i>")
|
|
#expect(s.string == "Tom & Jerry")
|
|
}
|
|
|
|
// MARK: - <q> 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: "<q>hi</q>", 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: "<b>Bold</b>")
|
|
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: "<u>Underlined</u>")
|
|
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: "<b>Café résumé</b>")
|
|
#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 `<abbr title="…">` must show as plain text, not
|
|
// raw markup.
|
|
@Test func abbrTitleRendersAsPlainText() throws {
|
|
let sanitized = try #require(ArticleStringFormatter.sanitizedTitle("[<abbr title=\"Not Safe For Work\">NSFW</abbr>]", 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")
|
|
}
|
|
}
|