2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/ChildDef.php';
|
|
|
|
require_once 'HTMLPurifier/Lexer.php';
|
|
|
|
require_once 'HTMLPurifier/Generator.php';
|
|
|
|
|
|
|
|
class HTMLPurifier_ChildDefTest extends UnitTestCase
|
|
|
|
{
|
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
var $def;
|
2006-07-23 00:11:03 +00:00
|
|
|
var $lex;
|
|
|
|
var $gen;
|
|
|
|
|
|
|
|
function HTMLPurifier_ChildDefTest() {
|
|
|
|
$this->lex = HTMLPurifier_Lexer::create();
|
|
|
|
$this->gen = new HTMLPurifier_Generator();
|
|
|
|
parent::UnitTestCase();
|
|
|
|
}
|
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
function assertSeries($inputs, $expect, $config, $context = array()) {
|
2006-07-23 00:11:03 +00:00
|
|
|
foreach ($inputs as $i => $input) {
|
2006-08-03 01:03:23 +00:00
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
if (!isset($context[$i])) {
|
|
|
|
$context[$i] = null;
|
|
|
|
}
|
|
|
|
if (!isset($config[$i])) {
|
|
|
|
$config[$i] = HTMLPurifier_Config::createDefault();
|
2006-08-03 01:03:23 +00:00
|
|
|
}
|
|
|
|
|
2006-08-15 00:31:12 +00:00
|
|
|
$tokens = $this->lex->tokenizeHTML($input, $config[$i]);
|
2006-08-14 02:46:34 +00:00
|
|
|
$result = $this->def->validateChildren($tokens, $config[$i], $context[$i]);
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
if (is_bool($expect[$i])) {
|
2006-08-14 02:46:34 +00:00
|
|
|
$this->assertIdentical($expect[$i], $result, "Test $i: %s");
|
2006-07-23 00:11:03 +00:00
|
|
|
} else {
|
2006-08-15 00:31:12 +00:00
|
|
|
$result_html = $this->gen->generateFromTokens($result, $config[$i]);
|
2006-08-14 02:46:34 +00:00
|
|
|
$this->assertIdentical($expect[$i], $result_html, "Test $i: %s");
|
2006-07-23 00:11:03 +00:00
|
|
|
paintIf($result_html, $result_html != $expect[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
function test_custom() {
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
$this->def = new HTMLPurifier_ChildDef_Custom(
|
2006-09-13 02:11:09 +00:00
|
|
|
'(a, b?, c*, d+, (a, b)*)');
|
|
|
|
|
|
|
|
$inputs[0] = '';
|
|
|
|
$expect[0] = false;
|
|
|
|
|
|
|
|
$inputs[1] = '<a /><b /><c /><d /><a /><b />';
|
|
|
|
$expect[1] = true;
|
|
|
|
|
|
|
|
$inputs[2] = '<a /><d>Dob</d><a /><b>foo</b><a href="moo"><b>foo</b>';
|
|
|
|
$expect[2] = true;
|
|
|
|
|
|
|
|
$inputs[3] = '<a /><a />';
|
|
|
|
$expect[3] = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-09-15 01:52:22 +00:00
|
|
|
function test_table() {
|
2006-09-13 02:11:09 +00:00
|
|
|
|
|
|
|
// currently inactive, awaiting augmentation
|
|
|
|
|
|
|
|
// the table definition
|
|
|
|
$this->def = new HTMLPurifier_ChildDef_Table();
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
$inputs = $expect = $config = array();
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
$inputs[0] = '';
|
|
|
|
$expect[0] = false;
|
|
|
|
|
2006-09-15 01:52:22 +00:00
|
|
|
// we're using empty tags to compact the tests: under real circumstances
|
|
|
|
// there would be contents in them
|
|
|
|
|
|
|
|
$inputs[1] = '<tr />';
|
2006-07-23 00:11:03 +00:00
|
|
|
$expect[1] = true;
|
|
|
|
|
2006-09-15 01:52:22 +00:00
|
|
|
$inputs[2] = '<caption /><col /><thead /><tfoot /><tbody>'.
|
|
|
|
'<tr><td>asdf</td></tr></tbody>';
|
2006-07-23 00:11:03 +00:00
|
|
|
$expect[2] = true;
|
|
|
|
|
2006-09-15 01:52:22 +00:00
|
|
|
$inputs[3] = '<col /><col /><col /><tr />';
|
2006-07-23 00:11:03 +00:00
|
|
|
$expect[3] = true;
|
|
|
|
|
2006-09-15 01:52:22 +00:00
|
|
|
// mixed up order
|
|
|
|
$inputs[4] = '<col /><colgroup /><tbody /><tfoot /><thead /><tr>1</tr><caption /><tr />';
|
|
|
|
$expect[4] = '<caption /><col /><colgroup /><thead /><tfoot /><tbody /><tr>1</tr><tr />';
|
|
|
|
|
|
|
|
// duplicates of singles
|
|
|
|
// - first caption serves
|
|
|
|
// - trailing tfoots/theads get turned into tbodys
|
|
|
|
$inputs[5] = '<caption>1</caption><caption /><tbody /><tbody /><tfoot>1</tfoot><tfoot />';
|
|
|
|
$expect[5] = '<caption>1</caption><tfoot>1</tfoot><tbody /><tbody /><tbody />';
|
|
|
|
|
|
|
|
// errant text dropped (until bubbling is implemented)
|
|
|
|
$inputs[6] = 'foo';
|
|
|
|
$expect[6] = false;
|
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
$this->assertSeries($inputs, $expect, $config);
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
function test_parsing() {
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
$def = new HTMLPurifier_ChildDef_Required('foobar | bang |gizmo');
|
2006-07-23 00:11:03 +00:00
|
|
|
$this->assertEqual($def->elements,
|
|
|
|
array(
|
|
|
|
'foobar' => true
|
|
|
|
,'bang' => true
|
|
|
|
,'gizmo' => true
|
|
|
|
));
|
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
$def = new HTMLPurifier_ChildDef_Required(array('href', 'src'));
|
2006-07-23 00:11:03 +00:00
|
|
|
$this->assertEqual($def->elements,
|
|
|
|
array(
|
|
|
|
'href' => true
|
|
|
|
,'src' => true
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_required_pcdata_forbidden() {
|
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
$this->def = new HTMLPurifier_ChildDef_Required('dt | dd');
|
|
|
|
$inputs = $expect = $config = array();
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
$inputs[0] = '';
|
|
|
|
$expect[0] = false;
|
|
|
|
|
|
|
|
$inputs[1] = '<dt>Term</dt>Text in an illegal location'.
|
|
|
|
'<dd>Definition</dd><b>Illegal tag</b>';
|
|
|
|
|
|
|
|
$expect[1] = '<dt>Term</dt><dd>Definition</dd>';
|
|
|
|
|
|
|
|
$inputs[2] = 'How do you do!';
|
|
|
|
$expect[2] = false;
|
|
|
|
|
|
|
|
// whitespace shouldn't trigger it
|
|
|
|
$inputs[3] = "\n<dd>Definition</dd> ";
|
|
|
|
$expect[3] = true;
|
|
|
|
|
|
|
|
$inputs[4] ='<dd>Definition</dd> <b></b> ';
|
|
|
|
$expect[4] = '<dd>Definition</dd> ';
|
|
|
|
|
|
|
|
$inputs[5] = "\t ";
|
|
|
|
$expect[5] = false;
|
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
$this->assertSeries($inputs, $expect, $config);
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_required_pcdata_allowed() {
|
2006-08-14 02:46:34 +00:00
|
|
|
$this->def = new HTMLPurifier_ChildDef_Required('#PCDATA | b');
|
|
|
|
|
|
|
|
$inputs = $expect = $config = array();
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
$inputs[0] = '<b>Bold text</b><img />';
|
2006-08-14 02:46:34 +00:00
|
|
|
$expect[0] = '<b>Bold text</b>';
|
|
|
|
|
|
|
|
// with child escaping on
|
|
|
|
$inputs[1] = '<b>Bold text</b><img />';
|
|
|
|
$expect[1] = '<b>Bold text</b><img />';
|
|
|
|
$config[1] = HTMLPurifier_Config::createDefault();
|
|
|
|
$config[1]->set('Core', 'EscapeInvalidChildren', true);
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
$this->assertSeries($inputs, $expect, $config);
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_optional() {
|
2006-08-14 02:46:34 +00:00
|
|
|
$this->def = new HTMLPurifier_ChildDef_Optional('b | i');
|
|
|
|
|
|
|
|
$inputs = $expect = $config = array();
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
$inputs[0] = '<b>Bold text</b><img />';
|
|
|
|
$expect[0] = '<b>Bold text</b>';
|
|
|
|
|
|
|
|
$inputs[1] = 'Not allowed text';
|
|
|
|
$expect[1] = '';
|
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
$this->assertSeries($inputs, $expect, $config);
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2006-08-03 01:03:23 +00:00
|
|
|
function test_chameleon() {
|
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
$this->def = new HTMLPurifier_ChildDef_Chameleon(
|
2006-08-03 01:03:23 +00:00
|
|
|
'b | i', // allowed only when in inline context
|
|
|
|
'b | i | div' // allowed only when in block context
|
|
|
|
);
|
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
$inputs = $expect = $config = array();
|
|
|
|
$context = array();
|
|
|
|
|
2006-08-03 01:03:23 +00:00
|
|
|
$inputs[0] = '<b>Allowed.</b>';
|
|
|
|
$expect[0] = true;
|
|
|
|
$context[0] = 'inline';
|
|
|
|
|
|
|
|
$inputs[1] = '<div>Not allowed.</div>';
|
|
|
|
$expect[1] = '';
|
|
|
|
$context[1] = 'inline';
|
|
|
|
|
|
|
|
$inputs[2] = '<div>Allowed.</div>';
|
|
|
|
$expect[2] = true;
|
|
|
|
$context[2] = 'block';
|
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
$this->assertSeries($inputs, $expect, $config, $context);
|
2006-08-03 01:03:23 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2006-07-22 00:01:38 +00:00
|
|
|
?>
|