0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-23 00:41:52 +00:00

[3.1.0] Implement ConfigSchema interchange

- Implement exception hierarchy

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1582 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-03-01 17:06:23 +00:00
parent c521e3a534
commit 240b565513
9 changed files with 133 additions and 1 deletions

2
NEWS
View File

@ -33,6 +33,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
from CSS, but you can enable it using %CSS.AllowImportant from CSS, but you can enable it using %CSS.AllowImportant
! Support for display and visibility CSS properties added, set %CSS.AllowTricky ! Support for display and visibility CSS properties added, set %CSS.AllowTricky
to true to use them. to true to use them.
! HTML Purifier now has its own Exception hierarchy under HTMLPurifier_Exception.
Developer error (not enduser error) can cause these to be triggered.
- Autoclose now operates iteratively, i.e. <span><span><div> now has - Autoclose now operates iteratively, i.e. <span><span><div> now has
both span tags closed. both span tags closed.
- Various HTMLPurifier_Config convenience functions now accept another parameter - Various HTMLPurifier_Config convenience functions now accept another parameter

View File

@ -43,6 +43,7 @@ require 'HTMLPurifier/EntityLookup.php';
require 'HTMLPurifier/EntityParser.php'; require 'HTMLPurifier/EntityParser.php';
require 'HTMLPurifier/Error.php'; require 'HTMLPurifier/Error.php';
require 'HTMLPurifier/ErrorCollector.php'; require 'HTMLPurifier/ErrorCollector.php';
require 'HTMLPurifier/Exception.php';
require 'HTMLPurifier/Filter.php'; require 'HTMLPurifier/Filter.php';
require 'HTMLPurifier/Generator.php'; require 'HTMLPurifier/Generator.php';
require 'HTMLPurifier/HTMLDefinition.php'; require 'HTMLPurifier/HTMLDefinition.php';
@ -122,6 +123,8 @@ require 'HTMLPurifier/ChildDef/Table.php';
require 'HTMLPurifier/ConfigDef/Directive.php'; require 'HTMLPurifier/ConfigDef/Directive.php';
require 'HTMLPurifier/ConfigDef/DirectiveAlias.php'; require 'HTMLPurifier/ConfigDef/DirectiveAlias.php';
require 'HTMLPurifier/ConfigDef/Namespace.php'; require 'HTMLPurifier/ConfigDef/Namespace.php';
require 'HTMLPurifier/ConfigSchema/Exception.php';
require 'HTMLPurifier/ConfigSchema/Interchange.php';
require 'HTMLPurifier/ConfigSchema/StringHash.php'; require 'HTMLPurifier/ConfigSchema/StringHash.php';
require 'HTMLPurifier/ConfigSchema/StringHashAdapter.php'; require 'HTMLPurifier/ConfigSchema/StringHashAdapter.php';
require 'HTMLPurifier/ConfigSchema/StringHashParser.php'; require 'HTMLPurifier/ConfigSchema/StringHashParser.php';

View File

@ -0,0 +1,9 @@
<?php
/**
* Exceptions related to configuration schema
*/
class HTMLPurifier_ConfigSchema_Exception extends HTMLPurifier_Exception
{
}

View File

@ -0,0 +1,54 @@
<?php
/**
* Generic schema interchange format that can be converted to a runtime
* representation (HTMLPurifier_ConfigSchema) or HTML documentation. Members
* are completely validated.
*/
class HTMLPurifier_ConfigSchema_Interchange
{
/**
* Hash table of allowed types.
*/
public $types = array(
'string' => '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'
);
/**
* Array of Namespace ID => array(namespace info)
*/
public $namespaces;
/**
* Array of Directive ID => array(directive info)
*/
public $directives;
/**
* Adds a namespace array to $namespaces
*/
public function addNamespace($arr) {
if (!isset($arr['ID'])) throw new HTMLPurifier_ConfigSchema_Exception('Namespace must have ID');
$this->namespaces[$arr['ID']] = $arr;
}
/**
* Adds a directive array to $directives
*/
public function addDirective($arr) {
if (!isset($arr['ID'])) throw new HTMLPurifier_ConfigSchema_Exception('Directive must have ID');
$this->directives[$arr['ID']] = $arr;
}
}

View File

@ -32,7 +32,7 @@ class HTMLPurifier_ConfigSchema_StringHashParser
public $default = 'ID'; public $default = 'ID';
public function parseFile($file) { public function parseFile($file) {
if (!file_exists($file)) throw new Exception('File does not exist'); if (!file_exists($file)) throw new HTMLPurifier_ConfigSchema_Exception('File ' . $file . ' does not exist');
$fh = fopen($file, 'r'); $fh = fopen($file, 'r');
$state = false; $state = false;
$single = false; $single = false;

View File

@ -0,0 +1,11 @@
<?php
/**
* Global exception class for HTML Purifier; any exceptions we throw
* are from here.
*/
class HTMLPurifier_Exception extends Exception
{
}

View File

@ -0,0 +1,44 @@
<?php
class HTMLPurifier_ConfigSchema_InterchangeTest extends UnitTestCase
{
protected $interchange;
public function setup() {
$this->interchange = new HTMLPurifier_ConfigSchema_Interchange();
}
public function testAddNamespace() {
$this->interchange->addNamespace($v = array(
'ID' => 'Namespace',
'Foo' => 'Bar',
));
$this->assertIdentical($v, $this->interchange->namespaces['Namespace']);
}
public function testAddNamespaceError() {
try {
$this->interchange->addNamespace(array());
} catch (HTMLPurifier_ConfigSchema_Exception $e) {
$this->assertIdentical($e->getMessage(), 'Namespace must have ID');
}
}
public function testAddDirective() {
$this->interchange->addDirective($v = array(
'ID' => 'Namespace.Directive',
'Foo' => 'Bar',
));
$this->assertIdentical($v, $this->interchange->directives['Namespace.Directive']);
}
public function testAddDirectiveError() {
try {
$this->interchange->addDirective(array());
} catch (HTMLPurifier_ConfigSchema_Exception $e) {
$this->assertIdentical($e->getMessage(), 'Directive must have ID');
}
}
}

View File

@ -52,4 +52,12 @@ class HTMLPurifier_ConfigSchema_StringHashParserTest extends UnitTestCase
)); ));
} }
function testError() {
try {
$this->parser->parseFile('NoExist.txt');
} catch (HTMLPurifier_ConfigSchema_Exception $e) {
$this->assertIdentical($e->getMessage(), 'File NoExist.txt does not exist');
}
}
} }

View File

@ -65,6 +65,7 @@ $test_files[] = 'HTMLPurifier/ChildDef/RequiredTest.php';
$test_files[] = 'HTMLPurifier/ChildDef/StrictBlockquoteTest.php'; $test_files[] = 'HTMLPurifier/ChildDef/StrictBlockquoteTest.php';
$test_files[] = 'HTMLPurifier/ChildDef/TableTest.php'; $test_files[] = 'HTMLPurifier/ChildDef/TableTest.php';
$test_files[] = 'HTMLPurifier/ConfigSchemaTest.php'; $test_files[] = 'HTMLPurifier/ConfigSchemaTest.php';
$test_files[] = 'HTMLPurifier/ConfigSchema/InterchangeTest.php';
$test_files[] = 'HTMLPurifier/ConfigSchema/StringHashAdapterTest.php'; $test_files[] = 'HTMLPurifier/ConfigSchema/StringHashAdapterTest.php';
$test_files[] = 'HTMLPurifier/ConfigSchema/StringHashReverseAdapterTest.php'; $test_files[] = 'HTMLPurifier/ConfigSchema/StringHashReverseAdapterTest.php';
$test_files[] = 'HTMLPurifier/ConfigSchema/StringHashParserTest.php'; $test_files[] = 'HTMLPurifier/ConfigSchema/StringHashParserTest.php';