diff --git a/NEWS b/NEWS index d9d33f08..bd07f290 100644 --- a/NEWS +++ b/NEWS @@ -73,6 +73,7 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier . HTMLPurifier_ConfigSchema->validate() deprecated in favor of HTMLPurifier_VarParser->parse() . Integers auto-cast into float type by VarParser. +. HTMLPURIFIER_STRICT 3.0.0, released 2008-01-06 # HTML Purifier is PHP 5 only! The 2.1.x branch will be maintained diff --git a/library/HTMLPurifier.includes.php b/library/HTMLPurifier.includes.php index 88ad66d3..4a496de8 100644 --- a/library/HTMLPurifier.includes.php +++ b/library/HTMLPurifier.includes.php @@ -127,22 +127,14 @@ require 'HTMLPurifier/ConfigDef/DirectiveAlias.php'; require 'HTMLPurifier/ConfigDef/Namespace.php'; require 'HTMLPurifier/ConfigSchema/Exception.php'; require 'HTMLPurifier/ConfigSchema/Interchange.php'; +require 'HTMLPurifier/ConfigSchema/InterchangeBuilder.php'; require 'HTMLPurifier/ConfigSchema/InterchangeValidator.php'; require 'HTMLPurifier/ConfigSchema/StringHash.php'; -require 'HTMLPurifier/ConfigSchema/StringHashAdapter.php'; require 'HTMLPurifier/ConfigSchema/StringHashParser.php'; -require 'HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php'; -require 'HTMLPurifier/ConfigSchema/Validator.php'; -require 'HTMLPurifier/ConfigSchema/Validator/Alnum.php'; -require 'HTMLPurifier/ConfigSchema/Validator/Composite.php'; -require 'HTMLPurifier/ConfigSchema/Validator/Exists.php'; -require 'HTMLPurifier/ConfigSchema/Validator/If.php'; -require 'HTMLPurifier/ConfigSchema/Validator/NamespaceExists.php'; -require 'HTMLPurifier/ConfigSchema/Validator/Or.php'; -require 'HTMLPurifier/ConfigSchema/Validator/ParseDefault.php'; -require 'HTMLPurifier/ConfigSchema/Validator/ParseId.php'; -require 'HTMLPurifier/ConfigSchema/Validator/ParseType.php'; -require 'HTMLPurifier/ConfigSchema/Validator/Unique.php'; +require 'HTMLPurifier/ConfigSchema/Builder/ConfigSchema.php'; +require 'HTMLPurifier/ConfigSchema/Interchange/Directive.php'; +require 'HTMLPurifier/ConfigSchema/Interchange/Id.php'; +require 'HTMLPurifier/ConfigSchema/Interchange/Namespace.php'; require 'HTMLPurifier/DefinitionCache/Decorator.php'; require 'HTMLPurifier/DefinitionCache/Null.php'; require 'HTMLPurifier/DefinitionCache/Serializer.php'; diff --git a/library/HTMLPurifier/ConfigDef/Directive.php b/library/HTMLPurifier/ConfigDef/Directive.php index 65ea4740..f14563c5 100644 --- a/library/HTMLPurifier/ConfigDef/Directive.php +++ b/library/HTMLPurifier/ConfigDef/Directive.php @@ -11,13 +11,11 @@ class HTMLPurifier_ConfigDef_Directive extends HTMLPurifier_ConfigDef public function __construct( $type = null, - $description = null, $allow_null = null, $allowed = null, $aliases = null ) { if ( $type !== null) $this->type = $type; - if ($description !== null) $this->description = $description; if ( $allow_null !== null) $this->allow_null = $allow_null; if ( $allowed !== null) $this->allowed = $allowed; if ( $aliases !== null) $this->aliases = $aliases; @@ -37,11 +35,6 @@ class HTMLPurifier_ConfigDef_Directive extends HTMLPurifier_ConfigDef */ public $type = 'mixed'; - /** - * Plaintext description of the configuration entity is. - */ - public $description = null; - /** * Is null allowed? Has no effect for mixed type. * @bool diff --git a/library/HTMLPurifier/ConfigDef/Namespace.php b/library/HTMLPurifier/ConfigDef/Namespace.php index 9a2c5c55..f282065b 100644 --- a/library/HTMLPurifier/ConfigDef/Namespace.php +++ b/library/HTMLPurifier/ConfigDef/Namespace.php @@ -3,18 +3,8 @@ /** * Structure object describing of a namespace */ -class HTMLPurifier_ConfigDef_Namespace extends HTMLPurifier_ConfigDef { - - public function HTMLPurifier_ConfigDef_Namespace($description = null) { - $this->description = $description; - } - +class HTMLPurifier_ConfigDef_Namespace extends HTMLPurifier_ConfigDef +{ public $class = 'namespace'; - - /** - * String description of what kinds of directives go in this namespace. - */ - public $description; - } diff --git a/library/HTMLPurifier/ConfigSchema.php b/library/HTMLPurifier/ConfigSchema.php index 1f30d93e..2e742d85 100644 --- a/library/HTMLPurifier/ConfigSchema.php +++ b/library/HTMLPurifier/ConfigSchema.php @@ -1,7 +1,5 @@ 'String', - 'istring' => 'Case-insensitive string', - 'text' => 'Text', - 'itext' => 'Case-insensitive text', - 'int' => 'Integer', - 'float' => 'Float', - 'bool' => 'Boolean', - 'lookup' => 'Lookup array', - 'list' => 'Array list', - 'hash' => 'Associative array', - 'mixed' => 'Mixed' - ); - public function __construct() { $this->parser = new HTMLPurifier_VarParser_Flexible(); } @@ -73,114 +49,37 @@ class HTMLPurifier_ConfigSchema { return HTMLPurifier_ConfigSchema::$singleton; } - /** - * Throws an E_USER_NOTICE stating that a method is deprecated. - */ - private static function deprecated($method) { - trigger_error("Static HTMLPurifier_ConfigSchema::$method deprecated, use add*() method instead", E_USER_NOTICE); - } - - /** @see HTMLPurifier_ConfigSchema->set() */ - public static function define($namespace, $name, $default, $type, $description) { - HTMLPurifier_ConfigSchema::deprecated(__METHOD__); - $def =& HTMLPurifier_ConfigSchema::instance(); - $def->add($namespace, $name, $default, $type, $description); - } - /** * Defines a directive for configuration - * @warning Will fail of directive's namespace is defined + * @warning Will fail of directive's namespace is defined. + * @warning This method's signature is slightly different from the legacy + * define() static method! Beware! * @param $namespace Namespace the directive is in * @param $name Key of directive * @param $default Default value of directive * @param $type Allowed type of the directive. See * HTMLPurifier_DirectiveDef::$type for allowed values - * @param $description Description of directive for documentation + * @param $allow_null Whether or not to allow null values */ - public function add($namespace, $name, $default, $type, $description) { - // basic sanity checks - if (HTMLPURIFIER_SCHEMA_STRICT) { - if (!isset($this->info[$namespace])) { - trigger_error('Cannot define directive for undefined namespace', - E_USER_ERROR); - return; - } - if (!ctype_alnum($name)) { - trigger_error('Directive name must be alphanumeric', - E_USER_ERROR); - return; - } - if (empty($description)) { - trigger_error('Description must be non-empty', - E_USER_ERROR); - return; - } - } - - if (isset($this->info[$namespace][$name])) { - // already defined - trigger_error('Cannot redefine directive'); - return; - } else { - // needs defining - - // process modifiers (OPTIMIZE!) - $type_values = explode('/', $type, 2); - $type = $type_values[0]; - $modifier = isset($type_values[1]) ? $type_values[1] : false; - $allow_null = ($modifier === 'null'); - - if (HTMLPURIFIER_SCHEMA_STRICT) { - if (!isset($this->types[$type])) { - trigger_error('Invalid type for configuration directive', - E_USER_ERROR); - return; - } - try { - $default = $this->parser->parse($default, $type, $allow_null); - } catch (HTMLPurifier_VarParserException $e) { - trigger_error('Default value does not match directive type', - E_USER_ERROR); - return; - } - } - - $this->info[$namespace][$name] = - new HTMLPurifier_ConfigDef_Directive(); - $this->info[$namespace][$name]->type = $type; - $this->info[$namespace][$name]->allow_null = $allow_null; - $this->defaults[$namespace][$name] = $default; - } - if (!HTMLPURIFIER_SCHEMA_STRICT) return; - $this->info[$namespace][$name]->description = $description; - } - - /** @see HTMLPurifier_ConfigSchema->addNamespace() */ - public static function defineNamespace($namespace, $description) { - HTMLPurifier_ConfigSchema::deprecated(__METHOD__); - $def =& HTMLPurifier_ConfigSchema::instance(); - $def->addNamespace($namespace, $description); + public function add($namespace, $name, $default, $type, $allow_null) { + $default = $this->parser->parse($default, $type, $allow_null); + $this->info[$namespace][$name] = new HTMLPurifier_ConfigDef_Directive(); + $this->info[$namespace][$name]->type = $type; + $this->info[$namespace][$name]->allow_null = $allow_null; + $this->defaults[$namespace][$name] = $default; } /** * Defines a namespace for directives to be put into. + * @warning This is slightly different from the corresponding static + * method. * @param $namespace Namespace's name - * @param $description Description of the namespace */ - public function addNamespace($namespace, $description) { + public function addNamespace($namespace) { $this->info[$namespace] = array(); - $this->info_namespace[$namespace] = new HTMLPurifier_ConfigDef_Namespace(); - $this->info_namespace[$namespace]->description = $description; $this->defaults[$namespace] = array(); } - /** @see HTMLPurifier_ConfigSchema->addValueAliases() */ - public static function defineValueAliases($namespace, $name, $aliases) { - HTMLPurifier_ConfigSchema::deprecated(__METHOD__); - $def =& HTMLPurifier_ConfigSchema::instance(); - $def->addValueAliases($namespace, $name, $aliases); - } - /** * Defines a directive value alias. * @@ -188,83 +87,26 @@ class HTMLPurifier_ConfigSchema { * them set a directive to several values and get the same result. * @param $namespace Directive's namespace * @param $name Name of Directive - * @param $alias Name of aliased value - * @param $real Value aliased value will be converted into + * @param $aliases Hash of aliased values to the real alias */ public function addValueAliases($namespace, $name, $aliases) { - if (HTMLPURIFIER_SCHEMA_STRICT && !isset($this->info[$namespace][$name])) { - trigger_error('Cannot set value alias for non-existant directive', - E_USER_ERROR); - return; - } foreach ($aliases as $alias => $real) { - if (HTMLPURIFIER_SCHEMA_STRICT) { - if (!$this->info[$namespace][$name] !== true && - !isset($this->info[$namespace][$name]->allowed[$real]) - ) { - trigger_error('Cannot define alias to value that is not allowed', - E_USER_ERROR); - return; - } - if (isset($this->info[$namespace][$name]->allowed[$alias])) { - trigger_error('Cannot define alias over allowed value', - E_USER_ERROR); - return; - } - } $this->info[$namespace][$name]->aliases[$alias] = $real; } } - /** @see HTMLPurifier_ConfigSchema->addAllowedValues() */ - public static function defineAllowedValues($namespace, $name, $allowed_values) { - HTMLPurifier_ConfigSchema::deprecated(__METHOD__); - $def =& HTMLPurifier_ConfigSchema::instance(); - $def->addAllowedValues($namespace, $name, $allowed_values); - } - /** * Defines a set of allowed values for a directive. + * @warning This is slightly different from the corresponding static + * method definition. * @param $namespace Namespace of directive * @param $name Name of directive - * @param $allowed_values Arraylist of allowed values + * @param $allowed Lookup array of allowed values */ - public function addAllowedValues($namespace, $name, $allowed_values) { - if (HTMLPURIFIER_SCHEMA_STRICT && !isset($this->info[$namespace][$name])) { - trigger_error('Cannot define allowed values for undefined directive', - E_USER_ERROR); - return; - } + public function addAllowedValues($namespace, $name, $allowed) { $directive =& $this->info[$namespace][$name]; $type = $directive->type; - if (HTMLPURIFIER_SCHEMA_STRICT && $type != 'string' && $type != 'istring') { - trigger_error('Cannot define allowed values for directive whose type is not string', - E_USER_ERROR); - return; - } - if ($directive->allowed === true) { - $directive->allowed = array(); - } - foreach ($allowed_values as $value) { - $directive->allowed[$value] = true; - } - if ( - HTMLPURIFIER_SCHEMA_STRICT && - $this->defaults[$namespace][$name] !== null && - !isset($directive->allowed[$this->defaults[$namespace][$name]]) - ) { - trigger_error('Default value must be in allowed range of variables', - E_USER_ERROR); - $directive->allowed = true; // undo undo! - return; - } - } - - /** @see HTMLPurifier_ConfigSchema->addAlias() */ - public static function defineAlias($namespace, $name, $new_namespace, $new_name) { - HTMLPurifier_ConfigSchema::deprecated(__METHOD__); - $def =& HTMLPurifier_ConfigSchema::instance(); - $def->addAlias($namespace, $name, $new_namespace, $new_name); + $directive->allowed = $allowed; } /** @@ -275,61 +117,54 @@ class HTMLPurifier_ConfigSchema { * @param $new_name Directive that the alias will be to */ public function addAlias($namespace, $name, $new_namespace, $new_name) { - if (HTMLPURIFIER_SCHEMA_STRICT) { - if (!isset($this->info[$namespace])) { - trigger_error('Cannot define directive alias in undefined namespace', - E_USER_ERROR); - return; - } - if (!ctype_alnum($name)) { - trigger_error('Directive name must be alphanumeric', - E_USER_ERROR); - return; - } - if (isset($this->info[$namespace][$name])) { - trigger_error('Cannot define alias over directive', - E_USER_ERROR); - return; - } - if (!isset($this->info[$new_namespace][$new_name])) { - trigger_error('Cannot define alias to undefined directive', - E_USER_ERROR); - return; - } - if ($this->info[$new_namespace][$new_name]->class == 'alias') { - trigger_error('Cannot define alias to alias', - E_USER_ERROR); - return; - } - } - $this->info[$namespace][$name] = - new HTMLPurifier_ConfigDef_DirectiveAlias( - $new_namespace, $new_name); + $this->info[$namespace][$name] = new HTMLPurifier_ConfigDef_DirectiveAlias($new_namespace, $new_name); $this->info[$new_namespace][$new_name]->directiveAliases[] = "$namespace.$name"; } - /** - * Takes an absolute path and munges it into a more manageable relative path - * @todo Consider making protected - * @param $filename Filename to check - * @return string munged filename - */ - public function mungeFilename($filename) { - if (!HTMLPURIFIER_SCHEMA_STRICT) return $filename; - $offset = strrpos($filename, 'HTMLPurifier'); - $filename = substr($filename, $offset); - $filename = str_replace('\\', '/', $filename); - return $filename; + // DEPRECATED METHODS + + /** @see HTMLPurifier_ConfigSchema->set() */ + public static function define($namespace, $name, $default, $type, $description) { + HTMLPurifier_ConfigSchema::deprecated(__METHOD__); + // process modifiers (OPTIMIZE!) + $type_values = explode('/', $type, 2); + $type = $type_values[0]; + $modifier = isset($type_values[1]) ? $type_values[1] : false; + $allow_null = ($modifier === 'null'); + $def =& HTMLPurifier_ConfigSchema::instance(); + $def->add($namespace, $name, $default, $type, $allow_null); } - /** - * Checks if var is an HTMLPurifier_Error object - * @todo Consider making protected - */ - public function isError($var) { - if (!is_object($var)) return false; - if (!($var instanceof HTMLPurifier_Error)) return false; - return true; + /** @see HTMLPurifier_ConfigSchema->addNamespace() */ + public static function defineNamespace($namespace, $description) { + HTMLPurifier_ConfigSchema::deprecated(__METHOD__); + $def =& HTMLPurifier_ConfigSchema::instance(); + $def->addNamespace($namespace); + } + + /** @see HTMLPurifier_ConfigSchema->addValueAliases() */ + public static function defineValueAliases($namespace, $name, $aliases) { + HTMLPurifier_ConfigSchema::deprecated(__METHOD__); + $def =& HTMLPurifier_ConfigSchema::instance(); + $def->addValueAliases($namespace, $name, $aliases); + } + + /** @see HTMLPurifier_ConfigSchema->addAllowedValues() */ + public static function defineAllowedValues($namespace, $name, $allowed_values) { + HTMLPurifier_ConfigSchema::deprecated(__METHOD__); + $allowed = array(); + foreach ($allowed_values as $value) { + $allowed[$value] = true; + } + $def =& HTMLPurifier_ConfigSchema::instance(); + $def->addAllowedValues($namespace, $name, $allowed); + } + + /** @see HTMLPurifier_ConfigSchema->addAlias() */ + public static function defineAlias($namespace, $name, $new_namespace, $new_name) { + HTMLPurifier_ConfigSchema::deprecated(__METHOD__); + $def =& HTMLPurifier_ConfigSchema::instance(); + $def->addAlias($namespace, $name, $new_namespace, $new_name); } /** @deprecated, use HTMLPurifier_VarParser->parse() */ @@ -338,6 +173,13 @@ class HTMLPurifier_ConfigSchema { return $this->parser->parse($a, $b, $c); } + /** + * Throws an E_USER_NOTICE stating that a method is deprecated. + */ + private static function deprecated($method) { + trigger_error("Static HTMLPurifier_ConfigSchema::$method deprecated, use add*() method instead", E_USER_NOTICE); + } + } diff --git a/library/HTMLPurifier/ConfigSchema/Builder/ConfigSchema.php b/library/HTMLPurifier/ConfigSchema/Builder/ConfigSchema.php new file mode 100644 index 00000000..40cc7b72 --- /dev/null +++ b/library/HTMLPurifier/ConfigSchema/Builder/ConfigSchema.php @@ -0,0 +1,48 @@ +namespaces as $n) { + $schema->addNamespace($n->namespace); + } + foreach ($this->directives as $d) { + $schema->add( + $d->id->namespace, + $d->id->directive, + $d->default, + $d->type, + $d->typeAllowsNull + ); + if ($d->allowed !== null) { + $schema->addAllowedValues( + $d->id->namespace, + $d->id->directive, + $d->allowed + ); + } + foreach ($d->aliases as $alias) { + $schema->addAlias( + $alias->id->namespace, + $alias->id->directive, + $d->id->namespace, + $d->id->directive + ); + } + if ($d->valueAliases !== null) { + $schema->addValueAliases( + $d->id->namespace, + $d->id->directive, + $d->valueAliases + ); + } + } + } + +} diff --git a/library/HTMLPurifier/ConfigSchema/Interchange.php b/library/HTMLPurifier/ConfigSchema/Interchange.php index 58df5d70..4390b480 100644 --- a/library/HTMLPurifier/ConfigSchema/Interchange.php +++ b/library/HTMLPurifier/ConfigSchema/Interchange.php @@ -21,69 +21,15 @@ class HTMLPurifier_ConfigSchema_Interchange /** * Adds a namespace array to $namespaces */ - public function addNamespace($arr) { - $this->namespaces[$arr['ID']] = $arr; + public function addNamespace($namespace) { + $this->namespaces[$namespace->namespace] = $namespace; } /** * Adds a directive array to $directives */ - public function addDirective($arr) { - $this->directives[$arr['ID']] = $arr; - } - - /** - * Retrieves a version of this object wrapped in the validator adapter - * to be used for data-input. - */ - public function getValidatorAdapter() { - $validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this); - - // Validators should be defined in the order they are to be called. - $namespace = $validator->namespace; - $directive = $validator->directive; - - // ID tests - $validator->addValidator($this->make('Exists', 'ID')); - $validator->addValidator($this->make('Unique')); - - // ID: Namespace test - $namespace->addValidator($this->make('Alnum', 'ID')); - - // ID: Common tests - $validator->addValidator($this->make('ParseId')); - $validator->addValidator($this->make('Exists', '_NAMESPACE')); - $validator->addValidator($this->make('Alnum', '_NAMESPACE')); - - // ID: Directive tests - $directive->addValidator($this->make('Exists', '_DIRECTIVE')); - $directive->addValidator($this->make('Alnum', '_DIRECTIVE')); - $directive->addValidator($this->make('NamespaceExists')); - - // Directive: Type and Default tests - $directive->addValidator($this->make('Exists', 'TYPE')); - $directive->addValidator($this->make('ParseType')); - $directive->addValidator($this->make('Exists', '_TYPE')); - $directive->addValidator($this->make('Exists', '_NULL')); - $directive->addValidator($this->make('Exists', 'DEFAULT')); - $directive->addValidator($this->make('ParseDefault')); - - // Common tests - $validator->addValidator($this->make('Exists', 'DESCRIPTION')); - - return $validator; - } - - /** - * Creates a validator. - * @warning - * Only *one* argument is supported; multiple args shouldn't use - * this function. - */ - protected function make($name, $arg = null) { - $class = "HTMLPurifier_ConfigSchema_Validator_$name"; - if ($arg === null) return new $class(); - else return new $class($arg); + public function addDirective($directive) { + $this->directives[(string) $directive->id] = $directive; } } diff --git a/library/HTMLPurifier/ConfigSchema/Interchange/Directive.php b/library/HTMLPurifier/ConfigSchema/Interchange/Directive.php new file mode 100644 index 00000000..688939a0 --- /dev/null +++ b/library/HTMLPurifier/ConfigSchema/Interchange/Directive.php @@ -0,0 +1,70 @@ + true). + * Null if all values are allowed. + */ + public $allowed; + + /** + * List of aliases for the directive, e.g. array('Alt.Directive'). + */ + public $aliases = array(); + + /** + * Hash of value aliases, e.g. array('alt' => 'real'). Null if value + * aliasing is disabled (necessary for non-scalar types). + */ + public $valueAliases; + + /** + * Version of HTML Purifier the directive was introduced, e.g. '1.3.1'. + * Null if the directive has always existed. + */ + public $version; + + /** + * ID of directive that supercedes this old directive, is an instance + * of HTMLPurifier_ConfigSchema_Interchange_Id. Null if not deprecated. + */ + public $deprecatedUse; + + /** + * Version of HTML Purifier this directive was deprecated. Null if not + * deprecated. + */ + public $deprecatedVersion; + + +} diff --git a/library/HTMLPurifier/ConfigSchema/Interchange/Id.php b/library/HTMLPurifier/ConfigSchema/Interchange/Id.php new file mode 100644 index 00000000..bb1d6ff3 --- /dev/null +++ b/library/HTMLPurifier/ConfigSchema/Interchange/Id.php @@ -0,0 +1,25 @@ +namespace = $namespace; + $this->directive = $directive; + } + + public function __toString() { + return $this->namespace . '.' . $this->directive; + } + + public static function make($id) { + list($namespace, $directive) = explode('.', $id); + return new HTMLPurifier_ConfigSchema_Interchange_Id($namespace, $directive); + } + +} diff --git a/library/HTMLPurifier/ConfigSchema/Interchange/Namespace.php b/library/HTMLPurifier/ConfigSchema/Interchange/Namespace.php new file mode 100644 index 00000000..8fe81581 --- /dev/null +++ b/library/HTMLPurifier/ConfigSchema/Interchange/Namespace.php @@ -0,0 +1,19 @@ +varParser = $varParser ? $varParser : new HTMLPurifier_VarParser_Native(); + } + + /** + * Builds an interchange object based on a hash. + * @param $interchange HTMLPurifier_ConfigSchema_Interchange object to build + * @param $hash HTMLPurifier_ConfigSchema_StringHash source data + */ + public function build($interchange, $hash) { + if (strpos($hash['ID'], '.') === false) { + $this->buildNamespace($interchange, $hash); + } else { + $this->buildDirective($interchange, $hash); + } + $this->_findUnused($hash); + } + + public function buildNamespace($interchange, $hash) { + $namespace = new HTMLPurifier_ConfigSchema_Interchange_Namespace(); + $namespace->namespace = $hash->offsetGet('ID'); + $namespace->description = $hash->offsetGet('DESCRIPTION'); + $interchange->addNamespace($namespace); + } + + public function buildDirective($interchange, $hash) { + $directive = new HTMLPurifier_ConfigSchema_Interchange_Directive(); + + // 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($directive['VALUE-ALIASES'])) { + $directive->valueAliases = $this->evalArray($directive->offsetGet('VALUE-ALIASES')); + } + if (isset($directive['ALIASES'])) { + $raw_aliases = trim($hash->offsetGet('ALIASES')); + $aliases = preg_split('/\s*,\s*/', $raw_aliases); + foreach ($aliases as $alias) { + $this->aliases[] = $this->id($alias); + } + } + if (isset($directive['VERSION'])) { + $directive->version = $directive->offsetGet('VERSION'); + } + if (isset($directive['DEPRECATED-USE'])) { + $directive->deprecatedUse = $this->id($directive->offsetGet('DEPRECATED-USE')); + } + if (isset($directive['DEPRECATED-VERSION'])) { + $directive->deprecatedVersion = $directive->offsetGet('DEPRECATED-VERSION'); + } + + $interchange->addDirective($directive); + } + + /** + * Evaluates an array PHP code string without array() wrapper + */ + protected function evalArray($contents) { + return eval('return array('. $contents .');'); + } + + /** + * Converts an array list into a lookup array. + */ + protected function lookup($array) { + $ret = array(); + foreach ($array as $val) $ret[$val] = true; + return $ret; + } + + /** + * Convenience function that creates an HTMLPurifier_ConfigSchema_Interchange_Id + * object based on a string Id. + */ + protected function id($id) { + return HTMLPurifier_ConfigSchema_Interchange_Id::make($id); + } + + /** + * Triggers errors for any unused keys passed in the hash; such keys + * may indicate typos, missing values, etc. + * @param $hash Instance of ConfigSchema_StringHash to check. + */ + protected function _findUnused($hash) { + $accessed = $hash->getAccessed(); + foreach ($hash as $k => $v) { + if (!isset($accessed[$k])) { + trigger_error("String hash key '$k' not used by builder", E_USER_NOTICE); + } + } + } + +} + diff --git a/library/HTMLPurifier/ConfigSchema/StringHashAdapter.php b/library/HTMLPurifier/ConfigSchema/StringHashAdapter.php deleted file mode 100644 index 3a07a403..00000000 --- a/library/HTMLPurifier/ConfigSchema/StringHashAdapter.php +++ /dev/null @@ -1,89 +0,0 @@ -addNamespace($hash->offsetGet('ID'), $hash->offsetGet('DESCRIPTION')); - $this->_findUnused($hash); - return; - } - - list($ns, $directive) = explode('.', $hash->offsetGet('ID'), 2); - - if (isset($hash['TYPE'], $hash['DEFAULT'], $hash['DESCRIPTION'])) { - $type = $hash->offsetGet('TYPE'); - $raw_default = $hash->offsetGet('DEFAULT'); - $default = eval("return $raw_default;"); - $description = $hash->offsetGet('DESCRIPTION'); - $schema->add($ns, $directive, $default, $type, $description); - } - - if (isset($hash['ALLOWED'])) { - $raw_allowed = $hash->offsetGet('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->offsetGet('VALUE-ALIASES'); - $value_aliases = eval("return array($raw_value_aliases);"); - $schema->addValueAliases($ns, $directive, $value_aliases); - } - - if (isset($hash['ALIASES'])) { - $raw_aliases = trim($hash->offsetGet('ALIASES')); - $aliases = preg_split('/\s*,\s*/', $raw_aliases); - foreach ($aliases as $alias) { - list($alias_ns, $alias_directive) = explode('.', $alias, 2); - $schema->addAlias($alias_ns, $alias_directive, $ns, $directive); - } - } - - // We don't use these yet, but they're specified - if (isset($hash['VERSION'])) $hash->offsetGet('VERSION'); - if (isset($hash['DEPRECATED-USE'])) $hash->offsetGet('DEPRECATED-USE'); - if (isset($hash['DEPRECATED-VERSION'])) $hash->offsetGet('DEPRECATED-VERSION'); - - $this->_findUnused($hash); - - } - - /** - * Triggers errors for any unused keys passed in the hash; such keys - * may indicate typos, missing values, etc. - * @param $hash Instance of ConfigSchema_StringHash to check. - */ - protected function _findUnused($hash) { - $accessed = $hash->getAccessed(); - foreach ($hash as $k => $v) { - if (!isset($accessed[$k])) { - trigger_error("String hash key '$k' not used by adapter", E_USER_NOTICE); - } - } - } - -} diff --git a/library/HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php b/library/HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php deleted file mode 100644 index 2eb1fb2e..00000000 --- a/library/HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php +++ /dev/null @@ -1,127 +0,0 @@ -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]; - - if ($def instanceof HTMLPurifier_ConfigDef_DirectiveAlias) { - return false; - } - - $ret['ID'] = "$ns.$directive"; - $ret['TYPE'] = $def->type; - - // Attempt to extract version information from description. - $description = $this->normalize($def->description); - list($description, $version) = $this->extractVersion($description); - - if ($version) $ret['VERSION'] = $version; - $ret['DEFAULT'] = $this->export($this->schema->defaults[$ns][$directive]); - $ret['DESCRIPTION'] = wordwrap($description, 75, "\n"); - - if ($def->allowed !== true) { - $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 - */ - public function export($var) { - if ($var === array()) return 'array()'; // single-line format - return var_export($var, true); - } - - /** - * Exports a lookup array into the form 'key1', 'key2', ... - */ - public function exportLookup($lookup) { - if (!is_array($lookup)) return $this->export($lookup); - if (empty($lookup)) return ''; - $keys = array_map(array($this, 'export'), array_keys($lookup)); - return implode(', ', $keys); - } - - /** - * Exports a hash into the form 'key' => 'val',\n ... - */ - public function exportHash($hash) { - if (!is_array($hash)) return $this->export($hash); - if (empty($hash)) return ''; - $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; - } - - /** - * Normalizes a string to Unix style newlines - */ - protected function normalize($string) { - return str_replace(array("\r\n", "\r"), "\n", $string); - } - - public function extractVersion($description) { - $regex = '/This directive (?:has been|was) available since (\d+\.\d+\.\d+)\./'; - $regex = str_replace(' ', '\s+', $regex); // allow any number of spaces between statements - $ok = preg_match($regex, $description, $matches); - if ($ok) { - $version = $matches[1]; - } else { - $version = false; - } - $description = preg_replace($regex, '', $description, 1); - return array($description, $version); - } - -} diff --git a/library/HTMLPurifier/ConfigSchema/Validator.php b/library/HTMLPurifier/ConfigSchema/Validator.php deleted file mode 100644 index cfd8d29c..00000000 --- a/library/HTMLPurifier/ConfigSchema/Validator.php +++ /dev/null @@ -1,26 +0,0 @@ -index = $index; - } - - public function validate(&$arr, $interchange) { - if (!ctype_alnum($arr[$this->index])) { - $this->error($arr[$this->index] . ' in '. $this->index .' must be alphanumeric'); - } - } - -} diff --git a/library/HTMLPurifier/ConfigSchema/Validator/Composite.php b/library/HTMLPurifier/ConfigSchema/Validator/Composite.php deleted file mode 100644 index 95625213..00000000 --- a/library/HTMLPurifier/ConfigSchema/Validator/Composite.php +++ /dev/null @@ -1,21 +0,0 @@ -validators[] = $validator; - } - - public function validate(&$arr, $interchange) { - foreach ($this->validators as $validator) { - $validator->validate($arr, $interchange); - } - } - -} diff --git a/library/HTMLPurifier/ConfigSchema/Validator/Exists.php b/library/HTMLPurifier/ConfigSchema/Validator/Exists.php deleted file mode 100644 index ac36878d..00000000 --- a/library/HTMLPurifier/ConfigSchema/Validator/Exists.php +++ /dev/null @@ -1,21 +0,0 @@ -index = $index; - } - - public function validate(&$arr, $interchange) { - if (empty($arr[$this->index])) { - $this->error($this->index . ' must exist'); - } - } - -} diff --git a/library/HTMLPurifier/ConfigSchema/Validator/If.php b/library/HTMLPurifier/ConfigSchema/Validator/If.php deleted file mode 100644 index 470fb802..00000000 --- a/library/HTMLPurifier/ConfigSchema/Validator/If.php +++ /dev/null @@ -1,49 +0,0 @@ -setCondition($cond); - } - - /** - * @param $validator Validator to run as a condition. Exceptions thrown by it - * do not bubble up. - */ - public function setCondition($validator) { - $this->condition = $validator; - } - - /** - * @param $validator Validator to run if condition is true - */ - public function setThen($validator) { - $this->then = $validator; - } - - /** - * @param $validator Validator to run if condition is false - */ - public function setElse($validator) { - $this->else = $validator; - } - - public function validate(&$arr, $interchange) { - try { - $this->condition->validate($arr, $interchange); - } catch (HTMLPurifier_ConfigSchema_Exception $e) { - if ($this->else) $this->else->validate($arr, $interchange); - return; - } - if ($this->then) $this->then->validate($arr, $interchange); - } - -} diff --git a/library/HTMLPurifier/ConfigSchema/Validator/NamespaceExists.php b/library/HTMLPurifier/ConfigSchema/Validator/NamespaceExists.php deleted file mode 100644 index 2edf5536..00000000 --- a/library/HTMLPurifier/ConfigSchema/Validator/NamespaceExists.php +++ /dev/null @@ -1,16 +0,0 @@ -namespaces[$arr['_NAMESPACE']])) { - $this->error('Cannot define directive for undefined namespace ' . $arr['_NAMESPACE']); - } - } - -} diff --git a/library/HTMLPurifier/ConfigSchema/Validator/Or.php b/library/HTMLPurifier/ConfigSchema/Validator/Or.php deleted file mode 100644 index fc818b64..00000000 --- a/library/HTMLPurifier/ConfigSchema/Validator/Or.php +++ /dev/null @@ -1,39 +0,0 @@ -validators[] = $validator; - } - - public function validate(&$arr, $interchange) { - $exceptions = array(); - $pass = false; - foreach ($this->validators as $validator) { - try { - $validator->validate($arr, $interchange); - } catch (HTMLPurifier_ConfigSchema_Exception $e) { - $exceptions[] = $e; - continue; - } - $exceptions = array(); - break; - } - if ($exceptions) { - // I wonder how we can make the exceptions "lossless" - throw new HTMLPurifier_ConfigSchema_Exception('All validators failed: ' . implode(";\n", $exceptions)); - } - } - -} diff --git a/library/HTMLPurifier/ConfigSchema/Validator/ParseDefault.php b/library/HTMLPurifier/ConfigSchema/Validator/ParseDefault.php deleted file mode 100644 index 3f21e691..00000000 --- a/library/HTMLPurifier/ConfigSchema/Validator/ParseDefault.php +++ /dev/null @@ -1,18 +0,0 @@ -parse($arr['DEFAULT'], $arr['_TYPE'], $arr['_NULL']); - } catch (HTMLPurifier_VarParserException $e) { - throw new HTMLPurifier_ConfigSchema_Exception('Invalid type for default value in '. $arr['ID'] .': ' . $e->getMessage()); - } - } - -} diff --git a/library/HTMLPurifier/ConfigSchema/Validator/ParseId.php b/library/HTMLPurifier/ConfigSchema/Validator/ParseId.php deleted file mode 100644 index 56d6ef3f..00000000 --- a/library/HTMLPurifier/ConfigSchema/Validator/ParseId.php +++ /dev/null @@ -1,15 +0,0 @@ -error('Invalid type ' . $r[0] . ' for configuration directive ' . $arr['ID']); - } - $arr['_TYPE'] = $r[0]; - $arr['_NULL'] = (isset($r[1]) && $r[1] === 'null'); - - } - -} diff --git a/library/HTMLPurifier/ConfigSchema/Validator/Unique.php b/library/HTMLPurifier/ConfigSchema/Validator/Unique.php deleted file mode 100644 index 671b3c93..00000000 --- a/library/HTMLPurifier/ConfigSchema/Validator/Unique.php +++ /dev/null @@ -1,23 +0,0 @@ -namespaces[$arr['ID']])) { - $this->error('Cannot redefine namespace'); - } - if (isset($interchange->directives[$arr['ID']])) { - $this->error('Cannot redefine directive'); - } - } - -} diff --git a/maintenance/generate-schema-cache.php b/maintenance/generate-schema-cache.php index 2f94ae2c..10eb81ba 100644 --- a/maintenance/generate-schema-cache.php +++ b/maintenance/generate-schema-cache.php @@ -17,25 +17,15 @@ $FS = new FSTools(); $files = $FS->globr('../library/HTMLPurifier/ConfigSchema/schema', '*.txt'); -$namespaces = array(); -$directives = array(); - -// Generate string hashes -$parser = new HTMLPurifier_ConfigSchema_StringHashParser(); +$parser = new HTMLPurifier_ConfigSchema_StringHashParser(); +$builder = new HTMLPurifier_ConfigSchema_InterchangeBuilder(); +$interchange = new HTMLPurifier_ConfigSchema_Interchange(); foreach ($files as $file) { - $hash = $parser->parseFile($file); - if (strpos($hash['ID'], '.') === false) { - $namespaces[] = $hash; - } else { - $directives[] = $hash; - } + $builder->build($interchange, $parser->parseFile($file)); } -$adapter = new HTMLPurifier_ConfigSchema_StringHashAdapter(); -$schema = new HTMLPurifier_ConfigSchema(); - -foreach ($namespaces as $hash) $adapter->adapt($hash, $schema); -foreach ($directives as $hash) $adapter->adapt($hash, $schema); +$schema_builder = new HTMLPurifier_ConfigSchema_Builder_ConfigSchema(); +$schema = $schema_builder->build($interchange); echo "Saving schema... "; file_put_contents($target, serialize($schema)); diff --git a/tests/HTMLPurifier/ConfigSchema/InterchangeTest.php b/tests/HTMLPurifier/ConfigSchema/InterchangeTest.php index c0fcc7e6..c255ae47 100644 --- a/tests/HTMLPurifier/ConfigSchema/InterchangeTest.php +++ b/tests/HTMLPurifier/ConfigSchema/InterchangeTest.php @@ -10,25 +10,17 @@ class HTMLPurifier_ConfigSchema_InterchangeTest extends UnitTestCase } public function testAddNamespace() { - $this->interchange->addNamespace($v = array( - 'ID' => 'Namespace', - 'DESCRIPTION' => 'Bar', - )); + $v = new HTMLPurifier_ConfigSchema_Interchange_Namespace(); + $v->namespace = 'Namespace'; + $this->interchange->addNamespace($v); $this->assertIdentical($v, $this->interchange->namespaces['Namespace']); } public function testAddDirective() { - $this->interchange->addDirective($v = array( - 'ID' => 'Namespace.Directive', - 'DESCRIPTION' => 'Bar', - )); + $v = new HTMLPurifier_ConfigSchema_Interchange_Directive(); + $v->id = new HTMLPurifier_ConfigSchema_Interchange_Id('Namespace', 'Directive'); + $this->interchange->addDirective($v); $this->assertIdentical($v, $this->interchange->directives['Namespace.Directive']); } - public function testValidator() { - $adapter = $this->interchange->getValidatorAdapter(); - $this->expectException(new HTMLPurifier_ConfigSchema_Exception('ID must exist')); - $adapter->addDirective(array()); - } - } diff --git a/tests/HTMLPurifier/ConfigSchema/InterchangeValidatorTest.php b/tests/HTMLPurifier/ConfigSchema/InterchangeValidatorTest.php deleted file mode 100644 index 0776646a..00000000 --- a/tests/HTMLPurifier/ConfigSchema/InterchangeValidatorTest.php +++ /dev/null @@ -1,44 +0,0 @@ -mock = new HTMLPurifier_ConfigSchema_InterchangeMock(); - $this->validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this->mock); - } - - protected function makeValidator($expect_params = null) { - generate_mock_once('HTMLPurifier_ConfigSchema_Validator'); - $validator = new HTMLPurifier_ConfigSchema_ValidatorMock(); - if ($expect_params !== null) $validator->expectOnce('validate', $expect_params); - else $validator->expectNever('validate'); - return $validator; - } - - public function testAddNamespaceNullValidator() { - $hash = array('ID' => 'Namespace'); - $this->mock->expectOnce('addNamespace', array($hash)); - $this->validator->addNamespace($hash); - } - - public function testAddNamespaceWithValidators() { - $hash = array('ID' => 'Namespace'); - $this->validator->addValidator($this->makeValidator(array($hash, $this->mock))); - $this->validator->namespace->addValidator($this->makeValidator(array($hash, $this->mock))); - $this->validator->directive->addValidator($this->makeValidator()); // not called - $this->mock->expectOnce('addNamespace', array($hash)); - $this->validator->addNamespace($hash); - } - - public function testAddDirectiveWithValidators() { - $hash = array('ID' => 'Namespace.Directive'); - $this->validator->addValidator($this->makeValidator(array($hash, $this->mock))); - $this->validator->namespace->addValidator($this->makeValidator()); // not called - $this->validator->directive->addValidator($this->makeValidator(array($hash, $this->mock))); - $this->mock->expectOnce('addDirective', array($hash)); - $this->validator->addDirective($hash); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchema/StringHashAdapterTest.php b/tests/HTMLPurifier/ConfigSchema/StringHashAdapterTest.php deleted file mode 100644 index 1e7c8460..00000000 --- a/tests/HTMLPurifier/ConfigSchema/StringHashAdapterTest.php +++ /dev/null @@ -1,137 +0,0 @@ -expectAt($called[$func]++, $func, $params); - } - $adapter = new HTMLPurifier_ConfigSchema_StringHashAdapter(); - $adapter->adapt($input, $schema); - } - - function testBasic() { - $this->assertAdapt( - array( - 'ID' => 'Namespace.Directive', - 'DEFAULT' => "'default' . 'bar'", - 'TYPE' => 'string', - 'DESCRIPTION' => "Description of default.\n", - ), - array( - array('add', array( - 'Namespace', 'Directive', 'defaultbar', 'string', - "Description of default.\n" - )), - ) - ); - } - - function testNamespace() { - $this->assertAdapt( - array( - 'ID' => 'Namespace', - 'DESCRIPTION' => 'Description of namespace', - ), - array( - array('addNamespace', array('Namespace', 'Description of namespace')), - ) - ); - } - - function testValueAliases() { - $this->assertAdapt( - array( - 'ID' => 'Ns.Dir', - 'VALUE-ALIASES' => " - 'milk' => 'dairy', - 'cheese' => 'dairy', - ", - ), - array( - array('addValueAliases', array('Ns', 'Dir', array('milk' => 'dairy', 'cheese' => 'dairy'))), - ) - ); - } - - function testAllowedValues() { - $this->assertAdapt( - array( - 'ID' => 'Ns.Dir', - 'ALLOWED' => "'val1', 'val2'", - ), - array( - array('addAllowedValues', array('Ns', 'Dir', array('val1', 'val2'))), - ) - ); - } - - function testAlias() { - $this->assertAdapt( - array( - 'ID' => 'Ns.Dir', - 'ALIASES' => "Ns.Dir2, Ns2.Dir", - ), - array( - array('addAlias', array('Ns', 'Dir2', 'Ns', 'Dir')), - array('addAlias', array('Ns2', 'Dir', 'Ns', 'Dir')), - ) - ); - } - - function testCombo() { - $this->assertAdapt( - array( - 'ID' => 'Ns.Dir', - 'DEFAULT' => "'val' . '1'", - 'TYPE' => 'string', - 'DESCRIPTION' => "Description of default.\n", - 'VALUE-ALIASES' => " - 'milk' => 'val1', - 'cheese' => 'val1', - ", - 'ALLOWED' => "'val1', 'val2'", - 'ALIASES' => "Ns.Dir2, Ns2.Dir", - ), - array( - array('add', array( - 'Ns', 'Dir', 'val1', 'string', - "Description of default.\n" - )), - array('addAllowedValues', array('Ns', 'Dir', array('val1', 'val2'))), - array('addValueAliases', array('Ns', 'Dir', array('milk' => 'val1', 'cheese' => 'val1'))), - array('addAlias', array('Ns', 'Dir2', 'Ns', 'Dir')), - array('addAlias', array('Ns2', 'Dir', 'Ns', 'Dir')), - ) - ); - } - - function testMissingIdError() { - $this->expectError('Missing key ID in string hash'); - $this->assertAdapt(array()); - } - - function testExtraError() { - $this->expectError("String hash key 'FOOBAR' not used by adapter"); - $this->assertAdapt( - array( - 'ID' => 'Namespace', - 'DESCRIPTION' => 'Description of namespace', - 'FOOBAR' => 'Extra stuff', - ), - array( - array('addNamespace', array('Namespace', 'Description of namespace')), - ) - ); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchema/StringHashReverseAdapterTest.php b/tests/HTMLPurifier/ConfigSchema/StringHashReverseAdapterTest.php deleted file mode 100644 index 852e2144..00000000 --- a/tests/HTMLPurifier/ConfigSchema/StringHashReverseAdapterTest.php +++ /dev/null @@ -1,102 +0,0 @@ -addNamespace('Ns', 'Description of ns.'); - $schema->addNamespace('Ns2', 'Description of ns2.'); - $schema->add('Ns', 'Dir', 'dairy', 'string', - "Description of default.\nThis directive has been available since 1.2.0."); - $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 HTMLPurifier_ConfigSchema_StringHashReverseAdapter($this->makeSchema()); - $result = $adapter->get('Ns'); - $expect = array( - 'ID' => 'Ns', - 'DESCRIPTION' => "Description of ns.", - ); - $this->assertIdentical($result, $expect); - } - - function testBadNamespace() { - $adapter = new HTMLPurifier_ConfigSchema_StringHashReverseAdapter($this->makeSchema()); - $this->expectError("Namespace 'BadNs' doesn't exist in schema"); - $adapter->get('BadNs'); - } - - function testDirective() { - - $adapter = new HTMLPurifier_ConfigSchema_StringHashReverseAdapter($this->makeSchema()); - - $result = $adapter->get('Ns', 'Dir'); - $expect = array( - 'ID' => 'Ns.Dir', - 'TYPE' => 'string', - 'VERSION' => '1.2.0', - '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 HTMLPurifier_ConfigSchema_StringHashReverseAdapter($this->makeSchema()); - $this->expectError("Directive 'BadNs.BadDir' doesn't exist in schema"); - $adapter->get('BadNs', 'BadDir'); - } - - function assertMethod($func, $input, $expect) { - $adapter = new HTMLPurifier_ConfigSchema_StringHashReverseAdapter($this->makeSchema()); - $result = $adapter->$func($input); - $this->assertIdentical($result, $expect); - } - - function testExportEmptyHash() { - $this->assertMethod('exportHash', array(), ''); - } - - function testExportHash() { - $this->assertMethod('exportHash', array('foo' => 'bar'), "'foo' => 'bar',\n"); - } - - function testExportEmptyLookup() { - $this->assertMethod('exportLookup', array(), ''); - } - - function testExportSingleLookup() { - $this->assertMethod('exportLookup', array('key' => true), "'key'"); - } - - function testExportLookup() { - $this->assertMethod('exportLookup', array('key' => true, 'key2' => true, 3 => true), "'key', 'key2', 3"); - } - - function assertExtraction($desc, $expect_desc, $expect_version) { - $adapter = new HTMLPurifier_ConfigSchema_StringHashReverseAdapter($this->makeSchema()); - list($result_desc, $result_version) = $adapter->extractVersion($desc); - $this->assertIdentical($result_desc, $expect_desc); - $this->assertIdentical($result_version, $expect_version); - } - - function testExtractSimple() { - $this->assertExtraction("Desc.\nThis directive has been available since 2.0.0.", "Desc.\n", '2.0.0'); - } - - function testExtractMultiline() { - $this->assertExtraction("Desc.\nThis directive was available\n since 23.4.333.", "Desc.\n", '23.4.333'); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/AlnumTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/AlnumTest.php deleted file mode 100644 index b68f53bd..00000000 --- a/tests/HTMLPurifier/ConfigSchema/Validator/AlnumTest.php +++ /dev/null @@ -1,17 +0,0 @@ -validator = new HTMLPurifier_ConfigSchema_Validator_Alnum('ID'); - parent::setup(); - } - - public function testValidate() { - $this->expectSchemaException('R&D in ID must be alphanumeric'); - $arr = array('ID' => 'R&D'); - $this->validator->validate($arr, $this->interchange); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/CompositeTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/CompositeTest.php deleted file mode 100644 index 0150146f..00000000 --- a/tests/HTMLPurifier/ConfigSchema/Validator/CompositeTest.php +++ /dev/null @@ -1,20 +0,0 @@ - 'RD'); - - generate_mock_once('HTMLPurifier_ConfigSchema_Validator'); - $mock1 = new HTMLPurifier_ConfigSchema_ValidatorMock(); - $mock2 = new HTMLPurifier_ConfigSchema_ValidatorMock(); - $mock1->expectOnce('validate', array($arr, $this->interchange)); - $mock2->expectOnce('validate', array($arr, $this->interchange)); - $this->validator->addValidator($mock1); - $this->validator->addValidator($mock2); - - $this->validator->validate($arr, $this->interchange); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/ExistsTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/ExistsTest.php deleted file mode 100644 index 4e6a73ba..00000000 --- a/tests/HTMLPurifier/ConfigSchema/Validator/ExistsTest.php +++ /dev/null @@ -1,17 +0,0 @@ -validator = new HTMLPurifier_ConfigSchema_Validator_Exists('ID'); - parent::setup(); - } - - public function testValidate() { - $this->expectSchemaException('ID must exist'); - $arr = array(); - $this->validator->validate($arr, $this->interchange); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/IfTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/IfTest.php deleted file mode 100644 index 73dbdb34..00000000 --- a/tests/HTMLPurifier/ConfigSchema/Validator/IfTest.php +++ /dev/null @@ -1,31 +0,0 @@ - 'RD'); - $this->validator->setCondition(new HTMLPurifier_ConfigSchema_Validator_Exists('ID')); - $this->validator->setThen($mock1 = new HTMLPurifier_ConfigSchema_ValidatorMock()); - $mock1->expectOnce('validate', array($arr, $this->interchange)); - $this->validator->setElse($mock2 = new HTMLPurifier_ConfigSchema_ValidatorMock()); - $mock2->expectNever('validate'); - $this->validator->validate($arr, $this->interchange); - } - - public function testValidateConditionIsFalse() { - $arr = array('ID' => 'RD'); - $this->validator->setCondition(new HTMLPurifier_ConfigSchema_Validator_Exists('ALTID')); - $this->validator->setThen($mock1 = new HTMLPurifier_ConfigSchema_ValidatorMock()); - $mock1->expectNever('validate'); - $this->validator->setElse($mock2 = new HTMLPurifier_ConfigSchema_ValidatorMock()); - $mock2->expectOnce('validate', array($arr, $this->interchange)); - $this->validator->validate($arr, $this->interchange); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/NamespaceExistsTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/NamespaceExistsTest.php deleted file mode 100644 index cc78ac8c..00000000 --- a/tests/HTMLPurifier/ConfigSchema/Validator/NamespaceExistsTest.php +++ /dev/null @@ -1,18 +0,0 @@ - 'Namespace'); - $this->expectSchemaException('Cannot define directive for undefined namespace Namespace'); - $this->validator->validate($arr, $this->interchange); - } - - public function testValidatePass() { - $arr = array('_NAMESPACE' => 'Namespace'); - $this->interchange->addNamespace(array('ID' => 'Namespace')); - $this->validator->validate($arr, $this->interchange); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/OrTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/OrTest.php deleted file mode 100644 index d8718ff5..00000000 --- a/tests/HTMLPurifier/ConfigSchema/Validator/OrTest.php +++ /dev/null @@ -1,31 +0,0 @@ - 'RD'); - $this->validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Alnum('ID')); - // Never called: - $this->validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('ALT-ID')); - $this->validator->validate($arr, $this->interchange); - } - - public function testValidatePassLater() { - $arr = array('ID' => 'RD'); - // This one fails: - $this->validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('ALT-ID')); - // But this one passes: - $this->validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Alnum('ID')); - $this->validator->validate($arr, $this->interchange); - } - - public function testValidateFail() { - $arr = array('ID' => 'RD'); - $this->validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('ALT-ID')); - $this->validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('FOOBAR')); - $this->expectException('HTMLPurifier_ConfigSchema_Exception'); - $this->validator->validate($arr, $this->interchange); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/ParseDefaultTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/ParseDefaultTest.php deleted file mode 100644 index 87b0b290..00000000 --- a/tests/HTMLPurifier/ConfigSchema/Validator/ParseDefaultTest.php +++ /dev/null @@ -1,34 +0,0 @@ - 'N.D', - 'DEFAULT' => 'true', - '_TYPE' => 'bool', - '_NULL' => false, - ); - $this->validator->validate($arr, $this->interchange); - $this->assertIdentical($arr, array( - 'ID' => 'N.D', - 'DEFAULT' => 'true', - '_TYPE' => 'bool', - '_NULL' => false, - '_DEFAULT' => true, - )); - } - - public function testValidateFail() { - $arr = array( - 'ID' => 'N.D', - 'DEFAULT' => '"asdf"', - '_TYPE' => 'int', - '_NULL' => true, - ); - $this->expectSchemaException('Invalid type for default value in N.D: Expected type int, got string'); - $this->validator->validate($arr, $this->interchange); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/ParseIdTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/ParseIdTest.php deleted file mode 100644 index 3e7bf429..00000000 --- a/tests/HTMLPurifier/ConfigSchema/Validator/ParseIdTest.php +++ /dev/null @@ -1,25 +0,0 @@ - 'Namespace'); - $this->validator->validate($arr, $this->interchange); - $this->assertIdentical($arr, array( - 'ID' => 'Namespace', - '_NAMESPACE' => 'Namespace' - )); - } - - public function testValidateDirective() { - $arr = array('ID' => 'Namespace.Directive'); - $this->validator->validate($arr, $this->interchange); - $this->assertIdentical($arr, array( - 'ID' => 'Namespace.Directive', - '_NAMESPACE' => 'Namespace', - '_DIRECTIVE' => 'Directive' - )); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/ParseTypeTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/ParseTypeTest.php deleted file mode 100644 index f84f7b71..00000000 --- a/tests/HTMLPurifier/ConfigSchema/Validator/ParseTypeTest.php +++ /dev/null @@ -1,34 +0,0 @@ - 'N.D', 'TYPE' => 'string'); - $this->validator->validate($arr, $this->interchange); - $this->assertIdentical($arr, array( - 'ID' => 'N.D', - 'TYPE' => 'string', - '_TYPE' => 'string', - '_NULL' => false, - )); - } - - public function testValidateWithNull() { - $arr = array('ID' => 'N.D', 'TYPE' => 'int/null'); - $this->validator->validate($arr, $this->interchange); - $this->assertIdentical($arr, array( - 'ID' => 'N.D', - 'TYPE' => 'int/null', - '_TYPE' => 'int', - '_NULL' => true, - )); - } - - public function testValidateInvalidType() { - $arr = array('ID' => 'N.D', 'TYPE' => 'aint/null'); - $this->expectSchemaException('Invalid type aint for configuration directive N.D'); - $this->validator->validate($arr, $this->interchange); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/UniqueTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/UniqueTest.php deleted file mode 100644 index b71aa428..00000000 --- a/tests/HTMLPurifier/ConfigSchema/Validator/UniqueTest.php +++ /dev/null @@ -1,20 +0,0 @@ -interchange->addNamespace(array('ID' => 'Namespace')); - $this->expectSchemaException('Cannot redefine namespace'); - $arr = array('ID' => 'Namespace'); - $this->validator->validate($arr, $this->interchange); - } - - public function testValidateDirective() { - $this->interchange->addDirective(array('ID' => 'Namespace.Directive')); - $this->expectSchemaException('Cannot redefine directive'); - $arr = array('ID' => 'Namespace.Directive'); - $this->validator->validate($arr, $this->interchange); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchema/ValidatorHarness.php b/tests/HTMLPurifier/ConfigSchema/ValidatorHarness.php deleted file mode 100644 index f9dd0922..00000000 --- a/tests/HTMLPurifier/ConfigSchema/ValidatorHarness.php +++ /dev/null @@ -1,24 +0,0 @@ -interchange = new HTMLPurifier_ConfigSchema_Interchange(); - if (empty($this->validator)) { - $class_to_test = substr(get_class($this), 0, -4); - $this->validator = new $class_to_test; - } - } - - public function teardown() { - unset($this->validator, $this->interchange); - } - - protected function expectSchemaException($msg) { - $this->expectException(new HTMLPurifier_ConfigSchema_Exception($msg)); - } - -} diff --git a/tests/HTMLPurifier/ConfigSchemaTest.php b/tests/HTMLPurifier/ConfigSchemaTest.php index 12eecf81..df757ee1 100644 --- a/tests/HTMLPurifier/ConfigSchemaTest.php +++ b/tests/HTMLPurifier/ConfigSchemaTest.php @@ -14,63 +14,44 @@ class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness } function test_defineNamespace() { - $this->schema->addNamespace('http', $d = 'This is an internet protocol.'); - - $this->assertIdentical($this->schema->info_namespace, array( - 'http' => new HTMLPurifier_ConfigDef_Namespace($d) - )); + $this->schema->addNamespace('http'); + $this->assertIdentical($this->schema->info['http'], array()); + $this->assertIdentical($this->schema->defaults['http'], array()); } function test_define() { - $this->schema->addNamespace('Car', 'Automobiles, those gas-guzzlers!'); + $this->schema->addNamespace('Car'); - $this->schema->add('Car', 'Seats', 5, 'int', $d = 'Standard issue.'); + $this->schema->add('Car', 'Seats', 5, 'int', false); $this->assertIdentical($this->schema->defaults['Car']['Seats'], 5); $this->assertIdentical($this->schema->info['Car']['Seats'], - new HTMLPurifier_ConfigDef_Directive('int', $d) + new HTMLPurifier_ConfigDef_Directive('int') ); - $this->schema->add('Car', 'Age', null, 'int/null', $d = 'Not always known.'); + $this->schema->add('Car', 'Age', null, 'int', true); $this->assertIdentical($this->schema->defaults['Car']['Age'], null); $this->assertIdentical($this->schema->info['Car']['Age'], - new HTMLPurifier_ConfigDef_Directive('int', $d, true) + new HTMLPurifier_ConfigDef_Directive('int', true) ); - $this->expectError('Cannot define directive for undefined namespace'); - $this->schema->add('Train', 'Cars', 10, 'int', 'Including the caboose.'); - - $this->expectError('Directive name must be alphanumeric'); - $this->schema->add('Car', 'Is it shiny?', true, 'bool', 'Indicates regular waxing.'); - - $this->expectError('Invalid type for configuration directive'); - $this->schema->add('Car', 'Efficiency', 50, 'mpg', 'The higher the better.'); - - $this->expectError('Default value does not match directive type'); - $this->schema->add('Car', 'Producer', 'Ford', 'int', 'ID of the company that made the car.'); - - $this->expectError('Description must be non-empty'); - $this->schema->add('Car', 'ComplexAttribute', 'lawyers', 'istring', null); } function test_defineAllowedValues() { $this->schema->addNamespace('QuantumNumber', 'D'); - $this->schema->add('QuantumNumber', 'Spin', 0.5, 'float', - 'Spin of particle. Fourth quantum number, represented by s.'); - $this->schema->add('QuantumNumber', 'Current', 's', 'string', - 'Currently selected quantum number.'); - $this->schema->add('QuantumNumber', 'Difficulty', null, 'string/null', $d = 'How hard are the problems?'); + $this->schema->add('QuantumNumber', 'Spin', 0.5, 'float', false); + $this->schema->add('QuantumNumber', 'Current', 's', 'string', false); + $this->schema->add('QuantumNumber', 'Difficulty', null, 'string', true); $this->schema->addAllowedValues( // okay, since default is null - 'QuantumNumber', 'Difficulty', array('easy', 'medium', 'hard') + 'QuantumNumber', 'Difficulty', array('easy' => true, 'medium' => true, 'hard' => true) ); $this->assertIdentical($this->schema->defaults['QuantumNumber']['Difficulty'], null); $this->assertIdentical($this->schema->info['QuantumNumber']['Difficulty'], new HTMLPurifier_ConfigDef_Directive( 'string', - $d, true, array( 'easy' => true, @@ -80,30 +61,16 @@ class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness ) ); - $this->expectError('Cannot define allowed values for undefined directive'); - $this->schema->addAllowedValues( - 'SpaceTime', 'Symmetry', array('time', 'spatial', 'projective') - ); - - $this->expectError('Cannot define allowed values for directive whose type is not string'); - $this->schema->addAllowedValues( - 'QuantumNumber', 'Spin', array(0.5, -0.5) - ); - - $this->expectError('Default value must be in allowed range of variables'); - $this->schema->addAllowedValues( - 'QuantumNumber', 'Current', array('n', 'l', 'm') // forgot s! - ); } function test_defineValueAliases() { $this->schema->addNamespace('Abbrev', 'Stuff on abbreviations.'); - $this->schema->add('Abbrev', 'HTH', 'Happy to Help', 'string', $d = 'Three-letters'); + $this->schema->add('Abbrev', 'HTH', 'Happy to Help', 'string', false); $this->schema->addAllowedValues( 'Abbrev', 'HTH', array( - 'Happy to Help', - 'Hope that Helps', - 'HAIL THE HAND!' + 'Happy to Help' => true, + 'Hope that Helps' => true, + 'HAIL THE HAND!' => true, ) ); $this->schema->addValueAliases( @@ -122,7 +89,6 @@ class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness $this->assertIdentical($this->schema->info['Abbrev']['HTH'], new HTMLPurifier_ConfigDef_Directive( 'string', - $d, false, array( 'Happy to Help' => true, @@ -137,25 +103,11 @@ class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness ) ); - $this->expectError('Cannot define alias to value that is not allowed'); - $this->schema->addValueAliases( - 'Abbrev', 'HTH', array( - 'head' => 'Head to Head' - ) - ); - - $this->expectError('Cannot define alias over allowed value'); - $this->schema->addValueAliases( - 'Abbrev', 'HTH', array( - 'Hope that Helps' => 'Happy to Help' - ) - ); - } function testAlias() { - $this->schema->addNamespace('Home', 'Sweet home.'); - $this->schema->add('Home', 'Rug', 3, 'int', 'ID.'); + $this->schema->addNamespace('Home'); + $this->schema->add('Home', 'Rug', 3, 'int', false); $this->schema->addAlias('Home', 'Carpet', 'Home', 'Rug'); $this->assertTrue(!isset($this->schema->defaults['Home']['Carpet'])); @@ -163,21 +115,6 @@ class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness new HTMLPurifier_ConfigDef_DirectiveAlias('Home', 'Rug') ); - $this->expectError('Cannot define directive alias in undefined namespace'); - $this->schema->addAlias('Store', 'Rug', 'Home', 'Rug'); - - $this->expectError('Directive name must be alphanumeric'); - $this->schema->addAlias('Home', 'R.g', 'Home', 'Rug'); - - $this->schema->add('Home', 'Rugger', 'Bob Max', 'string', 'Name of.'); - $this->expectError('Cannot define alias over directive'); - $this->schema->addAlias('Home', 'Rugger', 'Home', 'Rug'); - - $this->expectError('Cannot define alias to undefined directive'); - $this->schema->addAlias('Home', 'Rug2', 'Home', 'Rugavan'); - - $this->expectError('Cannot define alias to alias'); - $this->schema->addAlias('Home', 'Rug2', 'Home', 'Carpet'); } } diff --git a/tests/HTMLPurifier/ConfigTest.php b/tests/HTMLPurifier/ConfigTest.php index e0d71638..bb400160 100644 --- a/tests/HTMLPurifier/ConfigTest.php +++ b/tests/HTMLPurifier/ConfigTest.php @@ -17,20 +17,17 @@ class HTMLPurifier_ConfigTest extends HTMLPurifier_Harness // test functionality based on ConfigSchema function testNormal() { - $this->schema->addNamespace('Element', 'Chemical substances that cannot be further decomposed'); + $this->schema->addNamespace('Element'); - $this->schema->add('Element', 'Abbr', 'H', 'string', 'Abbreviation of element name.'); - $this->schema->add('Element', 'Name', 'hydrogen', 'istring', 'Full name of atoms.'); - $this->schema->add('Element', 'Number', 1, 'int', 'Atomic number, is identity.'); - $this->schema->add('Element', 'Mass', 1.00794, 'float', 'Atomic mass.'); - $this->schema->add('Element', 'Radioactive', false, 'bool', 'Does it have rapid decay?'); - $this->schema->add('Element', 'Isotopes', array(1 => true, 2 => true, 3 => true), 'lookup', - 'What numbers of neutrons for this element have been observed?'); - $this->schema->add('Element', 'Traits', array('nonmetallic', 'odorless', 'flammable'), 'list', - 'What are general properties of the element?'); - $this->schema->add('Element', 'IsotopeNames', array(1 => 'protium', 2 => 'deuterium', 3 => 'tritium'), 'hash', - 'Lookup hash of neutron counts to formal names.'); - $this->schema->add('Element', 'Object', new stdClass(), 'mixed', 'Model representation.'); + $this->schema->add('Element', 'Abbr', 'H', 'string', false); + $this->schema->add('Element', 'Name', 'hydrogen', 'istring', false); + $this->schema->add('Element', 'Number', 1, 'int', false); + $this->schema->add('Element', 'Mass', 1.00794, 'float', false); + $this->schema->add('Element', 'Radioactive', false, 'bool', false); + $this->schema->add('Element', 'Isotopes', array(1 => true, 2 => true, 3 => true), 'lookup', false); + $this->schema->add('Element', 'Traits', array('nonmetallic', 'odorless', 'flammable'), 'list', false); + $this->schema->add('Element', 'IsotopeNames', array(1 => 'protium', 2 => 'deuterium', 3 => 'tritium'), 'hash', false); + $this->schema->add('Element', 'Object', new stdClass(), 'mixed', false); $config = new HTMLPurifier_Config($this->schema); $config->autoFinalize = false; @@ -84,16 +81,18 @@ class HTMLPurifier_ConfigTest extends HTMLPurifier_Harness $this->schema->addNamespace('Instrument', 'Of the musical type.'); // case sensitive - $this->schema->add('Instrument', 'Manufacturer', 'Yamaha', 'string', 'Who made it?'); + $this->schema->add('Instrument', 'Manufacturer', 'Yamaha', 'string', false); $this->schema->addAllowedValues('Instrument', 'Manufacturer', array( - 'Yamaha', 'Conn-Selmer', 'Vandoren', 'Laubin', 'Buffet', 'other')); + 'Yamaha' => true, 'Conn-Selmer' => true, 'Vandoren' => true, + 'Laubin' => true, 'Buffet' => true, 'other' => true)); $this->schema->addValueAliases('Instrument', 'Manufacturer', array( 'Selmer' => 'Conn-Selmer')); // case insensitive - $this->schema->add('Instrument', 'Family', 'woodwind', 'istring', 'What family is it?'); + $this->schema->add('Instrument', 'Family', 'woodwind', 'istring', false); $this->schema->addAllowedValues('Instrument', 'Family', array( - 'brass', 'woodwind', 'percussion', 'string', 'keyboard', 'electronic')); + 'brass' => true, 'woodwind' => true, 'percussion' => true, + 'string' => true, 'keyboard' => true, 'electronic' => true)); $this->schema->addValueAliases('Instrument', 'Family', array( 'synth' => 'electronic')); @@ -129,9 +128,9 @@ class HTMLPurifier_ConfigTest extends HTMLPurifier_Harness function testNull() { - $this->schema->addNamespace('ReportCard', 'It is for grades.'); - $this->schema->add('ReportCard', 'English', null, 'string/null', 'Grade from English class.'); - $this->schema->add('ReportCard', 'Absences', 0, 'int', 'How many times missing from school?'); + $this->schema->addNamespace('ReportCard'); + $this->schema->add('ReportCard', 'English', null, 'string', false); + $this->schema->add('ReportCard', 'Absences', 0, 'int', false); $config = new HTMLPurifier_Config($this->schema); $config->autoFinalize = false; @@ -150,8 +149,8 @@ class HTMLPurifier_ConfigTest extends HTMLPurifier_Harness function testAliases() { - $this->schema->addNamespace('Home', 'Sweet home.'); - $this->schema->add('Home', 'Rug', 3, 'int', 'ID.'); + $this->schema->addNamespace('Home'); + $this->schema->add('Home', 'Rug', 3, 'int', false); $this->schema->addAlias('Home', 'Carpet', 'Home', 'Rug'); $config = new HTMLPurifier_Config($this->schema); @@ -171,9 +170,9 @@ class HTMLPurifier_ConfigTest extends HTMLPurifier_Harness function test_getBatch() { - $this->schema->addNamespace('Variables', 'Changing quantities in equation.'); - $this->schema->add('Variables', 'TangentialAcceleration', 'a_tan', 'string', 'In m/s^2'); - $this->schema->add('Variables', 'AngularAcceleration', 'alpha', 'string', 'In rad/s^2'); + $this->schema->addNamespace('Variables'); + $this->schema->add('Variables', 'TangentialAcceleration', 'a_tan', 'string', false); + $this->schema->add('Variables', 'AngularAcceleration', 'alpha', 'string', false); $config = new HTMLPurifier_Config($this->schema); $config->autoFinalize = false; @@ -196,9 +195,9 @@ class HTMLPurifier_ConfigTest extends HTMLPurifier_Harness function test_loadIni() { $this->schema->addNamespace('Shortcut', 'Keyboard shortcuts for commands'); - $this->schema->add('Shortcut', 'Copy', 'c', 'istring', 'Copy text'); - $this->schema->add('Shortcut', 'Paste', 'v', 'istring', 'Paste clipboard'); - $this->schema->add('Shortcut', 'Cut', 'x', 'istring', 'Cut text'); + $this->schema->add('Shortcut', 'Copy', 'c', 'istring', false); + $this->schema->add('Shortcut', 'Paste', 'v', 'istring', false); + $this->schema->add('Shortcut', 'Cut', 'x', 'istring', false); $config = new HTMLPurifier_Config($this->schema); $config->autoFinalize = false; @@ -275,13 +274,11 @@ class HTMLPurifier_ConfigTest extends HTMLPurifier_Harness function test_loadArray() { // setup a few dummy namespaces/directives for our testing - $this->schema->addNamespace('Zoo', 'Animals we have.'); - $this->schema->add('Zoo', 'Aadvark', 0, 'int', 'Have?'); - $this->schema->add('Zoo', 'Boar', 0, 'int', 'Have?'); - $this->schema->add('Zoo', 'Camel', 0, 'int', 'Have?'); - $this->schema->add( - 'Zoo', 'Others', array(), 'list', 'Other animals we have one of.' - ); + $this->schema->addNamespace('Zoo'); + $this->schema->add('Zoo', 'Aadvark', 0, 'int', false); + $this->schema->add('Zoo', 'Boar', 0, 'int', false); + $this->schema->add('Zoo', 'Camel', 0, 'int', false); + $this->schema->add('Zoo', 'Others', array(), 'list', false); $config_manual = new HTMLPurifier_Config($this->schema); $config_loadabbr = new HTMLPurifier_Config($this->schema); @@ -317,9 +314,9 @@ class HTMLPurifier_ConfigTest extends HTMLPurifier_Harness function test_create() { - $this->schema->addNamespace('Cake', 'Properties of it.'); - $this->schema->add('Cake', 'Sprinkles', 666, 'int', 'Number of.'); - $this->schema->add('Cake', 'Flavor', 'vanilla', 'string', 'Flavor of the batter.'); + $this->schema->addNamespace('Cake'); + $this->schema->add('Cake', 'Sprinkles', 666, 'int', false); + $this->schema->add('Cake', 'Flavor', 'vanilla', 'string', false); $config = new HTMLPurifier_Config($this->schema); $config->set('Cake', 'Sprinkles', 42); @@ -342,8 +339,8 @@ class HTMLPurifier_ConfigTest extends HTMLPurifier_Harness // test finalization - $this->schema->addNamespace('Poem', 'Violets are red, roses are blue...'); - $this->schema->add('Poem', 'Meter', 'iambic', 'string', 'Rhythm of poem.'); + $this->schema->addNamespace('Poem'); + $this->schema->add('Poem', 'Meter', 'iambic', 'string', false); $config = new HTMLPurifier_Config($this->schema); $config->autoFinalize = false; @@ -365,17 +362,17 @@ class HTMLPurifier_ConfigTest extends HTMLPurifier_Harness function __onlytest_loadArrayFromForm() { - $this->schema->addNamespace('Pancake', 'This should not be user customizable'); - $this->schema->add('Pancake', 'Mix', 'buttermilk', 'string', 'Type of pancake mix to use.'); - $this->schema->add('Pancake', 'Served', true, 'bool', 'But this is customizable by user.'); - $this->schema->addNamespace('Toppings', 'This is user customizable'); - $this->schema->add('Toppings', 'Syrup', true, 'bool', 'Absolutely standard!'); - $this->schema->add('Toppings', 'Flavor', 'maple', 'string', 'What flavor is the syrup?'); - $this->schema->add('Toppings', 'Strawberries', 3, 'int', 'Quite delightful fruit.'); - $this->schema->add('Toppings', 'Calories', 2000, 'int/null', 'Some things are best left unknown.'); - $this->schema->add('Toppings', 'DefinitionID', null, 'string/null', 'Do not let this be set'); - $this->schema->add('Toppings', 'DefinitionRev', 1, 'int', 'Do not let this be set'); - $this->schema->add('Toppings', 'Protected', 1, 'int', 'Do not let this be set'); + $this->schema->addNamespace('Pancake'); + $this->schema->add('Pancake', 'Mix', 'buttermilk', 'string', false); + $this->schema->add('Pancake', 'Served', true, 'bool', false); + $this->schema->addNamespace('Toppings', false); + $this->schema->add('Toppings', 'Syrup', true, 'bool', false); + $this->schema->add('Toppings', 'Flavor', 'maple', 'string', false); + $this->schema->add('Toppings', 'Strawberries', 3, 'int', false); + $this->schema->add('Toppings', 'Calories', 2000, 'int', true); + $this->schema->add('Toppings', 'DefinitionID', null, 'string', true); + $this->schema->add('Toppings', 'DefinitionRev', 1, 'int', false); + $this->schema->add('Toppings', 'Protected', 1, 'int', false); $get = array( 'breakfast' => array( @@ -426,16 +423,16 @@ class HTMLPurifier_ConfigTest extends HTMLPurifier_Harness } function test_getAllowedDirectivesForForm() { - $this->schema->addNamespace('Unused', 'Not mentioned, so deny'); - $this->schema->add('Unused', 'Unused', 'Foobar', 'string', 'Not mentioned, do not allow'); - $this->schema->addNamespace('Partial', 'Some are mentioned, allow only those'); - $this->schema->add('Partial', 'Allowed', true, 'bool', 'Mentioned, allowed'); - $this->schema->add('Partial', 'Unused', 'Foobar', 'string', 'Not mentioned, do not allow'); - $this->schema->addNamespace('All', 'Entire namespace allowed, allow all unless...'); - $this->schema->add('All', 'Allowed', true, 'bool', 'Not mentioned, allowed'); - $this->schema->add('All', 'Blacklisted', 'Foobar', 'string', 'Specifically blacklisted'); - $this->schema->add('All', 'DefinitionID', 'Foobar', 'string/null', 'Special case, auto-blacklisted'); - $this->schema->add('All', 'DefinitionRev', 2, 'int', 'Special case, auto-blacklisted'); + $this->schema->addNamespace('Unused'); + $this->schema->add('Unused', 'Unused', 'Foobar', 'string', false); + $this->schema->addNamespace('Partial'); + $this->schema->add('Partial', 'Allowed', true, 'bool', false); + $this->schema->add('Partial', 'Unused', 'Foobar', 'string', false); + $this->schema->addNamespace('All'); + $this->schema->add('All', 'Allowed', true, 'bool', false); + $this->schema->add('All', 'Blacklisted', 'Foobar', 'string', false); // explicitly blacklisted + $this->schema->add('All', 'DefinitionID', 'Foobar', 'string', true); // auto-blacklisted + $this->schema->add('All', 'DefinitionRev', 2, 'int', false); // auto-blacklisted $input = array('Partial.Allowed', 'All', '-All.Blacklisted'); $output = HTMLPurifier_Config::getAllowedDirectivesForForm($input, $this->schema);