From 2deb2fc2853447fe84e54f3ce6ebc5f56edb5fbe Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sat, 5 Aug 2006 02:56:57 +0000 Subject: [PATCH] Implement TextAlign and all hooks necessary, but it's kind of useless since CSS validation hasn't been built yet. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@166 48356398-32a2-884e-a903-53898d9a118a --- .../HTMLPurifier/AttrTransform/TextAlign.php | 33 +++++++++++ library/HTMLPurifier/Definition.php | 16 +++++- .../Strategy/ValidateAttributes.php | 11 +++- .../AttrTransform/TextAlignTest.php | 55 +++++++++++++++++++ .../Strategy/ValidateAttributesTest.php | 6 +- tests/index.php | 1 + 6 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 library/HTMLPurifier/AttrTransform/TextAlign.php create mode 100644 tests/HTMLPurifier/AttrTransform/TextAlignTest.php diff --git a/library/HTMLPurifier/AttrTransform/TextAlign.php b/library/HTMLPurifier/AttrTransform/TextAlign.php new file mode 100644 index 00000000..e97983d7 --- /dev/null +++ b/library/HTMLPurifier/AttrTransform/TextAlign.php @@ -0,0 +1,33 @@ + 1, + 'right' => 1, + 'center' => 1, + 'justify' => 1); + + if (!isset($values[$align])) { + return $attr; + } + + $attr['style'] = isset($attr['style']) ? $attr['style'] : ''; + $attr['style'] = "text-align:$align;" . $attr['style']; + + return $attr; + + } + +} + +?> \ No newline at end of file diff --git a/library/HTMLPurifier/Definition.php b/library/HTMLPurifier/Definition.php index 2ed84ab7..e99e5c67 100644 --- a/library/HTMLPurifier/Definition.php +++ b/library/HTMLPurifier/Definition.php @@ -5,6 +5,10 @@ require_once 'HTMLPurifier/AttrDef.php'; require_once 'HTMLPurifier/AttrDef/ID.php'; require_once 'HTMLPurifier/AttrDef/Class.php'; require_once 'HTMLPurifier/AttrDef/Text.php'; + require_once 'HTMLPurifier/AttrDef/Lang.php'; +require_once 'HTMLPurifier/AttrTransform.php'; + require_once 'HTMLPurifier/AttrTransform/Lang.php'; + require_once 'HTMLPurifier/AttrTransform/TextAlign.php'; require_once 'HTMLPurifier/ChildDef.php'; require_once 'HTMLPurifier/Generator.php'; require_once 'HTMLPurifier/Token.php'; @@ -286,7 +290,16 @@ class HTMLPurifier_Definition // or we can just create another info ////////////////////////////////////////////////////////////////////// - // UNIMP : info[]->attr_transform : attribute transformations in elements + // info[]->attr_transform : attribute transformations in elements + + $transform = new HTMLPurifier_AttrTransform_TextAlign(); + $this->info['h1']->attr_transform[] = + $this->info['h2']->attr_transform[] = + $this->info['h3']->attr_transform[] = + $this->info['h4']->attr_transform[] = + $this->info['h5']->attr_transform[] = + $this->info['h6']->attr_transform[] = + $this->info['p'] ->attr_transform[] = $transform; ////////////////////////////////////////////////////////////////////// // info_attr_transform : global attribute transformation that is @@ -303,6 +316,7 @@ class HTMLPurifier_ElementDef { var $attr = array(); + var $attr_transform = array(); var $auto_close = array(); var $child; var $type = 'unknown'; diff --git a/library/HTMLPurifier/Strategy/ValidateAttributes.php b/library/HTMLPurifier/Strategy/ValidateAttributes.php index 140fa8db..4a80cb2c 100644 --- a/library/HTMLPurifier/Strategy/ValidateAttributes.php +++ b/library/HTMLPurifier/Strategy/ValidateAttributes.php @@ -33,9 +33,16 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy $attr = $token->attributes; + // do global transformations // DEFINITION CALL - foreach ($this->definition->info_attr_transform as $transformer) { - $attr = $transformer->transform($attr); + foreach ($this->definition->info_attr_transform as $transform) { + $attr = $transform->transform($attr); + } + + // do local transformations + // DEFINITION CALL + foreach ($this->definition->info[$token->name]->attr_transform as $transform) { + $attr = $transform->transform($attr); } foreach ($attr as $attr_key => $value) { diff --git a/tests/HTMLPurifier/AttrTransform/TextAlignTest.php b/tests/HTMLPurifier/AttrTransform/TextAlignTest.php new file mode 100644 index 00000000..441abd65 --- /dev/null +++ b/tests/HTMLPurifier/AttrTransform/TextAlignTest.php @@ -0,0 +1,55 @@ +transform = new HTMLPurifier_AttrTransform_TextAlign(); + + $inputs = array(); + $expect = array(); + + // leave empty arrays alone + $inputs[0] = array(); + $expect[0] = true; + + // leave arrays without interesting stuff alone + $inputs[1] = array('style' => 'font-weight:bold;'); + $expect[1] = true; + + // test each of the conversions + + $inputs[2] = array('align' => 'left'); + $expect[2] = array('style' => 'text-align:left;'); + + $inputs[3] = array('align' => 'right'); + $expect[3] = array('style' => 'text-align:right;'); + + $inputs[4] = array('align' => 'center'); + $expect[4] = array('style' => 'text-align:center;'); + + $inputs[5] = array('align' => 'justify'); + $expect[5] = array('style' => 'text-align:justify;'); + + // drop garbage value + $inputs[6] = array('align' => 'invalid'); + $expect[6] = array(); + + // test CSS munging + $inputs[7] = array('align' => 'left', 'style' => 'font-weight:bold;'); + $expect[7] = array('style' => 'text-align:left;font-weight:bold;'); + + // test case insensitivity + $inputs[4] = array('align' => 'CENTER'); + $expect[4] = array('style' => 'text-align:center;'); + + $this->assertTransform($inputs, $expect); + + } + +} + +?> \ No newline at end of file diff --git a/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php b/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php index 078cde7a..ec32ae5e 100644 --- a/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php +++ b/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php @@ -59,10 +59,14 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends $inputs[10] = 'PHP'; $expect[10] = $inputs[10]; - // test lang (NEEDS CORRECTION!) + // test lang $inputs[11] = 'La soupe.'; $expect[11] = 'La soupe.'; + // test align (won't work till CSS validation is fixed) + // $inputs[12] = '

Centered Headline

'; + // $expect[12] = '

Centered Headline

'; + $this->assertStrategyWorks($strategy, $inputs, $expect, $config); } diff --git a/tests/index.php b/tests/index.php index eb27e454..6b5580cb 100644 --- a/tests/index.php +++ b/tests/index.php @@ -46,6 +46,7 @@ $test_files[] = 'AttrDef/LangTest.php'; $test_files[] = 'IDAccumulatorTest.php'; $test_files[] = 'TagTransformTest.php'; $test_files[] = 'AttrTransform/LangTest.php'; +$test_files[] = 'AttrTransform/TextAlignTest.php'; $test_file_lookup = array_flip($test_files);