Skip to content

Commit

Permalink
Merge pull request #4 from bplace/attr-support
Browse files Browse the repository at this point in the history
Retrieve attributes from the block before rendering
  • Loading branch information
aboks committed Dec 20, 2021
2 parents 4f3f609 + 5148cd6 commit ea8cc7b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/DefinitionListItemDefinitionRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function render(AbstractBlock $block, ElementRendererInterface $htmlRende
throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block));
}

return new HtmlElement('dd', [], $htmlRenderer->renderBlocks($block->children(), $inTightList));
$attrs = $block->getData('attributes', []);

return new HtmlElement('dd', $attrs, $htmlRenderer->renderBlocks($block->children(), $inTightList));
}
}
4 changes: 3 additions & 1 deletion src/DefinitionListItemTermRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function render(AbstractBlock $block, ElementRendererInterface $htmlRende
throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block));
}

return new HtmlElement('dt', [], $htmlRenderer->renderInlines($block->children()));
$attrs = $block->getData('attributes', []);

return new HtmlElement('dt', $attrs, $htmlRenderer->renderInlines($block->children()));
}
}
4 changes: 3 additions & 1 deletion src/DefinitionListRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function render(AbstractBlock $block, ElementRendererInterface $htmlRende
throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block));
}

return new HtmlElement('dl', [], $htmlRenderer->renderBlocks($block->children(), $block->isTight()));
$attrs = $block->getData('attributes', []);

return new HtmlElement('dl', $attrs, $htmlRenderer->renderBlocks($block->children(), $block->isTight()));
}
}
49 changes: 49 additions & 0 deletions test/AttributeRenderingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace Moxio\CommonMark\Extension\DefinitionList\Test;

use League\CommonMark\Environment;
use League\CommonMark\HtmlRenderer;
use Moxio\CommonMark\Extension\DefinitionList\DefinitionList;
use Moxio\CommonMark\Extension\DefinitionList\DefinitionListExtension;
use Moxio\CommonMark\Extension\DefinitionList\DefinitionListItemDefinition;
use Moxio\CommonMark\Extension\DefinitionList\DefinitionListItemTerm;
use PHPUnit\Framework\TestCase;

class AttributeRenderingTest extends TestCase
{
private HtmlRenderer $renderer;

protected function setUp(): void
{
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new DefinitionListExtension());
$this->renderer = new HtmlRenderer($environment);
}

public function testCorrectlyRendersAttributesOfDefinitionList(): void
{
$block = new DefinitionList();
$block->data['attributes'] = ['id' => 'foo'];
$actualOutput = $this->renderer->renderBlock($block);

$this->assertXmlStringEqualsXmlString("<html><dl id=\"foo\"></dl></html>", "<html>$actualOutput</html>");
}

public function testCorrectlyRendersAttributesOfDefinitionListItemDefinition(): void
{
$block = new DefinitionListItemDefinition();
$block->data['attributes'] = ['id' => 'foo'];
$actualOutput = $this->renderer->renderBlock($block);

$this->assertXmlStringEqualsXmlString("<html><dd id=\"foo\"></dd></html>", "<html>$actualOutput</html>");
}

public function testCorrectlyRendersAttributesOfDefinitionListItemTerm(): void
{
$block = new DefinitionListItemTerm([]);
$block->data['attributes'] = ['id' => 'foo'];
$actualOutput = $this->renderer->renderBlock($block);

$this->assertXmlStringEqualsXmlString("<html><dt id=\"foo\"></dt></html>", "<html>$actualOutput</html>");
}
}

0 comments on commit ea8cc7b

Please sign in to comment.