Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Parser/MjmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function parse(string $mjml, ?string $filePath = null): Node
$doc->loadXML($mjml, \LIBXML_NONET | \LIBXML_NOWARNING);

$collector->collect(restorePrevious: true);
$this->assertReadable($filePath);

$root = $doc->documentElement;

Expand Down Expand Up @@ -335,6 +336,7 @@ private function parseIncludeContent(string $mjml, string $filePath): Node
$doc->loadXML($mjml, \LIBXML_NONET | \LIBXML_NOWARNING);

$collector->collect();
$this->assertReadable($filePath);

$root = $doc->documentElement;

Expand All @@ -345,6 +347,24 @@ private function parseIncludeContent(string $mjml, string $filePath): Node
return $this->domToNode($root, $filePath);
}

/**
* A fatal libxml error leaves no usable tree, so rendering on would produce an empty email.
*
* @throws ParseException
*/
private function assertReadable(?string $filePath): void
{
if (!$this->libxmlErrors->hasFatalErrors()) {
return;
}

$source = $filePath !== null ? " in {$filePath}" : '';

throw new ParseException(
"Malformed MJML{$source}: " . implode('; ', $this->libxmlErrors->getErrorMessages()),
);
}

/**
* Merge accumulated CSS includes into the mj-head node.
*
Expand Down
30 changes: 30 additions & 0 deletions tests/Unit/Parser/MjmlParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use PHPUnit\Framework\TestCase;
use MjmlPHP\Component\ComponentRegistry;
use MjmlPHP\Mjml;
use MjmlPHP\MjmlOptions;
use MjmlPHP\Parser\MjmlParser;
use MjmlPHP\Parser\ParseException;
use MjmlPHP\Validation\ValidationLevel;

final class MjmlParserTest extends TestCase
Expand Down Expand Up @@ -769,6 +771,34 @@ public function testWhitespaceOnlyInputProducesEmptyMjmlNode(): void
self::assertSame([], $node->children);
}

public function testMissingClosingTagThrows(): void
{
$this->expectException(ParseException::class);
$this->expectExceptionMessageMatches('/^Malformed MJML: \[FATAL\] line 1: Opening and ending tag mismatch/');

$this->parser->parse('<mjml><mj-body><mj-section><mj-column><mj-text>Hi</mj-text></mj-column></mj-body></mjml>');
}

public function testBareAmpersandInAttributeThrows(): void
{
$this->expectException(ParseException::class);
$this->expectExceptionMessageMatches('/EntityRef/');

$this->parser->parse('<mjml><mj-body><mj-button href="https://example.com/?a=1&b=2">Go</mj-button></mj-body></mjml>');
}

public function testRenderThrowsForMalformedMarkupInEveryValidationLevel(): void
{
foreach (ValidationLevel::cases() as $level) {
try {
Mjml::render('<mjml><mj-body><mj-section></mj-body></mjml>', new MjmlOptions(validationLevel: $level));
self::fail("Expected a ParseException with validation level {$level->value}.");
} catch (ParseException $exception) {
self::assertStringStartsWith('Malformed MJML: ', $exception->getMessage());
}
}
}

public function testDeeplyNestedSectionsDoNotCrash(): void
{
// 50 nested wrappers — well below libxml limits but still non-trivial
Expand Down
Loading