feat: Divi BlockTree – byte-exact parser/serializer with round-trip fixtures

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
2026-09-26 11:26:14 +02:00
co-authored by Claude Opus 5.5
parent c9b31a61da
commit 8222240ed6
18 changed files with 819 additions and 0 deletions
@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Tests\Unit\Integrations\Divi\BlockTree;
use LIT\MCP\Integrations\Divi\BlockTree\Block;
use LIT\MCP\Integrations\Divi\BlockTree\Document;
use LIT\MCP\Integrations\Divi\BlockTree\Parser;
use LIT\MCP\Integrations\Divi\BlockTree\Serializer;
use PHPUnit\Framework\TestCase;
use stdClass;
final class SerializerTest extends TestCase
{
public function testEscapesAttributesLikeWordPress(): void
{
$variable = '$variable({"type":"color","value":{"name":"gcid-primary-color","settings":{}}})$';
self::assertSame(
'{"c":"$variable({\u0022type\u0022:\u0022color\u0022,\u0022value\u0022:{\u0022name\u0022:'
. '\u0022gcid-primary-color\u0022,\u0022settings\u0022:{}}})$"}',
Serializer::attributes(['c' => $variable])
);
self::assertSame(
'{"t":"\u003cp\u003eA \u0026 B\u003c/p\u003e \u002d\u002d \u005c/ ä","e":{},"l":[]}',
Serializer::attributes(['t' => '<p>A & B</p> -- \\/ ä', 'e' => new stdClass(), 'l' => []])
);
}
public function testRegeneratesOnlyTheChangedOpener(): void
{
$content = '<!-- wp:divi/section {"b":1} --><!-- wp:divi/text {"x":"y"} /--><!-- /wp:divi/section -->';
$document = (new Parser())->parse($content);
$text = $document->children()[0]->children()[0];
$text->setAttrs(['x' => 'z']);
self::assertSame(
'<!-- wp:divi/section {"b":1} --><!-- wp:divi/text {"x":"z"} /--><!-- /wp:divi/section -->',
Serializer::document($document)
);
}
public function testNewBlocksAreWrittenAsPairs(): void
{
$block = Block::create('divi/row', ['builderVersion' => '5.2.1'], [Block::create('divi/column')]);
self::assertSame(
'<!-- wp:divi/row {"builderVersion":"5.2.1"} --><!-- wp:divi/column --><!-- /wp:divi/column -->'
. '<!-- /wp:divi/row -->',
Serializer::block($block)
);
self::assertSame('<!-- wp:paragraph --><!-- /wp:paragraph -->', Serializer::block(Block::create('core/paragraph')));
}
public function testVoidBlockGettingAChildBecomesAPair(): void
{
$document = (new Parser())->parse('<!-- wp:divi/column {"a":1} /-->');
$column = $document->children()[0];
$column->insertChild(0, Block::create('divi/text'));
self::assertSame(
'<!-- wp:divi/column {"a":1} --><!-- wp:divi/text --><!-- /wp:divi/text --><!-- /wp:divi/column -->',
Serializer::document($document)
);
}
public function testInsertAndRemoveKeepSurroundingText(): void
{
$document = (new Parser())->parse("A<!-- wp:divi/text /-->\nB<!-- wp:divi/code /-->C");
[$text, $code] = $document->children();
$new = Block::create('divi/button');
$document->insertChild(1, $new);
self::assertSame(
"A<!-- wp:divi/text /-->\nB<!-- wp:divi/button --><!-- /wp:divi/button --><!-- wp:divi/code /-->C",
Serializer::document($document)
);
self::assertSame(2, $document->removeChild($code));
self::assertSame(0, $document->indexOf($text));
$document->insertChild(99, $code);
self::assertSame(2, $document->indexOf($code));
$empty = new Document();
$empty->append("\n");
$empty->insertChild(0, Block::create('divi/section'));
self::assertSame("\n<!-- wp:divi/section --><!-- /wp:divi/section -->", Serializer::document($empty));
}
public function testCopyIsDeepAndSerializesIdentically(): void
{
$content = '<!-- wp:divi/row {"a":1} --><!-- wp:divi/column /--><!-- /wp:divi/row -->';
$row = (new Parser())->parse($content)->children()[0];
$copy = $row->copy();
$copy->children()[0]->setAttrs(['b' => 2]);
self::assertSame($content, Serializer::block($row));
self::assertSame('<!-- wp:divi/row {"a":1} --><!-- wp:divi/column {"b":2} /--><!-- /wp:divi/row -->', Serializer::block($copy));
}
}