Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
nojimage committed Apr 22, 2020
2 parents 7e01f16 + dc2b568 commit 8e03a5a
Show file tree
Hide file tree
Showing 18 changed files with 248 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ before_script:
script:
- if [ $DEFAULT -eq 1 ]; then vendor/bin/phpunit --exclude-group deprecated,tld --stderr; fi
- if [ $TLD_TEST -eq 1 ]; then vendor/bin/phpunit --group tld --stderr; fi
- if [ $PHPCS -eq 1 ]; then vendor/bin/phpcs -psn --extensions=php --standard=PSR2 ./lib ./tests; fi
- if [ $PHPCS -eq 1 ]; then vendor/bin/phpcs -psn --extensions=php --standard=PSR12 ./lib ./tests; fi
- if [ $CODECOVERAGE -eq 1 ]; then vendor/bin/phpunit --exclude-group deprecated,tld --stderr --coverage-clover=coverage.xml; fi

after_success:
Expand Down
2 changes: 1 addition & 1 deletion build/build-emoji-regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require dirname(__DIR__) . '/vendor/autoload.php';

$classFile = dirname(__DIR__) . '/lib/Twitter/Text/EmojiRegex.php';
$emojiDataUrl = 'https://www.unicode.org/Public/emoji/11.0/emoji-test.txt';
$emojiDataUrl = 'https://www.unicode.org/Public/emoji/12.1/emoji-test.txt';

// --
$emojiData = file($emojiDataUrl);
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
"type": "package",
"package": {
"name": "twitter/twitter-text",
"version": "3.0.0",
"version": "3.1.0",
"source": {
"url": "https://github.com/twitter/twitter-text.git",
"type": "git",
"reference": "v3.0.0"
"reference": "v3.1.0"
}
}
}
Expand Down
105 changes: 77 additions & 28 deletions lib/Twitter/Text/Autolink.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,11 @@ class Autolink
protected $url_base_cash = 'https://twitter.com/search?q=%24';

/**
* Whether to include the value 'nofollow' in the 'rel' attribute.
*
* @var bool
*/
protected $nofollow = true;

/**
* Whether to include the value 'external' in the 'rel' attribute.
* the 'rel' attribute values.
*
* Often this is used to be matched on in JavaScript for dynamically adding
* the 'target' attribute which is deprecated in HTML 4.01. In HTML 5 it has
* been undeprecated and thus the 'target' attribute can be used. If this is
* set to false then the 'target' attribute will be output.
*
* @var bool
* @var array
*/
protected $external = true;
protected $rel = array('external', 'nofollow');

/**
* The scope to open the link in.
Expand Down Expand Up @@ -209,6 +197,24 @@ public function __construct($tweet = null, $escape = true, $full_encode = false)
$this->extractor = Extractor::create();
}

/**
* Set CSS class to all link types.
*
* @param string $v CSS class for links.
*
* @return Autolink Fluid method chaining.
*/
public function setToAllLinkClasses($v)
{
$this->setURLClass($v);
$this->setUsernameClass($v);
$this->setListClass($v);
$this->setHashtagClass($v);
$this->setCashtagClass($v);

return $this;
}

/**
* CSS class for auto-linked URLs.
*
Expand Down Expand Up @@ -331,7 +337,7 @@ public function setCashtagClass($v)
*/
public function getNoFollow()
{
return $this->nofollow;
return in_array('nofollow', $this->rel, true);
}

/**
Expand All @@ -343,7 +349,15 @@ public function getNoFollow()
*/
public function setNoFollow($v)
{
$this->nofollow = $v;
if ($v && !$this->getNoFollow()) {
$this->setRel('nofollow', true);
}
if (!$v && $this->getNoFollow()) {
$this->rel = array_filter($this->rel, function ($r) {
return $r !== 'nofollow';
});
}

return $this;
}

Expand All @@ -359,7 +373,7 @@ public function setNoFollow($v)
*/
public function getExternal()
{
return $this->external;
return in_array('external', $this->rel, true);
}

/**
Expand All @@ -376,7 +390,15 @@ public function getExternal()
*/
public function setExternal($v)
{
$this->external = $v;
if ($v && !$this->getExternal()) {
$this->setRel('external', true);
}
if (!$v && $this->getExternal()) {
$this->rel = array_filter($this->rel, function ($r) {
return $r !== 'external';
});
}

return $this;
}

Expand Down Expand Up @@ -822,15 +844,9 @@ public function linkToCashtag($entity, $tweet = null)
*/
public function linkToText(array $entity, $text, $attributes = array())
{
$rel = array();
if ($this->external) {
$rel[] = 'external';
}
if ($this->nofollow) {
$rel[] = 'nofollow';
}
if (!empty($rel)) {
$attributes['rel'] = implode(' ', $rel);
$rel = $this->getRel();
if ($rel !== '') {
$attributes['rel'] = $rel;
}
if ($this->target) {
$attributes['target'] = $this->target;
Expand Down Expand Up @@ -872,6 +888,39 @@ protected function linkToTextWithSymbol(array $entity, $symbol, $linkText, array
return $this->linkToText($entity, $linkText, $attributes);
}

/**
* get rel attribute
*
* @return string
*/
public function getRel()
{
$rel = $this->rel;
$rel = array_unique($rel);

return implode(' ', $rel);
}

/**
* Set rel attribute.
*
* This method override setExternal/setNoFollow setting.
*
* @param string[]|string $rel the rel attribute
* @param bool $merge if true, merge rel attributes instead replace.
* @return $this
*/
public function setRel($rel, $merge = false)
{
if (is_string($rel)) {
$rel = explode(' ', $rel);
}

$this->rel = $merge ? array_unique(array_merge($this->rel, $rel)) : $rel;

return $this;
}

/**
* html escape
*
Expand Down
13 changes: 7 additions & 6 deletions lib/Twitter/Text/EmojiRegex.php

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions lib/Twitter/Text/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,10 @@ public function extractURLsWithIndices($tweet)
// If protocol is missing and domain contains non-ASCII characters,
// extract ASCII-only domains.
if (empty($protocol)) {
if (!$this->extractURLWithoutProtocol
|| preg_match(Regex::getInvalidUrlWithoutProtocolPrecedingCharsMatcher(), $before)) {
if (
!$this->extractURLWithoutProtocol
|| preg_match(Regex::getInvalidUrlWithoutProtocolPrecedingCharsMatcher(), $before)
) {
continue;
}

Expand All @@ -359,8 +361,10 @@ public function extractURLsWithIndices($tweet)

if (preg_match(Regex::getValidAsciiDomainMatcher(), $domain, $asciiDomain)) {
// check hostname length
if (isset($asciiDomain[1])
&& strlen(rtrim($asciiDomain[1], '.')) > static::MAX_ASCII_HOSTNAME_LENGTH) {
if (
isset($asciiDomain[1])
&& strlen(rtrim($asciiDomain[1], '.')) > static::MAX_ASCII_HOSTNAME_LENGTH
) {
continue;
}

Expand All @@ -374,9 +378,11 @@ public function extractURLsWithIndices($tweet)
$start_position + $ascii_end_position
),
);
if (!empty($path)
if (
!empty($path)
|| preg_match(Regex::getValidSpecialShortDomainMatcher(), $asciiDomain[0])
|| !preg_match(Regex::getInvalidCharactersMatcher(), $asciiDomain[0])) {
|| !preg_match(Regex::getInvalidCharactersMatcher(), $asciiDomain[0])
) {
$urls[] = $last_url;
}
}
Expand Down
24 changes: 16 additions & 8 deletions lib/Twitter/Text/ParseResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,25 @@ public function __get($name)
*/
public function __set($name, $value)
{
if ($name === 'displayRangeStart'
&& $this->lte($value, $this->displayTextRange[1], $name, 'displayRangeEnd')) {
if (
$name === 'displayRangeStart'
&& $this->lte($value, $this->displayTextRange[1], $name, 'displayRangeEnd')
) {
$this->displayTextRange[0] = (int)$value;
} elseif ($name === 'displayRangeEnd'
&& $this->gte($value, $this->displayTextRange[0], $name, 'displayRangeStart')) {
} elseif (
$name === 'displayRangeEnd'
&& $this->gte($value, $this->displayTextRange[0], $name, 'displayRangeStart')
) {
$this->displayTextRange[1] = (int)$value;
} elseif ($name === 'validRangeStart'
&& $this->lte($value, $this->validTextRange[1], $name, 'validRangeEnd')) {
} elseif (
$name === 'validRangeStart'
&& $this->lte($value, $this->validTextRange[1], $name, 'validRangeEnd')
) {
$this->validTextRange[0] = (int)$value;
} elseif ($name === 'validRangeEnd'
&& $this->gte($value, $this->validTextRange[0], $name, 'validRangeStart')) {
} elseif (
$name === 'validRangeEnd'
&& $this->gte($value, $this->validTextRange[0], $name, 'validRangeStart')
) {
$this->validTextRange[1] = (int)$value;
} elseif ($name === 'valid') {
$this->result[$name] = (bool)$value;
Expand Down
4 changes: 2 additions & 2 deletions lib/Twitter/Text/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(Configuration $config = null)
public function parseTweet($tweet)
{
if ($tweet === null || '' === $tweet) {
return new ParseResults;
return new ParseResults();
}

$normalizedTweet = StringUtils::normalizeFromNFC($tweet);
Expand Down Expand Up @@ -96,7 +96,7 @@ public function parseTweet($tweet)
$emojiLength = StringUtils::strlen($emoji);
$charCount = StringUtils::charCount($emoji);

$weightedCount += $this->getCharacterWeight(StringUtils::substr($emoji, 0, 1), $this->config);
$weightedCount += $this->config->defaultWeight;
$offset += $emojiLength;
$displayOffset += $charCount;
if ($weightedCount <= $maxWeightedTweetLength) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Twitter/Text/TldLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ final public static function getValidGTLD()
}

$gTLD = implode('|', static::$gTLDs);
$regex = '(?:(?:' . $gTLD . ')(?=[^0-9a-z@]|$))';
$regex = '(?:(?:' . $gTLD . ')(?=[^0-9a-z@+-]|$))';

return $regex;
}
Expand All @@ -1639,7 +1639,7 @@ final public static function getValidCcTLD()
}

$ccTLD = implode('|', static::$ccTLDs);
$regex = '(?:(?:' . $ccTLD . ')(?=[^0-9a-z@]|$))';
$regex = '(?:(?:' . $ccTLD . ')(?=[^0-9a-z@+-]|$))';

return $regex;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/Twitter/Text/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,15 @@ public function isValidURL($url, $unicode_domains = true, $require_protocol = tr
list($scheme, $authority, $path, $query, $fragment) = array_pad($matches, 5, '');

# Check scheme, path, query, fragment:
if (($require_protocol && !(
if (
($require_protocol && !(
self::isValidMatch($scheme, Regex::getValidateUrlSchemeMatcher())
&& preg_match('/^https?$/i', $scheme)
))
|| !self::isValidMatch($path, Regex::getValidateUrlPathMatcher())
|| !self::isValidMatch($query, Regex::getValidateUrlQueryMatcher(), true)
|| !self::isValidMatch($fragment, Regex::getValidateUrlFragmentMatcher(), true)) {
|| !self::isValidMatch($fragment, Regex::getValidateUrlFragmentMatcher(), true)
) {
return false;
}

Expand Down
Loading

0 comments on commit 8e03a5a

Please sign in to comment.