瀏覽代碼

test(sanitizer): enhance tests for image width and height attributes

Frédéric Guillot 9 月之前
父節點
當前提交
cb617ff6e0
共有 2 個文件被更改,包括 48 次插入9 次删除
  1. 6 7
      internal/reader/sanitizer/sanitizer.go
  2. 42 2
      internal/reader/sanitizer/sanitizer_test.go

+ 6 - 7
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":

+ 42 - 2
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 := `<img src="https://example.org/image.png" width="1200" height="675">`
 	expected := `<img src="https://example.org/image.png" loading="lazy">`
 	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 := `<img src="https://example.org/image.png" width="10px" height="20">`
+	expected := `<img src="https://example.org/image.png" height="20" loading="lazy">`
+	output := SanitizeHTMLWithDefaultOptions("http://example.org/", input)
+
+	if output != expected {
+		t.Errorf(`Wrong output: %s`, output)
+	}
+}
+
+func TestImgWithEmptyWidthAndHeightAttribute(t *testing.T) {
 	input := `<img src="https://example.org/image.png" width="" height="">`
 	expected := `<img src="https://example.org/image.png" loading="lazy">`
 	output := SanitizeHTMLWithDefaultOptions("http://example.org/", input)
@@ -103,6 +113,36 @@ func TestImgWithEmptywidthAndHeightAttribute(t *testing.T) {
 	}
 }
 
+func TestImgWithIncorrectHeightAttribute(t *testing.T) {
+	input := `<img src="https://example.org/image.png" width="10" height="20px">`
+	expected := `<img src="https://example.org/image.png" width="10" loading="lazy">`
+	output := SanitizeHTMLWithDefaultOptions("http://example.org/", input)
+
+	if output != expected {
+		t.Errorf(`Wrong output: %s`, output)
+	}
+}
+
+func TestImgWithNegativeWidthAttribute(t *testing.T) {
+	input := `<img src="https://example.org/image.png" width="-10" height="20">`
+	expected := `<img src="https://example.org/image.png" height="20" loading="lazy">`
+	output := SanitizeHTMLWithDefaultOptions("http://example.org/", input)
+
+	if output != expected {
+		t.Errorf(`Wrong output: %s`, output)
+	}
+}
+
+func TestImgWithNegativeHeightAttribute(t *testing.T) {
+	input := `<img src="https://example.org/image.png" width="10" height="-20">`
+	expected := `<img src="https://example.org/image.png" width="10" loading="lazy">`
+	output := SanitizeHTMLWithDefaultOptions("http://example.org/", input)
+
+	if output != expected {
+		t.Errorf(`Wrong output: %s`, output)
+	}
+}
+
 func TestImgWithTextDataURL(t *testing.T) {
 	input := `<img src="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" alt="Example">`
 	expected := ``