Skip to content

Commit a2fd35b

Browse files
authored
External relative links (#124)
1 parent 58433e2 commit a2fd35b

6 files changed

+137
-2
lines changed

src/CssInlinerPlugin.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ public function loadOptions($options)
6666
{
6767
if (isset($options['css-files']) && count($options['css-files']) > 0) {
6868
$this->css = '';
69-
foreach ($options['css-files'] as $file) {
70-
$this->css .= file_get_contents($file);
69+
foreach ($options['css-files'] as $fileUrl) {
70+
// Fix relative protocols on hrefs. Assume https.
71+
if (substr($fileUrl, 0, 2) === '//') {
72+
$fileUrl = 'https:' . $fileUrl;
73+
}
74+
75+
$this->css .= file_get_contents($fileUrl);
7176
}
7277
}
7378
}

tests/CssInlinerPluginTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ class CssInlinerPluginTest extends TestCase
2424
'converted-html',
2525
'converted-html-with-css',
2626
'converted-html-with-link-css',
27+
'converted-html-with-link-external',
28+
'converted-html-with-link-relative-external',
2729
'converted-html-with-links-css',
2830
'converted-html-with-mixed-type-links',
2931
'converted-html-with-non-stylesheet-link',
3032
'original-html',
3133
'original-html-with-css',
3234
'original-html-with-link-css',
35+
'original-html-with-link-external',
36+
'original-html-with-link-relative-external',
3337
'original-html-with-links-css',
3438
'original-html-with-mixed-type-links',
3539
'original-html-with-non-stylesheet-link',
@@ -238,6 +242,50 @@ public function itShouldWorkWithMixedTypeLinks()
238242
);
239243
}
240244

245+
/** @test **/
246+
public function itShouldWorkWithExternalLink()
247+
{
248+
$mailer = new Swift_Mailer(new Swift_NullTransport());
249+
250+
$mailer->registerPlugin(new CssInlinerPlugin($this->options));
251+
252+
$message = new Swift_Message('Test');
253+
254+
$message->setFrom('[email protected]');
255+
$message->setTo('[email protected]');
256+
257+
$message->setBody($this->stubs['original-html-with-link-external'], 'text/html');
258+
259+
$mailer->send($message);
260+
261+
$this->assertEquals(
262+
$this->stubs['converted-html-with-link-external'],
263+
$this->normalize($message->getBody())
264+
);
265+
}
266+
267+
/** @test **/
268+
public function itShouldWorkWithRelativeExternalLink()
269+
{
270+
$mailer = new Swift_Mailer(new Swift_NullTransport());
271+
272+
$mailer->registerPlugin(new CssInlinerPlugin($this->options));
273+
274+
$message = new Swift_Message('Test');
275+
276+
$message->setFrom('[email protected]');
277+
$message->setTo('[email protected]');
278+
279+
$message->setBody($this->stubs['original-html-with-link-relative-external'], 'text/html');
280+
281+
$mailer->send($message);
282+
283+
$this->assertEquals(
284+
$this->stubs['converted-html-with-link-relative-external'],
285+
$this->normalize($message->getBody())
286+
);
287+
}
288+
241289
protected function normalize(string $html): string
242290
{
243291
$document = new \DomDocument();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2+
<html>
3+
<head>
4+
5+
</head>
6+
<body>
7+
<div class="pixels-10" style="width: 10px;">
8+
text
9+
10+
<ul>
11+
<li>
12+
Big list
13+
</li>
14+
<li>
15+
Small list
16+
</li>
17+
</ul>
18+
</div>
19+
</body>
20+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2+
<html>
3+
<head>
4+
5+
</head>
6+
<body>
7+
<div class="pixels-10" style="width: 10px;">
8+
text
9+
10+
<ul>
11+
<li>
12+
Big list
13+
</li>
14+
<li>
15+
Small list
16+
</li>
17+
</ul>
18+
</div>
19+
</body>
20+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2+
<html>
3+
<head>
4+
<link rel="stylesheet" type="text/css" href="https://github.com/fedeisas/laravel-mail-css-inliner/raw/master/tests/css/test.css">
5+
</head>
6+
<body>
7+
<div class="pixels-10">
8+
text
9+
10+
<ul>
11+
<li>
12+
Big list
13+
</li>
14+
<li>
15+
Small list
16+
</li>
17+
</ul>
18+
</div>
19+
</body>
20+
</html>
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
2+
<html>
3+
<head>
4+
<link rel="stylesheet" type="text/css" href="//github.com/fedeisas/laravel-mail-css-inliner/raw/master/tests/css/test.css">
5+
</head>
6+
<body>
7+
<div class="pixels-10">
8+
text
9+
10+
<ul>
11+
<li>
12+
Big list
13+
</li>
14+
<li>
15+
Small list
16+
</li>
17+
</ul>
18+
</div>
19+
</body>
20+
</html>
21+

0 commit comments

Comments
 (0)