From 6f5780abfd126c509b46343da5c5f6db5784c0e8 Mon Sep 17 00:00:00 2001 From: Aidan Woods Date: Mon, 21 Jan 2019 18:00:02 +0000 Subject: [PATCH] Improve Link API --- src/Components/Inlines/Image.php | 10 +++--- src/Components/Inlines/Link.php | 59 ++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/Components/Inlines/Image.php b/src/Components/Inlines/Image.php index a5178d5..e6198f1 100644 --- a/src/Components/Inlines/Image.php +++ b/src/Components/Inlines/Image.php @@ -59,15 +59,15 @@ final class Image implements Inline return new Handler( /** @return Element|Text */ function (State $State) use ($Parsedown) { - $linkAttributes = $this->Link->attributes(); - $attributes = [ - 'src' => $linkAttributes['href'], + 'src' => $this->Link->url(), 'alt' => $this->Link->label(), ]; - if (isset($linkAttributes['title'])) { - $attributes['title'] = $linkAttributes['title']; + $title = $this->Link->title(); + + if (isset($title)) { + $attributes['title'] = $title; } if ($State->getOrDefault(SafeMode::class)->enabled()) { diff --git a/src/Components/Inlines/Link.php b/src/Components/Inlines/Link.php index b0f9bd4..ac6cac6 100644 --- a/src/Components/Inlines/Link.php +++ b/src/Components/Inlines/Link.php @@ -21,18 +21,23 @@ final class Link implements Inline /** @var string */ private $label; - /** @var _Metadata */ - private $attributes; + /** @var string */ + private $url; + + /** @var string|null */ + private $title; /** * @param string $label - * @param _Metadata $attributes + * @param string $url + * @param string|null $title * @param int $width */ - public function __construct($label, $attributes, $width) + public function __construct($label, $url, $title, $width) { $this->label = $label; - $this->attributes = $attributes; + $this->url = $url; + $this->title = $title; $this->width = $width; } @@ -50,27 +55,24 @@ final class Link implements Inline } $rawLabelPart = $matches[0]; $label = $matches[1]; - /** @var _Metadata|null */ - $attributes = null; - $extent = \strlen($matches[0]); + $width = \strlen($matches[0]); - $remainder = \substr($remainder, $extent); + $remainder = \substr($remainder, $width); if (\preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*+"|\'[^\']*+\'))?\s*+[)]/', $remainder, $matches)) { - $attributes = ['href' => $matches[1]]; + $url = $matches[1]; + $title = isset($matches[2]) ? \substr($matches[2], 1, - 1) : null; - if (isset($matches[2])) { - $attributes['title'] = \substr($matches[2], 1, - 1); - } + $width += \strlen($matches[0]); - $extent += \strlen($matches[0]); + return new self($label, $url, $title, $width); } else { if (\preg_match('/^\s*\[(.*?)\]/', $remainder, $matches)) { $definition = \strlen($matches[1]) ? $matches[1] : $label; $definition = \strtolower($definition); - $extent += \strlen($matches[0]); + $width += \strlen($matches[0]); } else { $definition = \strtolower($label); } @@ -81,14 +83,11 @@ final class Link implements Inline return null; } - $attributes = ['href' => $data['url']]; + $url = $data['url']; + $title = isset($data['title']) ? $data['title'] : null; - if (isset($data['title'])) { - $attributes['title'] = $data['title']; - } + return new self($label, $url, $title, $width); } - - return new self($label, $attributes, $extent); } /** @return string */ @@ -97,10 +96,16 @@ final class Link implements Inline return $this->label; } - /** @return _Metadata */ - public function attributes() + /** @return string */ + public function url() { - return $this->attributes; + return $this->url; + } + + /** @return string|null */ + public function title() + { + return $this->title; } /** @@ -111,7 +116,11 @@ final class Link implements Inline return new Handler( /** @return Element|Text */ function (State $State) use ($Parsedown) { - $attributes = $this->attributes; + $attributes = ['href' => $this->url]; + + if (isset($this->title)) { + $attributes['title'] = $this->title; + } if ($State->getOrDefault(SafeMode::class)->enabled()) { $attributes['href'] = Element::filterUnsafeUrl($attributes['href']);