Skip to content

Commit

Permalink
add symbolTag/textWithSymboleTag options to Autolink class
Browse files Browse the repository at this point in the history
  • Loading branch information
nojimage committed Jan 28, 2019
1 parent 41710de commit eb7bf4f
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
69 changes: 69 additions & 0 deletions lib/Twitter/Text/Autolink.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ class Autolink
*/
protected $usernameIncludeSymbol = false;

/**
* HTML tag to be applied around #/@/# symbols in hashtags/usernames/lists/cashtag
*
* @var string
* @since 3.0.1
*/
protected $symbolTag = '';

/**
* HTML tag to be applied around text part of hashtags/usernames/lists/cashtag
*
* @var string
* @since 3.0.1
*/
protected $textWithSymbolTag = '';

/**
*
* @var Extractor
Expand Down Expand Up @@ -418,6 +434,52 @@ public function setUsernameIncludeSymbol($usernameIncludeSymbol)
return $this;
}

/**
* @return string
* @since 3.0.1
*/
public function getSymbolTag()
{
return $this->symbolTag;
}

/**
* Set HTML tag to be applied around #/@/# symbols in hashtags/usernames/lists/cashtag
*
* @param string $symbolTag HTML tag without bracket. e.g., 'b' or 's'
* @return Autolink
* @since 3.0.1
*/
public function setSymbolTag($symbolTag)
{
$this->symbolTag = $symbolTag;

return $this;
}

/**
* @return string
* @since 3.0.1
*/
public function getTextWithSymbolTag()
{
return $this->textWithSymbolTag;
}

/**
* Set HTML tag to be applied around text part of hashtags/usernames/lists/cashtag
*
* @param string $textWithSymbolTag HTML tag without bracket. e.g., 'b' or 's'
* @return Autolink
* @since 3.0.1
*/
public function setTextWithSymbolTag($textWithSymbolTag)
{
$this->textWithSymbolTag = $textWithSymbolTag;

return $this;
}

/**
* Autolink with entities
*
Expand Down Expand Up @@ -794,6 +856,13 @@ protected function linkToTextWithSymbol(array $entity, $symbol, $linkText, array
{
$includeSymbol = $this->usernameIncludeSymbol || !preg_match('/[@@]/u', $symbol);

if (!empty($this->symbolTag)) {
$symbol = sprintf('<%1$s>%2$s</%1$s>', $this->symbolTag, $symbol);
}
if (!empty($this->textWithSymbolTag)) {
$linkText = sprintf('<%1$s>%2$s</%1$s>', $this->textWithSymbolTag, $linkText);
}

if (!$includeSymbol) {
return $symbol . $this->linkToText($entity, $linkText, $attributes);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/AutolinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,26 @@ public function testUsernameIncludeSymbol()
$linkedText = $this->linker->autoLink($tweet);
$this->assertSame($expected, $linkedText);
}

public function testSymbolTag()
{
$this->linker
->setExternal(false)
->setTarget(false)
->setNoFollow(false)
->setSymbolTag('s')
->setTextWithSymbolTag('b');

$tweet = '#hash';
$expected = '<a href="https://twitter.com/search?q=%23hash" title="#hash" class="tweet-url hashtag"><s>#</s><b>hash</b></a>';
$this->assertSame($expected, $this->linker->autoLink($tweet));

$tweet = '@mention';
$expected = '<s>@</s><a class="tweet-url username" href="https://twitter.com/mention"><b>mention</b></a>';
$this->assertSame($expected, $this->linker->autoLink($tweet));

$this->linker->setUsernameIncludeSymbol(true);
$expected = '<a class="tweet-url username" href="https://twitter.com/mention"><s>@</s><b>mention</b></a>';
$this->assertSame($expected, $this->linker->autoLink($tweet));
}
}

0 comments on commit eb7bf4f

Please sign in to comment.