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 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9 *  - (c) John MacFarlane
10 *
11 * For the full copyright and license information, please view the LICENSE
12 * file that was distributed with this source code.
13 */
14
15namespace League\CommonMark\Block\Renderer;
16
17use League\CommonMark\Block\Element\AbstractBlock;
18use League\CommonMark\Block\Element\HtmlBlock;
19use League\CommonMark\ElementRendererInterface;
20use League\CommonMark\EnvironmentInterface;
21use League\CommonMark\Util\ConfigurationAwareInterface;
22use League\CommonMark\Util\ConfigurationInterface;
23
24final class HtmlBlockRenderer implements BlockRendererInterface, ConfigurationAwareInterface
25{
26    /**
27     * @var ConfigurationInterface
28     */
29    protected $config;
30
31    /**
32     * @param HtmlBlock                $block
33     * @param ElementRendererInterface $htmlRenderer
34     * @param bool                     $inTightList
35     *
36     * @return string
37     */
38    public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, bool $inTightList = false)
39    {
40        if (!($block instanceof HtmlBlock)) {
41            throw new \InvalidArgumentException('Incompatible block type: ' . \get_class($block));
42        }
43
44        if ($this->config->get('html_input') === EnvironmentInterface::HTML_INPUT_STRIP) {
45            return '';
46        }
47
48        if ($this->config->get('html_input') === EnvironmentInterface::HTML_INPUT_ESCAPE) {
49            return \htmlspecialchars($block->getStringContent(), \ENT_NOQUOTES);
50        }
51
52        return $block->getStringContent();
53    }
54
55    public function setConfiguration(ConfigurationInterface $configuration)
56    {
57        $this->config = $configuration;
58    }
59}
60