Skip to content

Commit 044edad

Browse files
committed
[Validator] Fixed Regex::getHtmlPattern() to work with complex and negated patterns
1 parent 8213195 commit 044edad

File tree

3 files changed

+118
-96
lines changed

3 files changed

+118
-96
lines changed

Constraints/Regex.php

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,6 @@ public function getRequiredOptions()
4444
return array('pattern');
4545
}
4646

47-
/**
48-
* Returns htmlPattern if exists or pattern is convertible.
49-
*
50-
* @return string|null
51-
*/
52-
public function getHtmlPattern()
53-
{
54-
// If htmlPattern is specified, use it
55-
if (null !== $this->htmlPattern) {
56-
return empty($this->htmlPattern)
57-
? null
58-
: $this->htmlPattern;
59-
}
60-
61-
return $this->getNonDelimitedPattern();
62-
}
63-
6447
/**
6548
* Converts the htmlPattern to a suitable format for HTML5 pattern.
6649
* Example: /^[a-z]+$/ would be converted to [a-z]+
@@ -69,29 +52,47 @@ public function getHtmlPattern()
6952
* Pattern is also ignored if match=false since the pattern should
7053
* then be reversed before application.
7154
*
72-
* @todo reverse pattern in case match=false as per issue #5307
73-
*
7455
* @link http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute
7556
*
7657
* @return string|null
7758
*/
78-
private function getNonDelimitedPattern()
59+
public function getHtmlPattern()
7960
{
80-
// If match = false, pattern should not be added to HTML5 validation
81-
if (!$this->match) {
61+
// If htmlPattern is specified, use it
62+
if (null !== $this->htmlPattern) {
63+
return empty($this->htmlPattern)
64+
? null
65+
: $this->htmlPattern;
66+
}
67+
68+
// Quit if delimiters not at very beginning/end (e.g. when options are passed)
69+
if ($this->pattern[0] !== $this->pattern[strlen($this->pattern) - 1]) {
8270
return;
8371
}
8472

85-
if (preg_match('/^(.)(\^?)(.*?)(\$?)\1$/', $this->pattern, $matches)) {
86-
$delimiter = $matches[1];
87-
$start = empty($matches[2]) ? '.*' : '';
88-
$pattern = $matches[3];
89-
$end = empty($matches[4]) ? '.*' : '';
73+
$delimiter = $this->pattern[0];
74+
75+
// Unescape the delimiter
76+
$pattern = str_replace('\\'.$delimiter, $delimiter, substr($this->pattern, 1, -1));
9077

91-
// Unescape the delimiter in pattern
92-
$pattern = str_replace('\\'.$delimiter, $delimiter, $pattern);
78+
// If the pattern is inverted, we can simply wrap it in
79+
// ((?!pattern).)*
80+
if (!$this->match) {
81+
return '((?!'.$pattern.').)*';
82+
}
9383

94-
return $start.$pattern.$end;
84+
// If the pattern contains an or statement, wrap the pattern in
85+
// .*(pattern).* and quit. Otherwise we'd need to parse the pattern
86+
if (false !== strpos($pattern, '|')) {
87+
return '.*('.$pattern.').*';
9588
}
89+
90+
// Trim leading ^, otherwise prepend .*
91+
$pattern = '^' === $pattern[0] ? substr($pattern, 1) : '.*'.$pattern;
92+
93+
// Trim trailing $, otherwise append .*
94+
$pattern = '$' === $pattern[strlen($pattern) - 1] ? substr($pattern, 0, -1) : $pattern.'.*';
95+
96+
return $pattern;
9697
}
9798
}

Tests/Constraints/RegexTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Constraints;
13+
14+
use Symfony\Component\Validator\Constraints\Regex;
15+
16+
/**
17+
* @author Bernhard Schussek <[email protected]>
18+
*/
19+
class RegexTest extends \PHPUnit_Framework_TestCase
20+
{
21+
public function testConstraintGetDefaultOption()
22+
{
23+
$constraint = new Regex('/^[0-9]+$/');
24+
25+
$this->assertSame('/^[0-9]+$/', $constraint->pattern);
26+
}
27+
28+
public function provideHtmlPatterns()
29+
{
30+
return array(
31+
// HTML5 wraps the pattern in ^(?:pattern)$
32+
array('/^[0-9]+$/', '[0-9]+'),
33+
array('/[0-9]+$/', '.*[0-9]+'),
34+
array('/^[0-9]+/', '[0-9]+.*'),
35+
array('/[0-9]+/', '.*[0-9]+.*'),
36+
// We need a smart way to allow matching of patterns that contain
37+
// ^ and $ at various sub-clauses of an or-clause
38+
// .*(pattern).* seems to work correctly
39+
array('/[0-9]$|[a-z]+/', '.*([0-9]$|[a-z]+).*'),
40+
array('/[0-9]$|^[a-z]+/', '.*([0-9]$|^[a-z]+).*'),
41+
array('/^[0-9]|[a-z]+$/', '.*(^[0-9]|[a-z]+$).*'),
42+
// Unescape escaped delimiters
43+
array('/^[0-9]+\/$/', '[0-9]+/'),
44+
array('#^[0-9]+\#$#', '[0-9]+#'),
45+
// Cannot be converted
46+
array('/^[0-9]+$/i', null),
47+
48+
// Inverse matches are simple, just wrap in
49+
// ((?!pattern).)*
50+
array('/^[0-9]+$/', '((?!^[0-9]+$).)*', false),
51+
array('/[0-9]+$/', '((?![0-9]+$).)*', false),
52+
array('/^[0-9]+/', '((?!^[0-9]+).)*', false),
53+
array('/[0-9]+/', '((?![0-9]+).)*', false),
54+
array('/[0-9]$|[a-z]+/', '((?![0-9]$|[a-z]+).)*', false),
55+
array('/[0-9]$|^[a-z]+/', '((?![0-9]$|^[a-z]+).)*', false),
56+
array('/^[0-9]|[a-z]+$/', '((?!^[0-9]|[a-z]+$).)*', false),
57+
array('/^[0-9]+\/$/', '((?!^[0-9]+/$).)*', false),
58+
array('#^[0-9]+\#$#', '((?!^[0-9]+#$).)*', false),
59+
array('/^[0-9]+$/i', null, false),
60+
);
61+
}
62+
63+
/**
64+
* @dataProvider provideHtmlPatterns
65+
*/
66+
public function testGetHtmlPattern($pattern, $htmlPattern, $match = true)
67+
{
68+
$constraint = new Regex(array(
69+
'pattern' => $pattern,
70+
'match' => $match,
71+
));
72+
73+
$this->assertSame($pattern, $constraint->pattern);
74+
$this->assertSame($htmlPattern, $constraint->getHtmlPattern());
75+
}
76+
77+
public function testGetCustomHtmlPattern()
78+
{
79+
$constraint = new Regex(array(
80+
'pattern' => '((?![0-9]$|[a-z]+).)*',
81+
'htmlPattern' => 'foobar',
82+
));
83+
84+
$this->assertSame('((?![0-9]$|[a-z]+).)*', $constraint->pattern);
85+
$this->assertSame('foobar', $constraint->getHtmlPattern());
86+
}
87+
}

Tests/Constraints/RegexValidatorTest.php

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -88,70 +88,4 @@ public function getInvalidValues()
8888
array('090foo'),
8989
);
9090
}
91-
92-
public function testConstraintGetDefaultOption()
93-
{
94-
$constraint = new Regex(array(
95-
'pattern' => '/^[0-9]+$/',
96-
));
97-
98-
$this->assertEquals('pattern', $constraint->getDefaultOption());
99-
}
100-
101-
public function testHtmlPatternEscaping()
102-
{
103-
$constraint = new Regex(array(
104-
'pattern' => '/^[0-9]+\/$/',
105-
));
106-
107-
$this->assertEquals('[0-9]+/', $constraint->getHtmlPattern());
108-
109-
$constraint = new Regex(array(
110-
'pattern' => '#^[0-9]+\#$#',
111-
));
112-
113-
$this->assertEquals('[0-9]+#', $constraint->getHtmlPattern());
114-
}
115-
116-
public function testHtmlPattern()
117-
{
118-
// Specified htmlPattern
119-
$constraint = new Regex(array(
120-
'pattern' => '/^[a-z]+$/i',
121-
'htmlPattern' => '[a-zA-Z]+',
122-
));
123-
$this->assertEquals('[a-zA-Z]+', $constraint->getHtmlPattern());
124-
125-
// Disabled htmlPattern
126-
$constraint = new Regex(array(
127-
'pattern' => '/^[a-z]+$/i',
128-
'htmlPattern' => false,
129-
));
130-
$this->assertNull($constraint->getHtmlPattern());
131-
132-
// Cannot be converted
133-
$constraint = new Regex(array(
134-
'pattern' => '/^[a-z]+$/i',
135-
));
136-
$this->assertNull($constraint->getHtmlPattern());
137-
138-
// Automatically converted
139-
$constraint = new Regex(array(
140-
'pattern' => '/^[a-z]+$/',
141-
));
142-
$this->assertEquals('[a-z]+', $constraint->getHtmlPattern());
143-
144-
// Automatically converted, adds .*
145-
$constraint = new Regex(array(
146-
'pattern' => '/[a-z]+/',
147-
));
148-
$this->assertEquals('.*[a-z]+.*', $constraint->getHtmlPattern());
149-
150-
// Dropped because of match=false
151-
$constraint = new Regex(array(
152-
'pattern' => '/[a-z]+/',
153-
'match' => false,
154-
));
155-
$this->assertNull($constraint->getHtmlPattern());
156-
}
15791
}

0 commit comments

Comments
 (0)