mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-01-16 21:48:14 +01:00
Add rgba support for css color attribute definition
This commit is contained in:
parent
8e4cacf0a7
commit
d41a59e422
@ -29,24 +29,49 @@ class HTMLPurifier_AttrDef_CSS_Color extends HTMLPurifier_AttrDef
|
|||||||
return $colors[$lower];
|
return $colors[$lower];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strpos($color, 'rgb(') !== false) {
|
if (preg_match('#(rgb|rgba)\(#', $color, $matches) === 1) {
|
||||||
|
// get used function : rgb or rgba
|
||||||
|
$function = $matches[1];
|
||||||
|
if ($function == 'rgba') {
|
||||||
|
$parameters_size = 4;
|
||||||
|
} else {
|
||||||
|
$parameters_size = 3;
|
||||||
|
}
|
||||||
|
|
||||||
// rgb literal handling
|
// rgb literal handling
|
||||||
$length = strlen($color);
|
$length = strlen($color);
|
||||||
if (strpos($color, ')') !== $length - 1) {
|
if (strpos($color, ')') !== $length - 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$triad = substr($color, 4, $length - 4 - 1);
|
|
||||||
$parts = explode(',', $triad);
|
$values = substr($color, strlen($function) + 1, $length - strlen($function) - 2);
|
||||||
if (count($parts) !== 3) {
|
|
||||||
|
$parts = explode(',', $values);
|
||||||
|
if (count($parts) !== $parameters_size) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$type = false; // to ensure that they're all the same type
|
$type = false; // to ensure that they're all the same type
|
||||||
$new_parts = array();
|
$new_parts = array();
|
||||||
|
$i = 0;
|
||||||
foreach ($parts as $part) {
|
foreach ($parts as $part) {
|
||||||
|
$i++;
|
||||||
$part = trim($part);
|
$part = trim($part);
|
||||||
if ($part === '') {
|
if ($part === '') {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// different check for alpha channel
|
||||||
|
if ($function === 'rgba' && $i === count($parts)) {
|
||||||
|
$result = (new HTMLPurifier_AttrDef_CSS_AlphaValue())->validate($part, $config, $context);
|
||||||
|
|
||||||
|
if ($result === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$new_parts[] = (string)$result;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$length = strlen($part);
|
$length = strlen($part);
|
||||||
if ($part[$length - 1] === '%') {
|
if ($part[$length - 1] === '%') {
|
||||||
// handle percents
|
// handle percents
|
||||||
@ -80,8 +105,8 @@ class HTMLPurifier_AttrDef_CSS_Color extends HTMLPurifier_AttrDef
|
|||||||
$new_parts[] = (string)$num;
|
$new_parts[] = (string)$num;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$new_triad = implode(',', $new_parts);
|
$new_values = implode(',', $new_parts);
|
||||||
$color = "rgb($new_triad)";
|
$color = "$function($new_values)";
|
||||||
} else {
|
} else {
|
||||||
// hexadecimal handling
|
// hexadecimal handling
|
||||||
if ($color[0] === '#') {
|
if ($color[0] === '#') {
|
||||||
|
@ -11,10 +11,15 @@ class HTMLPurifier_AttrDef_CSS_ColorTest extends HTMLPurifier_AttrDefHarness
|
|||||||
$this->assertDef('#fff');
|
$this->assertDef('#fff');
|
||||||
$this->assertDef('#eeeeee');
|
$this->assertDef('#eeeeee');
|
||||||
$this->assertDef('#808080');
|
$this->assertDef('#808080');
|
||||||
|
|
||||||
$this->assertDef('rgb(255, 0, 0)', 'rgb(255,0,0)'); // rm spaces
|
$this->assertDef('rgb(255, 0, 0)', 'rgb(255,0,0)'); // rm spaces
|
||||||
$this->assertDef('rgb(100%,0%,0%)');
|
$this->assertDef('rgb(100%,0%,0%)');
|
||||||
$this->assertDef('rgb(50.5%,23.2%,43.9%)'); // decimals okay
|
$this->assertDef('rgb(50.5%,23.2%,43.9%)'); // decimals okay
|
||||||
|
|
||||||
|
$this->assertDef('rgba(255, 0, 0, 0)', 'rgba(255,0,0,0)'); // rm spaces
|
||||||
|
$this->assertDef('rgba(100%,0%,0%,.4)');
|
||||||
|
$this->assertDef('rgba(38.1%,59.7%,1.8%,0.7)', 'rgba(38.1%,59.7%,1.8%,.7)'); // decimals okay
|
||||||
|
|
||||||
$this->assertDef('#G00', false);
|
$this->assertDef('#G00', false);
|
||||||
$this->assertDef('cmyk(40, 23, 43, 23)', false);
|
$this->assertDef('cmyk(40, 23, 43, 23)', false);
|
||||||
$this->assertDef('rgb(0%, 23, 68%)', false);
|
$this->assertDef('rgb(0%, 23, 68%)', false);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user