0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-01-20 12:31:53 +00:00

[3.1.0] Remove multi-description functionality as well as file detection.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1525 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-01-27 20:21:31 +00:00
parent 3baf1774b2
commit a74a590f1c
6 changed files with 78 additions and 189 deletions

View File

@ -21,7 +21,8 @@ error_reporting(E_ALL); // probably not possible to use E_STRICT
define('HTMLPURIFIER_SCHEMA_STRICT', true); // description data needs to be collected define('HTMLPURIFIER_SCHEMA_STRICT', true); // description data needs to be collected
// load dual-libraries // load dual-libraries
require_once '../library/HTMLPurifier.auto.php'; set_include_path(realpath('../library') . PATH_SEPARATOR . get_include_path() );
require_once '../library/HTMLPurifier.includes.php';
require_once 'library/ConfigDoc.auto.php'; require_once 'library/ConfigDoc.auto.php';
$purifier = HTMLPurifier::getInstance(array( $purifier = HTMLPurifier::getInstance(array(

View File

@ -96,21 +96,9 @@ class ConfigDoc_XMLSerializer_ConfigSchema extends ConfigDoc_XMLSerializer
$dom_constraints->appendChild($dom_default); $dom_constraints->appendChild($dom_default);
$dom_descriptions = $dom_document->createElement('descriptions');
$dom_directive->appendChild($dom_descriptions);
foreach ($info->descriptions as $file => $file_descriptions) {
foreach ($file_descriptions as $line => $description) {
$dom_description = $dom_document->createElement('description'); $dom_description = $dom_document->createElement('description');
// refuse to write $file if it's a full path $this->appendHTMLDiv($dom_document, $dom_description, $info->description);
if (str_replace('\\', '/', realpath($file)) != $file) { $dom_directive->appendChild($dom_description);
$dom_description->setAttribute('file', $file);
$dom_description->setAttribute('line', $line);
}
$this->appendHTMLDiv($dom_document, $dom_description, $description);
$dom_descriptions->appendChild($dom_description);
}
}
} }

View File

@ -85,18 +85,6 @@
<xsl:template match="directive/constraints"> <xsl:template match="directive/constraints">
<table class="constraints"> <table class="constraints">
<xsl:apply-templates /> <xsl:apply-templates />
<!-- Calculated other values -->
<xsl:if test="../descriptions/description[@file]">
<tr>
<th>Used by:</th>
<td>
<xsl:for-each select="../descriptions/description">
<xsl:if test="position()&gt;1">, </xsl:if>
<xsl:value-of select="@file" />
</xsl:for-each>
</td>
</tr>
</xsl:if>
<xsl:if test="../aliases/alias"> <xsl:if test="../aliases/alias">
<xsl:apply-templates select="../aliases" mode="constraints" /> <xsl:apply-templates select="../aliases" mode="constraints" />
</xsl:if> </xsl:if>
@ -111,7 +99,7 @@
</xsl:for-each> </xsl:for-each>
</td> </td>
</xsl:template> </xsl:template>
<xsl:template match="directive//description"> <xsl:template match="directive/description">
<div class="description"> <div class="description">
<xsl:copy-of select="div/node()" /> <xsl:copy-of select="div/node()" />
</div> </div>

View File

@ -11,13 +11,13 @@ class HTMLPurifier_ConfigDef_Directive extends HTMLPurifier_ConfigDef
public function __construct( public function __construct(
$type = null, $type = null,
$descriptions = null, $description = null,
$allow_null = null, $allow_null = null,
$allowed = null, $allowed = null,
$aliases = null $aliases = null
) { ) {
if ( $type !== null) $this->type = $type; if ( $type !== null) $this->type = $type;
if ($descriptions !== null) $this->descriptions = $descriptions; if ($description !== null) $this->description = $description;
if ( $allow_null !== null) $this->allow_null = $allow_null; if ( $allow_null !== null) $this->allow_null = $allow_null;
if ( $allowed !== null) $this->allowed = $allowed; if ( $allowed !== null) $this->allowed = $allowed;
if ( $aliases !== null) $this->aliases = $aliases; if ( $aliases !== null) $this->aliases = $aliases;
@ -38,10 +38,9 @@ class HTMLPurifier_ConfigDef_Directive extends HTMLPurifier_ConfigDef
public $type = 'mixed'; public $type = 'mixed';
/** /**
* Plaintext descriptions of the configuration entity is. Organized by * Plaintext description of the configuration entity is.
* file and line number, so multiple descriptions are allowed.
*/ */
public $descriptions = array(); public $description = null;
/** /**
* Is null allowed? Has no effect for mixed type. * Is null allowed? Has no effect for mixed type.
@ -65,13 +64,5 @@ class HTMLPurifier_ConfigDef_Directive extends HTMLPurifier_ConfigDef
*/ */
public $directiveAliases = array(); public $directiveAliases = array();
/**
* Adds a description to the array
*/
public function addDescription($file, $line, $description) {
if (!isset($this->descriptions[$file])) $this->descriptions[$file] = array();
$this->descriptions[$file][$line] = $description;
}
} }

View File

@ -135,13 +135,8 @@ class HTMLPurifier_ConfigSchema {
if (isset($this->info[$namespace][$name])) { if (isset($this->info[$namespace][$name])) {
// already defined // already defined
if ( trigger_error('Cannot redefine directive');
$this->info[$namespace][$name]->type !== $type ||
$this->defaults[$namespace][$name] !== $default
) {
trigger_error('Inconsistent default or type, cannot redefine');
return; return;
}
} else { } else {
// needs defining // needs defining
@ -172,11 +167,7 @@ class HTMLPurifier_ConfigSchema {
$this->defaults[$namespace][$name] = $default; $this->defaults[$namespace][$name] = $default;
} }
if (!HTMLPURIFIER_SCHEMA_STRICT) return; if (!HTMLPURIFIER_SCHEMA_STRICT) return;
// This will be removed soon: $this->info[$namespace][$name]->description = $description;
$backtrace = debug_backtrace();
$file = $this->mungeFilename($backtrace[1]['file']);
$line = $backtrace[1]['line'];
$this->info[$namespace][$name]->addDescription($file,$line,$description);
} }
/** @see HTMLPurifier_ConfigSchema->addNamespace() */ /** @see HTMLPurifier_ConfigSchema->addNamespace() */

View File

@ -9,132 +9,83 @@ if (!class_exists('CS')) {
class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness
{ {
/** protected $schema;
* Munged name of current file.
*/
protected $file;
/** function setup() {
* Copy of the real ConfigSchema to revert to. $this->schema = new HTMLPurifier_ConfigSchema();
*/
protected $old_copy;
/**
* Copy of dummy ConfigSchema for testing purposes.
*/
protected $our_copy;
function setUp() {
// yes, I know this is slightly convoluted, but that's the price
// you pay for using Singletons. Good thing we can overload it.
// first, let's get a clean copy to do tests
$our_copy = new HTMLPurifier_ConfigSchema();
// get the old copy
$this->old_copy = HTMLPurifier_ConfigSchema::instance();
// put in our copy, and reassign to the REAL reference
$this->our_copy =& HTMLPurifier_ConfigSchema::instance($our_copy);
$this->file = $this->our_copy->mungeFilename(__FILE__);
} }
function tearDown() { function tearDown() {
// testing is done, restore the old copy
HTMLPurifier_ConfigSchema::instance($this->old_copy);
tally_errors($this); tally_errors($this);
} }
function test_defineNamespace() { function test_defineNamespace() {
CS::defineNamespace('http', $d = 'This is an internet protocol.'); $this->schema->addNamespace('http', $d = 'This is an internet protocol.');
$this->assertIdentical($this->our_copy->info_namespace, array( $this->assertIdentical($this->schema->info_namespace, array(
'http' => new HTMLPurifier_ConfigDef_Namespace($d) 'http' => new HTMLPurifier_ConfigDef_Namespace($d)
)); ));
$this->expectError('Cannot redefine namespace'); $this->expectError('Cannot redefine namespace');
CS::defineNamespace('http', 'It is used to serve webpages.'); $this->schema->addNamespace('http', 'It is used to serve webpages.');
$this->expectError('Namespace name must be alphanumeric'); $this->expectError('Namespace name must be alphanumeric');
CS::defineNamespace('ssh+http', 'This http is tunneled through SSH.'); $this->schema->addNamespace('ssh+http', 'This http is tunneled through SSH.');
$this->expectError('Description must be non-empty'); $this->expectError('Description must be non-empty');
CS::defineNamespace('ftp', null); $this->schema->addNamespace('ftp', null);
} }
function test_define() { function test_define() {
CS::defineNamespace('Car', 'Automobiles, those gas-guzzlers!'); $this->schema->addNamespace('Car', 'Automobiles, those gas-guzzlers!');
CS::define('Car', 'Seats', 5, 'int', $d = 'Standard issue.'); $l = __LINE__; $this->schema->add('Car', 'Seats', 5, 'int', $d = 'Standard issue.');
$this->assertIdentical($this->our_copy->defaults['Car']['Seats'], 5); $this->assertIdentical($this->schema->defaults['Car']['Seats'], 5);
$this->assertIdentical($this->our_copy->info['Car']['Seats'], $this->assertIdentical($this->schema->info['Car']['Seats'],
new HTMLPurifier_ConfigDef_Directive('int', new HTMLPurifier_ConfigDef_Directive('int', $d)
array($this->file => array($l => $d))
)
); );
CS::define('Car', 'Age', null, 'int/null', $d = 'Not always known.'); $l = __LINE__; $this->schema->add('Car', 'Age', null, 'int/null', $d = 'Not always known.');
$this->assertIdentical($this->our_copy->defaults['Car']['Age'], null); $this->assertIdentical($this->schema->defaults['Car']['Age'], null);
$this->assertIdentical($this->our_copy->info['Car']['Age'], $this->assertIdentical($this->schema->info['Car']['Age'],
new HTMLPurifier_ConfigDef_Directive('int', new HTMLPurifier_ConfigDef_Directive('int', $d, true)
array($this->file => array($l => $d)), true
)
); );
$this->expectError('Cannot define directive for undefined namespace'); $this->expectError('Cannot define directive for undefined namespace');
CS::define('Train', 'Cars', 10, 'int', 'Including the caboose.'); $this->schema->add('Train', 'Cars', 10, 'int', 'Including the caboose.');
$this->expectError('Directive name must be alphanumeric'); $this->expectError('Directive name must be alphanumeric');
CS::define('Car', 'Is it shiny?', true, 'bool', 'Indicates regular waxing.'); $this->schema->add('Car', 'Is it shiny?', true, 'bool', 'Indicates regular waxing.');
$this->expectError('Invalid type for configuration directive'); $this->expectError('Invalid type for configuration directive');
CS::define('Car', 'Efficiency', 50, 'mpg', 'The higher the better.'); $this->schema->add('Car', 'Efficiency', 50, 'mpg', 'The higher the better.');
$this->expectError('Default value does not match directive type'); $this->expectError('Default value does not match directive type');
CS::define('Car', 'Producer', 'Ford', 'int', 'ID of the company that made the car.'); $this->schema->add('Car', 'Producer', 'Ford', 'int', 'ID of the company that made the car.');
$this->expectError('Description must be non-empty'); $this->expectError('Description must be non-empty');
CS::define('Car', 'ComplexAttribute', 'lawyers', 'istring', null); $this->schema->add('Car', 'ComplexAttribute', 'lawyers', 'istring', null);
}
function testRedefinition_define() {
CS::defineNamespace('Cat', 'Belongs to Schrodinger.');
CS::define('Cat', 'Dead', false, 'bool', $d1 = 'Well, is it?'); $l1 = __LINE__;
CS::define('Cat', 'Dead', false, 'bool', $d2 = 'It is difficult to say.'); $l2 = __LINE__;
$this->assertIdentical($this->our_copy->defaults['Cat']['Dead'], false);
$this->assertIdentical($this->our_copy->info['Cat']['Dead'],
new HTMLPurifier_ConfigDef_Directive('bool',
array($this->file => array($l1 => $d1, $l2 => $d2))
)
);
$this->expectError('Inconsistent default or type, cannot redefine');
CS::define('Cat', 'Dead', true, 'bool', 'Quantum mechanics does not know.');
$this->expectError('Inconsistent default or type, cannot redefine');
CS::define('Cat', 'Dead', 'maybe', 'string', 'Perhaps if we look we will know.');
} }
function test_defineAllowedValues() { function test_defineAllowedValues() {
CS::defineNamespace('QuantumNumber', 'D'); $this->schema->addNamespace('QuantumNumber', 'D');
CS::define('QuantumNumber', 'Spin', 0.5, 'float', $this->schema->add('QuantumNumber', 'Spin', 0.5, 'float',
'Spin of particle. Fourth quantum number, represented by s.'); 'Spin of particle. Fourth quantum number, represented by s.');
CS::define('QuantumNumber', 'Current', 's', 'string', $this->schema->add('QuantumNumber', 'Current', 's', 'string',
'Currently selected quantum number.'); 'Currently selected quantum number.');
CS::define('QuantumNumber', 'Difficulty', null, 'string/null', $d = 'How hard are the problems?'); $l = __LINE__; $this->schema->add('QuantumNumber', 'Difficulty', null, 'string/null', $d = 'How hard are the problems?');
CS::defineAllowedValues( // okay, since default is null $this->schema->addAllowedValues( // okay, since default is null
'QuantumNumber', 'Difficulty', array('easy', 'medium', 'hard') 'QuantumNumber', 'Difficulty', array('easy', 'medium', 'hard')
); );
$this->assertIdentical($this->our_copy->defaults['QuantumNumber']['Difficulty'], null); $this->assertIdentical($this->schema->defaults['QuantumNumber']['Difficulty'], null);
$this->assertIdentical($this->our_copy->info['QuantumNumber']['Difficulty'], $this->assertIdentical($this->schema->info['QuantumNumber']['Difficulty'],
new HTMLPurifier_ConfigDef_Directive( new HTMLPurifier_ConfigDef_Directive(
'string', 'string',
array($this->file => array($l => $d)), $d,
true, true,
array( array(
'easy' => true, 'easy' => true,
@ -145,48 +96,48 @@ class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness
); );
$this->expectError('Cannot define allowed values for undefined directive'); $this->expectError('Cannot define allowed values for undefined directive');
CS::defineAllowedValues( $this->schema->addAllowedValues(
'SpaceTime', 'Symmetry', array('time', 'spatial', 'projective') 'SpaceTime', 'Symmetry', array('time', 'spatial', 'projective')
); );
$this->expectError('Cannot define allowed values for directive whose type is not string'); $this->expectError('Cannot define allowed values for directive whose type is not string');
CS::defineAllowedValues( $this->schema->addAllowedValues(
'QuantumNumber', 'Spin', array(0.5, -0.5) 'QuantumNumber', 'Spin', array(0.5, -0.5)
); );
$this->expectError('Default value must be in allowed range of variables'); $this->expectError('Default value must be in allowed range of variables');
CS::defineAllowedValues( $this->schema->addAllowedValues(
'QuantumNumber', 'Current', array('n', 'l', 'm') // forgot s! 'QuantumNumber', 'Current', array('n', 'l', 'm') // forgot s!
); );
} }
function test_defineValueAliases() { function test_defineValueAliases() {
CS::defineNamespace('Abbrev', 'Stuff on abbreviations.'); $this->schema->addNamespace('Abbrev', 'Stuff on abbreviations.');
CS::define('Abbrev', 'HTH', 'Happy to Help', 'string', $d = 'Three-letters'); $l = __LINE__; $this->schema->add('Abbrev', 'HTH', 'Happy to Help', 'string', $d = 'Three-letters');
CS::defineAllowedValues( $this->schema->addAllowedValues(
'Abbrev', 'HTH', array( 'Abbrev', 'HTH', array(
'Happy to Help', 'Happy to Help',
'Hope that Helps', 'Hope that Helps',
'HAIL THE HAND!' 'HAIL THE HAND!'
) )
); );
CS::defineValueAliases( $this->schema->addValueAliases(
'Abbrev', 'HTH', array( 'Abbrev', 'HTH', array(
'happy' => 'Happy to Help', 'happy' => 'Happy to Help',
'hope' => 'Hope that Helps' 'hope' => 'Hope that Helps'
) )
); );
CS::defineValueAliases( // delayed addition $this->schema->addValueAliases( // delayed addition
'Abbrev', 'HTH', array( 'Abbrev', 'HTH', array(
'hail' => 'HAIL THE HAND!' 'hail' => 'HAIL THE HAND!'
) )
); );
$this->assertIdentical($this->our_copy->defaults['Abbrev']['HTH'], 'Happy to Help'); $this->assertIdentical($this->schema->defaults['Abbrev']['HTH'], 'Happy to Help');
$this->assertIdentical($this->our_copy->info['Abbrev']['HTH'], $this->assertIdentical($this->schema->info['Abbrev']['HTH'],
new HTMLPurifier_ConfigDef_Directive( new HTMLPurifier_ConfigDef_Directive(
'string', 'string',
array($this->file => array($l => $d)), $d,
false, false,
array( array(
'Happy to Help' => true, 'Happy to Help' => true,
@ -202,14 +153,14 @@ class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness
); );
$this->expectError('Cannot define alias to value that is not allowed'); $this->expectError('Cannot define alias to value that is not allowed');
CS::defineValueAliases( $this->schema->addValueAliases(
'Abbrev', 'HTH', array( 'Abbrev', 'HTH', array(
'head' => 'Head to Head' 'head' => 'Head to Head'
) )
); );
$this->expectError('Cannot define alias over allowed value'); $this->expectError('Cannot define alias over allowed value');
CS::defineValueAliases( $this->schema->addValueAliases(
'Abbrev', 'HTH', array( 'Abbrev', 'HTH', array(
'Hope that Helps' => 'Happy to Help' 'Hope that Helps' => 'Happy to Help'
) )
@ -218,41 +169,41 @@ class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness
} }
function testAlias() { function testAlias() {
CS::defineNamespace('Home', 'Sweet home.'); $this->schema->addNamespace('Home', 'Sweet home.');
CS::define('Home', 'Rug', 3, 'int', 'ID.'); $this->schema->add('Home', 'Rug', 3, 'int', 'ID.');
CS::defineAlias('Home', 'Carpet', 'Home', 'Rug'); $this->schema->addAlias('Home', 'Carpet', 'Home', 'Rug');
$this->assertTrue(!isset($this->our_copy->defaults['Home']['Carpet'])); $this->assertTrue(!isset($this->schema->defaults['Home']['Carpet']));
$this->assertIdentical($this->our_copy->info['Home']['Carpet'], $this->assertIdentical($this->schema->info['Home']['Carpet'],
new HTMLPurifier_ConfigDef_DirectiveAlias('Home', 'Rug') new HTMLPurifier_ConfigDef_DirectiveAlias('Home', 'Rug')
); );
$this->expectError('Cannot define directive alias in undefined namespace'); $this->expectError('Cannot define directive alias in undefined namespace');
CS::defineAlias('Store', 'Rug', 'Home', 'Rug'); $this->schema->addAlias('Store', 'Rug', 'Home', 'Rug');
$this->expectError('Directive name must be alphanumeric'); $this->expectError('Directive name must be alphanumeric');
CS::defineAlias('Home', 'R.g', 'Home', 'Rug'); $this->schema->addAlias('Home', 'R.g', 'Home', 'Rug');
CS::define('Home', 'Rugger', 'Bob Max', 'string', 'Name of.'); $this->schema->add('Home', 'Rugger', 'Bob Max', 'string', 'Name of.');
$this->expectError('Cannot define alias over directive'); $this->expectError('Cannot define alias over directive');
CS::defineAlias('Home', 'Rugger', 'Home', 'Rug'); $this->schema->addAlias('Home', 'Rugger', 'Home', 'Rug');
$this->expectError('Cannot define alias to undefined directive'); $this->expectError('Cannot define alias to undefined directive');
CS::defineAlias('Home', 'Rug2', 'Home', 'Rugavan'); $this->schema->addAlias('Home', 'Rug2', 'Home', 'Rugavan');
$this->expectError('Cannot define alias to alias'); $this->expectError('Cannot define alias to alias');
CS::defineAlias('Home', 'Rug2', 'Home', 'Carpet'); $this->schema->addAlias('Home', 'Rug2', 'Home', 'Carpet');
} }
function assertValid($var, $type, $ret = null) { function assertValid($var, $type, $ret = null) {
$ret = ($ret === null) ? $var : $ret; $ret = ($ret === null) ? $var : $ret;
$this->assertIdentical($this->our_copy->validate($var, $type), $ret); $this->assertIdentical($this->schema->validate($var, $type), $ret);
} }
function assertInvalid($var, $type) { function assertInvalid($var, $type) {
$this->assertTrue( $this->assertTrue(
$this->our_copy->isError( $this->schema->isError(
$this->our_copy->validate($var, $type) $this->schema->validate($var, $type)
) )
); );
} }
@ -307,39 +258,18 @@ class HTMLPurifier_ConfigSchemaTest extends HTMLPurifier_Harness
function testValidate_null() { function testValidate_null() {
$this->assertTrue( $this->assertTrue(
$this->our_copy->isError( $this->schema->isError(
$this->our_copy->validate(null, 'string', false) $this->schema->validate(null, 'string', false)
) )
); );
$this->assertFalse( $this->assertFalse(
$this->our_copy->isError( $this->schema->isError(
$this->our_copy->validate(null, 'string', true) $this->schema->validate(null, 'string', true)
) )
); );
} }
function assertMungeFilename($oldname, $newname) {
$this->assertIdentical(
$this->our_copy->mungeFilename($oldname),
$newname
);
}
function testMungeFilename() {
$this->assertMungeFilename(
'C:\\php\\My Libraries\\htmlpurifier\\library\\HTMLPurifier\\AttrDef.php',
'HTMLPurifier/AttrDef.php'
);
$this->assertMungeFilename(
'C:\\php\\My Libraries\\htmlpurifier\\library\\HTMLPurifier.php',
'HTMLPurifier.php'
);
}
} }