Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds default alt values for #1520 #10482

Closed
wants to merge 6 commits into from

Conversation

antpb
Copy link
Contributor

@antpb antpb commented Oct 10, 2018

Description

Sets default alt values to fix #1520 I am open to discussion around what the default value should be. I liked @afercia 's suggestion in the issue

How to test:

  1. create image block
  2. add image with no alt value
  3. inspect image to see that alt value is now set as a readable string "This image has an empty alt attribute, the file name is FILENAME"

@antpb antpb requested a review from tofumatt October 10, 2018 17:06

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

if ( ! alt ) {
alt = 'This image has an empty alt attribute, the file name is ' + filename;
Copy link
Member

Choose a reason for hiding this comment

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

This message should be translatable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

NICE catch. Thank you.

Copy link
Member

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

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

This approach works for me but as mentioned: the field should be translatable and it'd be great to add an E2E test for this (I guess in a11y tests or in a new image block test file).

Copy link
Member

@tofumatt tofumatt left a comment

Choose a reason for hiding this comment

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

This needs to use sprintf() for the translation to work.

Are you comfortable writing E2E tests for this as well? 😄


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

if ( ! alt ) {
alt = __( 'This image has an empty alt attribute, the file name is ' ) + filename;
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Also, a bit pedantic but I think the comma should be a semi-colon, so it should be:

alt = sprintf( __( 'This image has an empty alt attribute; its file name is "%s"' ), filename );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool. Changing. Does caption also need this?

Copy link
Member

Choose a reason for hiding this comment

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

Nope, caption is not content that we provide text for; it doesn't needs to be localised.

Copy link
Contributor Author

@antpb antpb Oct 10, 2018

Choose a reason for hiding this comment

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

I have not worked on E2E test before, but that is no reason to avoid doing so now. If you have any resources I'd be super appreciative for them. I'll continue working through it today.

@tofumatt
Copy link
Member

tofumatt commented Oct 10, 2018 via email

@antpb
Copy link
Contributor Author

antpb commented Oct 10, 2018

I'm comfortable learning how to write the tests. I spent a bit of time reviewing the docs/source code of other tests. I think I can get this going. :)

@chrisvanpatten chrisvanpatten added [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Feature] Media Anything that impacts the experience of managing media labels Oct 10, 2018
@antpb
Copy link
Contributor Author

antpb commented Oct 11, 2018

Worth noting here, in my learning to build tests, I found adding-inline-tokens.test.js did essentially what we would want to do to test this PR. I'll also need to go through other tests to make sure there are no other image adding tests that will not have the proper alt default.

This line is where the alt value should not have passed: https://github.com/WordPress/gutenberg/blob/master/test/e2e/specs/adding-inline-tokens.test.js#L47

This presented a different issue. We need to set default image alt on inline-image block as well. Going to do some digging into what needs to change there.

Alrighty! Latest PR fixes inline-image as well as image block. I'm still working on making the regular image tests. I've updated the inline tests to include the new default string.

@tofumatt tofumatt self-requested a review October 12, 2018 12:18
@tofumatt
Copy link
Member

Nice! I'll have a look sometimes today, thanks for that! 👍

@antpb
Copy link
Contributor Author

antpb commented Oct 12, 2018

Another thing to note that I am trying to figure out currently: Travis is not happy in comparing the raw post data because of the quotes encoding. I need to look into where that is being returned.

Expected: file name is "
Got: file name is &quot

    expect(received).toMatch(expected)
    Expected value to match:
      /<!-- wp:paragraph -->\s*<p>a\u00A0<img class="wp-image-\d+" style="width:\s*10px;?" src="[^"]+\/6c0dcb7f-627c-418b-97f8-7b563cf99b24\.png" alt="This image has an empty alt attribute; its file name is "10x10_e2e_test_image_z9T8jK.png""\/?><\/p>\s*<!-- \/wp:paragraph -->/
    Received:
      "<!-- wp:paragraph -->
    <p>a <img class=\"wp-image-153\" style=\"width: 10px;\" src=\"http://localhost:8889/wp-content/uploads/2018/10/6c0dcb7f-627c-418b-97f8-7b563cf99b24.png\" alt=\"This image has an empty alt attribute; its file name is &quot;6c0dcb7f-627c-418b-97f8-7b563cf99b24.png&quot;\"></p>
    <!-- /wp:paragraph -->"
      46 | 		// Check the content.
      47 | 		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 -->` );
    > 48 | 		expect( await getEditedPostContent() ).toMatch( regex );
         | 		                                       ^
      49 | 	} );
      50 | } );
      51 | 

Escaping in that string didnt seem to fix it (as you can see in the travis report.)

@afercia
Copy link
Contributor

afercia commented Oct 21, 2018

Worth noting also the featured image would need the same information. The featured image preview uses a button with an aria-label, see also #10869

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.


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.

@@ -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.

@antpb
Copy link
Contributor Author

antpb commented Oct 23, 2018

Closing this as I have rebased and made all of the above code change recommendations at his PR: #10960. As for @afercia 's recent comment about featured image, I need to look into that. Might make a new issue so we can get these default alt values added asap. I have a PR already open that is manipulating featured image behavior. I'll see if I can just wrap the default alt values into there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Media Anything that impacts the experience of managing media [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Images alt attribute in an editing context
6 participants