diff --git a/news/188.feature b/news/188.feature new file mode 100644 index 00000000..8e0d3af4 --- /dev/null +++ b/news/188.feature @@ -0,0 +1 @@ +Add default width and height attributes if none provided when using the srcset method @erral diff --git a/src/plone/namedfile/scaling.py b/src/plone/namedfile/scaling.py index 4d5ebb0d..f4bd8204 100644 --- a/src/plone/namedfile/scaling.py +++ b/src/plone/namedfile/scaling.py @@ -801,6 +801,10 @@ def srcset( scale = self.scale(fieldname=fieldname, scale=scale_in_src) attributes["src"] = scale.url + if "width" not in attributes: + attributes["width"] = scale.width + if "height" not in attributes: + attributes["height"] = scale.height return _image_tag_from_values(*attributes.items()) diff --git a/src/plone/namedfile/tests/test_scaling.py b/src/plone/namedfile/tests/test_scaling.py index 47163b88..f66cd455 100644 --- a/src/plone/namedfile/tests/test_scaling.py +++ b/src/plone/namedfile/tests/test_scaling.py @@ -830,7 +830,7 @@ def testImgSrcSet(self): } tag = self.scaling.srcset("image", sizes="50vw") base = self.item.absolute_url() - expected = f"""foo""" + expected = f"""foo""" self.assertTrue(_ellipsis_match(expected, tag.strip())) def testImgSrcSetCustomSrc(self): @@ -850,7 +850,7 @@ def testImgSrcSetCustomSrc(self): } tag = self.scaling.srcset("image", sizes="50vw", scale_in_src="mini") base = self.item.absolute_url() - expected = f"""foo""" + expected = f"""foo""" self.assertTrue(_ellipsis_match(expected, tag.strip())) def testImgSrcSetInexistentScale(self): @@ -874,7 +874,7 @@ def testImgSrcSetInexistentScale(self): "image", sizes="50vw", scale_in_src="inexistent-scale-name" ) base = self.item.absolute_url() - expected = f"""foo""" + expected = f"""foo""" self.assertTrue(_ellipsis_match(expected, tag.strip())) def testImgSrcSetCustomTitle(self): @@ -894,7 +894,7 @@ def testImgSrcSetCustomTitle(self): } tag = self.scaling.srcset("image", sizes="50vw", title="My Custom Title") base = self.item.absolute_url() - expected = f"""foo""" + expected = f"""foo""" self.assertTrue(_ellipsis_match(expected, tag.strip())) def testImgSrcSetAdditionalAttributes(self): @@ -922,7 +922,7 @@ def testImgSrcSetAdditionalAttributes(self): ) base = self.item.absolute_url() - expected = f"""This image shows nothing""" + expected = f"""This image shows nothing""" self.assertTrue(_ellipsis_match(expected, tag.strip()))