Skip to content

Commit 8ca8b27

Browse files
committed
WIP: fix center alignments and cover blocks
1 parent 0790b92 commit 8ca8b27

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

plugins/auto-sizes/includes/improve-calculate-sizes.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@ function auto_sizes_calculate_better_sizes( int $id, string $size, string $align
162162

163163
/*
164164
* Map alignment values to a weighting value so they can be compared.
165-
* Note that 'left', 'right', and 'center' alignments are only constrained by default containers.
165+
* Note that 'left' and 'right' alignments are only constrained by max alignment.
166166
*/
167167
$constraints = array(
168168
'full' => 0,
169-
'wide' => 1,
170-
'left' => 2,
171-
'right' => 2,
172-
'center' => 2,
169+
'left' => 1,
170+
'right' => 1,
171+
'wide' => 2,
173172
'default' => 3,
173+
'center' => 3,
174174
);
175175

176176
$alignment = $constraints[ $align ] > $constraints[ $max_alignment ] ? $align : $max_alignment;
@@ -187,12 +187,12 @@ function auto_sizes_calculate_better_sizes( int $id, string $size, string $align
187187

188188
case 'left':
189189
case 'right':
190-
case 'center':
191190
// These alignment get constrained by the wide layout size but do not get stretched.
192191
$alignment = auto_sizes_get_layout_width( 'wide' );
193192
$layout_width = sprintf( '%1$spx', min( (int) $alignment, $image_width ) );
194193
break;
195194

195+
case 'center':
196196
default:
197197
$alignment = auto_sizes_get_layout_width( 'default' );
198198
$layout_width = sprintf( '%1$spx', min( (int) $alignment, $image_width ) );

plugins/auto-sizes/tests/test-improve-calculate-sizes.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,14 @@ public function data_image_sizes_for_left_right_center_alignment(): array {
273273
'sizes="(max-width: 300px) 100vw, 300px" ',
274274
'center',
275275
),
276-
'Return large image size 1024px with center alignment' => array(
276+
'Return large image size 620px with center alignment' => array(
277277
'large',
278-
'sizes="(max-width: 1024px) 100vw, 1024px" ',
278+
'sizes="(max-width: 620px) 100vw, 620px" ',
279279
'center',
280280
),
281-
'Return full image size 1080px with center alignment' => array(
281+
'Return full image size 620px with center alignment' => array(
282282
'full',
283-
'sizes="(max-width: 1080px) 100vw, 1080px" ',
283+
'sizes="(max-width: 620px) 100vw, 620px" ',
284284
'center',
285285
),
286286
'Return resized size 100px instead of medium image size 300px with left alignment' => array(
@@ -346,8 +346,9 @@ public function data_image_sizes_for_left_right_center_alignment(): array {
346346
* @dataProvider data_image_left_right_center_alignment
347347
*
348348
* @param string $alignment Alignment of the image.
349+
* @param string $expected Expected output.
349350
*/
350-
public function test_cover_block_with_left_right_center_alignment( string $alignment ): void {
351+
public function test_cover_block_with_left_right_center_alignment( string $alignment, string $expected ): void {
351352
$image_url = wp_get_attachment_image_url( self::$image_id, 'full' );
352353
$block_content = '<!-- wp:cover {"url":"' . $image_url . '","id":' . self::$image_id . ',"dimRatio":50,"align":"' . $alignment . '","style":{"color":{}}} -->
353354
<div class="wp-block-cover align' . $alignment . '"><span aria-hidden="true" class="wp-block-cover__background has-background-dim"></span><img class="wp-block-cover__image-background wp-image-' . self::$image_id . '" alt="" src="' . $image_url . '" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","fontSize":"large"} -->
@@ -357,7 +358,7 @@ public function test_cover_block_with_left_right_center_alignment( string $align
357358

358359
$result = apply_filters( 'the_content', $block_content );
359360

360-
$this->assertStringContainsString( 'sizes="(max-width: 1080px) 100vw, 1080px" ', $result );
361+
$this->assertStringContainsString( $expected, $result );
361362
}
362363

363364
/**
@@ -367,9 +368,9 @@ public function test_cover_block_with_left_right_center_alignment( string $align
367368
*/
368369
public function data_image_left_right_center_alignment(): array {
369370
return array(
370-
array( 'left' ),
371-
array( 'right' ),
372-
array( 'center' ),
371+
array( 'left', 'sizes="(max-width: 420px) 100vw, 420px' ),
372+
array( 'right', 'sizes="(max-width: 420px) 100vw, 420px' ),
373+
array( 'center', 'sizes="(max-width: 620px) 100vw, 620px' ),
373374
);
374375
}
375376

0 commit comments

Comments
 (0)