1 | <?php |
||
26 | final class ImageRenderer implements InlineRendererInterface, ConfigurationAwareInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var ConfigurationInterface |
||
30 | */ |
||
31 | protected $config; |
||
32 | |||
33 | * @param Image $inline |
||
|
|||
34 | * @param NodeRendererInterface $htmlRenderer |
||
35 | * |
||
36 | * @return HtmlElement |
||
37 | */ |
||
38 | public function render(AbstractInline $inline, NodeRendererInterface $htmlRenderer) |
||
39 | { |
||
40 | if (!($inline instanceof Image)) { |
||
41 | throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline)); |
||
42 | } |
||
43 | |||
44 | $attrs = $inline->getData('attributes', []); |
||
45 | |||
46 | $forbidUnsafeLinks = !$this->config->get('allow_unsafe_links'); |
||
47 | if ($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl())) { |
||
48 | $attrs['src'] = ''; |
||
49 | } else { |
||
50 | $attrs['src'] = $inline->getUrl(); |
||
51 | } |
||
52 | |||
53 | $alt = $htmlRenderer->renderInlines($inline->children()); |
||
54 | $alt = \preg_replace('/\<[^>]*alt="([^"]*)"[^>]*\>/', '$1', $alt); |
||
55 | $attrs['alt'] = \preg_replace('/\<[^>]*\>/', '', $alt); |
||
56 | |||
57 | if (isset($inline->data['title'])) { |
||
58 | $attrs['title'] = $inline->data['title']; |
||
59 | } |
||
60 | |||
61 | return new HtmlElement('img', $attrs, '', true); |
||
62 | } |
||
63 | |||
64 | public function setConfiguration(ConfigurationInterface $configuration): void |
||
65 | { |
||
66 | $this->config = $configuration; |
||
67 | } |
||
68 | } |
||
69 |