Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
22 / 22 |
Configuration | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
22 / 22 |
__construct() | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
allowTag($tag_name) | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
allowAttribute( $tag_name, $attribute_name, $attribute_regexp = "/.*/" ) | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
isAllowedTag($tag_name) | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
isAllowedAttribute( $tag_name, $attribute_name, $attribute_value ) | |
100.00% |
1 / 1 |
3 | |
100.00% |
9 / 9 |
<?php | |
namespace AthosHun\HTMLFilter; | |
class Configuration | |
{ | |
private $allowed_tags_with_attributes; | |
public function __construct() | |
{ | |
$this->allowed_tags_with_attributes = array(); | |
} | |
public function allowTag($tag_name) | |
{ | |
if (!$this->isAllowedTag($tag_name)) { | |
$this->allowed_tags_with_attributes[(string)$tag_name] = array(); | |
} | |
return $this; | |
} | |
public function allowAttribute( | |
$tag_name, | |
$attribute_name, | |
$attribute_regexp = "/.*/" | |
) { | |
$this->allowTag($tag_name); | |
$tag = (string)$tag_name; | |
$attr = (string)$attribute_name; | |
$this->allowed_tags_with_attributes[$tag][$attr] = $attribute_regexp; | |
return $this; | |
} | |
public function isAllowedTag($tag_name) | |
{ | |
return array_key_exists((string)$tag_name, | |
$this->allowed_tags_with_attributes); | |
} | |
public function isAllowedAttribute( | |
$tag_name, | |
$attribute_name, | |
$attribute_value | |
) { | |
if (!$this->isAllowedTag($tag_name)) { | |
return false; | |
} | |
$tag = (string)$tag_name; | |
$attr = (string)$attribute_name; | |
$allowed_attributes = $this->allowed_tags_with_attributes[$tag]; | |
if (!array_key_exists($attr, $allowed_attributes)) { | |
return false; | |
} | |
$restriction = $allowed_attributes[$attr]; | |
return 1 === preg_match($restriction, $attribute_value); | |
} | |
} |