Skip to content

Commit ea8cc7b

Browse files
authored
Merge pull request #4 from bplace/attr-support
Retrieve attributes from the block before rendering
2 parents 4f3f609 + 5148cd6 commit ea8cc7b

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

src/DefinitionListItemDefinitionRenderer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public function render(AbstractBlock $block, ElementRendererInterface $htmlRende
1414
throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block));
1515
}
1616

17-
return new HtmlElement('dd', [], $htmlRenderer->renderBlocks($block->children(), $inTightList));
17+
$attrs = $block->getData('attributes', []);
18+
19+
return new HtmlElement('dd', $attrs, $htmlRenderer->renderBlocks($block->children(), $inTightList));
1820
}
1921
}

src/DefinitionListItemTermRenderer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public function render(AbstractBlock $block, ElementRendererInterface $htmlRende
1414
throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block));
1515
}
1616

17-
return new HtmlElement('dt', [], $htmlRenderer->renderInlines($block->children()));
17+
$attrs = $block->getData('attributes', []);
18+
19+
return new HtmlElement('dt', $attrs, $htmlRenderer->renderInlines($block->children()));
1820
}
1921
}

src/DefinitionListRenderer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public function render(AbstractBlock $block, ElementRendererInterface $htmlRende
1414
throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block));
1515
}
1616

17-
return new HtmlElement('dl', [], $htmlRenderer->renderBlocks($block->children(), $block->isTight()));
17+
$attrs = $block->getData('attributes', []);
18+
19+
return new HtmlElement('dl', $attrs, $htmlRenderer->renderBlocks($block->children(), $block->isTight()));
1820
}
1921
}

test/AttributeRenderingTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
namespace Moxio\CommonMark\Extension\DefinitionList\Test;
3+
4+
use League\CommonMark\Environment;
5+
use League\CommonMark\HtmlRenderer;
6+
use Moxio\CommonMark\Extension\DefinitionList\DefinitionList;
7+
use Moxio\CommonMark\Extension\DefinitionList\DefinitionListExtension;
8+
use Moxio\CommonMark\Extension\DefinitionList\DefinitionListItemDefinition;
9+
use Moxio\CommonMark\Extension\DefinitionList\DefinitionListItemTerm;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class AttributeRenderingTest extends TestCase
13+
{
14+
private HtmlRenderer $renderer;
15+
16+
protected function setUp(): void
17+
{
18+
$environment = Environment::createCommonMarkEnvironment();
19+
$environment->addExtension(new DefinitionListExtension());
20+
$this->renderer = new HtmlRenderer($environment);
21+
}
22+
23+
public function testCorrectlyRendersAttributesOfDefinitionList(): void
24+
{
25+
$block = new DefinitionList();
26+
$block->data['attributes'] = ['id' => 'foo'];
27+
$actualOutput = $this->renderer->renderBlock($block);
28+
29+
$this->assertXmlStringEqualsXmlString("<html><dl id=\"foo\"></dl></html>", "<html>$actualOutput</html>");
30+
}
31+
32+
public function testCorrectlyRendersAttributesOfDefinitionListItemDefinition(): void
33+
{
34+
$block = new DefinitionListItemDefinition();
35+
$block->data['attributes'] = ['id' => 'foo'];
36+
$actualOutput = $this->renderer->renderBlock($block);
37+
38+
$this->assertXmlStringEqualsXmlString("<html><dd id=\"foo\"></dd></html>", "<html>$actualOutput</html>");
39+
}
40+
41+
public function testCorrectlyRendersAttributesOfDefinitionListItemTerm(): void
42+
{
43+
$block = new DefinitionListItemTerm([]);
44+
$block->data['attributes'] = ['id' => 'foo'];
45+
$actualOutput = $this->renderer->renderBlock($block);
46+
47+
$this->assertXmlStringEqualsXmlString("<html><dt id=\"foo\"></dt></html>", "<html>$actualOutput</html>");
48+
}
49+
}

0 commit comments

Comments
 (0)