@@ -88,7 +88,8 @@ def patch_Img2PictureTag_picture_variants():
8888 "sourceset" : [
8989 {
9090 "scale" : "preview" ,
91- "additionalScales" : ["preview" , "large" , "larger" ],
91+ "additionalScales" : ["large" , "larger" ],
92+ "sizes" : "(min-width: 576px) 350px, (min-width: 768px) 600px, 98vw" ,
9293 }
9394 ],
9495 },
@@ -538,22 +539,57 @@ def testGetPictureTagByName(self, mock_uuid_to_object):
538539 mock_uuid_to_object .return_value = self .item
539540 tag = self .scaling .picture ("image" , picture_variant = "medium" )
540541 expected = """<picture>
541- <source srcset="http://nohost/item/@@images/image-600-....png 600w,
542+ <source... srcset="http://nohost/item/@@images/image-600-....png 600w,
542543http://nohost/item/@@images/image-400-....png 400w,
543544http://nohost/item/@@images/image-800-....png 800w,
544545http://nohost/item/@@images/image-1000-....png 1000w,
545- http://nohost/item/@@images/image-1200-....png 1200w"/>
546+ http://nohost/item/@@images/image-1200-....png 1200w"... />
546547 <img...src="http://nohost/item/@@images/image-600-....png".../>
547548</picture>"""
548549 self .assertTrue (_ellipsis_match (expected , tag .strip ()))
550+ # The exact placement of the img tag attributes can differ, especially
551+ # with different beautifulsoup versions.
552+ # So check here that all attributes are present.
553+ self .assertIn ('height="200"' , tag )
554+ self .assertIn ('loading="lazy"' , tag )
555+ self .assertIn ('title="foo"' , tag )
556+ self .assertIn ('width="200"' , tag )
557+ self .assertIn ('sizes="(min-width: 576px) 600px, 98vw"' , tag )
549558
559+ @patch .object (
560+ plone .namedfile .scaling ,
561+ "get_picture_variants" ,
562+ new = patch_Img2PictureTag_picture_variants ,
563+ spec = True ,
564+ )
565+ @patch .object (
566+ plone .namedfile .picture ,
567+ "get_allowed_scales" ,
568+ new = patch_Img2PictureTag_allowed_scales ,
569+ spec = True ,
570+ )
571+ @patch .object (plone .namedfile .picture , "uuidToObject" , spec = True )
572+ def testGetPictureTagCustomSizes (self , mock_uuid_to_object ):
573+ ImageScaling ._sizes = patch_Img2PictureTag_allowed_scales ()
574+ mock_uuid_to_object .return_value = self .item
575+ tag = self .scaling .picture ("image" , picture_variant = "small" )
576+ expected = """<picture>
577+ <source...srcset="http://nohost/item/@@images/image-400-....png 400w,
578+ http://nohost/item/@@images/image-800-....png 800w,
579+ http://nohost/item/@@images/image-1000-....png 1000w".../>
580+ <img...src="http://nohost/item/@@images/image-400-....png".../>
581+ </picture>"""
582+ self .assertTrue (_ellipsis_match (expected , tag .strip ()))
550583 # The exact placement of the img tag attributes can differ, especially
551584 # with different beautifulsoup versions.
552585 # So check here that all attributes are present.
553586 self .assertIn ('height="200"' , tag )
554587 self .assertIn ('loading="lazy"' , tag )
555588 self .assertIn ('title="foo"' , tag )
556589 self .assertIn ('width="200"' , tag )
590+ self .assertIn (
591+ 'sizes="(min-width: 576px) 350px, (min-width: 768px) 600px, 98vw"' , tag
592+ )
557593
558594 @patch .object (
559595 plone .namedfile .scaling ,
@@ -579,11 +615,11 @@ def testGetPictureTagWithAltAndTitle(self, mock_uuid_to_object):
579615 )
580616 base = self .item .absolute_url ()
581617 expected = f"""<picture>
582- <source srcset="{ base } /@@images/image-600-....png 600w,
618+ <source... srcset="{ base } /@@images/image-600-....png 600w,
583619{ base } /@@images/image-400-....png 400w,
584620{ base } /@@images/image-800-....png 800w,
585621{ base } /@@images/image-1000-....png 1000w,
586- { base } /@@images/image-1200-....png 1200w"/>
622+ { base } /@@images/image-1200-....png 1200w"... />
587623 <img...src="{ base } /@@images/image-600-....png".../>
588624</picture>"""
589625 self .assertTrue (_ellipsis_match (expected , tag .strip ()))
0 commit comments