Skip to content

Commit

Permalink
Add CSS content support - Fixes fedeisas#319
Browse files Browse the repository at this point in the history
  • Loading branch information
Muffinman committed Apr 12, 2024
1 parent c6916c3 commit 4f37061
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
12 changes: 12 additions & 0 deletions config/css-inliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@

'css-files' => [],

/*
|--------------------------------------------------------------------------
| Css Content
|--------------------------------------------------------------------------
|
| Optionally, you may prefer to provide the CSS content as a String.
| This allows for compatibility with Vite by using Vite::content()
| If supplied, this string will be appended after your css-files.
|
*/

'css-content' => null,
];
36 changes: 32 additions & 4 deletions src/CssInlinerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
namespace Fedeisas\LaravelMailCssInliner;

use DOMDocument;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Mail\Events\MessageSending;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mime\Part\AbstractPart;
use Symfony\Component\Mime\Part\AbstractMultipartPart;
use Symfony\Component\Mime\Part\Multipart\AlternativePart;
use Symfony\Component\Mime\Part\Multipart\MixedPart;
use Symfony\Component\Mime\Part\TextPart;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;

class CssInlinerPlugin
{
private array $config;

private CssToInlineStyles $converter;

private string $cssToAlwaysInclude;

public function __construct(array $filesToInline = [], CssToInlineStyles $converter = null)
public function __construct(array $config, CssToInlineStyles $converter = null)
{
$this->cssToAlwaysInclude = $this->loadCssFromFiles($filesToInline);

$this->config = $config;

$this->buildCssContent();

$this->converter = $converter ?? new CssToInlineStyles;
}
Expand All @@ -48,6 +52,30 @@ public function handleSymfonyEvent(MessageEvent $event): void
$this->handleSymfonyEmail($message);
}

private function buildCssContent(): void
{
$this->addCssFromFiles();
$this->addCssFromContent();
}

private function addCssFromFiles(): void
{
$filesToInline = $this->config['css-files'] ?? [];

$this->cssToAlwaysInclude = $this->loadCssFromFiles($filesToInline);
}

private function addCssFromContent(): void
{
$contentToInline = $this->config['css-content'] ?? null;

if (! $contentToInline) {
return;
}

$this->cssToAlwaysInclude .= $contentToInline;
}

private function processPart(AbstractPart $part): AbstractPart
{
if ($part instanceof TextPart && $part->getMediaType() === 'text' && $part->getMediaSubtype() === 'html') {
Expand Down
2 changes: 1 addition & 1 deletion src/LaravelMailCssInlinerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function register(): void
$this->mergeConfigFrom(__DIR__ . '/../config/css-inliner.php', 'css-files');

$this->app->singleton(CssInlinerPlugin::class, function ($app) {
return new CssInlinerPlugin($app['config']->get('css-inliner.css-files', []));
return new CssInlinerPlugin($app['config']->get('css-inliner', []));
});

Event::listen(MessageSending::class, CssInlinerPlugin::class);
Expand Down
20 changes: 17 additions & 3 deletions tests/CssInlinerPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ public function test_it_should_convert_html_body_with_given_css(): void
$this->assertBodyMatchesStub($message, 'converted-html-with-css');
}

public function test_it_should_convert_html_body_with_given_css_content(): void
{
$message = $this->fakeSendMessageUsingInlinePlugin(
(new Email)->html($this->stubs['original-html-with-css']),
[],
file_get_contents(__DIR__ . '/css/test.css')
);

$this->assertBodyMatchesStub($message, 'converted-html-with-css');
}

public function test_it_should_convert_html_body_with_given_css_and_attachment(): void
{
$originalMessage = $this->createMessageToSend(
Expand Down Expand Up @@ -305,13 +316,16 @@ private function getTextFromPart(AbstractPart $part, string $mediaSubType = 'htm
return null;
}

private function fakeSendMessageUsingInlinePlugin(Email $message, array $inlineCssFiles = []): Email
private function fakeSendMessageUsingInlinePlugin(Email $message, array $inlineCssFiles = [], string $inlineCssContent = null): Email
{
$processedMessage = null;

$dispatcher = new EventDispatcher;
$dispatcher->addListener(MessageEvent::class, static function (MessageEvent $event) use ($inlineCssFiles, &$processedMessage) {
$handler = new CssInlinerPlugin($inlineCssFiles);
$dispatcher->addListener(MessageEvent::class, static function (MessageEvent $event) use ($inlineCssFiles, $inlineCssContent, &$processedMessage) {
$handler = new CssInlinerPlugin([
'css-files' => $inlineCssFiles,
'css-content' => $inlineCssContent,
]);

$handler->handleSymfonyEvent($event);

Expand Down

0 comments on commit 4f37061

Please sign in to comment.