0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-08 14:58:42 +00:00

Implement ReverseAdapter. Also, debug some order of execution things in the Adapter.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1532 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-02-07 17:41:40 +00:00
parent dea117032f
commit 3ba42106ba
5 changed files with 170 additions and 19 deletions

View File

@ -33,32 +33,33 @@ class ConfigSchema_StringHashAdapter
list($ns, $directive) = explode('.', $hash['ID'], 2);
if (isset($hash['DEFAULT'], $hash['DESCRIPTION'], $hash['TYPE'])) {
if (isset($hash['TYPE'], $hash['DEFAULT'], $hash['DESCRIPTION'])) {
$type = $hash['TYPE'];
$raw_default = $hash['DEFAULT'];
$default = eval("return $raw_default;");
$description = $hash['DESCRIPTION'];
$type = $hash['TYPE'];
$schema->add($ns, $directive, $default, $type, $description);
}
if (isset($hash['VALUE-ALIASES'])) {
$raw_value_aliases = $hash['VALUE-ALIASES'];
$value_aliases = eval("return array($raw_value_aliases);");
$schema->addValueAliases($ns, $directive, $value_aliases);
}
if (isset($hash['ALLOWED'])) {
$raw_allowed = $hash['ALLOWED'];
$allowed = eval("return array($raw_allowed);");
$schema->addAllowedValues($ns, $directive, $allowed);
}
// This must be after ALLOWED
if (isset($hash['VALUE-ALIASES'])) {
$raw_value_aliases = $hash['VALUE-ALIASES'];
$value_aliases = eval("return array($raw_value_aliases);");
$schema->addValueAliases($ns, $directive, $value_aliases);
}
if (isset($hash['ALIASES'])) {
$raw_aliases = $hash['ALIASES'];
$aliases = preg_split('/\s*,\s*/', $raw_aliases);
foreach ($aliases as $alias) {
list($new_ns, $new_directive) = explode('.', $alias, 2);
$schema->addAlias($ns, $directive, $new_ns, $new_directive);
list($alias_ns, $alias_directive) = explode('.', $alias, 2);
$schema->addAlias($alias_ns, $alias_directive, $ns, $directive);
}
}

View File

@ -0,0 +1,89 @@
<?php
/**
* Converts HTMLPurifier_ConfigSchema into a StringHash which can be
* easily saved to a file.
*/
class ConfigSchema_StringHashReverseAdapter
{
protected $schema;
/**
* @param $schema Instance of HTMLPurifier_ConfigSchema to generate
* string hashes from.
*/
public function __construct($schema) {
$this->schema = $schema;
}
/**
* Retrieves a string hash from a specific ID, could be a directive
* or a namespace.
* @param $ns string namespace
* @param $directive string directive name
*/
public function get($ns, $directive = null) {
$ret = array();
if ($directive === null) {
if (!isset($this->schema->info_namespace[$ns])) {
trigger_error("Namespace '$ns' doesn't exist in schema");
return;
}
$def = $this->schema->info_namespace[$ns];
$ret['ID'] = $ns;
$ret['DESCRIPTION'] = $def->description;
return $ret;
}
if (!isset($this->schema->info[$ns][$directive])) {
trigger_error("Directive '$ns.$directive' doesn't exist in schema");
return;
}
$def = $this->schema->info[$ns][$directive];
$ret['ID'] = "$ns.$directive";
$ret['TYPE'] = $def->type;
$ret['DEFAULT'] = $this->export($this->schema->defaults[$ns][$directive]);
$ret['DESCRIPTION'] = $def->description;
if ($def->allowed !== null) {
$ret['ALLOWED'] = $this->exportLookup($def->allowed);
}
if (!empty($def->aliases)) {
$ret['VALUE-ALIASES'] = $this->exportHash($def->aliases);
}
if (!empty($def->directiveAliases)) {
$ret['ALIASES'] = implode(', ', $def->directiveAliases);
}
return $ret;
}
/**
* Exports a variable into a PHP-readable format
*/
protected function export($var) {
return var_export($var, true);
}
/**
* Exports a lookup array into the form 'key1', 'key2', ...
*/
protected function exportLookup($lookup) {
$keys = array_map(array($this, 'export'), array_keys($lookup));
return implode(', ', $keys);
}
/**
* Exports a hash into the form 'key' => 'val',\n ...
*/
protected function exportHash($hash) {
$code = $this->export($hash);
$lines = explode("\n", $code);
$ret = '';
foreach ($lines as $line) {
if ($line == 'array (') continue;
if ($line == ')') continue;
$ret .= substr($line, 2) . "\n";
}
return $ret;
}
}

View File

@ -82,8 +82,8 @@ class ConfigSchema_StringHashAdapterTest extends UnitTestCase
'ALIASES' => "Ns.Dir2, Ns2.Dir",
),
array(
array('addAlias', array('Ns', 'Dir', 'Ns', 'Dir2')),
array('addAlias', array('Ns', 'Dir', 'Ns2', 'Dir')),
array('addAlias', array('Ns', 'Dir2', 'Ns', 'Dir')),
array('addAlias', array('Ns2', 'Dir', 'Ns', 'Dir')),
)
);
}
@ -92,25 +92,25 @@ class ConfigSchema_StringHashAdapterTest extends UnitTestCase
$this->assertAdapt(
array(
'ID' => 'Ns.Dir',
'DEFAULT' => "'default' . 'bar'",
'DEFAULT' => "'val' . '1'",
'TYPE' => 'string',
'DESCRIPTION' => "Description of default.\n",
'VALUE-ALIASES' => "
'milk' => 'dairy',
'cheese' => 'dairy',
'milk' => 'val1',
'cheese' => 'val1',
",
'ALLOWED' => "'val1', 'val2'",
'ALIASES' => "Ns.Dir2, Ns2.Dir",
),
array(
array('add', array(
'Ns', 'Dir', 'defaultbar', 'string',
'Ns', 'Dir', 'val1', 'string',
"Description of default.\n"
)),
array('addValueAliases', array('Ns', 'Dir', array('milk' => 'dairy', 'cheese' => 'dairy'))),
array('addAllowedValues', array('Ns', 'Dir', array('val1', 'val2'))),
array('addAlias', array('Ns', 'Dir', 'Ns', 'Dir2')),
array('addAlias', array('Ns', 'Dir', 'Ns2', 'Dir')),
array('addValueAliases', array('Ns', 'Dir', array('milk' => 'val1', 'cheese' => 'val1'))),
array('addAlias', array('Ns', 'Dir2', 'Ns', 'Dir')),
array('addAlias', array('Ns2', 'Dir', 'Ns', 'Dir')),
)
);
}

View File

@ -0,0 +1,60 @@
<?php
class ConfigSchema_StringHashReverseAdapterTest extends UnitTestCase
{
function makeSchema() {
$schema = new HTMLPurifier_ConfigSchema();
$schema->addNamespace('Ns', 'Description of ns.');
$schema->addNamespace('Ns2', 'Description of ns2.');
$schema->add('Ns', 'Dir', 'dairy', 'string',
"Description of default.\n");
$schema->addAllowedValues('Ns', 'Dir', array('dairy', 'meat'));
$schema->addValueAliases('Ns', 'Dir', array('milk' => 'dairy', 'cheese' => 'dairy'));
$schema->addAlias('Ns', 'Dir2', 'Ns', 'Dir');
$schema->addAlias('Ns2', 'Dir', 'Ns', 'Dir');
return $schema;
}
function testNamespace() {
$adapter = new ConfigSchema_StringHashReverseAdapter($this->makeSchema());
$result = $adapter->get('Ns');
$expect = array(
'ID' => 'Ns',
'DESCRIPTION' => "Description of ns.",
);
$this->assertIdentical($result, $expect);
}
function testBadNamespace() {
$adapter = new ConfigSchema_StringHashReverseAdapter($this->makeSchema());
$this->expectError("Namespace 'BadNs' doesn't exist in schema");
$adapter->get('BadNs');
}
function testDirective() {
$adapter = new ConfigSchema_StringHashReverseAdapter($this->makeSchema());
$result = $adapter->get('Ns', 'Dir');
$expect = array(
'ID' => 'Ns.Dir',
'TYPE' => 'string',
'DEFAULT' => "'dairy'",
'DESCRIPTION' => "Description of default.\n",
'ALLOWED' => "'dairy', 'meat'",
'VALUE-ALIASES' => "'milk' => 'dairy',\n'cheese' => 'dairy',\n",
'ALIASES' => "Ns.Dir2, Ns2.Dir",
);
$this->assertIdentical($result, $expect);
}
function testBadDirective() {
$adapter = new ConfigSchema_StringHashReverseAdapter($this->makeSchema());
$this->expectError("Directive 'BadNs.BadDir' doesn't exist in schema");
$adapter->get('BadNs', 'BadDir');
}
}

View File

@ -137,5 +137,6 @@ $test_files[] = 'FSTools/FileTest.php';
// ConfigSchema auxiliary library
$test_files[] = 'ConfigSchema/StringHashAdapterTest.php';
$test_files[] = 'ConfigSchema/StringHashReverseAdapterTest.php';
$test_files[] = 'ConfigSchema/StringHashParserTest.php';
$test_files[] = 'ConfigSchema/StringHashTest.php';