From f3646a3a0692e0cb204c59ad65f4428f23577944 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sat, 21 Oct 2006 17:27:51 +0000 Subject: [PATCH] [1.2.0] - Add context parameter to AttrTransform objects. - Update documentation on attribute transformations in ValidateAttributes.php git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@493 48356398-32a2-884e-a903-53898d9a118a --- library/HTMLPurifier/AttrTransform.php | 3 ++- library/HTMLPurifier/AttrTransform/BdoDir.php | 2 +- .../HTMLPurifier/AttrTransform/ImgRequired.php | 2 +- library/HTMLPurifier/AttrTransform/Lang.php | 2 +- library/HTMLPurifier/AttrTransform/TextAlign.php | 2 +- .../HTMLPurifier/Strategy/ValidateAttributes.php | 16 +++++++++------- tests/HTMLPurifier/AttrTransformHarness.php | 9 +++++++-- 7 files changed, 22 insertions(+), 14 deletions(-) diff --git a/library/HTMLPurifier/AttrTransform.php b/library/HTMLPurifier/AttrTransform.php index 0b68df68..7edde559 100644 --- a/library/HTMLPurifier/AttrTransform.php +++ b/library/HTMLPurifier/AttrTransform.php @@ -23,9 +23,10 @@ class HTMLPurifier_AttrTransform * @param $attr Assoc array of attributes, usually from * HTMLPurifier_Token_Tag::$attributes * @param $config Mandatory HTMLPurifier_Config object. + * @param $context Mandatory HTMLPurifier_Context object * @returns Processed attribute array. */ - function transform($attr, $config) { + function transform($attr, $config, $context) { trigger_error('Cannot call abstract function', E_USER_ERROR); } } diff --git a/library/HTMLPurifier/AttrTransform/BdoDir.php b/library/HTMLPurifier/AttrTransform/BdoDir.php index a346bb18..dd20f26a 100644 --- a/library/HTMLPurifier/AttrTransform/BdoDir.php +++ b/library/HTMLPurifier/AttrTransform/BdoDir.php @@ -20,7 +20,7 @@ HTMLPurifier_ConfigSchema::defineAllowedValues( class HTMLPurifier_AttrTransform_BdoDir extends HTMLPurifier_AttrTransform { - function transform($attr, $config) { + function transform($attr, $config, $context) { if (isset($attr['dir'])) return $attr; $attr['dir'] = $config->get('Attr', 'DefaultTextDir'); return $attr; diff --git a/library/HTMLPurifier/AttrTransform/ImgRequired.php b/library/HTMLPurifier/AttrTransform/ImgRequired.php index 8bb6fc3f..c943d696 100644 --- a/library/HTMLPurifier/AttrTransform/ImgRequired.php +++ b/library/HTMLPurifier/AttrTransform/ImgRequired.php @@ -25,7 +25,7 @@ HTMLPurifier_ConfigSchema::define( class HTMLPurifier_AttrTransform_ImgRequired extends HTMLPurifier_AttrTransform { - function transform($attr, $config) { + function transform($attr, $config, $context) { $src = true; if (!isset($attr['src'])) { diff --git a/library/HTMLPurifier/AttrTransform/Lang.php b/library/HTMLPurifier/AttrTransform/Lang.php index 384b7a7f..97fd8064 100644 --- a/library/HTMLPurifier/AttrTransform/Lang.php +++ b/library/HTMLPurifier/AttrTransform/Lang.php @@ -10,7 +10,7 @@ require_once 'HTMLPurifier/AttrTransform.php'; class HTMLPurifier_AttrTransform_Lang extends HTMLPurifier_AttrTransform { - function transform($attr, $config) { + function transform($attr, $config, $context) { $lang = isset($attr['lang']) ? $attr['lang'] : false; $xml_lang = isset($attr['xml:lang']) ? $attr['xml:lang'] : false; diff --git a/library/HTMLPurifier/AttrTransform/TextAlign.php b/library/HTMLPurifier/AttrTransform/TextAlign.php index 5e04c627..e42354a0 100644 --- a/library/HTMLPurifier/AttrTransform/TextAlign.php +++ b/library/HTMLPurifier/AttrTransform/TextAlign.php @@ -8,7 +8,7 @@ require_once 'HTMLPurifier/AttrTransform.php'; class HTMLPurifier_AttrTransform_TextAlign extends HTMLPurifier_AttrTransform { - function transform($attr, $config) { + function transform($attr, $config, $context) { if (!isset($attr['align'])) return $attr; diff --git a/library/HTMLPurifier/Strategy/ValidateAttributes.php b/library/HTMLPurifier/Strategy/ValidateAttributes.php index 2aae1bd7..2924aed2 100644 --- a/library/HTMLPurifier/Strategy/ValidateAttributes.php +++ b/library/HTMLPurifier/Strategy/ValidateAttributes.php @@ -38,19 +38,17 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy $attr = $token->attributes; // do global transformations (pre) - // ex. to - // DEFINITION CALL + // nothing currently utilizes this foreach ($definition->info_attr_transform_pre as $transform) { - $attr = $transform->transform($attr, $config); + $attr = $transform->transform($attr, $config, $context); } // do local transformations only applicable to this element (pre) // ex.

to

- // DEFINITION CALL foreach ($definition->info[$token->name]->attr_transform_pre as $transform ) { - $attr = $transform->transform($attr, $config); + $attr = $transform->transform($attr, $config, $context); } // create alias to this element's attribute definition array, see @@ -106,11 +104,15 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy } // post transforms + + // ex. to foreach ($definition->info_attr_transform_post as $transform) { - $attr = $transform->transform($attr, $config); + $attr = $transform->transform($attr, $config, $context); } + + // ex. to foreach ($definition->info[$token->name]->attr_transform_post as $transform) { - $attr = $transform->transform($attr, $config); + $attr = $transform->transform($attr, $config, $context); } // commit changes diff --git a/tests/HTMLPurifier/AttrTransformHarness.php b/tests/HTMLPurifier/AttrTransformHarness.php index ceff33b7..689799c4 100644 --- a/tests/HTMLPurifier/AttrTransformHarness.php +++ b/tests/HTMLPurifier/AttrTransformHarness.php @@ -1,15 +1,20 @@ $input) { if (!isset($config[$i])) $config[$i] = $default_config; - $result = $this->transform->transform($input, $config[$i]); + if (!isset($context[$i])) $context[$i] = $default_context; + $result = $this->transform->transform($input, $config[$i], $context[$i]); if ($expect[$i] === true) $expect[$i] = $input; $this->assertEqual($expect[$i], $result, "Test $i: %s"); }