mirror of
https://github.com/miniflux/v2.git
synced 2025-08-06 17:41:00 +00:00
test(sanitizer): enhance tests for image width and height attributes
This commit is contained in:
parent
8c3f280f32
commit
cb617ff6e0
2 changed files with 48 additions and 9 deletions
|
@ -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) {
|
func sanitizeAttributes(parsedBaseUrl *url.URL, baseURL, tagName string, attributes []html.Attribute, sanitizerOptions *SanitizerOptions) ([]string, string) {
|
||||||
var htmlAttrs, attrNames []string
|
var htmlAttrs, attrNames []string
|
||||||
var err error
|
var err error
|
||||||
var isImageLargerThanLayout bool
|
|
||||||
var isAnchorLink bool
|
var isAnchorLink bool
|
||||||
|
|
||||||
if tagName == "img" {
|
|
||||||
imgWidth := getIntegerAttributeValue("width", attributes)
|
|
||||||
isImageLargerThanLayout = imgWidth > 750
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, attribute := range attributes {
|
for _, attribute := range attributes {
|
||||||
if !isValidAttribute(tagName, attribute.Key) {
|
if !isValidAttribute(tagName, attribute.Key) {
|
||||||
continue
|
continue
|
||||||
|
@ -336,7 +330,12 @@ func sanitizeAttributes(parsedBaseUrl *url.URL, baseURL, tagName string, attribu
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
case "width", "height":
|
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
|
continue
|
||||||
}
|
}
|
||||||
case "srcset":
|
case "srcset":
|
||||||
|
|
|
@ -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">`
|
input := `<img src="https://example.org/image.png" width="1200" height="675">`
|
||||||
expected := `<img src="https://example.org/image.png" loading="lazy">`
|
expected := `<img src="https://example.org/image.png" loading="lazy">`
|
||||||
output := SanitizeHTMLWithDefaultOptions("http://example.org/", input)
|
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="">`
|
input := `<img src="https://example.org/image.png" width="" height="">`
|
||||||
expected := `<img src="https://example.org/image.png" loading="lazy">`
|
expected := `<img src="https://example.org/image.png" loading="lazy">`
|
||||||
output := SanitizeHTMLWithDefaultOptions("http://example.org/", input)
|
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) {
|
func TestImgWithTextDataURL(t *testing.T) {
|
||||||
input := `<img src="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" alt="Example">`
|
input := `<img src="data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" alt="Example">`
|
||||||
expected := ``
|
expected := ``
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue