1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-06-27 16:36:00 +00:00

feat(sanitizer): allow img tags with only a srcset and no src attribute

This commit is contained in:
Frédéric Guillot 2025-02-15 17:57:01 -08:00
parent 991c54150d
commit 462ba8d7f7
2 changed files with 13 additions and 3 deletions

View file

@ -296,9 +296,9 @@ func hasRequiredAttributes(tagName string, attributes []string) bool {
switch tagName { switch tagName {
case "a": case "a":
return slices.Contains(attributes, "href") return slices.Contains(attributes, "href")
case "iframe", "img": case "iframe":
return slices.Contains(attributes, "src") return slices.Contains(attributes, "src")
case "source": case "source", "img":
return slices.Contains(attributes, "src") || slices.Contains(attributes, "srcset") return slices.Contains(attributes, "src") || slices.Contains(attributes, "srcset")
default: default:
return true return true

View file

@ -119,7 +119,7 @@ func TestImgWithDataURL(t *testing.T) {
} }
} }
func TestImgWithSrcset(t *testing.T) { func TestImgWithSrcsetAttribute(t *testing.T) {
input := `<img srcset="example-320w.jpg, example-480w.jpg 1.5x, example-640w.jpg 2x, example-640w.jpg 640w" src="example-640w.jpg" alt="Example">` input := `<img srcset="example-320w.jpg, example-480w.jpg 1.5x, example-640w.jpg 2x, example-640w.jpg 640w" src="example-640w.jpg" alt="Example">`
expected := `<img srcset="http://example.org/example-320w.jpg, http://example.org/example-480w.jpg 1.5x, http://example.org/example-640w.jpg 2x, http://example.org/example-640w.jpg 640w" src="http://example.org/example-640w.jpg" alt="Example" loading="lazy">` expected := `<img srcset="http://example.org/example-320w.jpg, http://example.org/example-480w.jpg 1.5x, http://example.org/example-640w.jpg 2x, http://example.org/example-640w.jpg 640w" src="http://example.org/example-640w.jpg" alt="Example" loading="lazy">`
output := Sanitize("http://example.org/", input) output := Sanitize("http://example.org/", input)
@ -129,6 +129,16 @@ func TestImgWithSrcset(t *testing.T) {
} }
} }
func TestImgWithSrcsetAndNoSrcAttribute(t *testing.T) {
input := `<img srcset="example-320w.jpg, example-480w.jpg 1.5x, example-640w.jpg 2x, example-640w.jpg 640w" alt="Example">`
expected := `<img srcset="http://example.org/example-320w.jpg, http://example.org/example-480w.jpg 1.5x, http://example.org/example-640w.jpg 2x, http://example.org/example-640w.jpg 640w" alt="Example" loading="lazy">`
output := Sanitize("http://example.org/", input)
if output != expected {
t.Errorf(`Wrong output: %s`, output)
}
}
func TestSourceWithSrcsetAndMedia(t *testing.T) { func TestSourceWithSrcsetAndMedia(t *testing.T) {
input := `<picture><source media="(min-width: 800px)" srcset="elva-800w.jpg"></picture>` input := `<picture><source media="(min-width: 800px)" srcset="elva-800w.jpg"></picture>`
expected := `<picture><source media="(min-width: 800px)" srcset="http://example.org/elva-800w.jpg"></picture>` expected := `<picture><source media="(min-width: 800px)" srcset="http://example.org/elva-800w.jpg"></picture>`