diff --git a/Modules/ArticlesDatabase/Sources/ArticlesDatabase/SearchTable.swift b/Modules/ArticlesDatabase/Sources/ArticlesDatabase/SearchTable.swift index 3e3de26a0..8535d387e 100644 --- a/Modules/ArticlesDatabase/Sources/ArticlesDatabase/SearchTable.swift +++ b/Modules/ArticlesDatabase/Sources/ArticlesDatabase/SearchTable.swift @@ -43,7 +43,7 @@ final class ArticleSearchInfo: Hashable, Sendable { }() self.bodyForIndex = { - let s = preferredText.rsparser_stringByDecodingHTMLEntities() + let s = preferredText.decodingHTMLEntities() let sanitizedBody = s.strippingHTML() if let authorsNames { diff --git a/Modules/RSParser/Sources/Swift/Feeds/JSON/JSONFeedParser.swift b/Modules/RSParser/Sources/Swift/Feeds/JSON/JSONFeedParser.swift index b6e34336f..a8c04b29b 100644 --- a/Modules/RSParser/Sources/Swift/Feeds/JSON/JSONFeedParser.swift +++ b/Modules/RSParser/Sources/Swift/Feeds/JSON/JSONFeedParser.swift @@ -179,7 +179,7 @@ private extension JSONFeedParser { } if isSpecialCaseTitleWithEntitiesFeed(feedURL) { - return (title as NSString).rsparser_stringByDecodingHTMLEntities() + return title.decodingHTMLEntities() } return title diff --git a/Modules/RSParser/Sources/Swift/XML/String+HTMLEntities.swift b/Modules/RSParser/Sources/Swift/XML/String+HTMLEntities.swift new file mode 100644 index 000000000..6a2728e6b --- /dev/null +++ b/Modules/RSParser/Sources/Swift/XML/String+HTMLEntities.swift @@ -0,0 +1,39 @@ +// +// String+HTMLEntities.swift +// RSParser +// +// Created by Brent Simmons on 4/20/26. +// + +// Swift replacement for the old ObjC `-[NSString rsparser_stringByDecodingHTMLEntities]`. +// Scans a string for `&…;` entity references and expands them via `XMLEntities.decode` +// using `.html` mode (predefined XML, numeric, and HTML named entities). Unrecognized +// entities pass through literally — matches libxml2 HTML-parser behavior. + +public extension String { + + /// Return the string with all HTML entities (named, decimal, hex) decoded. + /// Unknown entities pass through as-is. + func decodingHTMLEntities() -> String { + let bytes = Array(utf8) + // Fast path: if no `&`, the string has no entities — return it unchanged. + if !bytes.contains(UInt8(ascii: "&")) { + return self + } + var out = [UInt8]() + out.reserveCapacity(bytes.count) + var i = 0 + while i < bytes.count { + let b = bytes[i] + if b == UInt8(ascii: "&") { + let decoded = XMLEntities.decode(bytes: bytes, at: i, mode: .html) + out.append(contentsOf: decoded.bytes) + i = decoded.nextIndex + } else { + out.append(b) + i += 1 + } + } + return String(decoding: out, as: UTF8.self) + } +} diff --git a/Shared/Extensions/ArticleStringFormatter.swift b/Shared/Extensions/ArticleStringFormatter.swift index f99142754..1a064837d 100644 --- a/Shared/Extensions/ArticleStringFormatter.swift +++ b/Shared/Extensions/ArticleStringFormatter.swift @@ -81,7 +81,7 @@ import RSParser s = s.replacingOccurrences(of: "\t", with: "") if !forHTML { - s = s.rsparser_stringByDecodingHTMLEntities() + s = s.decodingHTMLEntities() } s = s.trimmingWhitespace @@ -113,7 +113,7 @@ import RSParser if let cachedBody = summaryCache[key] { return cachedBody } - var s = body.rsparser_stringByDecodingHTMLEntities() + var s = body.decodingHTMLEntities() s = s.strippingHTML(maxCharacters: 300) if s == "Comments" { // Hacker News. s = "" diff --git a/Shared/Extensions/NSAttributedString+Extensions.swift b/Shared/Extensions/NSAttributedString+Extensions.swift index 0371ff9c7..6e9164169 100644 --- a/Shared/Extensions/NSAttributedString+Extensions.swift +++ b/Shared/Extensions/NSAttributedString+Extensions.swift @@ -288,7 +288,6 @@ private struct CountedSet where Element: Hashable { private extension String { var decodedEntity: String { - // It's possible the implementation will change, but for now it just calls this. - (self as NSString).rsparser_stringByDecodingHTMLEntities() as String + decodingHTMLEntities() } }