Regex - isValidHtmlAttribute() & areValidHtmlAttributes() methods - returns information if given html attribute is valid & if given html attributes are valid

This commit is contained in:
Meritoo
2017-12-23 23:08:49 +01:00
parent 7f713e0c6e
commit 6753076937
2 changed files with 176 additions and 0 deletions

View File

@@ -32,6 +32,7 @@ class Regex
'urlDomain' => '([\da-z\.-]+)\.([a-z\.]{2,6})(\/)?([\w\.\-]*)?(\?)?([\w \.\-\/=&]*)\/?$/i',
'letterOrDigit' => '/[a-zA-Z0-9]+/',
'htmlEntity' => '/&[a-z0-9]+;/',
'htmlAttribute' => '/([\w-]+)="([\w -]+)"/',
'fileName' => '/.+\.\w+$/',
'isQuoted' => '/^[\'"]{1}.+[\'"]{1}$/',
'windowsBasedPath' => '/^[A-Z]{1}:\\\.*$/',
@@ -751,4 +752,48 @@ class Regex
{
return self::$patterns['bundleName'];
}
/**
* Returns pattern used to validate / verify html attribute
*
* @return string
*/
public static function getHtmlAttributePattern()
{
return self::$patterns['htmlAttribute'];
}
/**
* Returns information if given html attribute is valid
*
* @param string $htmlAttribute The html attribute to verify
* @return bool
*/
public static function isValidHtmlAttribute($htmlAttribute)
{
if (!is_string($htmlAttribute)) {
return false;
}
$pattern = self::getHtmlAttributePattern();
return (bool)preg_match($pattern, $htmlAttribute);
}
/**
* Returns information if given html attributes are valid
*
* @param string $htmlAttributes The html attributes to verify
* @return bool
*/
public static function areValidHtmlAttributes($htmlAttributes)
{
if (!is_string($htmlAttributes)) {
return false;
}
$pattern = self::getHtmlAttributePattern();
return (bool)preg_match_all($pattern, $htmlAttributes);
}
}

View File

@@ -299,6 +299,51 @@ class RegexTest extends BaseTestCase
self::assertEquals('/^(([A-Z]{1}[a-z0-9]+)((?2))*)(Bundle)$/', Regex::getBundleNamePattern());
}
public function testGetHtmlAttributePattern()
{
self::assertEquals('/([\w-]+)="([\w -]+)"/', Regex::getHtmlAttributePattern());
}
/**
* @param mixed $emptyValue Empty value, e.g. ""
* @dataProvider provideEmptyValue
*/
public function testIsValidHtmlAttributeUsingEmptyValue($emptyValue)
{
self::assertFalse(Regex::isValidHtmlAttribute($emptyValue));
}
/**
* @param string $htmlAttribute The html attribute to verify
* @param bool $expected Information if it's valid attribute
*
* @dataProvider provideHtmlAttribute
*/
public function testIsValidHtmlAttribute($htmlAttribute, $expected)
{
self::assertEquals($expected, Regex::isValidHtmlAttribute($htmlAttribute));
}
/**
* @param mixed $emptyValue Empty value, e.g. ""
* @dataProvider provideEmptyValue
*/
public static function testAreValidHtmlAttributesUsingEmptyValue($emptyValue)
{
self::assertFalse(Regex::areValidHtmlAttributes($emptyValue));
}
/**
* @param string $htmlAttributes The html attributes to verify
* @param bool $expected Information if attributes are valid
*
* @dataProvider provideHtmlAttributes
*/
public static function testAreValidHtmlAttributes($htmlAttributes, $expected)
{
self::assertEquals($expected, Regex::areValidHtmlAttributes($htmlAttributes));
}
/**
* Provides name of bundle and information if it's valid name
*
@@ -342,6 +387,92 @@ class RegexTest extends BaseTestCase
];
}
/**
* Provides html attribute and information if it's valid
*
* @return Generator
*/
public function provideHtmlAttribute()
{
yield[
'abc = def',
false,
];
yield[
'a b c=def',
false,
];
yield[
'abc=def',
false,
];
yield[
'a1b2c=d3e4f',
false,
];
yield[
'abc="def"',
true,
];
yield[
'a1b2c="d3e4f"',
true,
];
}
/**
* Provides html attribute and information if attributes are valid
*
* @return Generator
*/
public function provideHtmlAttributes()
{
yield[
'abc = def',
false,
];
yield[
'abc = def ghi = jkl',
false,
];
yield[
'abc=def ghi=jkl',
false,
];
yield[
'abc=def ghi=jkl mno=pqr',
false,
];
yield[
'abc="def"',
true,
];
yield[
'abc="def" ghi="jkl"',
true,
];
yield[
'abc="def" ghi="jkl" mno="pqr"',
true,
];
yield[
'a2bc="d4ef" ghi="j k l" mno="pq9r"',
true,
];
}
/**
* {@inheritdoc}
*/