@@ -437,38 +437,41 @@ public function get_common_lcp_element(): ?OD_Element {
437
437
438
438
$ result = ( function () {
439
439
440
- // If every group isn't populated, then we can't say whether there is a common LCP element across every viewport group.
441
- if ( ! $ this ->is_every_group_populated () ) {
440
+ // Ensure both the narrowest (first) and widest (last) viewport groups are populated.
441
+ $ first_group = $ this ->get_first_group ();
442
+ $ last_group = $ this ->get_last_group ();
443
+ if ( $ first_group ->count () === 0 || $ last_group ->count () === 0 ) {
442
444
return null ;
443
445
}
444
446
445
- // Look at the LCP elements across all the viewport groups.
446
- $ groups_by_lcp_element_xpath = array ();
447
- $ lcp_elements_by_xpath = array ();
448
- $ group_has_unknown_lcp_element = false ;
449
- foreach ( $ this ->groups as $ group ) {
450
- $ lcp_element = $ group ->get_lcp_element ();
451
- if ( $ lcp_element instanceof OD_Element ) {
452
- $ groups_by_lcp_element_xpath [ $ lcp_element ->get_xpath () ][] = $ group ;
453
- $ lcp_elements_by_xpath [ $ lcp_element ->get_xpath () ][] = $ lcp_element ;
454
- } else {
455
- $ group_has_unknown_lcp_element = true ;
456
- }
457
- }
447
+ $ first_group_lcp_element = $ first_group ->get_lcp_element ();
448
+ $ last_group_lcp_element = $ last_group ->get_lcp_element ();
458
449
450
+ // Validate LCP elements exist and have matching XPaths in the extreme viewport groups.
459
451
if (
460
- // All breakpoints share the same LCP element.
461
- 1 === count ( $ groups_by_lcp_element_xpath )
462
- &&
463
- // The breakpoints don't share a common lack of a detected LCP element.
464
- ! $ group_has_unknown_lcp_element
452
+ ! $ first_group_lcp_element instanceof OD_Element
453
+ ||
454
+ ! $ last_group_lcp_element instanceof OD_Element
455
+ ||
456
+ $ first_group_lcp_element -> get_xpath () !== $ last_group_lcp_element -> get_xpath ()
465
457
) {
466
- $ xpath = key ( $ lcp_elements_by_xpath );
458
+ return null ; // No common LCP element across the narrowest and widest viewports.
459
+ }
467
460
468
- return $ lcp_elements_by_xpath [ $ xpath ][0 ];
461
+ // Check intermediate viewport groups for conflicting LCP elements.
462
+ $ num_groups = count ( $ this ->groups );
463
+ for ( $ i = 1 ; $ i < $ num_groups - 1 ; $ i ++ ) {
464
+ $ group_lcp_element = $ this ->groups [ $ i ]->get_lcp_element ();
465
+ if (
466
+ $ group_lcp_element instanceof OD_Element
467
+ &&
468
+ $ group_lcp_element ->get_xpath () !== $ first_group_lcp_element ->get_xpath ()
469
+ ) {
470
+ return null ; // Conflicting LCP element found in an intermediate group.
471
+ }
469
472
}
470
473
471
- return null ;
474
+ return $ first_group_lcp_element ;
472
475
} )();
473
476
474
477
$ this ->result_cache [ __FUNCTION__ ] = $ result ;
0 commit comments