Skip to content

File Block: Follow HTML API best practices#71034

Merged
Mamaduka merged 2 commits into
trunkfrom
update/file-block-render-callback
Aug 11, 2025
Merged

File Block: Follow HTML API best practices#71034
Mamaduka merged 2 commits into
trunkfrom
update/file-block-render-callback

Conversation

@Mamaduka
Copy link
Copy Markdown
Member

@Mamaduka Mamaduka commented Aug 3, 2025

What?

This is a follow-up #60494 (comment).

PR updates render_block_core_file render callback to follow HTML API best practices.

Why?

See: #60494 (comment)

Testing Instructions

  1. Create a post.
  2. Add a file block and upload a PDF file.
  3. Save the post and preview it.
  4. Confirm that the inline preview is rendered correctly.

Testing Instructions for Keyboard

Same.

Screenshots or screencast

CleanShot 2025-08-03 at 13 47 28

@Mamaduka Mamaduka self-assigned this Aug 3, 2025
@Mamaduka Mamaduka added [Type] Code Quality Issues or PRs that relate to code quality [Block] File Affects the File Block labels Aug 3, 2025
@Mamaduka Mamaduka requested review from dmsnell and talldan August 3, 2025 09:49
return $content;
while ( 'OBJECT' !== $processor->get_tag() && $processor->next_tag( 'object' ) ) {
continue;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this and the lines down to 38 below should be equivalent to the following…

// If there are no OBJECT elements, something might have already modified the block.
if ( ! $processor->next_tag( 'OBJECT' ) ) {
	return $content;
}

if I’m understanding correctly, the goal is to find the first OBJECT element, and return the $content if none is found.

the while is going to stop at the first OBJECT opening tag because once it finds one, 'OBJECT' !== $processor->get_tag() will be false unless it failed to find one at all, which is picked up below.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, @dmsnell!

That makes sense and is more convenient.

$processor->set_attribute( 'hidden', true );

$filename = $processor->get_attribute( 'aria-label' );
$has_filename = ! empty( $filename ) && 'PDF embed' !== $filename;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

always worth a check for is_string( $filename ) because boolean attributes carry the non-empty value of true. at some point I think we’ll have something like get_string_attribute() or get_attribute_value() but we don’t have a super-clear picture of the best method at the current time. for now, it’s annoying, but true masquerades the empty string, which in the DOM are equivalent.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Aug 5, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: dmsnell <dmsnell@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

return $content;
// If there are no OBJECT elements, something might have already modified the block.
if ( ! $processor->next_tag( 'OBJECT' ) ) {
return $content;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

something I didn’t notice the first time around is that if we don’t find an OBJECT element, we also don’t set the data-wp-interactive attribute. this may be what we intend, but I wanted to point it out because I see it now

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Technically, this shouldn't add any overhead. If OBJECT doesn't exist, we return unmodified content.

I guess we can bookmark the wrapper element, then check the objects' existence and only add attributes if the objects exist. Is there a better way to do it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

performance shouldn’t be in question here, only behavior. so for example, if you wanted every file block to get the Interactivity attribute, then that wouldn’t be the case if they contain no OBJECT.

I think that’s intended, so it should be fine, but I wanted to note that as a thing to double-check.

the point is that even if we call set_attribute() we won’t see that applied unless we use or return $processor->get_updated_html(). it is practically “free” to set an attribute and then abandon the processor though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

As far as know it should be only interactive if the File block displays PDF preview, which won’t be the case when object element isn’t present.

If a plugin fully replaces the output then they should handle it, or try and match the core block structure.

I don’t have a strong preference here, if you have a suggestion to improve and showcase the best practices, I would gladly apply it 🙇

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks all good from my vantage point 👍

@Mamaduka
Copy link
Copy Markdown
Member Author

@dmsnell, thanks for the feedback. Should I merge this?

Copy link
Copy Markdown
Member

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

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

Sorry @Mamaduka — I could have hit approve before. Inasmuch as the HTML API code looks, this is good. I didn’t test any of the changes, assuming you did 👍

@Mamaduka Mamaduka merged commit fc937dc into trunk Aug 11, 2025
70 of 72 checks passed
@Mamaduka Mamaduka deleted the update/file-block-render-callback branch August 11, 2025 04:44
@github-actions github-actions Bot added this to the Gutenberg 21.5 milestone Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] File Affects the File Block [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants