diff --git a/docs/Internals/API_changes.md b/docs/Internals/API_changes.md index c99864acc8..8636bb2f54 100644 --- a/docs/Internals/API_changes.md +++ b/docs/Internals/API_changes.md @@ -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: diff --git a/src/Configurator/Helpers/FilterSyntaxMatcher.php b/src/Configurator/Helpers/FilterSyntaxMatcher.php index 4ed3084425..720dbdccc0 100644 --- a/src/Configurator/Helpers/FilterSyntaxMatcher.php +++ b/src/Configurator/Helpers/FilterSyntaxMatcher.php @@ -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); } /** diff --git a/src/Configurator/Items/Regexp.php b/src/Configurator/Items/Regexp.php index 47393e2ede..22cceaefd3 100644 --- a/src/Configurator/Items/Regexp.php +++ b/src/Configurator/Items/Regexp.php @@ -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" */ @@ -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; } /** @@ -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; diff --git a/src/Configurator/JavaScript/RegexpConvertor.php b/src/Configurator/JavaScript/RegexpConvertor.php index c9677ee60d..289ef454ae 100644 --- a/src/Configurator/JavaScript/RegexpConvertor.php +++ b/src/Configurator/JavaScript/RegexpConvertor.php @@ -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); @@ -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; } /** diff --git a/src/Plugins/Emoji/Configurator.php b/src/Plugins/Emoji/Configurator.php index d5f0c5e652..56b730a469 100644 --- a/src/Plugins/Emoji/Configurator.php +++ b/src/Plugins/Emoji/Configurator.php @@ -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) diff --git a/src/Plugins/Keywords/Configurator.php b/src/Plugins/Keywords/Configurator.php index da5b53180d..5db38e6b86 100644 --- a/src/Plugins/Keywords/Configurator.php +++ b/src/Plugins/Keywords/Configurator.php @@ -139,7 +139,7 @@ public function asConfig() $regexp .= 'u'; } - $config['regexps'][] = new Regexp($regexp, true); + $config['regexps'][] = new Regexp($regexp); } return $config; diff --git a/src/Plugins/Preg/Configurator.php b/src/Plugins/Preg/Configurator.php index a933cd74b1..834b607553 100644 --- a/src/Plugins/Preg/Configurator.php +++ b/src/Plugins/Preg/Configurator.php @@ -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]; diff --git a/tests/Configurator/Helpers/FilterSyntaxMatcherTest.php b/tests/Configurator/Helpers/FilterSyntaxMatcherTest.php index 9876907f76..ce07be410b 100644 --- a/tests/Configurator/Helpers/FilterSyntaxMatcherTest.php +++ b/tests/Configurator/Helpers/FilterSyntaxMatcherTest.php @@ -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' ] ] ] ], @@ -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' ] ] ] ], diff --git a/tests/Configurator/Items/RegexpTest.php b/tests/Configurator/Items/RegexpTest.php index 84e5dc3ed8..679bdc52b5 100644 --- a/tests/Configurator/Items/RegexpTest.php +++ b/tests/Configurator/Items/RegexpTest.php @@ -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()); } @@ -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'), diff --git a/tests/Plugins/Emoticons/ConfiguratorTest.php b/tests/Plugins/Emoticons/ConfiguratorTest.php index 6cb6ee34aa..ae8d3747f4 100644 --- a/tests/Plugins/Emoticons/ConfiguratorTest.php +++ b/tests/Plugins/Emoticons/ConfiguratorTest.php @@ -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']); } /**