Fixes #8671: Extracted yii\helpers\Html::escapeJsRegularExpression() method from yii\validators\RegularExpressionValidator

This commit is contained in:
Dmitry Naumenko
2015-07-27 17:39:36 +03:00
committed by Alexander Makarov
parent aba7effa2b
commit b16d734c29
3 changed files with 28 additions and 13 deletions

View File

@ -47,6 +47,7 @@ Yii Framework 2 Change Log
- Enh #8574: Added `yii\console\controllers\MessageController` support .pot file creation (pgaultier)
- Enh #8625: Added `markUnused` option to `yii\console\controllers\MessageController` (marius7383)
- Enh #8670: Added support for saving extra fields in session table for `yii\web\DbSession` (klimov-paul)
- Enh #8671: Extracted `yii\helpers\Html::escapeJsRegularExpression()` method from `yii\validators\RegularExpressionValidator` (silverfire, klimov-paul, samdark, qiangxue)
- Enh #8903: PostgreSQL `QueryBuilder::createIndex()` can now specify the index method to use (LAV45)
- Enh #9011: Allow `yii\widgets\MaskedInput` to produce an input tag of a custom type (TriAnMan)
- Enh #9038: Write warning to log in case `FileCache` fails to write into file (foccy)

View File

@ -2095,4 +2095,29 @@ class BaseHtml
$name = strtolower(static::getInputName($model, $attribute));
return str_replace(['[]', '][', '[', ']', ' ', '.'], ['', '-', '-', '', '-', '-'], $name);
}
/**
* Escapes regular expression to use in JavaScript
* @param string $regexp
* @return string
*
* @since 2.0.6
*/
public static function escapeJsRegularExpression($regexp)
{
$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $regexp);
$deliminator = substr($pattern, 0, 1);
$pos = strrpos($pattern, $deliminator, 1);
$flag = substr($pattern, $pos + 1);
if ($deliminator !== '/') {
$pattern = '/' . str_replace('/', '\\/', substr($pattern, 1, $pos - 1)) . '/';
} else {
$pattern = substr($pattern, 0, $pos + 1);
}
if (!empty($flag)) {
$pattern .= preg_replace('/[^igm]/', '', $flag);
}
return new $pattern;
}
}

View File

@ -9,6 +9,7 @@ namespace yii\validators;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\web\JsExpression;
use yii\helpers\Json;
@ -64,19 +65,7 @@ class RegularExpressionValidator extends Validator
*/
public function clientValidateAttribute($model, $attribute, $view)
{
$pattern = $this->pattern;
$pattern = preg_replace('/\\\\x\{?([0-9a-fA-F]+)\}?/', '\u$1', $pattern);
$deliminator = substr($pattern, 0, 1);
$pos = strrpos($pattern, $deliminator, 1);
$flag = substr($pattern, $pos + 1);
if ($deliminator !== '/') {
$pattern = '/' . str_replace('/', '\\/', substr($pattern, 1, $pos - 1)) . '/';
} else {
$pattern = substr($pattern, 0, $pos + 1);
}
if (!empty($flag)) {
$pattern .= preg_replace('/[^igm]/', '', $flag);
}
$pattern = Html::escapeJsRegularExpression($this->pattern);
$options = [
'pattern' => new JsExpression($pattern),