Skip to content

Commit

Permalink
Merge pull request #142 from levimykel/target-blank
Browse files Browse the repository at this point in the history
Add support for target blank
  • Loading branch information
srenault authored Oct 17, 2017
2 parents a1914b2 + 25575ed commit 8606405
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Prismic/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function getLastPublicationDate()
*/
public function asDocumentLink()
{
return new DocumentLink($this->id, $this->uid, $this->type, $this->tags, $this->getSlug(), $this->lang, $this->getFragments(), false);
return new DocumentLink($this->id, $this->uid, $this->type, $this->tags, $this->getSlug(), $this->lang, $this->getFragments(), false, null);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion src/Prismic/Fragment/ImageView.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,16 @@ public function asHtml($linkResolver = null, $attributes = array())

if ($this->getLink() && ($url = $this->getLink()->getUrl($linkResolver)) !== null) {
$a = $doc->createElement('a');
$a->setAttribute('href', $url);
$attributes = array( 'href' => $url );
if ($this->getLink()->getTarget()) {
$attributes = array_merge(array(
'target' => $this->getLink()->getTarget(),
'rel' => 'noopener',
), $attributes);
}
foreach ($attributes as $attr => $value) {
$a->setAttribute($attr, $value);
};
$a->appendChild($img);
$doc->appendChild($a);
} else {
Expand Down
23 changes: 21 additions & 2 deletions src/Prismic/Fragment/Link/DocumentLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class DocumentLink extends WithFragments implements LinkInterface
* @var boolean returns true if the link is towards a document that is not live, for instance
*/
private $isBroken;
/**
* @var string the target of the link
*/
private $target;

/**
* Constructs a document link.
Expand All @@ -60,8 +64,9 @@ class DocumentLink extends WithFragments implements LinkInterface
* @param string $lang the language code of the document
* @param array $fragments the additional fragment data
* @param boolean $isBroken returns true if the link is towards a document that is not live, for instance
* @param string $target the target of the link
*/
public function __construct($id, $uid, $type, $tags, $slug, $lang, array $fragments, $isBroken)
public function __construct($id, $uid, $type, $tags, $slug, $lang, array $fragments, $isBroken, $target = null)
{
parent::__construct($fragments);
$this->id = $id;
Expand All @@ -71,6 +76,7 @@ public function __construct($id, $uid, $type, $tags, $slug, $lang, array $fragme
$this->slug = $slug;
$this->lang = $lang;
$this->isBroken = $isBroken;
$this->target = $target;
}

/**
Expand Down Expand Up @@ -103,6 +109,7 @@ public static function parse($json)
$uid = isset($json->document->uid) ? $json->document->uid : null;
$lang = isset($json->document->lang) ? $json->document->lang : null;
$fragments = isset($json->document->data) ? WithFragments::parseFragments($json->document->data) : array();
$target = property_exists($json, "target") ? $json->target : null;
return new DocumentLink(
$json->document->id,
$uid,
Expand All @@ -111,7 +118,8 @@ public static function parse($json)
$json->document->slug,
$lang,
$fragments,
$json->isBroken
$json->isBroken,
$target
);
}

Expand Down Expand Up @@ -216,6 +224,17 @@ public function getLang()
return $this->lang;
}

/**
* Returns the target of the link
*
*
* @return string the target of the link
*/
public function getTarget()
{
return $this->target;
}

/**
* Checks if the link is broken (towards a document that is not live, for instance)
*
Expand Down
24 changes: 22 additions & 2 deletions src/Prismic/Fragment/Link/FileLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class FileLink implements LinkInterface
* @var string the resource's original filename, in bytes
*/
private $filename;
/**
* @var string the target of the link
*/
private $target;

/**
* Constructs a file link.
Expand All @@ -43,13 +47,15 @@ class FileLink implements LinkInterface
* @param string $kind the kind of resource it is (document, image, ...)
* @param string $size the size of the resource, in bytes
* @param string $filename the resource's original filename, in bytes
* @param string $target the target of the link
*/
public function __construct($url, $kind, $size, $filename)
public function __construct($url, $kind, $size, $filename, $target)
{
$this->url = $url;
$this->kind = $kind;
$this->size = $size;
$this->filename = $filename;
$this->target = $target;
}

/**
Expand Down Expand Up @@ -129,6 +135,18 @@ public function getFilename()
return $this->filename;
}

/**
* Returns the target of the link
*
*
*
* @return string the target of the link
*/
public function getTarget()
{
return $this->target;
}

/**
* Parses a proper bit of unmarshaled JSON into a FileLink object.
* This is used internally during the unmarshaling of API calls.
Expand All @@ -139,11 +157,13 @@ public function getFilename()
*/
public static function parse($json)
{
$target = property_exists($json, "target") ? $json->target : null;
return new FileLink(
$json->file->url,
$json->file->kind,
$json->file->size,
$json->file->name
$json->file->name,
$target
);
}
}
7 changes: 5 additions & 2 deletions src/Prismic/Fragment/Link/ImageLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ class ImageLink extends FileLink
* @param string $kind the kind of resource it is (document, image, ...)
* @param string $size the size of the resource, in bytes
* @param string $filename the resource's original filename, in bytes
* @param string $target the target of the link
* @param int $height the height of the image
* @param int $width the width of the image
*/
public function __construct($url, $kind, $size, $filename, $height, $width)
public function __construct($url, $kind, $size, $filename, $target, $height, $width)
{
parent::__construct($url, $kind, $size, $filename);
parent::__construct($url, $kind, $size, $filename, $target);
$this->height = $height;
$this->width = $width;
}
Expand Down Expand Up @@ -79,11 +80,13 @@ public function getWidth()
*/
public static function parse($json)
{
$target = property_exists($json, "target") ? $json->target : null;
return new ImageLink(
$json->image->url,
$json->image->kind,
$json->image->size,
$json->image->name,
$target,
$json->image->height,
$json->image->width
);
Expand Down
26 changes: 23 additions & 3 deletions src/Prismic/Fragment/Link/WebLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class WebLink implements LinkInterface
* @var string the URL of the resource we're linking to online
*/
private $url;
/**
* @var string the target, if known
*/
private $target;
/**
* @var string the content type, if known
*/
Expand All @@ -32,11 +36,13 @@ class WebLink implements LinkInterface
* Constructs a media link.
*
* @param string $url the URL of the resource we're linking to online
* @param string $target the target, if known
* @param string $maybeContentType the content type, if known
*/
public function __construct($url, $maybeContentType = null)
public function __construct($url, $target = null, $maybeContentType = null)
{
$this->url = $url;
$this->target = $target;
$this->maybeContentType = $maybeContentType;
}

Expand All @@ -52,7 +58,8 @@ public function __construct($url, $maybeContentType = null)
*/
public function asHtml($linkResolver = null)
{
return '<a href="' . $this->url . '">' . $this->url . '</a>';
$target = $this->target ? ' target="' . $this->target . '" rel="noopener"' : '';
return '<a href="' . $this->url . '"' . $target . '>' . $this->url . '</a>';
}

/**
Expand Down Expand Up @@ -81,6 +88,18 @@ public function getUrl($linkResolver = null)
return $this->url;
}

/**
* Returns the target, if known
*
*
*
* @return string the target, if known
*/
public function getTarget()
{
return $this->target;
}

/**
* Returns the content type, if known
*
Expand All @@ -103,6 +122,7 @@ public function getContentType()
*/
public static function parse($json)
{
return new WebLink($json->url);
$target = property_exists($json, "target") ? $json->target : null;
return new WebLink($json->url, $target);
}
}
20 changes: 19 additions & 1 deletion src/Prismic/Fragment/Span/HyperlinkSpan.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class HyperlinkSpan implements SpanInterface
* @var string the label (optional, may be null)
*/
private $label;
/**
* @var string the target (optional, may be null)
*/
private $target;

/**
* Constructs a link span
Expand All @@ -45,13 +49,15 @@ class HyperlinkSpan implements SpanInterface
* @param int $end the end of the span
* @param LinkInterface $link the link to point to
* @param string $label can be null
* @param string $target can be null
*/
public function __construct($start, $end, $link, $label = NULL)
public function __construct($start, $end, $link, $label = NULL, $target = NULL)
{
$this->start = $start;
$this->end = $end;
$this->link = $link;
$this->label = $label;
$this->target = $target;
}

/**
Expand Down Expand Up @@ -101,4 +107,16 @@ public function getLabel()
{
return $this->label;
}

/**
* Returns the target
*
*
*
* @return string the target
*/
public function getTarget()
{
return $this->target;
}
}
9 changes: 8 additions & 1 deletion src/Prismic/Fragment/StructuredText.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ private static function serialize($element, $content, $linkResolver, $htmlSerial
$nodeName = 'em';
} elseif ($element instanceof HyperlinkSpan) {
$nodeName = 'a';
if ($element->getTarget()) {
$attributes = array_merge(array(
'target' => $element->getTarget(),
'rel' => 'noopener',
), $attributes);
}
if ($element->getLink() instanceof DocumentLink) {
$attributes['href'] = $linkResolver ? $linkResolver($element->getLink()) : '';
} else {
Expand Down Expand Up @@ -460,7 +466,8 @@ public static function parseSpan($json)
}

if ("hyperlink" == $type && ($link = self::extractLink($json->data))) {
return new HyperlinkSpan($start, $end, $link, $label);
$target = $link->getTarget() ? $link->getTarget() : NULL;
return new HyperlinkSpan($start, $end, $link, $label, $target);
}

if ("label" == $type) {
Expand Down
3 changes: 2 additions & 1 deletion src/Prismic/LinkResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ private function asLink($document)
$document->getSlug(),
$document->getLang(),
$document->getFragments(),
false
false,
null
);
}
}
7 changes: 7 additions & 0 deletions tests/Prismic/FragmentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public function testWebLinkAsHtml()
$this->assertEquals('<a href="http://prismic.io">http://prismic.io</a>', $link->asHtml());
}

public function testWebLinkAsHtmlTargetBlank()
{
$weblinks = json_decode(file_get_contents(__DIR__.'/../fixtures/weblinks_targetblank.json'));
$document = Document::parse($weblinks);
$this->assertEquals($document->get('product.link')->asHtml(), '<a href="https://prismic.io" target="_blank" rel="noopener">https://prismic.io</a>');
}

public function testSlices()
{
$response = json_decode(file_get_contents(__DIR__.'/../fixtures/slices.json'));
Expand Down
14 changes: 14 additions & 0 deletions tests/Prismic/StructuredTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ public function testDocumentLinkWithLambdaRendering()
$this->assertEquals($content, $structuredText->asHtml($linkResolver));
}

public function testWebLinkRenderingTargetBlank()
{
$weblinks = json_decode(file_get_contents(__DIR__.'/../fixtures/weblinks_targetblank.json'));
$document = Document::parse($weblinks);
$this->assertEquals($document->getStructuredText('product.description')->asHtml(), '<p>Link that <a target="_blank" rel="noopener" href="https://prismic.io">opens in a new tab</a>.</p>');
}

public function testImageRenderingTargetBlank()
{
$weblinks = json_decode(file_get_contents(__DIR__.'/../fixtures/weblinks_targetblank.json'));
$document = Document::parse($weblinks);
$this->assertEquals($document->getStructuredText('product.image_with_link')->asHtml(), '<p class="block-img"><a target="_blank" rel="noopener" href="https://prismic.io"><img src="https://prismic-io.s3.amazonaws.com/serializer-example/aa4f6471fd8b920e92914146a4688154a2d727d6_daria-nepriakhina-1277.jpg" alt="" width="500" height="200"></a></p>');
}

public function testStructuredTextHtmlHasBreakTags()
{
$this->assertRegExp('`can be rough sometimes\.\s*<br\s*/?>\s*This is after a new line\.`s', $this->structuredText->asHtml());
Expand Down
Loading

0 comments on commit 8606405

Please sign in to comment.