diff --git a/internal/reader/sanitizer/sanitizer.go b/internal/reader/sanitizer/sanitizer.go index 4c6e30a7..e9aaf06f 100644 --- a/internal/reader/sanitizer/sanitizer.go +++ b/internal/reader/sanitizer/sanitizer.go @@ -303,14 +303,8 @@ func SanitizeHTML(baseURL, rawHTML string, sanitizerOptions *SanitizerOptions) s func sanitizeAttributes(parsedBaseUrl *url.URL, baseURL, tagName string, attributes []html.Attribute, sanitizerOptions *SanitizerOptions) ([]string, string) { var htmlAttrs, attrNames []string var err error - var isImageLargerThanLayout bool var isAnchorLink bool - if tagName == "img" { - imgWidth := getIntegerAttributeValue("width", attributes) - isImageLargerThanLayout = imgWidth > 750 - } - for _, attribute := range attributes { if !isValidAttribute(tagName, attribute.Key) { continue @@ -336,7 +330,12 @@ func sanitizeAttributes(parsedBaseUrl *url.URL, baseURL, tagName string, attribu continue } case "width", "height": - if isImageLargerThanLayout || !isPositiveInteger(value) { + if !isPositiveInteger(value) { + continue + } + + // Discard width and height attributes when width is larger than Miniflux layout (750px) + if imgWidth := getIntegerAttributeValue("width", attributes); imgWidth > 750 { continue } case "srcset": diff --git a/internal/reader/sanitizer/sanitizer_test.go b/internal/reader/sanitizer/sanitizer_test.go index 5ffb6a39..4d80302e 100644 --- a/internal/reader/sanitizer/sanitizer_test.go +++ b/internal/reader/sanitizer/sanitizer_test.go @@ -73,7 +73,7 @@ func TestImgWithWidthAndHeightAttribute(t *testing.T) { } } -func TestImgWithWidthAndHeightAttributeLargerThanMinifluxLayout(t *testing.T) { +func TestImgWithWidthAttributeLargerThanMinifluxLayout(t *testing.T) { input := `` expected := `` output := SanitizeHTMLWithDefaultOptions("http://example.org/", input) @@ -93,7 +93,17 @@ func TestImgWithIncorrectWidthAndHeightAttribute(t *testing.T) { } } -func TestImgWithEmptywidthAndHeightAttribute(t *testing.T) { +func TestImgWithIncorrectWidthAttribute(t *testing.T) { + input := `` + expected := `` + output := SanitizeHTMLWithDefaultOptions("http://example.org/", input) + + if output != expected { + t.Errorf(`Wrong output: %s`, output) + } +} + +func TestImgWithEmptyWidthAndHeightAttribute(t *testing.T) { input := `` expected := `` output := SanitizeHTMLWithDefaultOptions("http://example.org/", input) @@ -103,6 +113,36 @@ func TestImgWithEmptywidthAndHeightAttribute(t *testing.T) { } } +func TestImgWithIncorrectHeightAttribute(t *testing.T) { + input := `` + expected := `` + output := SanitizeHTMLWithDefaultOptions("http://example.org/", input) + + if output != expected { + t.Errorf(`Wrong output: %s`, output) + } +} + +func TestImgWithNegativeWidthAttribute(t *testing.T) { + input := `` + expected := `` + output := SanitizeHTMLWithDefaultOptions("http://example.org/", input) + + if output != expected { + t.Errorf(`Wrong output: %s`, output) + } +} + +func TestImgWithNegativeHeightAttribute(t *testing.T) { + input := `` + expected := `` + output := SanitizeHTMLWithDefaultOptions("http://example.org/", input) + + if output != expected { + t.Errorf(`Wrong output: %s`, output) + } +} + func TestImgWithTextDataURL(t *testing.T) { input := `Example` expected := ``