Empty #tag in HtmlTag causes <> to be printed at the top of the pages.

Created on 2 May 2018, over 6 years ago
Updated 3 March 2023, over 1 year ago

Empty #tag in HtmlTag causes <> to be printed at the top of the pages.
For example it can be reproduced by using following code from AdSense module:

/**
 * Implements hook_page_attachments().
 */
function adsense_page_attachments(array &$attachments) {
$attachments['#attached']['html_head'][] = [
        [
          '#tag' => '',
          '#theme' => 'adsense_comment',
          '#comment' => 'adsense page-level ads: page not in match list',
        ],
        'adsense_unmatched_page',
      ];
}

This will cause empty html tag <> to be printed at the top of the pages. Issue is due to preRenderHtmlTag() in HtmlTag.php which doesn't check if the #tag value is set or not. This patch fixes it.

🐛 Bug report
Status

Needs work

Version

10.1

Component
Render 

Last updated 4 days ago

Created by

🇭🇷Croatia tamerzg

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India sonam.chaturvedi Pune

    Patch #16 applied successfully on 10.1.x-dev with one hunk.

    Checking patch core/lib/Drupal/Core/Render/Element/HtmlTag.php...
    Hunk #1 succeeded at 86 (offset -1 lines).
    Checking patch core/tests/Drupal/Tests/Core/Render/Element/HtmlTagTest.php...
    Applied patch core/lib/Drupal/Core/Render/Element/HtmlTag.php cleanly.
    Applied patch core/tests/Drupal/Tests/Core/Render/Element/HtmlTagTest.php cleanly.
    
  • 🇺🇸United States smustgrave

    Triggering for D10 tests

  • Status changed to RTBC over 1 year ago
  • 🇺🇸United States smustgrave

    Tests passed fine for D10

  • Status changed to Needs work over 1 year ago
  • 🇬🇧United Kingdom catch
    +++ b/core/lib/Drupal/Core/Render/Element/HtmlTag.php
    @@ -87,24 +87,32 @@ public static function preRenderHtmlTag($element) {
    +    }
    

    Why add the newline? Shouldn't this be a complete no-op?

    Also should there be an assert() when this receives an empty tag? Right now this silently fails.

  • First commit to issue fork.
  • 🇺🇸United States andy-blum Ohio, USA

    Removing @Avanishyadav issue credit for opening empty MR

Production build 0.71.5 2024