Improved regex, that's not greedy and only matches inside html tags

This commit is contained in:
Dan Berglund committed 2026-10-08 19:14:14 +02:00
1 parent 69ff87bda1
commit bdf638ba30
2 files changed
+4 -3

No files matched your search

@@ -205,8 +205,8 @@ struct ArticleRenderingSpecialCases {
///
/// Doing this allows inline footnotes to work, without it they are broken, since they rely on matching `id` and `class` attributes.
static func desanitizeFreshRSSContent(_ content: String) -> String {
let classRegex = /(?'open'<.*)data-sanitized-class(?'close'.*>)/
let idRegex = /(?'open'<.*)data-sanitized-id(?'close'.*>)/
let classRegex = /(?'open'<[^>]*?)data-sanitized-class(?'close'[^<]*?>)/
let idRegex = /(?'open'<[^>]*?)data-sanitized-id(?'close'[^<]*?>)/
return content
.replacing(classRegex, with: { match in
match.output.open + "class" + match.output.close
@@ -117,6 +117,7 @@ import Testing
<a href="#footnote-1-ref" data-sanitized-class="footnoteBackLink">Go back to content</a>
</p>
</li>
<li data-sanitized-id="footnote-2"><p>Another footnote, about `data-sanitized-id`.<a href="#footnote-2-ref" data-sanitized-class="footnoteBackLink">Go back to content</a></p></li>
</div>
"""
let expected = """
@@ -127,10 +128,10 @@ import Testing
<a href="#footnote-1-ref" class="footnoteBackLink">Go back to content</a>
</p>
</li>
<li id="footnote-2"><p>Another footnote, about `data-sanitized-id`.<a href="#footnote-2-ref" class="footnoteBackLink">Go back to content</a></p></li>
</div>
"""
#expect(ArticleRenderingSpecialCases.desanitizeFreshRSSContent(sanitized) == expected)
}
}