0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 16:31:53 +00:00

[1.6.1] Implement BoolToCSS attribute transformations for td,th.nowrap and hr.noshade

- Implement CSS property white-space:nowrap;
- Update TODO with more ambitious goal: all transforms by 1.6.1

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1012 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-05-03 04:07:47 +00:00
parent a3ed9196b9
commit bd58a7ba77
10 changed files with 111 additions and 8 deletions

5
NEWS
View File

@ -19,9 +19,12 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
images will hang around with an empty src images will hang around with an empty src
! Support for more deprecated attributes via transformations: ! Support for more deprecated attributes via transformations:
+ hspace and vspace in img + hspace and vspace in img
+ size in hr + size and noshade in hr
+ nowrap in td
! target attribute in a tag supported, use %Attr.AllowedFrameTargets ! target attribute in a tag supported, use %Attr.AllowedFrameTargets
to enable to enable
! CSS property white-space now allows nowrap (supported in all modern
browsers) but not others (which have spotty browser implementations)
- Possibly fatal bug with __autoload() fixed in module manager - Possibly fatal bug with __autoload() fixed in module manager
- Invert HTMLModuleManager->addModule() processing order to check - Invert HTMLModuleManager->addModule() processing order to check
prefixes first and then the literal module prefixes first and then the literal module

5
TODO
View File

@ -8,8 +8,9 @@ TODO List
========================== ==========================
1.6.1 [Oh Dear, We Missed Something!] 1.6.1 [Oh Dear, We Missed Something!]
# align in img and table # align in img, table, hr and caption
# noshade in hr # clear in br
# type in ul, ol, li
1.7 release [Advanced API] 1.7 release [Advanced API]
# Complete advanced API, and fully document it # Complete advanced API, and fully document it

View File

@ -168,9 +168,9 @@ thead th {text-align:left;padding:0.1em;background-color:#EEE;}
<tr class="impl-no"><td>quotes</td><td>May be dropped from CSS2, fairly useless for inline context</td></tr> <tr class="impl-no"><td>quotes</td><td>May be dropped from CSS2, fairly useless for inline context</td></tr>
<tr class="impl-no"><td>visibility</td><td>ENUM(visible, hidden, collapse), <tr class="impl-no"><td>visibility</td><td>ENUM(visible, hidden, collapse),
Dangerous</td></tr> Dangerous</td></tr>
<tr class="css1 feature"><td>white-space</td><td>ENUM(normal, pre, nowrap, pre-wrap, <tr class="css1 feature impl-partial"><td>white-space</td><td>ENUM(normal, pre, nowrap, pre-wrap,
pre-line), Spotty implementation: pre-line), Spotty implementation:
pre (no IE 5/6), nowrap (no IE 5), pre (no IE 5/6), <em>nowrap</em> (no IE 5, supported),
pre-wrap (only Opera), pre-line (no support). Fixable? Unknown target milestone.</td></tr> pre-wrap (only Opera), pre-line (no support). Fixable? Unknown target milestone.</td></tr>
</tbody> </tbody>
@ -281,8 +281,8 @@ Mozilla on inside and needs -moz-outline, no IE support.</td></tr>
<tr class="impl-yes"><td>lang</td><td>*</td><td>Copy value to xml:lang</td></tr> <tr class="impl-yes"><td>lang</td><td>*</td><td>Copy value to xml:lang</td></tr>
<tr class="impl-yes"><td rowspan="2">name</td><td>IMG</td><td>Turn into ID</td></tr> <tr class="impl-yes"><td rowspan="2">name</td><td>IMG</td><td>Turn into ID</td></tr>
<tr class="impl-yes"><td>A</td><td>Turn into ID</td></tr> <tr class="impl-yes"><td>A</td><td>Turn into ID</td></tr>
<tr><td>noshade</td><td>HR</td><td>Boolean, style 'border-style:solid;'</td></tr> <tr class="impl-yes"><td>noshade</td><td>HR</td><td>Boolean, style 'border-style:solid;'</td></tr>
<tr><td>nowrap</td><td>TD, TH</td><td>Boolean, style 'white-space:nowrap;' (not compat with IE5)</td></tr> <tr class="impl-yes"><td>nowrap</td><td>TD, TH</td><td>Boolean, style 'white-space:nowrap;' (not compat with IE5)</td></tr>
<tr class="impl-yes"><td>size</td><td>HR</td><td>Near-equiv 'height', needs px suffix if original was pixels</td></tr> <tr class="impl-yes"><td>size</td><td>HR</td><td>Near-equiv 'height', needs px suffix if original was pixels</td></tr>
<tr class="required impl-yes"><td>src</td><td>IMG</td><td>Required, insert blank or default img if not set</td></tr> <tr class="required impl-yes"><td>src</td><td>IMG</td><td>Required, insert blank or default img if not set</td></tr>
<tr class="impl-yes"><td>start</td><td>OL</td><td>Poorly supported 'counter-reset', allowed in loose, dropped in strict</td></tr> <tr class="impl-yes"><td>start</td><td>OL</td><td>Poorly supported 'counter-reset', allowed in loose, dropped in strict</td></tr>

View File

@ -0,0 +1,42 @@
<?php
require_once 'HTMLPurifier/AttrTransform.php';
/**
* Pre-transform that changes converts a boolean attribute to fixed CSS
*/
class HTMLPurifier_AttrTransform_BoolToCSS
extends HTMLPurifier_AttrTransform {
/**
* Name of boolean attribute that is trigger
*/
var $attr;
/**
* CSS declarations to add to style, needs trailing semicolon
*/
var $css;
/**
* @param $attr string attribute name to convert from
* @param $css string CSS declarations to add to style (needs semicolon)
*/
function HTMLPurifier_AttrTransform_BoolToCSS($attr, $css) {
$this->attr = $attr;
$this->css = $css;
}
function transform($attr, $config, &$context) {
if (!isset($attr[$this->attr])) return $attr;
unset($attr[$this->attr]);
if (!isset($attr['style'])) $attr['style'] = '';
$attr['style'] = $this->css . $attr['style'];
return $attr;
}
}
?>

View File

@ -206,6 +206,9 @@ class HTMLPurifier_CSSDefinition
new HTMLPurifier_AttrDef_CSS_Percentage() new HTMLPurifier_AttrDef_CSS_Percentage()
)); ));
// partial support
$this->info['white-space'] = new HTMLPurifier_AttrDef_Enum(array('nowrap'));
} }
} }

View File

@ -9,6 +9,7 @@ require_once 'HTMLPurifier/TagTransform/Font.php';
require_once 'HTMLPurifier/AttrTransform/Lang.php'; require_once 'HTMLPurifier/AttrTransform/Lang.php';
require_once 'HTMLPurifier/AttrTransform/TextAlign.php'; require_once 'HTMLPurifier/AttrTransform/TextAlign.php';
require_once 'HTMLPurifier/AttrTransform/BgColor.php'; require_once 'HTMLPurifier/AttrTransform/BgColor.php';
require_once 'HTMLPurifier/AttrTransform/BoolToCSS.php';
require_once 'HTMLPurifier/AttrTransform/Border.php'; require_once 'HTMLPurifier/AttrTransform/Border.php';
require_once 'HTMLPurifier/AttrTransform/Name.php'; require_once 'HTMLPurifier/AttrTransform/Name.php';
require_once 'HTMLPurifier/AttrTransform/Length.php'; require_once 'HTMLPurifier/AttrTransform/Length.php';
@ -92,6 +93,8 @@ class HTMLPurifier_HTMLModule_TransformToStrict extends HTMLPurifier_HTMLModule
$this->info['td']->attr_transform_pre['width'] = $this->info['td']->attr_transform_pre['width'] =
$this->info['th']->attr_transform_pre['width'] = $this->info['th']->attr_transform_pre['width'] =
$this->info['hr']->attr_transform_pre['width'] = new HTMLPurifier_AttrTransform_Length('width'); $this->info['hr']->attr_transform_pre['width'] = new HTMLPurifier_AttrTransform_Length('width');
$this->info['td']->attr_transform_pre['nowrap'] =
$this->info['th']->attr_transform_pre['nowrap'] = new HTMLPurifier_AttrTransform_BoolToCSS('nowrap', 'white-space:nowrap;');
$this->info['td']->attr_transform_pre['height'] = $this->info['td']->attr_transform_pre['height'] =
$this->info['th']->attr_transform_pre['height'] = new HTMLPurifier_AttrTransform_Length('height'); $this->info['th']->attr_transform_pre['height'] = new HTMLPurifier_AttrTransform_Length('height');
@ -100,6 +103,7 @@ class HTMLPurifier_HTMLModule_TransformToStrict extends HTMLPurifier_HTMLModule
$this->info['img']->attr_transform_pre['vspace'] = new HTMLPurifier_AttrTransform_ImgSpace('vspace'); $this->info['img']->attr_transform_pre['vspace'] = new HTMLPurifier_AttrTransform_ImgSpace('vspace');
$this->info['hr']->attr_transform_pre['size'] = new HTMLPurifier_AttrTransform_Length('size', 'height'); $this->info['hr']->attr_transform_pre['size'] = new HTMLPurifier_AttrTransform_Length('size', 'height');
$this->info['hr']->attr_transform_pre['noshade'] = new HTMLPurifier_AttrTransform_BoolToCSS('noshade', 'border-style:solid;');
} }

View File

@ -0,0 +1,39 @@
<?php
require_once 'HTMLPurifier/AttrTransform/BoolToCSS.php';
require_once 'HTMLPurifier/AttrTransformHarness.php';
class HTMLPurifier_AttrTransform_BoolToCSSTest extends HTMLPurifier_AttrTransformHarness
{
function setUp() {
parent::setUp();
$this->obj = new HTMLPurifier_AttrTransform_BoolToCSS('foo', 'bar:3in;');
}
function test() {
$this->assertResult( array() );
$this->assertResult(
array('foo' => 'foo'),
array('style' => 'bar:3in;')
);
// boolean attribute just has to be set: we don't care about
// anything else
$this->assertResult(
array('foo' => 'no'),
array('style' => 'bar:3in;')
);
$this->assertResult(
array('foo' => 'foo', 'style' => 'background-color:#F00;'),
array('style' => 'bar:3in;background-color:#F00;')
);
}
}
?>

View File

@ -220,6 +220,16 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends
'<hr size="3" />', '<hr size="3" />',
'<hr style="height:3px;" />' '<hr style="height:3px;" />'
); );
$this->assertResult(
'<hr noshade />',
'<hr style="border-style:solid;" />'
);
// td boolean transformation
$this->assertResult(
'<td nowrap />',
'<td style="white-space:nowrap;" />'
);
} }

View File

@ -37,6 +37,7 @@ $test_files[] = 'AttrDef/URITest.php';
$test_files[] = 'AttrDefTest.php'; $test_files[] = 'AttrDefTest.php';
$test_files[] = 'AttrTransform/BdoDirTest.php'; $test_files[] = 'AttrTransform/BdoDirTest.php';
$test_files[] = 'AttrTransform/BgColorTest.php'; $test_files[] = 'AttrTransform/BgColorTest.php';
$test_files[] = 'AttrTransform/BoolToCSSTest.php';
$test_files[] = 'AttrTransform/BorderTest.php'; $test_files[] = 'AttrTransform/BorderTest.php';
$test_files[] = 'AttrTransform/ImgRequiredTest.php'; $test_files[] = 'AttrTransform/ImgRequiredTest.php';
$test_files[] = 'AttrTransform/ImgSpaceTest.php'; $test_files[] = 'AttrTransform/ImgSpaceTest.php';