diff --git a/src/CssInlinerPlugin.php b/src/CssInlinerPlugin.php index ac5233e..72fc382 100644 --- a/src/CssInlinerPlugin.php +++ b/src/CssInlinerPlugin.php @@ -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); } } } diff --git a/tests/CssInlinerPluginTest.php b/tests/CssInlinerPluginTest.php index b664e31..e26882f 100644 --- a/tests/CssInlinerPluginTest.php +++ b/tests/CssInlinerPluginTest.php @@ -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', @@ -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('test@example.com'); + $message->setTo('test2@example.com'); + + $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('test@example.com'); + $message->setTo('test2@example.com'); + + $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(); diff --git a/tests/stubs/converted-html-with-link-external.stub b/tests/stubs/converted-html-with-link-external.stub new file mode 100644 index 0000000..74a67d2 --- /dev/null +++ b/tests/stubs/converted-html-with-link-external.stub @@ -0,0 +1,20 @@ + + + + + + +
+ text + + +
+ + diff --git a/tests/stubs/converted-html-with-link-relative-external.stub b/tests/stubs/converted-html-with-link-relative-external.stub new file mode 100644 index 0000000..74a67d2 --- /dev/null +++ b/tests/stubs/converted-html-with-link-relative-external.stub @@ -0,0 +1,20 @@ + + + + + + +
+ text + + +
+ + diff --git a/tests/stubs/original-html-with-link-external.stub b/tests/stubs/original-html-with-link-external.stub new file mode 100644 index 0000000..769f2ee --- /dev/null +++ b/tests/stubs/original-html-with-link-external.stub @@ -0,0 +1,21 @@ + + + + + + +
+ text + + +
+ + + diff --git a/tests/stubs/original-html-with-link-relative-external.stub b/tests/stubs/original-html-with-link-relative-external.stub new file mode 100644 index 0000000..dc424c5 --- /dev/null +++ b/tests/stubs/original-html-with-link-relative-external.stub @@ -0,0 +1,21 @@ + + + + + + +
+ text + + +
+ + +