@@ -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,93 @@ 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 )
558+
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 testGetPictureTagByNameNotLazy (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 = "medium" , lazy = False )
576+ print (tag )
577+ expected = """<picture>
578+ <source...srcset="http://nohost/item/@@images/image-600-....png 600w,
579+ http://nohost/item/@@images/image-400-....png 400w,
580+ http://nohost/item/@@images/image-800-....png 800w,
581+ http://nohost/item/@@images/image-1000-....png 1000w,
582+ http://nohost/item/@@images/image-1200-....png 1200w".../>
583+ <img...src="http://nohost/item/@@images/image-600-....png".../>
584+ </picture>"""
585+ self .assertTrue (_ellipsis_match (expected , tag .strip ()))
586+ # The exact placement of the img tag attributes can differ, especially
587+ # with different beautifulsoup versions.
588+ # So check here that all attributes are present.
589+ self .assertIn ('height="200"' , tag )
590+ self .assertNotIn ('loading="lazy"' , tag )
591+ self .assertIn ('title="foo"' , tag )
592+ self .assertIn ('width="200"' , tag )
593+ self .assertIn ('sizes="(min-width: 576px) 600px, 98vw"' , tag )
549594
595+ @patch .object (
596+ plone .namedfile .scaling ,
597+ "get_picture_variants" ,
598+ new = patch_Img2PictureTag_picture_variants ,
599+ spec = True ,
600+ )
601+ @patch .object (
602+ plone .namedfile .picture ,
603+ "get_allowed_scales" ,
604+ new = patch_Img2PictureTag_allowed_scales ,
605+ spec = True ,
606+ )
607+ @patch .object (plone .namedfile .picture , "uuidToObject" , spec = True )
608+ def testGetPictureTagCustomSizes (self , mock_uuid_to_object ):
609+ ImageScaling ._sizes = patch_Img2PictureTag_allowed_scales ()
610+ mock_uuid_to_object .return_value = self .item
611+ tag = self .scaling .picture ("image" , picture_variant = "small" )
612+ expected = """<picture>
613+ <source...srcset="http://nohost/item/@@images/image-400-....png 400w,
614+ http://nohost/item/@@images/image-800-....png 800w,
615+ http://nohost/item/@@images/image-1000-....png 1000w".../>
616+ <img...src="http://nohost/item/@@images/image-400-....png".../>
617+ </picture>"""
618+ self .assertTrue (_ellipsis_match (expected , tag .strip ()))
550619 # The exact placement of the img tag attributes can differ, especially
551620 # with different beautifulsoup versions.
552621 # So check here that all attributes are present.
553622 self .assertIn ('height="200"' , tag )
554623 self .assertIn ('loading="lazy"' , tag )
555624 self .assertIn ('title="foo"' , tag )
556625 self .assertIn ('width="200"' , tag )
626+ self .assertIn (
627+ 'sizes="(min-width: 576px) 350px, (min-width: 768px) 600px, 98vw"' , tag
628+ )
557629
558630 @patch .object (
559631 plone .namedfile .scaling ,
@@ -579,11 +651,11 @@ def testGetPictureTagWithAltAndTitle(self, mock_uuid_to_object):
579651 )
580652 base = self .item .absolute_url ()
581653 expected = f"""<picture>
582- <source srcset="{ base } /@@images/image-600-....png 600w,
654+ <source... srcset="{ base } /@@images/image-600-....png 600w,
583655{ base } /@@images/image-400-....png 400w,
584656{ base } /@@images/image-800-....png 800w,
585657{ base } /@@images/image-1000-....png 1000w,
586- { base } /@@images/image-1200-....png 1200w"/>
658+ { base } /@@images/image-1200-....png 1200w"... />
587659 <img...src="{ base } /@@images/image-600-....png".../>
588660</picture>"""
589661 self .assertTrue (_ellipsis_match (expected , tag .strip ()))
0 commit comments