League\CommonMark\Inline\Renderer\LinkRenderer::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 Link) {
            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['href'] = $htmlRenderer->escape($inline->getUrl(), true);
        }
        if (isset($inline->data['title'])) {
            $attrs['title'] = $htmlRenderer->escape($inline->data['title'], true);
        }
        return new HtmlElement('a', $attrs, $htmlRenderer->renderInlines($inline->children()));
    }

Usage Example

Beispiel #1
0
 /**
  * @param Link $inline
  * @param ElementRendererInterface $htmlRenderer
  *
  * @return HtmlElement
  */
 public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
 {
     // This can't be in the method type as
     // the method is an abstract and should
     // have the same interface
     if (!$inline instanceof Link) {
         throw new \RuntimeException("Wrong type passed to " . __CLASS__ . "::" . __METHOD__ . " the expected type was 'League\\CommonMark\\Inline\\Element\\Link' but '" . get_class($inline) . "' was provided");
     }
     $element = parent::render($inline, $htmlRenderer);
     $url = $inline->getUrl();
     if (!empty($url) && $url[0] == '!') {
         $file = $this->resolveInternalFile(ltrim($url, "!"));
         $element->setAttribute('href', $this->daux['base_url'] . $file->getUrl());
     }
     return $element;
 }
All Usage Examples Of League\CommonMark\Inline\Renderer\LinkRenderer::render