Skip to content

Commit

Permalink
Regexp: removed global flag from constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Jul 21, 2019
1 parent 27f7090 commit 15588bd
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 34 deletions.
5 changes: 5 additions & 0 deletions docs/Internals/API_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

See also [general changes](Changes.md).

## 3.0.0

The second argument of `s9e\TextFormatter\Configurator\Items\Regexp` constructor has been removed. All JavaScript regexps have their global flag set. This can be overridden if necessary by manually setting a JavaScript representation via `setJS()`.


## 2.0.0

The following elements have been removed:
Expand Down
2 changes: 1 addition & 1 deletion src/Configurator/Helpers/FilterSyntaxMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function parseRegexp(string $regexp, string $flags): Regexp
{
$regexp .= str_replace('g', '', $flags);

return new Regexp($regexp, true);
return new Regexp($regexp);
}

/**
Expand Down
14 changes: 4 additions & 10 deletions src/Configurator/Items/Regexp.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@

class Regexp implements ConfigProvider, FilterableConfigValue
{
/**
* @var bool Whether this regexp should have the global flag set in JavaScript
*/
protected $isGlobal;

/**
* @var string JavaScript regexp, with delimiters and modifiers, e.g. "/foo/i"
*/
Expand All @@ -34,17 +29,16 @@ class Regexp implements ConfigProvider, FilterableConfigValue
/**
* Constructor
*
* @param string $regexp PCRE regexp, with delimiters and modifiers, e.g. "/foo/i"
* @param string $regexp PCRE regexp, with delimiters and modifiers, e.g. "/foo/i"
*/
public function __construct($regexp, $isGlobal = false)
public function __construct($regexp)
{
if (@preg_match($regexp, '') === false)
{
throw new InvalidArgumentException('Invalid regular expression ' . var_export($regexp, true));
}

$this->regexp = $regexp;
$this->isGlobal = $isGlobal;
$this->regexp = $regexp;
}

/**
Expand Down Expand Up @@ -92,7 +86,7 @@ public function getJS()
{
if (!isset($this->jsRegexp))
{
$this->jsRegexp = RegexpConvertor::toJS($this->regexp, $this->isGlobal);
$this->jsRegexp = RegexpConvertor::toJS($this->regexp);
}

return $this->jsRegexp;
Expand Down
13 changes: 4 additions & 9 deletions src/Configurator/JavaScript/RegexpConvertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ abstract class RegexpConvertor
/**
* Convert a PCRE regexp to a JavaScript regexp
*
* @param string $regexp PCRE regexp
* @param bool $isGlobal Whether the global flag should be set
* @return Code JavaScript regexp
* @param string $regexp PCRE regexp
* @return Code JavaScript regexp
*/
public static function toJS($regexp, $isGlobal = false)
public static function toJS($regexp)
{
$regexpInfo = RegexpParser::parse($regexp);
$dotAll = (strpos($regexpInfo['modifiers'], 's') !== false);
Expand Down Expand Up @@ -104,17 +103,13 @@ public static function toJS($regexp, $isGlobal = false)
}

$modifiers = preg_replace('#[^im]#', '', $regexpInfo['modifiers']);
if ($isGlobal)
{
$modifiers .= 'g';
}

if ($regexp === '')
{
$regexp = '(?:)';
}

return '/' . self::escapeLineTerminators($regexp) . '/' . $modifiers;
return '/' . self::escapeLineTerminators($regexp) . '/g' . $modifiers;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/Emoji/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected function getAliasesConfig()
if (!empty($custom))
{
$regexp = '/' . RegexpBuilder::fromList($custom) . '/';
$config['customRegexp'] = new Regexp($regexp, true);
$config['customRegexp'] = new Regexp($regexp);

$quickMatch = ConfigHelper::generateQuickMatchFromList($custom);
if ($quickMatch !== false)
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/Keywords/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function asConfig()
$regexp .= 'u';
}

$config['regexps'][] = new Regexp($regexp, true);
$config['regexps'][] = new Regexp($regexp);
}

return $config;
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/Preg/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function asConfig()
foreach ($this->collection as list($tagName, $regexp, $passthroughIdx))
{
$captures = RegexpParser::getCaptureNames($regexp);
$pregs[] = [$tagName, new Regexp($regexp, true), $passthroughIdx, $captures];
$pregs[] = [$tagName, new Regexp($regexp), $passthroughIdx, $captures];
}

return ['generics' => $pregs];
Expand Down
12 changes: 6 additions & 6 deletions tests/Configurator/Helpers/FilterSyntaxMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ public function getParseTests()
[
'filter' => 'preg_replace',
'params' => [
['Value', new Regexp('/foo/', true)],
['Value', '' ],
['Name', 'attrValue' ]
['Value', new Regexp('/foo/')],
['Value', '' ],
['Name', 'attrValue' ]
]
]
],
Expand All @@ -176,9 +176,9 @@ public function getParseTests()
[
'filter' => 'preg_replace',
'params' => [
['Value', new Regexp('/foo/is', true)],
['Value', '' ],
['Name', 'attrValue' ]
['Value', new Regexp('/foo/is')],
['Value', '' ],
['Name', 'attrValue' ]
]
]
],
Expand Down
6 changes: 3 additions & 3 deletions tests/Configurator/Items/RegexpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public function testAsConfigReturnsItself()
}

/**
* @testdox The JS regexp has a global flag if isGlobal is true
* @testdox The JS regexp has a global flag
*/
public function testJSGlobal()
{
$regexp = new Regexp('/x/', true);
$regexp = new Regexp('/x/');
$this->assertEquals('/x/g', $regexp->getJS());
}

Expand Down Expand Up @@ -157,7 +157,7 @@ public function testFilterConfigPHP()
*/
public function testFilterConfigJS()
{
$regexp = new Regexp('/foo/', true);
$regexp = new Regexp('/foo/');

$this->assertEquals(
new Code('/foo/g'),
Expand Down
4 changes: 2 additions & 2 deletions tests/Plugins/Emoticons/ConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ public function testNotAfterJavaScript()

$config = ConfigHelper::filterConfig($plugin->asConfig(), 'JS');

$this->assertEquals(new Code('/x/g', 'g'), $config['regexp']);
$this->assertEquals(new Code('/\\w/'), $config['notAfter']);
$this->assertEquals(new Code('/x/g'), $config['regexp']);
$this->assertEquals(new Code('/\\w/'), $config['notAfter']);
}

/**
Expand Down

0 comments on commit 15588bd

Please sign in to comment.