Skip to content

Commit e0f6299

Browse files
aferciacbravobernal
authored andcommitted
Improve the image block lightbox translations, labelling, and escaping (#50962)
* Fix translatable labels and escaping. * Improve labels. * Adjust test. * Simplify labels and use aria-label for the dialog. * Add missing button type attribute. * Fix typo.
1 parent 18307c1 commit e0f6299

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

packages/block-library/src/image/index.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,32 @@ function render_block_core_image( $attributes, $content ) {
4949

5050
if ( ! empty( $experiments['gutenberg-interactivity-api-core-blocks'] ) && 'none' === $link_destination && $lightbox ) {
5151

52-
$aria_label = 'Open image lightbox';
53-
if ( $processor->get_attribute( 'alt' ) ) {
54-
$aria_label .= ' : ' . $processor->get_attribute( 'alt' );
52+
$aria_label = __( 'Enlarge image' );
53+
54+
$alt_attribute = trim( $processor->get_attribute( 'alt' ) );
55+
56+
if ( $alt_attribute ) {
57+
/* translators: %s: Image alt text. */
58+
$aria_label = sprintf( __( 'Enlarge image: %s' ), $alt_attribute );
5559
}
5660
$content = $processor->get_updated_html();
5761

5862
// Wrap the image in the body content with a button.
5963
$img = null;
6064
preg_match( '/<img[^>]+>/', $content, $img );
6165
$button = '<div class="img-container">
62-
<button aria-haspopup="dialog" aria-label="' . $aria_label . '" data-wp-on.click="actions.core.image.showLightbox"></button>'
66+
<button type="button" aria-haspopup="dialog" aria-label="' . esc_attr( $aria_label ) . '" data-wp-on.click="actions.core.image.showLightbox"></button>'
6367
. $img[0] .
6468
'</div>';
6569
$body_content = preg_replace( '/<img[^>]+>/', $button, $content );
6670

67-
// For the modal, set an ID on the image to be used for an aria-labelledby attribute.
68-
$modal_content = new WP_HTML_Tag_Processor( $content );
69-
$modal_content->next_tag( 'img' );
70-
$image_lightbox_id = $modal_content->get_attribute( 'class' ) . '-lightbox';
71-
$modal_content->set_attribute( 'id', $image_lightbox_id );
72-
$modal_content = $modal_content->get_updated_html();
73-
7471
$background_color = wp_get_global_styles( array( 'color', 'background' ) );
7572
$close_button_icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="30" height="30" aria-hidden="true" focusable="false"><path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.6 6.5 6.6 1-1z"></path></svg>';
7673

74+
$dialog_label = $alt_attribute ? $alt_attribute : __( 'Image' );
75+
76+
$close_button_label = __( 'Close' );
77+
7778
return
7879
<<<HTML
7980
<div class="wp-lightbox-container"
@@ -82,7 +83,7 @@ function render_block_core_image( $attributes, $content ) {
8283
$body_content
8384
<div data-wp-body="" class="wp-lightbox-overlay"
8485
data-wp-bind.role="selectors.core.image.roleAttribute"
85-
aria-labelledby="$image_lightbox_id"
86+
aria-label="$dialog_label"
8687
data-wp-class.initialized="context.core.image.initialized"
8788
data-wp-class.active="context.core.image.lightboxEnabled"
8889
data-wp-bind.aria-hidden="!context.core.image.lightboxEnabled"
@@ -92,10 +93,10 @@ function render_block_core_image( $attributes, $content ) {
9293
data-wp-on.mousewheel="actions.core.image.hideLightbox"
9394
data-wp-on.click="actions.core.image.hideLightbox"
9495
>
95-
<button aria-label="Close lightbox" class="close-button" data-wp-on.click="actions.core.image.hideLightbox">
96+
<button type="button" aria-label="$close_button_label" class="close-button" data-wp-on.click="actions.core.image.hideLightbox">
9697
$close_button_icon
9798
</button>
98-
$modal_content
99+
$content
99100
<div class="scrim" style="background-color: $background_color"></div>
100101
</div>
101102
</div>

test/e2e/specs/editor/blocks/image.spec.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -831,14 +831,12 @@ test.describe( 'Image - interactivity', () => {
831831
const image = lightbox.locator( 'img' );
832832
await expect( image ).toHaveAttribute( 'src', new RegExp( filename ) );
833833

834-
await page
835-
.getByRole( 'button', { name: 'Open image lightbox' } )
836-
.click();
834+
await page.getByRole( 'button', { name: 'Enlarge image' } ).click();
837835

838836
await expect( lightbox ).toBeVisible();
839837

840-
const closeButton = page.getByRole( 'button', {
841-
name: 'Close lightbox',
838+
const closeButton = lightbox.getByRole( 'button', {
839+
name: 'Close',
842840
} );
843841
await closeButton.click();
844842

@@ -860,11 +858,11 @@ test.describe( 'Image - interactivity', () => {
860858
await page.goto( `/?p=${ postId }` );
861859

862860
openLightboxButton = page.getByRole( 'button', {
863-
name: 'Open image lightbox',
861+
name: 'Enlarge image',
864862
} );
865863
lightbox = page.getByRole( 'dialog' );
866864
closeButton = lightbox.getByRole( 'button', {
867-
name: 'Close lightbox',
865+
name: 'Close',
868866
} );
869867
} );
870868

0 commit comments

Comments
 (0)