Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Playlist, Playlist Track: Allow tracks without an attachment ID to render on the front end if they have a source URL ([#83880](https://github.com/WordPress/gutenberg/pull/83880)).
- Math: Let keyboard users move focus out of the LaTeX field when the formula has an error, instead of sending focus back to the field ([#86027](https://github.com/WordPress/gutenberg/pull/86027)).
- Image: Position the lightbox trigger button against the `.wp-lightbox-container` figure instead of the image's parent element, so the button stays over the image when a plugin wraps the `img` in a `picture` element ([#82312](https://github.com/WordPress/gutenberg/pull/82312)).
- Page List: Keep page items visible in the Navigation sidebar during content-only editing ([#85357](https://github.com/WordPress/gutenberg/pull/85357)).
- Template Part: Show the "Default based on area" HTML element as selected instead of the `Select` placeholder ([#85172](https://github.com/WordPress/gutenberg/pull/85172)).
- Navigation: Apply a block's Additional CSS only to its outer wrapper, so borders and padding do not appear twice on its inner list ([#84881](https://github.com/WordPress/gutenberg/pull/84881)).
Expand Down
24 changes: 15 additions & 9 deletions packages/block-library/src/image/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -922,15 +922,21 @@ const { state, actions, callbacks } = store(
return;
}

const figure = ref.parentElement;
const figureWidth = ref.parentElement.clientWidth;
// The image is not necessarily a direct child of the figure, as
// plugins can wrap it, so it uses the container the button is
// positioned against rather than the image's parent element.
const container = ref.closest( '.wp-lightbox-container' );
if ( ! container ) {
return;
}
const containerWidth = container.clientWidth;

// It needs special handling for the height because a caption will cause
// the figure to be taller than the image, which means it needs to
// the container to be taller than the image, which means it needs to
// account for that when calculating the placement of the button in the
// top right corner of the image.
let figureHeight = ref.parentElement.clientHeight;
const caption = figure.querySelector( 'figcaption' );
let containerHeight = container.clientHeight;
const caption = container.querySelector( 'figcaption' );
if ( caption ) {
const captionComputedStyle =
window.getComputedStyle( caption );
Expand All @@ -939,16 +945,16 @@ const { state, actions, callbacks } = store(
captionComputedStyle.position
)
) {
figureHeight =
figureHeight -
containerHeight =
containerHeight -
caption.offsetHeight -
parseFloat( captionComputedStyle.marginTop ) -
parseFloat( captionComputedStyle.marginBottom );
}
}

const buttonOffsetTop = figureHeight - offsetHeight;
const buttonOffsetRight = figureWidth - offsetWidth;
const buttonOffsetTop = containerHeight - offsetHeight;
const buttonOffsetRight = containerWidth - offsetWidth;

let buttonTop = buttonOffsetTop + 16;
let buttonRight = buttonOffsetRight + 16;
Expand Down
30 changes: 30 additions & 0 deletions packages/e2e-tests/plugins/image-picture-wrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Plugin Name: Gutenberg Test Image Picture Wrapper
* Plugin URI: https://github.com/WordPress/gutenberg
* Author: Gutenberg Team
*
* @package gutenberg-test-image-picture-wrapper
*/

/**
* Wraps the `img` of an Image block in a `picture` element, the way the Modern
* Image Formats plugin does when its picture element setting is enabled.
*
* @param string $block_content Rendered block content.
* @param array $block Parsed block.
* @return string Filtered block content.
*/
function gutenberg_test_wrap_image_in_picture( $block_content, $block ) {
if ( 'core/image' !== $block['blockName'] || ! str_contains( $block_content, '<img' ) ) {
return $block_content;
}

return preg_replace(
'/<img[^>]+>/',
'<picture style="display: contents;">$0</picture>',
$block_content,
1
);
}
add_filter( 'render_block', 'gutenberg_test_wrap_image_in_picture', 10, 2 );
68 changes: 68 additions & 0 deletions test/e2e/specs/editor/blocks/image.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,74 @@ test.describe( 'Image - lightbox', () => {
expect( margin ).toBe( '0px' );
} );

// Regression test for https://github.com/WordPress/gutenberg/issues/82283.
test.describe( 'when a plugin wraps the image', () => {
let largeMedia;

test.beforeAll( async ( { requestUtils } ) => {
largeMedia = await requestUtils.uploadMedia(
'./assets/1024x768_e2e_test_image_size.jpeg'
);
await requestUtils.activatePlugin(
'gutenberg-test-image-picture-wrapper'
);
} );

test.afterAll( async ( { requestUtils } ) => {
await requestUtils.deactivatePlugin(
'gutenberg-test-image-picture-wrapper'
);
} );

test( 'Trigger button should still be placed inside the figure', async ( {
editor,
page,
} ) => {
await editor.setContent( `<!-- wp:image {"id":${ largeMedia.id },"sizeSlug":"full","linkDestination":"none","lightbox":{"enabled":true}} -->
<figure class="wp-block-image size-full"><img src="${ largeMedia.source_url }" alt="" class="wp-image-${ largeMedia.id }"/><figcaption class="wp-element-caption">A caption.</figcaption></figure>
<!-- /wp:image -->` );

const postId = await editor.publishPost();
await page.setViewportSize( { width: 500, height: 800 } );
await page.goto( `/?p=${ postId }` );

const image = page.locator( '.wp-lightbox-container img' );
await expect( image ).toBeVisible();
// The button is only positioned once the image has loaded.
await expect
.poll( () =>
page.evaluate(
() =>
document.querySelector(
'button.lightbox-trigger'
).style.top
)
)
.not.toBe( '' );

const figureBox = await page
.locator( '.wp-lightbox-container' )
.boundingBox();
const buttonBox = await page
.locator( 'button.lightbox-trigger' )
.boundingBox();

expect( buttonBox.y ).toBeGreaterThanOrEqual( figureBox.y );
expect( buttonBox.x + buttonBox.width ).toBeLessThanOrEqual(
figureBox.x + figureBox.width
);

// A mispositioned button pushes the page sideways.
const { scrollWidth, clientWidth } = await page.evaluate(
() => ( {
scrollWidth: document.documentElement.scrollWidth,
clientWidth: document.documentElement.clientWidth,
} )
);
expect( scrollWidth ).toBe( clientWidth );
} );
} );

test.describe( 'Overlay not a direct child of body', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activatePlugin(
Expand Down
Loading