Opened 22 months ago
Last modified 16 months ago
#58174 new defect (bug)
A shortcode block that evaluates to nothing, renders as a space in the HTML
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Shortcodes | Keywords: | |
Focuses: | Cc: |
Description
A shortcode block that evaluates to nothing renders as a space in the HTML.
A screenshot:
https://drive.google.com/file/d/1wjDAXVgrqE5L5pzTJDKuTo606XzuTZ4k/view?usp=drivesdk
I've noticed it while using an [acf] shortcode referencing an empty field, but it also happens with an empty [audio] core shortcode.
This makes styling with CSS cumbersome. For example, the :empty selector can't be used.
When I remove the shortcode block, leaving the column block completely empty, the redundant space isn't being rendered (so it isn't a browser issue).
I've tried to reverse-engineer it really hard, even trying to catch the HTML at the last minute in the get_the_block_template_html() function, but I couldn't find the culprit spaces.
Change History (4)
#2
@
22 months ago
Okay, so it is a line break (\n).
It is being added here:
$content = preg_replace_callback("/$pattern/", 'do_shortcode_tag', $content);
In the do_shortcode() function in shortcodes.php.
I made sure the 'do_shortcode_tag' output never contains \n. But still, after this line, the $content contains \n in place of empty shortcodes. It doesn't make a lot of sense, but it seems there is some kind of bug with the native preg_replace_callback() PHP function.
Adding a
$content = str_replace(">\n", ">", $content);
fixes the problem.
There is no filter after this point though, so it seems it can't be fixed without modifying the core code.
On second thought it might be some sort of line break. Although I've searched for \n and \r between the tags and couldn't find anything.