From 67530769372cd456922c299ec53a8314bf4ba7af Mon Sep 17 00:00:00 2001 From: Meritoo Date: Sat, 23 Dec 2017 23:08:49 +0100 Subject: [PATCH] Regex - isValidHtmlAttribute() & areValidHtmlAttributes() methods - returns information if given html attribute is valid & if given html attributes are valid --- src/Utilities/Regex.php | 45 ++++++++++++ tests/Utilities/RegexTest.php | 131 ++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+) diff --git a/src/Utilities/Regex.php b/src/Utilities/Regex.php index 3004740..427bf49 100644 --- a/src/Utilities/Regex.php +++ b/src/Utilities/Regex.php @@ -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); + } } diff --git a/tests/Utilities/RegexTest.php b/tests/Utilities/RegexTest.php index 196aef4..e97544c 100644 --- a/tests/Utilities/RegexTest.php +++ b/tests/Utilities/RegexTest.php @@ -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} */