2026-10-27 13:54:50 -07:00
//
// HTMLImageAttributeInserterTests.swift
// RSParserTests
//
// Created by Brent Simmons on 10/27/26.
//
import Foundation
import Testing
import RSParser
@ Suite struct HTMLImageAttributeInserterTests {
@ Test func attributesAreInsertedIntoImagesWhoseSourceResolvesToURL () {
let html = """
<p>Café</p><img src=" / images / photo . jpg " alt=" Photo "><IMG SRC=" https : //example.com/images/photo.jpg">\
< img src = "/images/photo.jpg" srcset = "/images/photo-2x.jpg 2x" >< picture >< img src = "/images/photo.jpg" ></ picture > \
< img src = "/images/other.jpg" >
"""
2026-11-03 16:04:05 -08:00
let result = HTMLImageAttributeInserter.insertingAttributes(" src = \ "nnw:x \" " , intoImagesWithSource : "https://example.com/images/photo.jpg" , html : html , baseURL : URL ( string : "https://example.com/2026/10/post.html" ))
2026-10-27 13:54:50 -07:00
let expected = """
<p>Café</p><img src=" nnw : x " src=" / images / photo . jpg " alt=" Photo "><IMG src=" nnw : x " SRC=" https : //example.com/images/photo.jpg">\
< img src = "/images/photo.jpg" srcset = "/images/photo-2x.jpg 2x" >< picture >< img src = "/images/photo.jpg" ></ picture > \
< img src = "/images/other.jpg" >
"""
#expect(result == expected)
}
2026-10-29 09:21:12 -07:00
@Test func imagesWrittenAsTextInTextareaAreLeftAlone() {
let html = #" < textarea > Embed : < img src = "/images/photo.jpg" ></ textarea >< img src = "/images/photo.jpg" > "#
2026-11-03 16:04:05 -08:00
let result = HTMLImageAttributeInserter.insertingAttributes(" src = \ "nnw:x \" " , intoImagesWithSource : "https://example.com/images/photo.jpg" , html : html , baseURL : URL ( string : "https://example.com/" ))
2026-10-29 09:21:12 -07:00
# expect ( result == # "<textarea>Embed: <img src=" / images / photo . jpg "></textarea><img src=" nnw : x " src=" / images / photo . jpg ">" #)
}
2026-10-27 13:54:50 -07:00
}