0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-01-03 05:11:52 +00:00

Add CSSLength support, and roll out to all applicable styles.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@237 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-08-13 23:08:38 +00:00
parent ff7fdaca38
commit b5ff592157
8 changed files with 125 additions and 4 deletions

View File

@ -143,7 +143,7 @@ thead th {text-align:left;padding:0.1em;background-color:#EEE;}
<tr class="impl-yes"><td>border-*-color</td><td>COMPOSITE(&lt;color&gt;, transparent)</td></tr>
<tr class="impl-yes"><td>border-*-style</td><td>ENUM(none, hidden, dotted, dashed,
solid, double, groove, ridge, inset, outset)</td></tr>
<tr class="css1"><td>border-*-width</td><td>COMPOSITE(&lt;length&gt;, thin, medium, thick)</td></tr>
<tr class="css1 impl-yes"><td>border-*-width</td><td>COMPOSITE(&lt;length&gt;, 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>color</td><td>&lt;color&gt;</td></tr>
<tr class="css1 impl-yes"><td>float</td><td>ENUM(left, right, none), May require layout
@ -158,7 +158,7 @@ thead th {text-align:left;padding:0.1em;background-color:#EEE;}
<tr class="css1 impl-yes"><td>font-weight</td><td>ENUM(normal, bold, bolder, lighter,
100, 200, 300, 400, 500, 600, 700, 800, 900), maybe special code for
in-between integers</td></tr>
<tr class="css1"><td>letter-spacing</td><td>COMPOSITE(&lt;length&gt;, normal)</td></tr>
<tr class="css1 impl-yes"><td>letter-spacing</td><td>COMPOSITE(&lt;length&gt;, normal)</td></tr>
<tr class="css1"><td>line-height</td><td>COMPOSITE(&lt;number&gt;,
&lt;length&gt;, &lt;percentage&gt;, normal)</td></tr>
<tr class="css1 impl-yes"><td>list-style-position</td><td>ENUM(inside, outside),
@ -185,7 +185,7 @@ thead th {text-align:left;padding:0.1em;background-color:#EEE;}
lowercase, none)</td></tr>
<tr class="css1"><td>width</td><td>COMPOSITE(&lt;length&gt;,
&lt;percentage&gt;, auto), Interesting</td></tr>
<tr class="css1"><td>word-spacing</td><td>COMPOSITE(&lt;length&gt;, auto),
<tr class="css1 impl-yes"><td>word-spacing</td><td>COMPOSITE(&lt;length&gt;, auto),
IE 5 no support</td></tr>
</tbody>

View File

@ -0,0 +1,40 @@
<?php
require_once 'HTMLPurifier/AttrDef.php';
require_once 'HTMLPurifier/AttrDef/Number.php';
class HTMLPurifier_AttrDef_CSSLength extends HTMLPurifier_AttrDef
{
var $units = array('em' => true, 'ex' => true, 'px' => true, 'in' => true,
'cm' => true, 'mm' => true, 'pt' => true, 'pc' => true);
var $number_def;
function HTMLPurifier_AttrDef_CSSLength($non_negative = false) {
$this->number_def = new HTMLPurifier_AttrDef_Number($non_negative);
}
function validate($length, $config, &$context) {
$length = $this->parseCDATA($length);
if ($length === '') return false;
if ($length === '0') return '0';
$strlen = strlen($length);
if ($strlen === 1) return false; // impossible!
// we assume all units are two characters
$unit = substr($length, $strlen - 2);
$number = substr($length, 0, $strlen - 2);
if (!isset($this->units[$unit])) return false;
$number = $this->number_def->validate($number, $config, $context);
if ($number === false) return false;
return $number . $unit;
}
}
?>

View File

@ -3,6 +3,12 @@
class HTMLPurifier_AttrDef_Number extends HTMLPurifier_AttrDef
{
var $non_negative = false;
function HTMLPurifier_AttrDef_Number($non_negative = false) {
$this->non_negative = $non_negative;
}
function validate($number, $config, &$context) {
$number = $this->parseCDATA($number);
@ -12,6 +18,7 @@ class HTMLPurifier_AttrDef_Number extends HTMLPurifier_AttrDef
$sign = '';
switch ($number[0]) {
case '-':
if ($this->non_negative) return false;
$sign = '-';
case '+':
$number = substr($number, 1);

View File

@ -54,6 +54,24 @@ class HTMLPurifier_CSSDefinition
new HTMLPurifier_AttrDef_Color()
));
$this->info['border-top-width'] =
$this->info['border-bottom-width'] =
$this->info['border-left-width'] =
$this->info['border-right-width'] = new HTMLPurifier_AttrDef_Composite(array(
new HTMLPurifier_AttrDef_Enum(array('thin', 'medium', 'thick')),
new HTMLPurifier_AttrDef_CSSLength(true) //disallow negative
));
$this->info['letter-spacing'] = new HTMLPurifier_AttrDef_Composite(array(
new HTMLPurifier_AttrDef_Enum(array('normal')),
new HTMLPurifier_AttrDef_CSSLength()
));
$this->info['word-spacing'] = new HTMLPurifier_AttrDef_Composite(array(
new HTMLPurifier_AttrDef_Enum(array('normal')),
new HTMLPurifier_AttrDef_CSSLength()
));
// this could use specialized code
$this->info['font-weight'] = new HTMLPurifier_AttrDef_Enum(
array('normal', 'bold', 'bolder', 'lighter', '100', '200', '300',

View File

@ -0,0 +1,40 @@
<?php
require_once 'HTMLPurifier/AttrDef/CSSLength.php';
class HTMLPurifier_AttrDef_CSSLengthTest extends HTMLPurifier_AttrDefHarness
{
function test() {
$this->def = new HTMLPurifier_AttrDef_CSSLength();
$this->assertDef('0');
$this->assertDef('0px');
$this->assertDef('4.5px');
$this->assertDef('-4.5px');
$this->assertDef('3ex');
$this->assertDef('3em');
$this->assertDef('3in');
$this->assertDef('3cm');
$this->assertDef('3mm');
$this->assertDef('3pt');
$this->assertDef('3pc');
$this->assertDef('3', false);
$this->assertDef('3miles', false);
}
function testNonNegative() {
$this->def = new HTMLPurifier_AttrDef_CSSLength(true);
$this->assertDef('3cm');
$this->assertDef('-3mm', false);
}
}
?>

View File

@ -24,6 +24,13 @@ class HTMLPurifier_AttrDef_CSSTest extends HTMLPurifier_AttrDefHarness
$this->assertDef('background-color:transparent;');
$this->assertDef('color:#F00;');
$this->assertDef('border-top-color:#F00;');
$this->assertDef('border-top-width:thin;');
$this->assertDef('border-top-width:12px;');
$this->assertDef('border-top-width:-12px;', false);
$this->assertDef('letter-spacing:normal;');
$this->assertDef('letter-spacing:2px;');
$this->assertDef('word-spacing:normal;');
$this->assertDef('word-spacing:3em;');
// duplicates
$this->assertDef('text-align:right;text-align:left;',

View File

@ -26,6 +26,14 @@ class HTMLPurifier_AttrDef_NumberTest extends HTMLPurifier_AttrDefHarness
}
function testNonNegative() {
$this->def = new HTMLPurifier_AttrDef_Number(true);
$this->assertDef('23');
$this->assertDef('-12', false);
}
}
?>

View File

@ -68,6 +68,7 @@ $test_files[] = 'AttrDef/CompositeTest.php';
$test_files[] = 'AttrDef/ColorTest.php';
$test_files[] = 'AttrDef/IntegerTest.php';
$test_files[] = 'AttrDef/NumberTest.php';
$test_files[] = 'AttrDef/CSSLengthTest.php';
$test_files[] = 'IDAccumulatorTest.php';
$test_files[] = 'TagTransformTest.php';
$test_files[] = 'AttrTransform/LangTest.php';