Skip to content

Commit

Permalink
External relative links (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeisas authored Sep 12, 2020
1 parent 58433e2 commit a2fd35b
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/CssInlinerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ public function loadOptions($options)
{
if (isset($options['css-files']) && count($options['css-files']) > 0) {
$this->css = '';
foreach ($options['css-files'] as $file) {
$this->css .= file_get_contents($file);
foreach ($options['css-files'] as $fileUrl) {
// Fix relative protocols on hrefs. Assume https.
if (substr($fileUrl, 0, 2) === '//') {
$fileUrl = 'https:' . $fileUrl;
}

$this->css .= file_get_contents($fileUrl);
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions tests/CssInlinerPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ class CssInlinerPluginTest extends TestCase
'converted-html',
'converted-html-with-css',
'converted-html-with-link-css',
'converted-html-with-link-external',
'converted-html-with-link-relative-external',
'converted-html-with-links-css',
'converted-html-with-mixed-type-links',
'converted-html-with-non-stylesheet-link',
'original-html',
'original-html-with-css',
'original-html-with-link-css',
'original-html-with-link-external',
'original-html-with-link-relative-external',
'original-html-with-links-css',
'original-html-with-mixed-type-links',
'original-html-with-non-stylesheet-link',
Expand Down Expand Up @@ -238,6 +242,50 @@ public function itShouldWorkWithMixedTypeLinks()
);
}

/** @test **/
public function itShouldWorkWithExternalLink()
{
$mailer = new Swift_Mailer(new Swift_NullTransport());

$mailer->registerPlugin(new CssInlinerPlugin($this->options));

$message = new Swift_Message('Test');

$message->setFrom('[email protected]');
$message->setTo('[email protected]');

$message->setBody($this->stubs['original-html-with-link-external'], 'text/html');

$mailer->send($message);

$this->assertEquals(
$this->stubs['converted-html-with-link-external'],
$this->normalize($message->getBody())
);
}

/** @test **/
public function itShouldWorkWithRelativeExternalLink()
{
$mailer = new Swift_Mailer(new Swift_NullTransport());

$mailer->registerPlugin(new CssInlinerPlugin($this->options));

$message = new Swift_Message('Test');

$message->setFrom('[email protected]');
$message->setTo('[email protected]');

$message->setBody($this->stubs['original-html-with-link-relative-external'], 'text/html');

$mailer->send($message);

$this->assertEquals(
$this->stubs['converted-html-with-link-relative-external'],
$this->normalize($message->getBody())
);
}

protected function normalize(string $html): string
{
$document = new \DomDocument();
Expand Down
20 changes: 20 additions & 0 deletions tests/stubs/converted-html-with-link-external.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>

</head>
<body>
<div class="pixels-10" style="width: 10px;">
text

<ul>
<li>
Big list
</li>
<li>
Small list
</li>
</ul>
</div>
</body>
</html>
20 changes: 20 additions & 0 deletions tests/stubs/converted-html-with-link-relative-external.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>

</head>
<body>
<div class="pixels-10" style="width: 10px;">
text

<ul>
<li>
Big list
</li>
<li>
Small list
</li>
</ul>
</div>
</body>
</html>
21 changes: 21 additions & 0 deletions tests/stubs/original-html-with-link-external.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://github.com/fedeisas/laravel-mail-css-inliner/raw/master/tests/css/test.css">
</head>
<body>
<div class="pixels-10">
text

<ul>
<li>
Big list
</li>
<li>
Small list
</li>
</ul>
</div>
</body>
</html>

21 changes: 21 additions & 0 deletions tests/stubs/original-html-with-link-relative-external.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="//github.com/fedeisas/laravel-mail-css-inliner/raw/master/tests/css/test.css">
</head>
<body>
<div class="pixels-10">
text

<ul>
<li>
Big list
</li>
<li>
Small list
</li>
</ul>
</div>
</body>
</html>

0 comments on commit a2fd35b

Please sign in to comment.