League\CommonMark\Inline\Renderer\ImageRenderer::render PHP Method

render() public method

public render ( AbstractInline $inline, League\CommonMark\ElementRendererInterface $htmlRenderer ) : League\CommonMark\HtmlElement
$inline League\CommonMark\Inline\Element\AbstractInline
$htmlRenderer League\CommonMark\ElementRendererInterface
return League\CommonMark\HtmlElement
    public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
    {
        if (!$inline instanceof Image) {
            throw new \InvalidArgumentException('Incompatible inline type: ' . get_class($inline));
        }
        $attrs = [];
        foreach ($inline->getData('attributes', []) as $key => $value) {
            $attrs[$key] = $htmlRenderer->escape($value, true);
        }
        $forbidUnsafeLinks = $this->config->getConfig('safe') || !$this->config->getConfig('allow_unsafe_links');
        if ($forbidUnsafeLinks && RegexHelper::isLinkPotentiallyUnsafe($inline->getUrl())) {
            $attrs['src'] = '';
        } else {
            $attrs['src'] = $htmlRenderer->escape($inline->getUrl(), true);
        }
        $alt = $htmlRenderer->renderInlines($inline->children());
        $alt = preg_replace('/\\<[^>]*alt="([^"]*)"[^>]*\\>/', '$1', $alt);
        $attrs['alt'] = preg_replace('/\\<[^>]*\\>/', '', $alt);
        if (isset($inline->data['title'])) {
            $attrs['title'] = $htmlRenderer->escape($inline->data['title'], true);
        }
        return new HtmlElement('img', $attrs, '', true);
    }

Usage Example

Beispiel #1
0
 /**
  * @param Image                    $inline
  * @param ElementRendererInterface $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
 {
     // External Images need special handling
     if (strpos($inline->getUrl(), 'http') === 0) {
         return new HtmlElement('ac:image', [], new HtmlElement('ri:url', ['ri:value' => $inline->getUrl()]));
     }
     return parent::render($inline, $htmlRenderer);
 }
All Usage Examples Of League\CommonMark\Inline\Renderer\ImageRenderer::render