0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-09 23:28:42 +00:00
- 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
This commit is contained in:
Edward Z. Yang 2006-10-21 17:27:51 +00:00
parent 29716bf8f4
commit f3646a3a06
7 changed files with 22 additions and 14 deletions

View File

@ -23,9 +23,10 @@ class HTMLPurifier_AttrTransform
* @param $attr Assoc array of attributes, usually from * @param $attr Assoc array of attributes, usually from
* HTMLPurifier_Token_Tag::$attributes * HTMLPurifier_Token_Tag::$attributes
* @param $config Mandatory HTMLPurifier_Config object. * @param $config Mandatory HTMLPurifier_Config object.
* @param $context Mandatory HTMLPurifier_Context object
* @returns Processed attribute array. * @returns Processed attribute array.
*/ */
function transform($attr, $config) { function transform($attr, $config, $context) {
trigger_error('Cannot call abstract function', E_USER_ERROR); trigger_error('Cannot call abstract function', E_USER_ERROR);
} }
} }

View File

@ -20,7 +20,7 @@ HTMLPurifier_ConfigSchema::defineAllowedValues(
class HTMLPurifier_AttrTransform_BdoDir extends HTMLPurifier_AttrTransform class HTMLPurifier_AttrTransform_BdoDir extends HTMLPurifier_AttrTransform
{ {
function transform($attr, $config) { function transform($attr, $config, $context) {
if (isset($attr['dir'])) return $attr; if (isset($attr['dir'])) return $attr;
$attr['dir'] = $config->get('Attr', 'DefaultTextDir'); $attr['dir'] = $config->get('Attr', 'DefaultTextDir');
return $attr; return $attr;

View File

@ -25,7 +25,7 @@ HTMLPurifier_ConfigSchema::define(
class HTMLPurifier_AttrTransform_ImgRequired extends HTMLPurifier_AttrTransform class HTMLPurifier_AttrTransform_ImgRequired extends HTMLPurifier_AttrTransform
{ {
function transform($attr, $config) { function transform($attr, $config, $context) {
$src = true; $src = true;
if (!isset($attr['src'])) { if (!isset($attr['src'])) {

View File

@ -10,7 +10,7 @@ require_once 'HTMLPurifier/AttrTransform.php';
class HTMLPurifier_AttrTransform_Lang extends HTMLPurifier_AttrTransform class HTMLPurifier_AttrTransform_Lang extends HTMLPurifier_AttrTransform
{ {
function transform($attr, $config) { function transform($attr, $config, $context) {
$lang = isset($attr['lang']) ? $attr['lang'] : false; $lang = isset($attr['lang']) ? $attr['lang'] : false;
$xml_lang = isset($attr['xml:lang']) ? $attr['xml:lang'] : false; $xml_lang = isset($attr['xml:lang']) ? $attr['xml:lang'] : false;

View File

@ -8,7 +8,7 @@ require_once 'HTMLPurifier/AttrTransform.php';
class HTMLPurifier_AttrTransform_TextAlign class HTMLPurifier_AttrTransform_TextAlign
extends HTMLPurifier_AttrTransform { extends HTMLPurifier_AttrTransform {
function transform($attr, $config) { function transform($attr, $config, $context) {
if (!isset($attr['align'])) return $attr; if (!isset($attr['align'])) return $attr;

View File

@ -38,19 +38,17 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy
$attr = $token->attributes; $attr = $token->attributes;
// do global transformations (pre) // do global transformations (pre)
// ex. <ELEMENT lang="fr"> to <ELEMENT lang="fr" xml:lang="fr"> // nothing currently utilizes this
// DEFINITION CALL
foreach ($definition->info_attr_transform_pre as $transform) { 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) // do local transformations only applicable to this element (pre)
// ex. <p align="right"> to <p style="text-align:right;"> // ex. <p align="right"> to <p style="text-align:right;">
// DEFINITION CALL
foreach ($definition->info[$token->name]->attr_transform_pre foreach ($definition->info[$token->name]->attr_transform_pre
as $transform as $transform
) { ) {
$attr = $transform->transform($attr, $config); $attr = $transform->transform($attr, $config, $context);
} }
// create alias to this element's attribute definition array, see // create alias to this element's attribute definition array, see
@ -106,11 +104,15 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy
} }
// post transforms // post transforms
// ex. <x lang="fr"> to <x lang="fr" xml:lang="fr">
foreach ($definition->info_attr_transform_post as $transform) { foreach ($definition->info_attr_transform_post as $transform) {
$attr = $transform->transform($attr, $config); $attr = $transform->transform($attr, $config, $context);
} }
// ex. <bdo> to <bdo dir="ltr">
foreach ($definition->info[$token->name]->attr_transform_post as $transform) { foreach ($definition->info[$token->name]->attr_transform_post as $transform) {
$attr = $transform->transform($attr, $config); $attr = $transform->transform($attr, $config, $context);
} }
// commit changes // commit changes

View File

@ -1,15 +1,20 @@
<?php <?php
// todo: change testing harness from accepting arrays to
// have one call per test
class HTMLPurifier_AttrTransformHarness extends UnitTestCase class HTMLPurifier_AttrTransformHarness extends UnitTestCase
{ {
var $transform; var $transform;
function assertTransform($inputs, $expect, $config = array()) { function assertTransform($inputs, $expect, $config = array(), $context = array()) {
$default_config = HTMLPurifier_Config::createDefault(); $default_config = HTMLPurifier_Config::createDefault();
$default_context = new HTMLPurifier_Context();
foreach ($inputs as $i => $input) { foreach ($inputs as $i => $input) {
if (!isset($config[$i])) $config[$i] = $default_config; 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; if ($expect[$i] === true) $expect[$i] = $input;
$this->assertEqual($expect[$i], $result, "Test $i: %s"); $this->assertEqual($expect[$i], $result, "Test $i: %s");
} }