1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-07-30 19:00:10 +02:00

[3.1.0] Define *.vtest test hierarchy, and continue work on validator.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1625 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2008-03-23 01:06:35 +00:00
parent 848795d4a0
commit aedfbd1e93
21 changed files with 195 additions and 110 deletions

View File

@@ -22,14 +22,20 @@ class HTMLPurifier_ConfigSchema_Interchange
* Adds a namespace array to $namespaces
*/
public function addNamespace($namespace) {
$this->namespaces[$namespace->namespace] = $namespace;
if (isset($this->namespaces[$i = $namespace->namespace])) {
throw new HTMLPurifier_ConfigSchema_Exception("Cannot redefine namespace '$i'");
}
$this->namespaces[$i] = $namespace;
}
/**
* Adds a directive array to $directives
*/
public function addDirective($directive) {
$this->directives[$directive->id->__toString()] = $directive;
if (isset($this->directives[$i = $directive->id->__toString()])) {
throw new HTMLPurifier_ConfigSchema_Exception("Cannot redefine directive '$i'");
}
$this->directives[$i] = $directive;
}
}

View File

@@ -15,6 +15,12 @@ class HTMLPurifier_ConfigSchema_InterchangeBuilder
* @param $hash HTMLPurifier_ConfigSchema_StringHash source data
*/
public function build($interchange, $hash) {
if (!$hash instanceof HTMLPurifier_StringHash) {
$hash = new HTMLPurifier_StringHash($hash);
}
if (!isset($hash['ID'])) {
throw new HTMLPurifier_ConfigSchema_Exception('Hash does not have any ID');
}
if (strpos($hash['ID'], '.') === false) {
$this->buildNamespace($interchange, $hash);
} else {
@@ -26,7 +32,9 @@ class HTMLPurifier_ConfigSchema_InterchangeBuilder
public function buildNamespace($interchange, $hash) {
$namespace = new HTMLPurifier_ConfigSchema_Interchange_Namespace();
$namespace->namespace = $hash->offsetGet('ID');
$namespace->description = $hash->offsetGet('DESCRIPTION');
if (isset($hash['DESCRIPTION'])) {
$namespace->description = $hash->offsetGet('DESCRIPTION');
}
$interchange->addNamespace($namespace);
}
@@ -35,33 +43,43 @@ class HTMLPurifier_ConfigSchema_InterchangeBuilder
// These are required elements:
$directive->id = $this->id($hash->offsetGet('ID'));
$type = explode('/', $hash->offsetGet('TYPE'));
if (isset($type[1])) $directive->typeAllowsNull = true;
$directive->type = $type[0];
$directive->description = $directive->offsetGet('DESCRIPTION');
// These are extras:
if (isset($directive['ALLOWED'])) {
$directive->allowed = $this->lookup($this->evalArray($directive->offsetGet('ALLOWED')));
if (isset($hash['TYPE'])) {
$type = explode('/', $hash->offsetGet('TYPE'));
if (isset($type[1])) $directive->typeAllowsNull = true;
$directive->type = $type[0];
}
if (isset($directive['VALUE-ALIASES'])) {
$directive->valueAliases = $this->evalArray($directive->offsetGet('VALUE-ALIASES'));
if (isset($hash['DESCRIPTION'])) {
$directive->description = $hash->offsetGet('DESCRIPTION');
}
if (isset($directive['ALIASES'])) {
if (isset($hash['ALLOWED'])) {
$directive->allowed = $this->lookup($this->evalArray($hash->offsetGet('ALLOWED')));
}
if (isset($hash['VALUE-ALIASES'])) {
$directive->valueAliases = $this->evalArray($hash->offsetGet('VALUE-ALIASES'));
}
if (isset($hash['ALIASES'])) {
$raw_aliases = trim($hash->offsetGet('ALIASES'));
$aliases = preg_split('/\s*,\s*/', $raw_aliases);
foreach ($aliases as $alias) {
$this->aliases[] = $this->id($alias);
$directive->aliases[] = $this->id($alias);
}
}
if (isset($directive['VERSION'])) {
$directive->version = $directive->offsetGet('VERSION');
if (isset($hash['VERSION'])) {
$directive->version = $hash->offsetGet('VERSION');
}
if (isset($directive['DEPRECATED-USE'])) {
$directive->deprecatedUse = $this->id($directive->offsetGet('DEPRECATED-USE'));
if (isset($hash['DEPRECATED-USE'])) {
$directive->deprecatedUse = $this->id($hash->offsetGet('DEPRECATED-USE'));
}
if (isset($directive['DEPRECATED-VERSION'])) {
$directive->deprecatedVersion = $directive->offsetGet('DEPRECATED-VERSION');
if (isset($hash['DEPRECATED-VERSION'])) {
$directive->deprecatedVersion = $hash->offsetGet('DEPRECATED-VERSION');
}
$interchange->addDirective($directive);

View File

@@ -11,7 +11,7 @@ class HTMLPurifier_ConfigSchema_Validator
/**
* Volatile context variables to provide a fluent interface.
*/
protected $context, $obj, $member;
protected $context = array(), $obj, $member;
/**
* Validates a fully-formed interchange object. Throws an
@@ -28,38 +28,53 @@ class HTMLPurifier_ConfigSchema_Validator
}
public function validateNamespace($n) {
$this->context = "namespace '{$n->namespace}'";
$this->context[] = "namespace '{$n->namespace}'";
$this->with($n, 'namespace')
->assertNotEmpty()
->assertAlnum();
$this->with($n, 'description')
->assertIsString()
->assertNotEmpty();
->assertNotEmpty()
->assertIsString(); // technically redundant
array_pop($this->context);
}
public function validateDirective($d) {
$this->context = "directive '{$d->id}'";
$this->context[] = "directive '{$d->id}'";
$this->validateId($d->id);
$this->with($d, 'description')
->assertNotEmpty();
if (!isset(HTMLPurifier_VarParser::$types[$d->type])) {
$this->error('type', 'is invalid');
}
array_pop($this->context);
}
public function validateId($id) {
$this->context = "id '$id'";
$this->context[] = "id '$id'";
if (!isset($this->interchange->namespaces[$id->namespace])) {
$this->error('namespace', 'does not exist');
}
$this->with($id, 'namespace')
->assertNotEmpty()
->assertAlnum();
$this->with($id, 'directive')
->assertNotEmpty()
->assertAlnum();
array_pop($this->context);
}
// protected helper functions
protected function with($obj, $member) {
return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->context, $obj, $member);
return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->getFormattedContext(), $obj, $member);
}
protected function error($msg) {
throw new HTMLPurifier_ConfigSchema_Exception($msg . ' in ' . $this->context);
protected function error($target, $msg) {
throw new HTMLPurifier_ConfigSchema_Exception(ucfirst($target) . ' in ' . $this->getFormattedContext() . ' ' . $msg);
}
protected function getFormattedContext() {
return implode(' in ', array_reverse($this->context));
}
}

View File

@@ -40,7 +40,7 @@ class HTMLPurifier_ConfigSchema_ValidatorAtom
}
protected function error($msg) {
throw new HTMLPurifier_ConfigSchema_Exception('Member variable \'' . $this->member . '\' in ' . $this->context . ' ' . $msg);
throw new HTMLPurifier_ConfigSchema_Exception(ucfirst($this->member) . ' in ' . $this->context . ' ' . $msg);
}
}