1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This is part of the league/commonmark package. |
7
|
|
|
* |
8
|
|
|
* (c) Martin Hasoň <[email protected]> |
9
|
|
|
* (c) Webuni s.r.o. <[email protected]> |
10
|
|
|
* (c) Colin O'Dell <[email protected]> |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace League\CommonMark\Extension\Table; |
17
|
|
|
|
18
|
|
|
use League\CommonMark\Extension\Attributes\Util\AttributesHelper; |
19
|
|
|
use League\CommonMark\Node\Node; |
20
|
|
|
use League\CommonMark\Renderer\ChildNodeRendererInterface; |
21
|
|
|
use League\CommonMark\Renderer\NodeRendererInterface; |
22
|
|
|
use League\CommonMark\Util\HtmlElement; |
23
|
|
|
use League\CommonMark\Xml\XmlNodeRendererInterface; |
24
|
|
|
|
25
|
|
|
final class TableCellRenderer implements NodeRendererInterface, XmlNodeRendererInterface |
26
|
|
|
{ |
27
|
|
|
private const DEFAULT_ATTRIBUTES = [ |
28
|
|
|
TableCell::ALIGN_LEFT => ['align' => 'left'], |
29
|
|
|
TableCell::ALIGN_CENTER => ['align' => 'center'], |
30
|
|
|
TableCell::ALIGN_RIGHT => ['align' => 'right'], |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** @var array<TableCell::ALIGN_*, array<string, string|string[]|bool>> */ |
|
|
|
|
34
|
|
|
private array $alignmentAttributes; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array<TableCell::ALIGN_*, array<string, string|string[]|bool>> $alignmentAttributes |
|
|
|
|
38
|
|
|
*/ |
39
|
140 |
|
public function __construct(array $alignmentAttributes = self::DEFAULT_ATTRIBUTES) |
40
|
|
|
{ |
41
|
140 |
|
$this->alignmentAttributes = $alignmentAttributes; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param TableCell $node |
46
|
|
|
* |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
* |
49
|
|
|
* @psalm-suppress MoreSpecificImplementedParamType |
50
|
|
|
*/ |
51
|
64 |
|
public function render(Node $node, ChildNodeRendererInterface $childRenderer): \Stringable |
52
|
|
|
{ |
53
|
64 |
|
TableCell::assertInstanceOf($node); |
54
|
|
|
|
55
|
62 |
|
$attrs = $node->data->get('attributes'); |
56
|
62 |
|
if (($alignment = $node->getAlign()) !== null) { |
57
|
14 |
|
$attrs = AttributesHelper::mergeAttributes($attrs, $this->alignmentAttributes[$alignment]); |
58
|
|
|
} |
59
|
|
|
|
60
|
62 |
|
$tag = $node->getType() === TableCell::TYPE_HEADER ? 'th' : 'td'; |
61
|
|
|
|
62
|
62 |
|
return new HtmlElement($tag, $attrs, $childRenderer->renderNodes($node->children())); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
public function getXmlTagName(Node $node): string |
66
|
|
|
{ |
67
|
2 |
|
return 'table_cell'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param TableCell $node |
72
|
|
|
* |
73
|
|
|
* @return array<string, scalar> |
74
|
|
|
* |
75
|
|
|
* @psalm-suppress MoreSpecificImplementedParamType |
76
|
|
|
*/ |
77
|
2 |
|
public function getXmlAttributes(Node $node): array |
78
|
|
|
{ |
79
|
2 |
|
TableCell::assertInstanceOf($node); |
80
|
|
|
|
81
|
2 |
|
$ret = ['type' => $node->getType()]; |
82
|
|
|
|
83
|
2 |
|
if (($align = $node->getAlign()) !== null) { |
84
|
2 |
|
$ret['align'] = $align; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
return $ret; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|