diff --git a/Shared/Extensions/ArticleStringFormatter.swift b/Shared/Extensions/ArticleStringFormatter.swift index 037bd1768..ec45d85c2 100644 --- a/Shared/Extensions/ArticleStringFormatter.swift +++ b/Shared/Extensions/ArticleStringFormatter.swift @@ -233,25 +233,37 @@ import RSParser let tagName = Array(utf8[nameStart..` if the input actually had one. An + // unclosed tag (no `>` before end of input) must not gain a + // synthesized `>` — otherwise a title like `<16s in UK…` + // renders with a spurious trailing `>` (issue #4742). + let tagWasClosed = j < count + if isAllowed { if forHTML { out.append(lt) out.append(contentsOf: utf8[tagStart..`. + func testLiteralLessThanTitleIsNotTruncated() { + let input = "No place in children's hands: <16s in UK to be banned" + let sanitized = ArticleStringFormatter.sanitizedTitle(input, forHTML: true)! + let s = NSAttributedString(simpleHTML: sanitized) + XCTAssertEqual(s.string, "No place in children's hands: <16s in UK to be banned") + } + } diff --git a/Tests/NetNewsWireTests/SanitizedTitleTests.swift b/Tests/NetNewsWireTests/SanitizedTitleTests.swift index f6a86dfa9..687c24bd9 100644 --- a/Tests/NetNewsWireTests/SanitizedTitleTests.swift +++ b/Tests/NetNewsWireTests/SanitizedTitleTests.swift @@ -137,19 +137,34 @@ import Testing // MARK: - Malformed input - // Unclosed tag (no `>` to terminate). We scan to end of input as - // the tag body and still emit closing punctuation — a slightly - // weird but stable behavior inherited from the Scanner-based - // predecessor, which tests pinned to preserve. + // Unclosed tag (no `>` to terminate). The `<` and its trailing text + // are kept, but no closing `>` is synthesized — so a literal `<` + // followed by text stays faithful to the input (issue #4742). - @Test("Unclosed tag with forHTML=false emits literal `` with synthesized `>`") + @Test("Unclosed tag with forHTML=false keeps ``") func unclosedTagNotForHTML() { - #expect(ArticleStringFormatter.sanitizedTitle("foo ") + #expect(ArticleStringFormatter.sanitizedTitle("foo `") func unclosedTagForHTML() { - #expect(ArticleStringFormatter.sanitizedTitle("foo `. The whole + // title survives; the `<` is escaped so `simpleHTML` shows it as text. + @Test func literalLessThanInTitleIsNotTruncated() { + let input = "No place in children's hands: <16s in UK to be banned" + #expect( + ArticleStringFormatter.sanitizedTitle(input, forHTML: true) + == "No place in children's hands: <16s in UK to be banned" + ) + #expect( + ArticleStringFormatter.sanitizedTitle(input, forHTML: false) + == "No place in children's hands: <16s in UK to be banned" + ) } // Bare `<` at end of input: no tag body, emit nothing for it.