diff --git a/composer.json b/composer.json
index a7c4c8d..db41c6a 100644
--- a/composer.json
+++ b/composer.json
@@ -19,7 +19,8 @@
"illuminate/contracts": "^5.3 | ^6.0 | ^7.0",
"illuminate/support": "^5.3 | ^6.0 | ^7.0",
"illuminate/mail": "^5.3 | ^6.0 | ^7.0 | ^8.0",
- "squizlabs/php_codesniffer": "^3.5"
+ "squizlabs/php_codesniffer": "^3.5",
+ "masterminds/html5": "^2.0"
},
"require-dev": {
"php": ">=7.0",
diff --git a/src/Listeners/SwiftEmbedImages.php b/src/Listeners/SwiftEmbedImages.php
index f286513..4f37a50 100644
--- a/src/Listeners/SwiftEmbedImages.php
+++ b/src/Listeners/SwiftEmbedImages.php
@@ -8,6 +8,7 @@
use Eduardokum\LaravelMailAutoEmbed\Embedder\Base64Embedder;
use Eduardokum\LaravelMailAutoEmbed\Embedder\Embedder;
use Eduardokum\LaravelMailAutoEmbed\Models\EmbeddableEntity;
+use Masterminds\HTML5;
use ReflectionClass;
use Swift_Events_SendEvent;
use Swift_Events_SendListener;
@@ -61,8 +62,9 @@ private function attachImages()
$body = $this->message->getBody();
// Parse document
- $document = new DOMDocument();
- if (!$document->loadHTML($body)) {
+ $parser = new HTML5();
+ $document = $parser->loadHTML($body);
+ if (!$document) {
// Cannot read
return;
}
@@ -71,7 +73,7 @@ private function attachImages()
$this->attachImagesToDom($document);
// Replace body
- $this->message->setBody($document->saveHTML());
+ $this->message->setBody($parser->saveHTML($document));
}
/**
diff --git a/tests/FormatTest.php b/tests/FormatTest.php
new file mode 100644
index 0000000..3e4d365
--- /dev/null
+++ b/tests/FormatTest.php
@@ -0,0 +1,45 @@
+ true,
+ 'method' => 'attachment'
+ ];
+
+ /**
+ * @test
+ */
+ public function testValidHtml5Message()
+ {
+ $message = $this->handleBeforeSendPerformedEvent('formats/html5-valid.html', self::HANDLE_CONFIG);
+
+ $this->assertEmailImageTags([
+ 'url' => 'cid:',
+ 'entity' => 'cid:',
+ ], $message->getBody());
+ }
+
+ /**
+ * @test
+ */
+ public function testUserGeneratedHtml5Message()
+ {
+ $message = $this->handleBeforeSendPerformedEvent('formats/html5-user-generated.html', self::HANDLE_CONFIG);
+
+ $this->assertEmailImageTags([
+ 'url' => 'cid:',
+ 'entity' => 'cid:',
+ ], $message->getBody());
+ }
+}
diff --git a/tests/MailTest.php b/tests/MailTest.php
index 2c6c39d..1a39529 100644
--- a/tests/MailTest.php
+++ b/tests/MailTest.php
@@ -3,48 +3,11 @@
namespace Eduardokum\LaravelMailAutoEmbed\Tests;
use Eduardokum\LaravelMailAutoEmbed\Listeners\SwiftEmbedImages;
+use Eduardokum\LaravelMailAutoEmbed\Tests\Traits\InteractsWithSwift;
class MailTest extends TestCase
{
- /**
- * @param string $htmlMessage
- * @return \Swift_Message
- */
- private function createSwiftMessage($htmlMessage)
- {
- $message = new \Swift_Message('test', $htmlMessage);
-
- return $message;
- }
-
- /**
- * @param \Swift_Message $message
- * @return \Swift_Events_SendEvent
- */
- private function createSwiftEvent(\Swift_Message $message)
- {
- $dispatcher = new \Swift_Events_SimpleEventDispatcher();
- $transport = new \Swift_Transport_NullTransport($dispatcher);
- $event = new \Swift_Events_SendEvent($transport, $message);
-
- return $event;
- }
-
- /**
- * @param string $libraryFile
- * @param array $options
- * @return \Swift_Message
- */
- private function handleBeforeSendPerformedEvent($libraryFile, $options)
- {
- $htmlMessage = $this->getLibraryFile($libraryFile);
- $message = $this->createSwiftMessage($htmlMessage);
-
- $embedPlugin = new SwiftEmbedImages($options);
- $embedPlugin->beforeSendPerformed($this->createSwiftEvent($message));
-
- return $message;
- }
+ use InteractsWithSwift;
/**
* @test
@@ -181,28 +144,4 @@ public function testDoesntHandleSendPerformedEvent()
$embedPlugin->sendPerformed($this->createSwiftEvent($message))
);
}
-
- /**
- * Check the body for image tags with the given keys as comment preceding them.
- * @param array $expectations
- * @param string $body
- * @return void
- */
- protected function assertEmailImageTags($expectations, $body)
- {
- foreach ($expectations as $comment => $src) {
- // Gimmick of using a DOMDocument parser
- $src = str_replace('\\', '%5C', \htmlspecialchars($src));
-
- // Fix for PHPUnit <8.0
- // phpcs:ignore Generic.Files.LineLength.TooLong
- $method = \method_exists($this, 'assertStringContainsString') ? 'assertStringContainsString' : 'assertContains';
-
- // Check if the string is contained within the string
- $this->$method(
- sprintf('
+
+
+
+
+
+
+
+
+
+
Lorem Ipsum
+
+
+ Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla, dolorum assumenda aliquam blanditiis,
+ necessitatibus mollitia delectus sapiente amet earum minima qui non deserunt quidem, doloremque
+ architecto voluptatem eveniet illo aperiam.
+
+ Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla, dolorum assumenda aliquam blanditiis,
+ necessitatibus mollitia delectus sapiente amet earum minima qui non deserunt quidem, doloremque
+ architecto voluptatem eveniet illo aperiam.
+