I may have this wrong or it may be an issue I am not sure and apologize if this is my error, but when putting [node:summary] token in the description field it renders the html tags when posting content on FB.

Comments

sushichris created an issue. See original summary.

damienmckenna’s picture

Priority: Normal » Major
Parent issue: » #2563607: Plan for Metatag 8.x-1.0-beta4 release

... oh dear.

Need to fix this and write tests for it.

damienmckenna’s picture

Title: OG node-summary renders HTML tags when posting to FB » Meta tags don't filter out HTML tags
damienmckenna’s picture

Version: 8.x-1.0-beta3 » 8.x-1.x-dev
juampynr’s picture

Status: Active » Needs review

Here is a patch that fixes it. I took the approach from core:

core/modules/user/user.module:920:  $message['subject'] .= PlainTextOutput::renderFromHtml($token_service->replace($mail_config->get($key . '.subject'), $variables, $token_options));
juampynr’s picture

StatusFileSize
new1013 bytes
damienmckenna’s picture

Status: Needs review » Fixed

Committed. Thanks!

juliencarnot’s picture

Just updated from beta3 to dev through drush up metatag-1.x-dev (which worked I got several of the fixes, such as issue #2642430) and rebuilt cache but I'm still seing html tags when using [node-summary] in the description meta:

Value of the summary:
This <strong>is a test</strong>
Meta description rendering:
<meta name="description" content="This &lt;strong&gt;is a test&lt;/strong&gt;" />
<meta property="og:description" content="This &lt;strong&gt;is a test&lt;/strong&gt;" />

Did I miss a step?

damienmckenna’s picture

Status: Fixed » Needs work

Ok, lets add some tests.

mr.baileys’s picture

Issue tags: +Needs tests
sushichris’s picture

If I embed a youtube video in my sites content under "body" and post the content on facebook it does not embed the video, I know when posting videos to facebook you just throw the url in the box and it displays the video, how can I display the video via my sites content? Only the code shows, is that related to this issue? I just created a simple content with the default "body"

<div class="video-container"><iframe width="560" height="315" src="https://www.youtube.com/embed/7t8kmTNLNUQ?rel=0" frameborder="0" allowfullscreen></iframe></iframe></div>

I am not sure if this is a metatag feature or if it even is possible, can't tell because of the metatag rendering the html tags anyway.

damienmckenna’s picture

What's weird is that the API docs say that the HTML tags are supposed to be stripped from the content, so it should be working correctly. Definitely need to dig into this a little bit for the next beta release.

damienmckenna’s picture

Bumping this to beta5 so that beta4 can be released sooner.

yury n’s picture

Patch from #6 will break metatags with tokens for node's image field - it will just strip out <img> tag. So needs at least check like

if ($tag->image()) {
  $processed_value = $this->tokenService->tokenReplace($tag->value(), $token_replacements);
}
else {
  $processed_value = PlainTextOutput::renderFromHtml($this->tokenService->tokenReplace($tag->value(), $token_replacements));
}
mikeyk’s picture

StatusFileSize
new883 bytes

@sushichris @DamienMcKenna @juliencarnot - I've been working on the issue of HTML tags still being rendered and think I've found the reason. The PlainTextOutput::renderFromHtml function correctly strips HTML tags, however the value in $token_replacements seems to already be sanitized with HTML tags converted to HTML special characters. Therefore <, > etc remain in the output.
Adding htmlspecialchars_decode to the function resolves this and means all HTML tags are correctly removed. Suggested patch attached

sushichris’s picture

:) I wasn't able to apply the patch but I swapped the line of code that the patch was supposed to and it works. I think because the following line of code wasn't in my file.

$langcode = \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();

damienmckenna’s picture

Would someone mind trying to write a test for this? Thanks.

sushichris’s picture

well it kinda worked, one content renders this:

*/ /*-->*/ Product Addons SEO

From this code:

<style type="text/css">#header {
    background-color:#555555;
    color:white;
    text-align:center;
    padding:5px;
}
#nav {
    line-height:30px;
    background-color:#eeeeee;
    height:300px;
    width:100px;
    float:left;
    padding:5px;	      
}
#section {
    width:350px;
    float:left;
    padding:10px;	 	 
}
#footer {
    background-color:#555555;
    color:white;
    clear:both;
    text-align:center;
   padding:5px;	 	 
}
</style>
<div id="header">
<h1>Product</h1>
</div>

<div id="nav">Addons<br />
SEO<br />
Mobile first</div>

<div id="section">
<h2>Products</h2>

<p>Choose from an assortment of options to customize your site.</p>

<p>Modelsushi, your one-stop-shop for all your online business needs!</p>
</div>

<div id="footer">MODELSUSHI LLC</div>
damienmckenna’s picture

@sushichris: Can you please provide the exact HTML tag that is output on the page? No parts of the "*/ /*-->*/ Product Addons SEO" string appear in the HTML chunk you provided, are you sure you're looking at the right HTML?

mikeyk’s picture

StatusFileSize
new2.51 KB

@DamienMcKenna Attached is an updated version of my patch with tests. The tests work on three values in metatags -- one without any HTML; one with raw html; and one with escaped HTML. To pass all HTML including escaped should be removed.

mikeyk’s picture

StatusFileSize
new2.63 KB

Updated patch which includes code from #15 to fix issue with images @YurikK_

juliencarnot’s picture

@mikeyk:

I applied meta_tags_don_t_filter-2631408-22.patch from comment #22 on a fresh 8.x-1.x clone, uninstalled and reinstalled metatag and opengraph&twitter submodules to rule out problems with previous config and used drush cr to rebuild cache.

I get a fatal error on the homepage:
Fatal error: Unsupported operand types in /var/www/html/modules/metatag/src/MetatagToken.php on line 99

On the extensions page, there are some notices and warnings:

    Warning: Missing argument 3 for Drupal\metatag\MetatagToken::tokenReplace(), called in /var/www/html/modules/metatag/src/MetatagManager.php on line 303 and defined in Drupal\metatag\MetatagToken->tokenReplace() (line 52 of modules/metatag/src/MetatagToken.php).

    Drupal\metatag\MetatagToken->tokenReplace('[node:title] | [site:name]', Array)
    Drupal\metatag\MetatagManager->generateElements(Array, Object)
    metatag_get_tags_from_route()
    metatag_page_attachments(Array)
    Drupal\Core\Render\MainContent\HtmlRenderer->invokePageAttachmentHooks(Array)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1)
    Stack\StackedHttpKernel->handle(Object, 1, 1)
    Drupal\Core\DrupalKernel->handle(Object)

    Notice: Undefined variable: settings in Drupal\metatag\MetatagToken->tokenReplace() (line 54 of modules/metatag/src/MetatagToken.php).

    Drupal\metatag\MetatagToken->tokenReplace('[node:title] | [site:name]', Array)
    Drupal\metatag\MetatagManager->generateElements(Array, Object)
    metatag_get_tags_from_route()
    metatag_page_attachments(Array)
    Drupal\Core\Render\MainContent\HtmlRenderer->invokePageAttachmentHooks(Array)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1)
    Stack\StackedHttpKernel->handle(Object, 1, 1)
    Drupal\Core\DrupalKernel->handle(Object)

    Warning: Missing argument 3 for Drupal\metatag\MetatagToken::tokenReplace(), called in /var/www/html/modules/metatag/src/MetatagManager.php on line 303 and defined in Drupal\metatag\MetatagToken->tokenReplace() (line 52 of modules/metatag/src/MetatagToken.php).

    Drupal\metatag\MetatagToken->tokenReplace('[node:title] | [site:name]', Array)
    Drupal\metatag\MetatagManager->generateElements(Array, Object)
    metatag_get_tags_from_route()
    metatag_page_attachments(Array)
    Drupal\Core\Render\MainContent\HtmlRenderer->invokePageAttachmentHooks(Array)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1)
    Stack\StackedHttpKernel->handle(Object, 1, 1)
    Drupal\Core\DrupalKernel->handle(Object)

    Notice: Undefined variable: settings in Drupal\metatag\MetatagToken->tokenReplace() (line 54 of modules/metatag/src/MetatagToken.php).

    Drupal\metatag\MetatagToken->tokenReplace('[node:title] | [site:name]', Array)
    Drupal\metatag\MetatagManager->generateElements(Array, Object)
    metatag_get_tags_from_route()
    metatag_page_attachments(Array)
    Drupal\Core\Render\MainContent\HtmlRenderer->invokePageAttachmentHooks(Array)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1)
    Stack\StackedHttpKernel->handle(Object, 1, 1)
    Drupal\Core\DrupalKernel->handle(Object)

    Warning: Missing argument 3 for Drupal\metatag\MetatagToken::tokenReplace(), called in /var/www/html/modules/metatag/src/MetatagManager.php on line 303 and defined in Drupal\metatag\MetatagToken->tokenReplace() (line 52 of modules/metatag/src/MetatagToken.php).

    Drupal\metatag\MetatagToken->tokenReplace('[node:title] | [site:name]', Array)
    Drupal\metatag\MetatagManager->generateElements(Array, Object)
    metatag_get_tags_from_route()
    metatag_page_attachments(Array)
    Drupal\Core\Render\MainContent\HtmlRenderer->invokePageAttachmentHooks(Array)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1)
    Stack\StackedHttpKernel->handle(Object, 1, 1)
    Drupal\Core\DrupalKernel->handle(Object)

    Notice: Undefined variable: settings in Drupal\metatag\MetatagToken->tokenReplace() (line 54 of modules/metatag/src/MetatagToken.php).

    Drupal\metatag\MetatagToken->tokenReplace('[node:title] | [site:name]', Array)
    Drupal\metatag\MetatagManager->generateElements(Array, Object)
    metatag_get_tags_from_route()
    metatag_page_attachments(Array)
    Drupal\Core\Render\MainContent\HtmlRenderer->invokePageAttachmentHooks(Array)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1)
    Stack\StackedHttpKernel->handle(Object, 1, 1)
    Drupal\Core\DrupalKernel->handle(Object)
mikeyk’s picture

StatusFileSize
new2.7 KB

@juliencarnot Thanks for the feedback - Here is an updated version with the the langcode array added. This should fix the errors you got from patch #22.

juliencarnot’s picture

@mikeyk: Thanks, the patch fixes the errors and now I get the relative path of the first image stored in my field for the node for twitter_cards_image or all the relative paths separated by commas for og_image (which is not compliant with opengraph apparently, but it might be an issue pertaining to the og submodule). However, it seems that Twitter card validator needs the absolute path. I tried using [site:url][node:field_image] or even putting the website's URL in front of [node:field_image] , to no avail.

Also, a bonus question: shouldn't [node:field_image:1] give only the URL of the second image in the field? It could allow to pick the main image for open-graph.

damienmckenna’s picture

Status: Needs work » Needs review

The last submitted patch, 22: meta_tags_don_t_filter-2631408-22.patch, failed testing.

The last submitted patch, 22: meta_tags_don_t_filter-2631408-22.patch, failed testing.

mikeyk’s picture

StatusFileSize
new4.08 KB

@juliencarnot @DamienMcKenna I've done some more work on this and fixed a couple more things. The issue that @juliencarnot raised with relative paths is an interesting one - I tested this module with #24 patch on a Drupal 8.0.3 installation and the paths produced were absolute, however once I upgraded to 8.0.4 or 8.0.5 the paths became relative. Drupal 8.0.4 was a security release so this must have been changed for a good reason, but it explains why it's only recently become an issue.
The updated patch should convert a relative path to an absolute one based on the global $base_url. Let me know how you go with this one!

This patch also fixes two related issues I've found with OgImageSecureUrl (not sure if this is going beyond the scope of this issue - I can open a new one if so. The code to fix this is in the same block so I thought to include it in this)
1. Paths for OgImageSecureUrl containing http:// should be converted to https:// but weren't
2. OgImageSecureUrl's attributes had its image property set to False, and so would not be processed as the regular OgImage tag. This has been changed to True.

mikeyk’s picture

@juliencarnot - As for your bonus question, as far as I know the [node:field_image:1] token isn't provided in core or the Token module - but is in the Field Tokens module. If you install that you should be able to do what you want by using the token [node:field_image-formatted:1] (note the -formatted).

juliencarnot’s picture

Tested and approved, the absolute URL is shown! Not using OgImageSecureUrl as all my site is in https, so I'll let somebody else review that part! Thanks for the patches and for your advice regarding Field Tokens, I knew I was missing something!

stevenpatz’s picture

Any idea on when a new release can happen? #22 fixes an issue I was having with image urls. Applied the patch and all looks good.

sushichris’s picture

@DamienMcKenna: https://modelsushi.com/basic-page/sample If you scrape that link you would see the */ /*-->*/ Product Addons SEO (code in the content description section (body)). It was that the patch I had applied previous, #16 I believe. I reverted back to the original and this is the result

<style type="text/css"> <!--/*--><![CDATA[/* ><!--*/ <!--/*--><![CDATA[/* ><!--*/ #header { background-color:#555555; color:white; text-align:center; padding:5px; } #nav { line-height:30px; background-color:#eeeeee; height:300px; width:100px; float:left; padding:5px; } #section { width:350px; float:left; padding:10px; } #footer { background-color:#555555; color:white; clear:both; text-align:center; padding:5px; } /*--><!]]]]><![CDATA[>*/ /*--><!]]>*/ </style><div id="header"> <h1>Product</h1> </div> <div id="nav">Addons<br /> SEO</div>

I may be applying the patches wrong as I am new to patching I applied patch using this method:

curl https://www.drupal.org/files/issues/meta_tags_don_t_filter-2631408-29.patch

Also I would like to note that I reverted by changing the name of the current MetatagManager.php to MetatagManager.php.current and changed the name of the MetatagManager.php.original back to MetatagManager.php

If I am doing something wrong I apologize, I did try this as well but I get errors:

curl https://www.drupal.org/files/issues/meta_tags_don_t_filter-2631408-29.patch | patch -p1

Might this have something to do with the extra html tags drupal always seems to sneak into my code? (right after typing this I unchecked: Correct faulty and chopped off HTML) And the previous code now looks like this:

<style type="text/css">#header { background-color:#555555; color:white; text-align:center; padding:5px; } #nav { line-height:30px; background-color:#eeeeee; height:300px; width:100px; float:left; padding:5px; } #section { width:350px; float:left; padding:10px; } #footer { background-color:#555555; color:white; clear:both; text-align:center; padding:5px; } </style> <div id="header"> <h1>Product</h1> </div> <div id="nav">Addons<br /> SEO

damienmckenna’s picture

This just tidies up the code & comments a little, no actual logic changes.

damienmckenna’s picture

Oh wait, there was one logic change - I removed the special handling for the og:image:secure_url tag because it didn't belong there, there needs to be a new 'secure' attribute that is tag-agnostic.

damienmckenna’s picture

  • DamienMcKenna committed 5732d4e on 8.x-1.x
    Issue #2631408 by mikeyk, juampynr, DamienMcKenna: Filter out HTML from...
damienmckenna’s picture

Status: Needs review » Fixed

Committed. Thanks all!

sushichris’s picture

Awesome, Thanks!

kplanz’s picture

Status: Fixed » Needs review
StatusFileSize
new740 bytes

Hey there,

first of all: great work guys, the metatag module is really a must-have and now even available for D8 :)

We use it on a Drupal 8 blog which lives in a subdirectory, e.g. www.site.com/blog/ and the extracted image URLs are wrong in that case.

Currently: www.site.com/blog/blog/sites/default/files/2016-03/team.jpg
Correct would be: www.site.com/blog/sites/default/files/2016-03/team.jpg

The problem seems to be the use of $base_url because both the base url AND the image path contain the subdirectory (/blog).
I suggest to use $base_root in this case since the relative image path starts with a "/" and thus references www.site.com/ and not www.site.com/blog/.

Patch is attached.

(Is it OK to re-open this issue or should I have created a new one?)

damienmckenna’s picture

Status: Needs review » Fixed

@kplanz: Oh bummer, thanks for reporting it. Please open a new issue for this issue? Thanks.

kplanz’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.