Skip to content

Commit

Permalink
bug #36995 [TwigBridge] fix fallback html-to-txt body converter (nico…
Browse files Browse the repository at this point in the history
…las-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[TwigBridge] fix fallback html-to-txt body converter

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Right now, the content of the `<head>` and `<style>` are dumped as text. This fixes it.

Of course, use `league/html-to-markdown` if you need a better parser.

Commits
-------

6f59d60 [TwigBridge] fix fallback html-to-txt body converter
  • Loading branch information
fabpot committed May 28, 2020
2 parents 15d4f7a + 6f59d60 commit 96d2d19
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/Mime/BodyRenderer.php
Expand Up @@ -74,6 +74,6 @@ private function convertHtmlToText(string $html): string
return $this->converter->convert($html);
}

return strip_tags($html);
return strip_tags(preg_replace('{<(head|style)\b.*?</\1>}i', '', $html));
}
}
5 changes: 3 additions & 2 deletions src/Symfony/Bridge/Twig/Tests/Mime/BodyRendererTest.php
Expand Up @@ -29,11 +29,12 @@ public function testRenderTextOnly(): void

public function testRenderHtmlOnly(): void
{
$email = $this->prepareEmail(null, '<b>HTML</b>');
$html = '<head>head</head><b>HTML</b><style type="text/css">css</style>';
$email = $this->prepareEmail(null, $html);
$body = $email->getBody();
$this->assertInstanceOf(AlternativePart::class, $body);
$this->assertEquals('HTML', $body->getParts()[0]->bodyToString());
$this->assertEquals('<b>HTML</b>', $body->getParts()[1]->bodyToString());
$this->assertEquals(str_replace('=', '=3D', $html), $body->getParts()[1]->bodyToString());
}

public function testRenderHtmlOnlyWithTextSet(): void
Expand Down

0 comments on commit 96d2d19

Please sign in to comment.