mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-12-23 00:41:52 +00:00
Implement the color AttrDef.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@230 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
parent
415b7d3913
commit
4ffb2da238
@ -133,19 +133,19 @@ thead th {text-align:left;padding:0.1em;background-color:#EEE;}
|
|||||||
|
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr><th colspan="2">Standard</th></tr>
|
<tr><th colspan="2">Standard</th></tr>
|
||||||
<tr class="css1"><td>background-color</td><td>COMPOSITE(<color>, transparent)</td></tr>
|
<tr class="css1 impl-yes"><td>background-color</td><td>COMPOSITE(<color>, transparent)</td></tr>
|
||||||
<tr class="css1"><td>background</td><td>SHORTHAND</td></tr>
|
<tr class="css1"><td>background</td><td>SHORTHAND</td></tr>
|
||||||
<tr class="css1"><td>border</td><td>SHORTHAND, MULTIPLE</td></tr>
|
<tr class="css1"><td>border</td><td>SHORTHAND, MULTIPLE</td></tr>
|
||||||
<tr class="css1"><td>border-color</td><td>MULTIPLE</td></tr>
|
<tr class="css1"><td>border-color</td><td>MULTIPLE</td></tr>
|
||||||
<tr class="css1"><td>border-style</td><td>MULTIPLE</td></tr>
|
<tr class="css1"><td>border-style</td><td>MULTIPLE</td></tr>
|
||||||
<tr class="css1"><td>border-width</td><td>MULTIPLE</td></tr>
|
<tr class="css1"><td>border-width</td><td>MULTIPLE</td></tr>
|
||||||
<tr class="css1"><td>border-*</td><td>SHORTHAND</td></tr>
|
<tr class="css1"><td>border-*</td><td>SHORTHAND</td></tr>
|
||||||
<tr><td>border-*-color</td><td>COMPOSITE(<color>, transparent)</td></tr>
|
<tr class="impl-yes"><td>border-*-color</td><td>COMPOSITE(<color>, transparent)</td></tr>
|
||||||
<tr class="impl-yes"><td>border-*-style</td><td>ENUM(none, hidden, dotted, dashed,
|
<tr class="impl-yes"><td>border-*-style</td><td>ENUM(none, hidden, dotted, dashed,
|
||||||
solid, double, groove, ridge, inset, outset)</td></tr>
|
solid, double, groove, ridge, inset, outset)</td></tr>
|
||||||
<tr class="css1"><td>border-*-width</td><td>COMPOSITE(<length>, thin, medium, thick)</td></tr>
|
<tr class="css1"><td>border-*-width</td><td>COMPOSITE(<length>, thin, medium, thick)</td></tr>
|
||||||
<tr class="css1 impl-yes"><td>clear</td><td>ENUM(none, left, right, both)</td></tr>
|
<tr class="css1 impl-yes"><td>clear</td><td>ENUM(none, left, right, both)</td></tr>
|
||||||
<tr class="css1"><td>color</td><td><color></td></tr>
|
<tr class="css1 impl-yes"><td>color</td><td><color></td></tr>
|
||||||
<tr class="css1 impl-yes"><td>float</td><td>ENUM(left, right, none), May require layout
|
<tr class="css1 impl-yes"><td>float</td><td>ENUM(left, right, none), May require layout
|
||||||
precautions with clear</td></tr>
|
precautions with clear</td></tr>
|
||||||
<tr class="css1"><td>font</td><td>SHORTHAND</td></tr>
|
<tr class="css1"><td>font</td><td>SHORTHAND</td></tr>
|
||||||
|
67
library/HTMLPurifier/AttrDef/Color.php
Normal file
67
library/HTMLPurifier/AttrDef/Color.php
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/AttrDef.php';
|
||||||
|
|
||||||
|
class HTMLPurifier_AttrDef_Color
|
||||||
|
{
|
||||||
|
|
||||||
|
function validate($color, $config, &$context) {
|
||||||
|
|
||||||
|
$color = trim($color);
|
||||||
|
if (!$color) return false;
|
||||||
|
|
||||||
|
if ($color[0] === '#') {
|
||||||
|
// hexadecimal handling
|
||||||
|
$hex = substr($color, 1);
|
||||||
|
$length = strlen($hex);
|
||||||
|
if ($length !== 3 && $length !== 6) return false;
|
||||||
|
if (!ctype_xdigit($hex)) return false;
|
||||||
|
} else {
|
||||||
|
// rgb literal handling
|
||||||
|
if (strpos($color, 'rgb(')) return false;
|
||||||
|
$length = strlen($color);
|
||||||
|
if (strpos($color, ')') !== $length - 1) return false;
|
||||||
|
$triad = substr($color, 4, $length - 4 - 1);
|
||||||
|
$parts = explode(',', $triad);
|
||||||
|
if (count($parts) !== 3) return false;
|
||||||
|
$type = false; // to ensure that they're all the same type
|
||||||
|
$new_parts = array();
|
||||||
|
foreach ($parts as $part) {
|
||||||
|
$part = trim($part);
|
||||||
|
if ($part === '') return false;
|
||||||
|
$length = strlen($part);
|
||||||
|
if ($part[$length - 1] === '%') {
|
||||||
|
// handle percents
|
||||||
|
if (!$type) {
|
||||||
|
$type = 'percentage';
|
||||||
|
} elseif ($type !== 'percentage') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$num = (float) substr($part, 0, $length - 1);
|
||||||
|
if ($num < 0) $num = 0;
|
||||||
|
if ($num > 100) $num = 100;
|
||||||
|
$new_parts[] = "$num%";
|
||||||
|
} else {
|
||||||
|
// handle integers
|
||||||
|
if (!$type) {
|
||||||
|
$type = 'integer';
|
||||||
|
} elseif ($type !== 'integer') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$num = (int) $part;
|
||||||
|
if ($num < 0) $num = 0;
|
||||||
|
if ($num > 255) $num = 255;
|
||||||
|
$new_parts[] = (string) $num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$new_triad = implode(',', $new_parts);
|
||||||
|
$color = "rgb($new_triad)";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $color;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -5,8 +5,8 @@ class HTMLPurifier_AttrDef_Composite extends HTMLPurifier_AttrDef
|
|||||||
|
|
||||||
var $defs;
|
var $defs;
|
||||||
|
|
||||||
function HTMLPurifier_AttrDef_Composite(&$defs) {
|
function HTMLPurifier_AttrDef_Composite($defs) {
|
||||||
$this->defs =& $defs;
|
$this->defs = $defs;
|
||||||
}
|
}
|
||||||
|
|
||||||
function validate($string, $config, &$context) {
|
function validate($string, $config, &$context) {
|
||||||
|
@ -43,6 +43,16 @@ class HTMLPurifier_CSSDefinition
|
|||||||
'upper-roman', 'lower-alpha', 'upper-alpha'), false);
|
'upper-roman', 'lower-alpha', 'upper-alpha'), false);
|
||||||
$this->info['text-transform'] = new HTMLPurifier_AttrDef_Enum(
|
$this->info['text-transform'] = new HTMLPurifier_AttrDef_Enum(
|
||||||
array('capitalize', 'uppercase', 'lowercase', 'none'), false);
|
array('capitalize', 'uppercase', 'lowercase', 'none'), false);
|
||||||
|
$this->info['color'] = new HTMLPurifier_AttrDef_Color();
|
||||||
|
|
||||||
|
$this->info['border-top-color'] =
|
||||||
|
$this->info['border-bottom-color'] =
|
||||||
|
$this->info['border-left-color'] =
|
||||||
|
$this->info['border-right-color'] =
|
||||||
|
$this->info['background-color'] = new HTMLPurifier_AttrDef_Composite( array(
|
||||||
|
new HTMLPurifier_AttrDef_Enum(array('transparent')),
|
||||||
|
new HTMLPurifier_AttrDef_Color()
|
||||||
|
));
|
||||||
|
|
||||||
// this could use specialized code
|
// this could use specialized code
|
||||||
$this->info['font-weight'] = new HTMLPurifier_AttrDef_Enum(
|
$this->info['font-weight'] = new HTMLPurifier_AttrDef_Enum(
|
||||||
|
@ -20,6 +20,10 @@ class HTMLPurifier_AttrDef_CSSTest extends HTMLPurifier_AttrDefHarness
|
|||||||
$this->assertDef('list-style-position:outside;');
|
$this->assertDef('list-style-position:outside;');
|
||||||
$this->assertDef('list-style-type:upper-roman;');
|
$this->assertDef('list-style-type:upper-roman;');
|
||||||
$this->assertDef('text-transform:capitalize;');
|
$this->assertDef('text-transform:capitalize;');
|
||||||
|
$this->assertDef('background-color:rgb(0,0,255);');
|
||||||
|
$this->assertDef('background-color:transparent;');
|
||||||
|
$this->assertDef('color:#F00;');
|
||||||
|
$this->assertDef('border-top-color:#F00;');
|
||||||
|
|
||||||
// duplicates
|
// duplicates
|
||||||
$this->assertDef('text-align:right;text-align:left;',
|
$this->assertDef('text-align:right;text-align:left;',
|
||||||
|
33
tests/HTMLPurifier/AttrDef/ColorTest.php
Normal file
33
tests/HTMLPurifier/AttrDef/ColorTest.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/AttrDef/Color.php';
|
||||||
|
|
||||||
|
class HTMLPurifier_AttrDef_ColorTest extends HTMLPurifier_AttrDefHarness
|
||||||
|
{
|
||||||
|
|
||||||
|
function test() {
|
||||||
|
|
||||||
|
$this->def = new HTMLPurifier_AttrDef_Color();
|
||||||
|
|
||||||
|
$this->assertDef('#F00');
|
||||||
|
$this->assertDef('#808080');
|
||||||
|
$this->assertDef('rgb(255, 0, 0)', 'rgb(255,0,0)'); // rm spaces
|
||||||
|
$this->assertDef('rgb(100%,0%,0%)');
|
||||||
|
$this->assertDef('rgb(50.5%,23.2%,43.9%)'); // decimals okay
|
||||||
|
|
||||||
|
$this->assertDef('#G00', false);
|
||||||
|
$this->assertDef('cmyk(40, 23, 43, 23)', false);
|
||||||
|
$this->assertDef('rgb(0%, 23, 68%)', false);
|
||||||
|
|
||||||
|
// clip numbers outside sRGB gamut
|
||||||
|
$this->assertDef('rgb(200%, -10%, 0%)', 'rgb(100%,0%,0%)');
|
||||||
|
$this->assertDef('rgb(256,-23,34)', 'rgb(255,0,34)');
|
||||||
|
|
||||||
|
// maybe hex transformations would be another nice feature
|
||||||
|
// at the very least transform rgb percent to rgb integer
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -2,6 +2,16 @@
|
|||||||
|
|
||||||
require_once 'HTMLPurifier/AttrDef/Composite.php';
|
require_once 'HTMLPurifier/AttrDef/Composite.php';
|
||||||
|
|
||||||
|
class HTMLPurifier_AttrDef_Composite_Testable extends
|
||||||
|
HTMLPurifier_AttrDef_Composite
|
||||||
|
{
|
||||||
|
|
||||||
|
function HTMLPurifier_AttrDef_Composite_Testable(&$defs) {
|
||||||
|
$this->defs =& $defs;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
class HTMLPurifier_AttrDef_CompositeTest extends HTMLPurifier_AttrDefHarness
|
class HTMLPurifier_AttrDef_CompositeTest extends HTMLPurifier_AttrDefHarness
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -20,7 +30,7 @@ class HTMLPurifier_AttrDef_CompositeTest extends HTMLPurifier_AttrDefHarness
|
|||||||
$def1 =& new HTMLPurifier_AttrDefMock($this);
|
$def1 =& new HTMLPurifier_AttrDefMock($this);
|
||||||
$def2 =& new HTMLPurifier_AttrDefMock($this);
|
$def2 =& new HTMLPurifier_AttrDefMock($this);
|
||||||
$defs = array(&$def1, &$def2);
|
$defs = array(&$def1, &$def2);
|
||||||
$def =& new HTMLPurifier_AttrDef_Composite($defs);
|
$def =& new HTMLPurifier_AttrDef_Composite_Testable($defs);
|
||||||
$input = 'FOOBAR';
|
$input = 'FOOBAR';
|
||||||
$output = 'foobar';
|
$output = 'foobar';
|
||||||
$def1_params = array($input, $config, $context);
|
$def1_params = array($input, $config, $context);
|
||||||
@ -39,7 +49,7 @@ class HTMLPurifier_AttrDef_CompositeTest extends HTMLPurifier_AttrDefHarness
|
|||||||
$def1 =& new HTMLPurifier_AttrDefMock($this);
|
$def1 =& new HTMLPurifier_AttrDefMock($this);
|
||||||
$def2 =& new HTMLPurifier_AttrDefMock($this);
|
$def2 =& new HTMLPurifier_AttrDefMock($this);
|
||||||
$defs = array(&$def1, &$def2);
|
$defs = array(&$def1, &$def2);
|
||||||
$def =& new HTMLPurifier_AttrDef_Composite($defs);
|
$def =& new HTMLPurifier_AttrDef_Composite_Testable($defs);
|
||||||
$input = 'BOOMA';
|
$input = 'BOOMA';
|
||||||
$output = 'booma';
|
$output = 'booma';
|
||||||
$def_params = array($input, $config, $context);
|
$def_params = array($input, $config, $context);
|
||||||
@ -59,7 +69,7 @@ class HTMLPurifier_AttrDef_CompositeTest extends HTMLPurifier_AttrDefHarness
|
|||||||
$def1 =& new HTMLPurifier_AttrDefMock($this);
|
$def1 =& new HTMLPurifier_AttrDefMock($this);
|
||||||
$def2 =& new HTMLPurifier_AttrDefMock($this);
|
$def2 =& new HTMLPurifier_AttrDefMock($this);
|
||||||
$defs = array(&$def1, &$def2);
|
$defs = array(&$def1, &$def2);
|
||||||
$def =& new HTMLPurifier_AttrDef_Composite($defs);
|
$def =& new HTMLPurifier_AttrDef_Composite_Testable($defs);
|
||||||
$input = 'BOOMA';
|
$input = 'BOOMA';
|
||||||
$output = false;
|
$output = false;
|
||||||
$def_params = array($input, $config, $context);
|
$def_params = array($input, $config, $context);
|
||||||
|
@ -65,6 +65,7 @@ $test_files[] = 'AttrDef/NumberSpanTest.php';
|
|||||||
$test_files[] = 'AttrDef/URITest.php';
|
$test_files[] = 'AttrDef/URITest.php';
|
||||||
$test_files[] = 'AttrDef/CSSTest.php';
|
$test_files[] = 'AttrDef/CSSTest.php';
|
||||||
$test_files[] = 'AttrDef/CompositeTest.php';
|
$test_files[] = 'AttrDef/CompositeTest.php';
|
||||||
|
$test_files[] = 'AttrDef/ColorTest.php';
|
||||||
$test_files[] = 'IDAccumulatorTest.php';
|
$test_files[] = 'IDAccumulatorTest.php';
|
||||||
$test_files[] = 'TagTransformTest.php';
|
$test_files[] = 'TagTransformTest.php';
|
||||||
$test_files[] = 'AttrTransform/LangTest.php';
|
$test_files[] = 'AttrTransform/LangTest.php';
|
||||||
|
Loading…
Reference in New Issue
Block a user