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

Post Title: Add support for text decoration #38986

Closed
wants to merge 2 commits into from

Conversation

scruffian
Copy link
Contributor

Description

We removed support for text decoration from the Post Title block in #34064, though I'm not quite sure why. This adds back that support, but it makes a few changes to ensure that it is applied to the link, not to the wrapper, if there is a link.

We might want to get #31768 shipped as part of this change too?

Testing Instructions

Open a page template that contains a Post Title
Edit the block and confirm that you can add a "strikethrough" text decoration
Also confirm that the setting can be "none" as per Automattic/themes#5573

Screenshots

Screenshot 2022-02-22 at 13 53 02

Types of changes

New feature (non-breaking change which adds functionality)

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).
  • I've updated related schemas if appropriate.

if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
$title = sprintf( '<a href="%1$s" target="%2$s" rel="%3$s">%4$s</a>', get_the_permalink( $post_ID ), esc_attr( $attributes['linkTarget'] ), esc_attr( $attributes['rel'] ), $title );
return sprintf(
'<%1$s><a href="%2$s" target="%3$s" rel="%4$s" %5$s>%6$s</a></%1$s>',
Copy link
Contributor

Choose a reason for hiding this comment

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

Where did $tag_name come from? Why do we now have a wrapping element?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously this was output to a $title variable. $title was then wrapped in a wrapping element after the if statement.

This change simply returns the whole markup rather than creating a variable, which I thought was easier to read than having multiple if statements.

Copy link
Contributor

Choose a reason for hiding this comment

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

I appreciate this might seem like a nit pick, but in order to keep this PR focused I really think we should go back to the previous setup whereby we had a single return statement.

Why?

Having two return statements means the HTML output is effectively generated in x2 places thus increasingly the complexity of reasoning around the code. It also doubles the surface area for the introduction of XSS bugs (as evidenced by the original omission of the esc_* functions in this PR).

Copy link
Contributor

@getdave getdave left a comment

Choose a reason for hiding this comment

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

Appreciate your work here and patience in responding to my queries.

Nonetheless, I'm still a bit confused here. Could you help me to understand a few things?

I just want to make sure I fully understand the change and how it's being applied and what implications there are in terms of backwards compatibility.

if ( isset( $attributes['isLink'] ) && $attributes['isLink'] ) {
$title = sprintf( '<a href="%1$s" target="%2$s" rel="%3$s">%4$s</a>', get_the_permalink( $post_ID ), esc_attr( $attributes['linkTarget'] ), esc_attr( $attributes['rel'] ), $title );
return sprintf(
'<%1$s><a href="%2$s" target="%3$s" rel="%4$s" %5$s>%6$s</a></%1$s>',
Copy link
Contributor

Choose a reason for hiding this comment

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

I appreciate this might seem like a nit pick, but in order to keep this PR focused I really think we should go back to the previous setup whereby we had a single return statement.

Why?

Having two return statements means the HTML output is effectively generated in x2 places thus increasingly the complexity of reasoning around the code. It also doubles the surface area for the introduction of XSS bugs (as evidenced by the original omission of the esc_* functions in this PR).

get_the_permalink( $post_ID ),
esc_attr( $attributes['linkTarget'] ),
esc_attr( $attributes['rel'] ),
$wrapper_attributes,
Copy link
Contributor

Choose a reason for hiding this comment

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

We're now applying the wrapper_attributes to the <a> as opposed to the wrapping tag. What are the impacts on this on Theme styling and potential regressions?

@scruffian
Copy link
Contributor Author

The aim of the PR is to be able to control the text decoration of the post title block. The block markup is dependent on whether or not the post-title has the isLink attribute set. With isLink the title gets wrapped in an additional a tag before the wrapper tag. In this case the text-decoration of the block is being overridden by the a tag which has its own text-decoration (by default all a tags will have a text-decoration of underline). Therefore to override the default text-decoration of link we need to apply the text-decoration` to the link element itself, not just to the parent wrapper.

This isn't easy to do, since the block supports just return a long string of styles and classes, so you can't apply some of them to some elements and some to others, so instead I am just applying all of the rules to the a tag. It's a heavy handed approach but we don't have the ability to have more granular control. I'm not sure if this would cause other issues - I can't think of any but that doesn't mean there aren't any!

We could avoid the double return by assigning the link to a variable and and outputting the wrapper attributes based on the isLink condition, but I think this would probably be harder to understand. Alternatively we could apply the wrapper attributes to both the wrapper element and the link.

There are a few other options we could explore if we want to more granular control, but they would take significant work:

Block supports have the concept of an __experimentalSelector which is used to determine which elements the supports apply to. The block markup isn't aware of these yet, but we could use that to determine where the wrapper attributes were applied. One problem with this is that the block doesn't always contain a link. Another problem is that this approach still doesn't allow us to apply different styles to different elements.

Another option would be to create more granular hooks for every block support mechanism, which would allow us to control which tags they are applied to. This would require a significant rework of how block supports work, see #38167.

Another option would be to always output the a tag regardless of whether the post title is a link, and just not include the href. This would make the markup more consistent for all options but would still require some reworking of the markup.

@paaljoachim
Copy link
Contributor

I just want to say it is just a matter of time before the Post Title block becomes the go to when a user wants to add a page/post title to their layout. As in this issue: #27093 It would be natural to have all the "regular" settings as one have in a Paragraph block also in the Post Title block.

@getdave
Copy link
Contributor

getdave commented Mar 1, 2022

@scruffian Thanks for the context. It helps a lot. I'm going to pull this down and take a more detailed look later today.

Copy link
Contributor

@getdave getdave left a comment

Choose a reason for hiding this comment

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

Just noticed during testing that the text decoration UI isn't showing up in the editor

Screen.Capture.on.2022-03-01.at.11-59-44.mov

@scruffian
Copy link
Contributor Author

I think that's ok for now. The aim of this PR is to get text decoration working via theme.json. We can add the UI in a later PR.

@getdave
Copy link
Contributor

getdave commented Mar 1, 2022

Ok that's fine 👍

Would you mind re-visiting the testing instructions in order that we can validate the PR a little?

Thanks!

@scruffian
Copy link
Contributor Author

Sorry I just checked again and the controls are there, but they are hidden under the three dots:
Screenshot 2022-03-01 at 17 17 20

Copy link
Contributor

@getdave getdave left a comment

Choose a reason for hiding this comment

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

Some things I noticed on first checkout and test.

Editor does not reflect text decoration when it's a link

Screen.Capture.on.2022-03-02.at.14-19-17.mp4

Classname placement inconsistency on front of site

When switching between making the block a link or not, the markup varies. The main issue I see is that the .wp-block-post-title moves from the <h2> to the <a>.

Screenshot 2022-03-02 at 14 20 33

Screenshot 2022-03-02 at 14 20 13

I imagine this is likely to cause regressions and seems heavy handed considering what we actually want to achieve is just moving the style="text-decoration:line-through;" to the <a>.


I think we should investigate using __experimentalSelector to see if we can better target the <a> element.

@scruffian scruffian mentioned this pull request May 17, 2022
23 tasks
@skorasaurus skorasaurus added the [Block] Post Title Affects the Post Title Block label Aug 12, 2022
@t-hamano
Copy link
Contributor

I found that this support was already added by #42328. Can this PR be closed?

@scruffian
Copy link
Contributor Author

Closed by #42328

@scruffian scruffian closed this Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Title Affects the Post Title Block
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants