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