Handle < in title that is not used as beginning of HTML tag.

This commit is contained in:
Brent Simmons committed 2026-07-04 17:26:43 -07:00
1 parent 3a948b7556
commit de464b6a34
3 files changed
+49 -12

No files matched your search

+16 -4
View File
@@ -233,25 +233,37 @@ import RSParser
let tagName = Array(utf8[nameStart..<nameEnd])
let isAllowed = allowedTagsBytes.contains(tagName)
// Only emit a closing `>` 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..<tagEnd])
out.append(gt)
if tagWasClosed {
out.append(gt)
}
}
} else {
if forHTML {
out.append(contentsOf: ltEntity)
out.append(contentsOf: utf8[tagStart..<tagEnd])
out.append(contentsOf: gtEntity)
if tagWasClosed {
out.append(contentsOf: gtEntity)
}
} else {
out.append(lt)
out.append(contentsOf: utf8[tagStart..<tagEnd])
out.append(gt)
if tagWasClosed {
out.append(gt)
}
}
}
i = (j < count) ? j + 1 : count
i = tagWasClosed ? j + 1 : count
}
return String(decoding: out, as: UTF8.self)
@@ -109,4 +109,14 @@ import UIKit
XCTAssertEqual(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 `>`.
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")
}
}
@@ -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 `<tag>` with synthesized `>`")
@Test("Unclosed tag with forHTML=false keeps `<tag`, no synthesized `>`")
func unclosedTagNotForHTML() {
#expect(ArticleStringFormatter.sanitizedTitle("foo <bar", forHTML: false) == "foo <bar>")
#expect(ArticleStringFormatter.sanitizedTitle("foo <bar", forHTML: false) == "foo <bar")
}
@Test("Unclosed tag with forHTML=true emits escaped `&lt;tag&gt;`")
@Test("Unclosed tag with forHTML=true escapes `<` and keeps text, no synthesized `>`")
func unclosedTagForHTML() {
#expect(ArticleStringFormatter.sanitizedTitle("foo <bar", forHTML: true) == "foo &lt;bar&gt;")
#expect(ArticleStringFormatter.sanitizedTitle("foo <bar", forHTML: true) == "foo &lt;bar")
}
// #4742: a title with a literal `<` (from an entity like `&#x3C;`)
// followed by non-tag text — here `<16s` — must not be truncated at
// the `<`, and must not gain a spurious trailing `>`. 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: &lt;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.