| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the league/commonmark package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * (c) Colin O'Dell <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  *  - (c) John MacFarlane | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | namespace League\CommonMark\Extension\CommonMark\Renderer\Inline; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use League\CommonMark\Extension\CommonMark\Node\Inline\Image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use League\CommonMark\Node\Inline\Newline; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use League\CommonMark\Node\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use League\CommonMark\Node\NodeIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use League\CommonMark\Node\StringContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use League\CommonMark\Renderer\ChildNodeRendererInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use League\CommonMark\Renderer\NodeRendererInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use League\CommonMark\Util\HtmlElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use League\CommonMark\Util\RegexHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use League\CommonMark\Xml\XmlNodeRendererInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use League\Config\ConfigurationAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use League\Config\ConfigurationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | final class ImageRenderer implements NodeRendererInterface, XmlNodeRendererInterface, ConfigurationAwareInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** @psalm-readonly-allow-private-mutation */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private ConfigurationInterface $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param Image $node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @psalm-suppress MoreSpecificImplementedParamType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 64 |  |     public function render(Node $node, ChildNodeRendererInterface $childRenderer): \Stringable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 64 |  |         Image::assertInstanceOf($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 62 |  |         $attrs = $node->data->get('attributes'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 62 |  |         $forbidUnsafeLinks = ! $this->config->get('allow_unsafe_links'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 62 |  |         if ($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($node->getUrl())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |             $attrs['src'] = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 60 |  |             $attrs['src'] = $node->getUrl(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 62 |  |         $attrs['alt'] = $this->getAltText($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 62 |  |         if (($title = $node->getTitle()) !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 26 |  |             $attrs['title'] = $title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 62 |  |         return new HtmlElement('img', $attrs, '', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2178 |  |     public function setConfiguration(ConfigurationInterface $configuration): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2178 |  |         $this->config = $configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2178 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |     public function getXmlTagName(Node $node): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 2 |  |         return 'image'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param Image $node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return array<string, scalar> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @psalm-suppress MoreSpecificImplementedParamType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |     public function getXmlAttributes(Node $node): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 2 |  |         Image::assertInstanceOf($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |             'destination' => $node->getUrl(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |             'title' => $node->getTitle() ?? '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 62 |  |     private function getAltText(Image $node): string | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 62 |  |         $altText = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 62 |  |         foreach ((new NodeIterator($node)) as $n) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 62 |  |             if ($n instanceof StringContainerInterface) { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 56 |  |                 $altText .= $n->getLiteral(); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 62 |  |             } elseif ($n instanceof Newline) { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |                 $altText .= "\n"; | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 62 |  |         return $altText; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |