diff --git a/reader/rewrite/rewrite_functions.go b/reader/rewrite/rewrite_functions.go index c01545c9..8bcc1091 100644 --- a/reader/rewrite/rewrite_functions.go +++ b/reader/rewrite/rewrite_functions.go @@ -100,9 +100,14 @@ func addDynamicImage(entryURL, entryContent string) string { "data-380src", } + candidateSrcsetAttrs := []string{ + "data-srcset", + } + changed := false doc.Find("img,div").Each(func(i int, img *goquery.Selection) { + // Src-linked candidates for _, candidateAttr := range candidateAttrs { if srcAttr, found := img.Attr(candidateAttr); found { changed = true @@ -117,6 +122,22 @@ func addDynamicImage(entryURL, entryContent string) string { break } } + + // Srcset-linked candidates + for _, candidateAttr := range candidateSrcsetAttrs { + if srcAttr, found := img.Attr(candidateAttr); found { + changed = true + + if img.Is("img") { + img.SetAttr("srcset", srcAttr) + } else { + altAttr := img.AttrOr("alt", "") + img.ReplaceWithHtml(`` + altAttr + ``) + } + + break + } + } }) if !changed { diff --git a/reader/rewrite/rewriter_test.go b/reader/rewrite/rewriter_test.go index 7f63473e..38e3df93 100644 --- a/reader/rewrite/rewriter_test.go +++ b/reader/rewrite/rewriter_test.go @@ -176,6 +176,26 @@ func TestRewriteWithUnknownLazyNoScriptImage(t *testing.T) { } } +func TestRewriteWithLazySrcset(t *testing.T) { + description := `Image` + output := Rewriter("https://example.org/article", description, "add_dynamic_image") + expected := `Image` + + if expected != output { + t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) + } +} + +func TestRewriteWithImageAndLazySrcset(t *testing.T) { + description := `Image` + output := Rewriter("https://example.org/article", description, "add_dynamic_image") + expected := `Image` + + if expected != output { + t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) + } +} + func TestNewLineRewriteRule(t *testing.T) { description := "A\nB\nC" output := Rewriter("https://example.org/article", description, "nl2br")