@@ -181,6 +181,7 @@ public function transform(Document $document, ErrorCollection $errors)
181
181
}
182
182
183
183
for ($ index = 0 ; $ index < $ heroImageCount ; $ index ++) {
184
+ $ this ->removeLazyLoading ($ heroImages [$ index ]);
184
185
$ this ->generatePreload ($ heroImages [$ index ], $ document , $ errors );
185
186
$ this ->generateImg ($ heroImages [$ index ], $ document );
186
187
}
@@ -470,22 +471,12 @@ private function getPlaceholderImage(Element $element)
470
471
}
471
472
472
473
/**
473
- * Generate the preload link for a given hero image.
474
+ * Remove the lazy loading from the hero image.
474
475
*
475
- * @param HeroImage $heroImage Hero image to generate the preload link for.
476
- * @param Document $document Document to generate the preload link in.
477
- * @param ErrorCollection $errors Collection of errors that are collected during transformation.
476
+ * @param HeroImage $heroImage Hero image to remove the lazy loading for.
478
477
*/
479
- private function generatePreload (
480
- HeroImage $ heroImage ,
481
- Document $ document ,
482
- ErrorCollection $ errors
483
- ) {
484
- if ($ heroImage ->getSrcset () && ! $ this ->supportsSrcset ()) {
485
- $ errors ->add (Error \CannotPreloadImage::fromImageWithSrcsetAttribute ($ heroImage ->getAmpImg ()));
486
- return ;
487
- }
488
-
478
+ private function removeLazyLoading (HeroImage $ heroImage )
479
+ {
489
480
$ img = $ heroImage ->getAmpImg ();
490
481
491
482
if (
@@ -495,13 +486,28 @@ private function generatePreload(
495
486
) {
496
487
$ img ->removeAttribute (Attribute::LOADING );
497
488
}
489
+ }
498
490
491
+ /**
492
+ * Generate the preload link for a given hero image.
493
+ *
494
+ * @param HeroImage $heroImage Hero image to generate the preload link for.
495
+ * @param Document $document Document to generate the preload link in.
496
+ * @param ErrorCollection $errors Collection of errors that are collected during transformation.
497
+ */
498
+ private function generatePreload (HeroImage $ heroImage , Document $ document , ErrorCollection $ errors )
499
+ {
499
500
if (empty ($ heroImage ->getMedia ())) {
500
501
// We can only safely preload a hero image if there's a media attribute
501
502
// as we can't detect whether it's hidden on certain viewport sizes otherwise.
502
503
return ;
503
504
}
504
505
506
+ if ($ heroImage ->getSrcset () && ! $ this ->supportsSrcset ()) {
507
+ $ errors ->add (Error \CannotPreloadImage::fromImageWithSrcsetAttribute ($ heroImage ->getAmpImg ()));
508
+ return ;
509
+ }
510
+
505
511
if ($ this ->hasExistingImagePreload ($ document , $ heroImage ->getSrc ())) {
506
512
return ;
507
513
}
@@ -517,6 +523,7 @@ private function generatePreload(
517
523
$ preload ->appendChild ($ document ->createAttribute (Attribute::DATA_HERO ));
518
524
if ($ heroImage ->getSrcset ()) {
519
525
$ preload ->setAttribute (Attribute::IMAGESRCSET , $ heroImage ->getSrcset ());
526
+ $ img = $ heroImage ->getAmpImg ();
520
527
if ($ img && $ img ->hasAttribute (Attribute::SIZES )) {
521
528
$ preload ->setAttribute (Attribute::IMAGESIZES , $ img ->getAttribute (Attribute::SIZES ));
522
529
}
0 commit comments