mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-11-09 23:28:42 +00:00
AttrDef_Length implemented. Reuses a bit of stuff from Pixel.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@170 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
parent
a520b5469e
commit
fb18fe31e1
37
library/HTMLPurifier/AttrDef/Length.php
Normal file
37
library/HTMLPurifier/AttrDef/Length.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/AttrDef.php';
|
||||
require_once 'HTMLPurifier/AttrDef/Pixels.php';
|
||||
|
||||
class HTMLPurifier_AttrDef_Length extends HTMLPurifier_AttrDef_Pixels
|
||||
{
|
||||
|
||||
function validate($string) {
|
||||
|
||||
$string = trim($string);
|
||||
if ($string === '') return false;
|
||||
|
||||
$parent_result = parent::validate($string);
|
||||
if ($parent_result !== false) return $parent_result;
|
||||
|
||||
$length = strlen($string);
|
||||
$last_char = $string[$length - 1];
|
||||
|
||||
if ($last_char !== '%') return false;
|
||||
|
||||
$points = substr($string, 0, $length - 1);
|
||||
|
||||
if (!is_numeric($points)) return false;
|
||||
|
||||
$points = (int) $points;
|
||||
|
||||
if ($points < 0) return '0%';
|
||||
if ($points > 100) return '100%';
|
||||
|
||||
return ((string) $points) . '%';
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -7,6 +7,7 @@ require_once 'HTMLPurifier/AttrDef.php';
|
||||
require_once 'HTMLPurifier/AttrDef/Text.php';
|
||||
require_once 'HTMLPurifier/AttrDef/Lang.php';
|
||||
require_once 'HTMLPurifier/AttrDef/Pixels.php';
|
||||
require_once 'HTMLPurifier/AttrDef/Length.php';
|
||||
require_once 'HTMLPurifier/AttrTransform.php';
|
||||
require_once 'HTMLPurifier/AttrTransform/Lang.php';
|
||||
require_once 'HTMLPurifier/AttrTransform/TextAlign.php';
|
||||
@ -243,10 +244,6 @@ class HTMLPurifier_Definition
|
||||
// complex attribute value substitution
|
||||
|
||||
$e_Text = new HTMLPurifier_AttrDef_Text();
|
||||
$e_TFrame = new HTMLPurifier_AttrDef_Enum(array('void', 'above',
|
||||
'below', 'hsides', 'lhs', 'rhs', 'vsides', 'box', 'border'), false);
|
||||
$e_TRules = new HTMLPurifier_AttrDef_Enum(array('none', 'groups',
|
||||
'rows', 'cols', 'all'), false);
|
||||
|
||||
// attrs, included in almost every single one except for a few,
|
||||
// which manually override these in their local definitions
|
||||
@ -271,34 +268,34 @@ class HTMLPurifier_Definition
|
||||
$this->info['td']->attr['abbr'] = $e_Text;
|
||||
$this->info['th']->attr['abbr'] = $e_Text;
|
||||
|
||||
$this->info['col']->attr['align'] =
|
||||
$this->info['colgroup']->attr['align'] =
|
||||
$this->info['tbody']->attr['align'] =
|
||||
$this->info['td']->attr['align'] =
|
||||
$this->info['tfoot']->attr['align'] =
|
||||
$this->info['th']->attr['align'] =
|
||||
$this->info['thead']->attr['align'] =
|
||||
$this->info['tr']->attr['align'] = new HTMLPurifier_AttrDef_Enum(
|
||||
array('left', 'center', 'right', 'justify', 'char'), false);
|
||||
$this->setAttrForTableElements('align', new HTMLPurifier_AttrDef_Enum(
|
||||
array('left', 'center', 'right', 'justify', 'char'), false));
|
||||
|
||||
$this->info['col']->attr['valign'] =
|
||||
$this->info['colgroup']->attr['valign'] =
|
||||
$this->info['tbody']->attr['valign'] =
|
||||
$this->info['td']->attr['valign'] =
|
||||
$this->info['tfoot']->attr['valign'] =
|
||||
$this->info['th']->attr['valign'] =
|
||||
$this->info['thead']->attr['valign'] =
|
||||
$this->info['tr']->attr['valign'] = new HTMLPurifier_AttrDef_Enum(
|
||||
array('top', 'middle', 'bottom', 'baseline'), false);
|
||||
$this->setAttrForTableElements('valign', new HTMLPurifier_AttrDef_Enum(
|
||||
array('top', 'middle', 'bottom', 'baseline'), false));
|
||||
|
||||
$this->info['img']->attr['alt'] = $e_Text;
|
||||
|
||||
$e_TFrame = new HTMLPurifier_AttrDef_Enum(array('void', 'above',
|
||||
'below', 'hsides', 'lhs', 'rhs', 'vsides', 'box', 'border'), false);
|
||||
$this->info['table']->attr['frame'] = $e_TFrame;
|
||||
|
||||
$e_TRules = new HTMLPurifier_AttrDef_Enum(array('none', 'groups',
|
||||
'rows', 'cols', 'all'), false);
|
||||
$this->info['table']->attr['rules'] = $e_TRules;
|
||||
|
||||
$this->info['table']->attr['summary'] = $e_Text;
|
||||
|
||||
$this->info['table']->attr['border'] = new HTMLPurifier_AttrDef_Pixels();
|
||||
|
||||
$e_Length = new HTMLPurifier_AttrDef_Length();
|
||||
$this->info['table']->attr['cellpadding'] = $e_Length;
|
||||
$this->info['table']->attr['cellspacing'] = $e_Length;
|
||||
$this->info['table']->attr['width'] = $e_Length;
|
||||
$this->setAttrForTableElements('charoff', $e_Length);
|
||||
$this->info['img']->attr['height'] = $e_Length;
|
||||
$this->info['img']->attr['width'] = $e_Length;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// UNIMP : info_tag_transform : transformations of tags
|
||||
|
||||
@ -346,6 +343,17 @@ class HTMLPurifier_Definition
|
||||
|
||||
}
|
||||
|
||||
function setAttrForTableElements($attr, $def) {
|
||||
$this->info['col']->attr[$attr] =
|
||||
$this->info['colgroup']->attr[$attr] =
|
||||
$this->info['tbody']->attr[$attr] =
|
||||
$this->info['td']->attr[$attr] =
|
||||
$this->info['tfoot']->attr[$attr] =
|
||||
$this->info['th']->attr[$attr] =
|
||||
$this->info['thead']->attr[$attr] =
|
||||
$this->info['tr']->attr[$attr] = $def;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class HTMLPurifier_ElementDef
|
||||
|
35
tests/HTMLPurifier/AttrDef/LengthTest.php
Normal file
35
tests/HTMLPurifier/AttrDef/LengthTest.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/AttrDefHarness.php';
|
||||
require_once 'HTMLPurifier/AttrDef/Length.php';
|
||||
|
||||
class HTMLPurifier_AttrDef_LengthTest extends HTMLPurifier_AttrDef_PixelsTest
|
||||
{
|
||||
|
||||
function setup() {
|
||||
$this->def = new HTMLPurifier_AttrDef_Length();
|
||||
}
|
||||
|
||||
function test() {
|
||||
|
||||
// pixel check
|
||||
parent::test();
|
||||
|
||||
// percent check
|
||||
$this->assertDef('25%');
|
||||
|
||||
// Firefox maintains percent, so will we
|
||||
$this->assertDef('0%');
|
||||
|
||||
// 0% <= percent <= 100%
|
||||
$this->assertDef('-15%', '0%');
|
||||
$this->assertDef('120%', '100%');
|
||||
|
||||
// fractional percents, apparently, aren't allowed
|
||||
$this->assertDef('56.5%', '56%');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -6,9 +6,11 @@ require_once 'HTMLPurifier/AttrDef/Pixels.php';
|
||||
class HTMLPurifier_AttrDef_PixelsTest extends HTMLPurifier_AttrDefHarness
|
||||
{
|
||||
|
||||
function test() {
|
||||
|
||||
function setup() {
|
||||
$this->def = new HTMLPurifier_AttrDef_Pixels();
|
||||
}
|
||||
|
||||
function test() {
|
||||
|
||||
$this->assertDef('1');
|
||||
$this->assertDef('0');
|
||||
|
@ -69,12 +69,16 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends
|
||||
|
||||
// test table
|
||||
$inputs[13] = <<<HTML
|
||||
<table frame="above" rules="rows" summary="A test table" border="2">
|
||||
<table frame="above" rules="rows" summary="A test table" border="2" cellpadding="5%" cellspacing="3" width="100%">
|
||||
<col align="right" />
|
||||
<col charoff="5" align="char" />
|
||||
<tr valign="top">
|
||||
<th abbr="super" align="left">Supercalifragilistic</th>
|
||||
<th abbr="name">Fiddly name</th>
|
||||
<th abbr="price">Super-duper-price</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td abbr="one">Cell one</td>
|
||||
<td abbr="carrot">Carrot Humungous</td>
|
||||
<td>$500.23</td>
|
||||
</tr>
|
||||
</table>
|
||||
HTML;
|
||||
|
@ -44,6 +44,7 @@ $test_files[] = 'AttrDef/ClassTest.php';
|
||||
$test_files[] = 'AttrDef/TextTest.php';
|
||||
$test_files[] = 'AttrDef/LangTest.php';
|
||||
$test_files[] = 'AttrDef/PixelsTest.php';
|
||||
$test_files[] = 'AttrDef/LengthTest.php';
|
||||
$test_files[] = 'IDAccumulatorTest.php';
|
||||
$test_files[] = 'TagTransformTest.php';
|
||||
$test_files[] = 'AttrTransform/LangTest.php';
|
||||
|
Loading…
Reference in New Issue
Block a user