1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Improve Link API

This commit is contained in:
Aidan Woods 2019-01-21 18:00:02 +00:00
parent 2757274854
commit 6f5780abfd
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
2 changed files with 39 additions and 30 deletions

View File

@ -59,15 +59,15 @@ final class Image implements Inline
return new Handler( return new Handler(
/** @return Element|Text */ /** @return Element|Text */
function (State $State) use ($Parsedown) { function (State $State) use ($Parsedown) {
$linkAttributes = $this->Link->attributes();
$attributes = [ $attributes = [
'src' => $linkAttributes['href'], 'src' => $this->Link->url(),
'alt' => $this->Link->label(), 'alt' => $this->Link->label(),
]; ];
if (isset($linkAttributes['title'])) { $title = $this->Link->title();
$attributes['title'] = $linkAttributes['title'];
if (isset($title)) {
$attributes['title'] = $title;
} }
if ($State->getOrDefault(SafeMode::class)->enabled()) { if ($State->getOrDefault(SafeMode::class)->enabled()) {

View File

@ -21,18 +21,23 @@ final class Link implements Inline
/** @var string */ /** @var string */
private $label; private $label;
/** @var _Metadata */ /** @var string */
private $attributes; private $url;
/** @var string|null */
private $title;
/** /**
* @param string $label * @param string $label
* @param _Metadata $attributes * @param string $url
* @param string|null $title
* @param int $width * @param int $width
*/ */
public function __construct($label, $attributes, $width) public function __construct($label, $url, $title, $width)
{ {
$this->label = $label; $this->label = $label;
$this->attributes = $attributes; $this->url = $url;
$this->title = $title;
$this->width = $width; $this->width = $width;
} }
@ -50,27 +55,24 @@ final class Link implements Inline
} }
$rawLabelPart = $matches[0]; $rawLabelPart = $matches[0];
$label = $matches[1]; $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)) { 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])) { $width += \strlen($matches[0]);
$attributes['title'] = \substr($matches[2], 1, - 1);
}
$extent += \strlen($matches[0]); return new self($label, $url, $title, $width);
} else { } else {
if (\preg_match('/^\s*\[(.*?)\]/', $remainder, $matches)) { if (\preg_match('/^\s*\[(.*?)\]/', $remainder, $matches)) {
$definition = \strlen($matches[1]) ? $matches[1] : $label; $definition = \strlen($matches[1]) ? $matches[1] : $label;
$definition = \strtolower($definition); $definition = \strtolower($definition);
$extent += \strlen($matches[0]); $width += \strlen($matches[0]);
} else { } else {
$definition = \strtolower($label); $definition = \strtolower($label);
} }
@ -81,14 +83,11 @@ final class Link implements Inline
return null; return null;
} }
$attributes = ['href' => $data['url']]; $url = $data['url'];
$title = isset($data['title']) ? $data['title'] : null;
if (isset($data['title'])) { return new self($label, $url, $title, $width);
$attributes['title'] = $data['title'];
}
} }
return new self($label, $attributes, $extent);
} }
/** @return string */ /** @return string */
@ -97,10 +96,16 @@ final class Link implements Inline
return $this->label; return $this->label;
} }
/** @return _Metadata */ /** @return string */
public function attributes() 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 new Handler(
/** @return Element|Text */ /** @return Element|Text */
function (State $State) use ($Parsedown) { 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()) { if ($State->getOrDefault(SafeMode::class)->enabled()) {
$attributes['href'] = Element::filterUnsafeUrl($attributes['href']); $attributes['href'] = Element::filterUnsafeUrl($attributes['href']);