-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Remove the div wrapper from the aligned image blocks #38657
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -233,3 +233,36 @@ function( $matches ) { | |
| remove_filter( 'render_block', 'wp_restore_group_inner_container', 10, 2 ); | ||
| } | ||
| add_filter( 'render_block', 'gutenberg_restore_group_inner_container', 10, 2 ); | ||
|
|
||
|
|
||
| /** | ||
| * For themes without theme.json file, make sure | ||
| * to restore the outer div for the aligned image block | ||
| * to avoid breaking styles relying on that div. | ||
| * | ||
| * @param string $block_content Rendered block content. | ||
| * @param array $block Block object. | ||
| * @return string Filtered block content. | ||
| */ | ||
| function gutenberg_restore_image_outer_container( $block_content, $block ) { | ||
| $image_with_align = '/(^\s*<figure\b[^>]*)wp-block-image\s([^"]*((alignleft)|(alignright)|(aligncenter))(\s|")[^>]*>((.|\S|\s)*)<\/figure>)/U'; | ||
|
|
||
| if ( | ||
| 'core/image' !== $block['blockName'] || | ||
| WP_Theme_JSON_Resolver::theme_has_support() || | ||
| 0 === preg_match( $image_with_align, $block_content ) | ||
| ) { | ||
| return $block_content; | ||
| } | ||
|
|
||
| $updated_content = preg_replace_callback( | ||
| $image_with_align, | ||
| static function( $matches ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the effect of the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know it was copied from the "group" implementation but If I'm not wrong most of these |
||
| return '<div class="wp-block-image">' . $matches[1] . $matches[2] . '</div>'; | ||
| }, | ||
| $block_content | ||
| ); | ||
| return $updated_content; | ||
| } | ||
|
|
||
| add_filter( 'render_block', 'gutenberg_restore_image_outer_container', 10, 2 ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was there a reason for not using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No particular reason, I guess my reasoning is that this change is tied to the "layout support" so I put here similarly to the previous one we have for group blocks. (same use-case)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe you mean why not use the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, sorry for not being clear here. The filter was introduced in 5.7, see https://core.trac.wordpress.org/changeset/50123. It basically allows you to skip the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice 👍 We should probably use it for both image and group filters.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened a follow-up here #38657 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| <!-- wp:core/image {"align":"center"} --> | ||
| <div class="wp-block-image"><figure class="aligncenter"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" /><figcaption>Give it a try. Press the "really wide" button on the image toolbar.</figcaption></figure></div> | ||
| <figure class="wp-block-image aligncenter"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" /><figcaption>Give it a try. Press the "really wide" button on the image toolbar.</figcaption></figure> | ||
| <!-- /wp:core/image --> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| <!-- wp:image {"align":"center"} --> | ||
| <div class="wp-block-image"><figure class="aligncenter"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt=""/><figcaption>Give it a try. Press the "really wide" button on the image toolbar.</figcaption></figure></div> | ||
| <figure class="wp-block-image aligncenter"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt=""/><figcaption>Give it a try. Press the "really wide" button on the image toolbar.</figcaption></figure> | ||
| <!-- /wp:image --> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| <!-- wp:image {"align":"left"} --> | ||
| <div class="wp-block-image"><figure class="alignleft"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt=""/></figure></div> | ||
| <figure class="wp-block-image alignleft"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt=""/></figure> | ||
| <!-- /wp:image --> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| <!-- wp:image {"align":"left","width":100,"height":100} --> | ||
| <div class="wp-block-image"><figure class="alignleft is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" width="100" height="100"/></figure></div> | ||
| <figure class="wp-block-image alignleft is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" width="100" height="100"/></figure> | ||
| <!-- /wp:image --> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| <!-- wp:image {"align":"left","width":100,"height":100} --> | ||
| <div class="wp-block-image"><figure class="alignleft is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" width="100" height="100"/></figure></div> | ||
| <figure class="wp-block-image alignleft is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" width="100" height="100"/></figure> | ||
| <!-- /wp:image --> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <!-- wp:image {"align":"left","id":13,"width":358,"height":164,"sizeSlug":"large","linkDestination":"none","style":{"border":{"radius":"10px"}}} --> | ||
| <div class="wp-block-image" style="border-radius:10px"><figure class="alignleft size-large is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" class="wp-image-13" width="358" height="164"/></figure></div> | ||
| <!-- /wp:image --> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| [ | ||
| { | ||
| "name": "core/image", | ||
| "isValid": true, | ||
| "attributes": { | ||
| "align": "left", | ||
| "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==", | ||
| "alt": "", | ||
| "caption": "", | ||
| "id": 13, | ||
| "width": 358, | ||
| "height": 164, | ||
| "linkDestination": "none", | ||
| "sizeSlug": "large", | ||
| "style": { | ||
| "border": { | ||
| "radius": "10px" | ||
| } | ||
| } | ||
| }, | ||
| "innerBlocks": [] | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| [ | ||
| { | ||
| "blockName": "core/image", | ||
| "attrs": { | ||
| "align": "left", | ||
| "id": 13, | ||
| "width": 358, | ||
| "height": 164, | ||
| "sizeSlug": "large", | ||
| "linkDestination": "none", | ||
| "style": { | ||
| "border": { | ||
| "radius": "10px" | ||
| } | ||
| } | ||
| }, | ||
| "innerBlocks": [], | ||
| "innerHTML": "\n<div class=\"wp-block-image\" style=\"border-radius:10px\"><figure class=\"alignleft size-large is-resized\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"\" class=\"wp-image-13\" width=\"358\" height=\"164\"/></figure></div>\n", | ||
| "innerContent": [ | ||
| "\n<div class=\"wp-block-image\" style=\"border-radius:10px\"><figure class=\"alignleft size-large is-resized\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"\" class=\"wp-image-13\" width=\"358\" height=\"164\"/></figure></div>\n" | ||
| ] | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <!-- wp:image {"align":"left","id":13,"width":358,"height":164,"sizeSlug":"large","linkDestination":"none","style":{"border":{"radius":"10px"}}} --> | ||
| <figure class="wp-block-image alignleft size-large is-resized" style="border-radius:10px"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" class="wp-image-13" width="358" height="164"/></figure> | ||
| <!-- /wp:image --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ockham @mcsf if you have any thoughts about my regex (I'm not an expert there :P)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick (non-blocker): We can probably return earlier if the block isn't an image or doesn't have
theme.json.P.S. Sorry, I'm also not an expert here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, that's what it does already but since this is just an assignment, I think it's fine. (the variable is also used for the early return)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notes from reviewing the regular expression:
Word boundary needed before "wp-block-image", otherwise you would match
<figure class="not-wp-block-image […]. Fix:\bwp-block-image.The RE doesn't target the
classattribute specifically; shouldn't it? Right now I can match<figure not-class="....No parens needed around each string:
(alignleft|alignright|aligncenter)Many of the remaining groups don't need capturing: we only care about
$match[1]and$match[2], so the above example could become(?:alignleft|alignright:aligncenter), and same for(?:\s|").((.|\S|\s)*)is amusing!\S|\smeans "I want to match something as well as its opposite", i.e. "everything", which is the same as.. So, unless I missed something, the outer group could be.*(\s|"): I'm not sure what purpose this serves. Oh, maybe it's used as a word boundary after the alignment class? Right, and now I notice that there is missing word boundary at the start of the alignment class. You'll see that right now you're able to (incorrectly) matchzzzzalignleft. So let's use proper word boundaries on both sides. Fix:\b(?:alignleft|alignright|aligncenter)\b.(I don't think I'd ever seen the U (ungreedy) flag in use, that's cool)
Based on all of the above except for the fact that we don't target the
classattribute, the RE could be:I've tested this in the venerable regexr.com, but not with live content, so take my review accordingly. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the first remark, it could also be "wp-block-image, is " a word boundary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2- It could target class to be 100% accurate but I didn't think it was mandatory (like group blocks).
Indeed, but for some reason in the site I was using
.was not catching everything (I think related to multiline)6-
(\s|")this one targets a space or end of attribute (if it's the last class)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should work just as well, if you're sure that
wp-block-imageis always the first class.Hm, interesting. Take my advice with caution, then. :) But make sure the trick is really necessary.