1<?php
2
3/*
4 * This file is part of the league/commonmark package.
5 *
6 * (c) Colin O'Dell <colinodell@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace League\CommonMark\Extension\InlinesOnly;
13
14use League\CommonMark\Block\Element\AbstractBlock;
15use League\CommonMark\Block\Element\Document;
16use League\CommonMark\Block\Element\InlineContainerInterface;
17use League\CommonMark\Block\Renderer\BlockRendererInterface;
18use League\CommonMark\ElementRendererInterface;
19use League\CommonMark\Inline\Element\AbstractInline;
20
21/**
22 * Simply renders child elements as-is, adding newlines as needed.
23 */
24final class ChildRenderer implements BlockRendererInterface
25{
26    public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, bool $inTightList = false)
27    {
28        $out = '';
29
30        if ($block instanceof InlineContainerInterface) {
31            /** @var iterable<AbstractInline> $children */
32            $children = $block->children();
33            $out .= $htmlRenderer->renderInlines($children);
34        } else {
35            /** @var iterable<AbstractBlock> $children */
36            $children = $block->children();
37            $out .= $htmlRenderer->renderBlocks($children);
38        }
39
40        if (!($block instanceof Document)) {
41            $out .= "\n";
42        }
43
44        return $out;
45    }
46}
47