From 5f9d6fd81b80ded9de342e7205da21a29f8b477c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Wed, 13 Oct 2021 21:27:39 -0700 Subject: [PATCH] Handle srcset images with no space after comma --- reader/sanitizer/sanitizer.go | 12 ++++-------- reader/sanitizer/sanitizer_test.go | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/reader/sanitizer/sanitizer.go b/reader/sanitizer/sanitizer.go index d86fa64b..89df6851 100644 --- a/reader/sanitizer/sanitizer.go +++ b/reader/sanitizer/sanitizer.go @@ -19,7 +19,7 @@ import ( var ( youtubeEmbedRegex = regexp.MustCompile(`//www\.youtube\.com/embed/(.*)`) - splitSrcsetRegex = regexp.MustCompile(`,\s+`) + splitSrcsetRegex = regexp.MustCompile(`,\s?`) ) // Sanitize returns safe HTML. @@ -457,13 +457,9 @@ func sanitizeSrcsetAttr(baseURL, value string) string { nbParts := len(parts) if nbParts > 0 { - sanitizedSource := parts[0] - if !strings.HasPrefix(parts[0], "data:") { - var err error - sanitizedSource, err = url.AbsoluteURL(baseURL, parts[0]) - if err != nil { - continue - } + sanitizedSource, err := url.AbsoluteURL(baseURL, parts[0]) + if err != nil { + continue } if nbParts == 2 && isValidWidthOrDensityDescriptor(parts[1]) { diff --git a/reader/sanitizer/sanitizer_test.go b/reader/sanitizer/sanitizer_test.go index 72729b37..fedb98ee 100644 --- a/reader/sanitizer/sanitizer_test.go +++ b/reader/sanitizer/sanitizer_test.go @@ -45,16 +45,6 @@ func TestImgWithSrcset(t *testing.T) { } } -func TestImgWithSrcsetAndDataURL(t *testing.T) { - input := `Example` - expected := `Example` - output := Sanitize("http://example.org/", input) - - if output != expected { - t.Errorf(`Wrong output: %s`, output) - } -} - func TestSourceWithSrcsetAndMedia(t *testing.T) { input := `` expected := `` @@ -75,6 +65,16 @@ func TestMediumImgWithSrcset(t *testing.T) { } } +func TestEconomistImgWithSrcset(t *testing.T) { + input := `` + expected := `` + output := Sanitize("http://example.org/", input) + + if output != expected { + t.Errorf(`Wrong output: %s`, output) + } +} + func TestSelfClosingTags(t *testing.T) { input := `

This
is a text
with an image: Test.

` output := Sanitize("http://example.org/", input)