Skip to content
Closed
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
12 changes: 9 additions & 3 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';
import { getBlobByURL, revokeBlobURL } from '@wordpress/blob';
import {
Expand Down Expand Up @@ -58,15 +58,21 @@ const LINK_DESTINATION_CUSTOM = 'custom';
const ALLOWED_MEDIA_TYPES = [ 'image' ];

export const pickRelevantMediaFiles = ( image ) => {
let { caption } = image;
let { caption, alt } = image;
const { filename } = image;

if ( typeof caption !== 'object' ) {
caption = create( { html: caption } );
}

if ( ! alt ) {
alt = sprintf( __( 'This image has an empty alt attribute; its file name is \"%s\"' ), filename );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the escaping.

}

return {
...pick( image, [ 'alt', 'id', 'link', 'url' ] ),
...pick( image, [ 'id', 'link', 'url' ] ),
caption,
alt,
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
Expand Down Expand Up @@ -35,7 +35,10 @@ export const settings = {
);
},

save( { id, url, alt, width } ) {
save( { id, url, alt, width, filename } ) {
if ( ! alt ) {
alt = sprintf( __( 'This image has an empty alt attribute; its file name is \"%s\"' ), filename );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the escaping.

}
return (
<img
className={ `wp-image-${ id }` }
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/specs/adding-inline-tokens.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe( 'adding inline tokens', () => {
await page.click( '.media-modal button.media-button-select' );

// Check the content.
const regex = new RegExp( `<!-- wp:paragraph -->\\s*<p>a\\u00A0<img class="wp-image-\\d+" style="width:\\s*10px;?" src="[^"]+\\/${ filename }\\.png" alt=""\\/?><\\/p>\\s*<!-- \\/wp:paragraph -->` );
const regex = new RegExp( `<!-- wp:paragraph -->\\s*<p>a\\u00A0<img class="wp-image-\\d+" style="width:\\s*10px;?" src="[^"]+\\/${ filename }\\.png" alt="This image has an empty alt attribute; its file name is "10x10_e2e_test_image_z9T8jK.png""\\/?><\\/p>\\s*<!-- \\/wp:paragraph -->` );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to use the HTML entity &quot; for the quotes to check for valid HTML.

expect( await getEditedPostContent() ).toMatch( regex );
} );
} );