2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
require_once 'HTMLPurifier/Harness.php';
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
require_once 'HTMLPurifier/ChildDef.php';
|
2006-09-24 02:08:18 +00:00
|
|
|
require_once 'HTMLPurifier/Lexer/DirectLex.php';
|
2006-07-23 00:11:03 +00:00
|
|
|
require_once 'HTMLPurifier/Generator.php';
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
class HTMLPurifier_ChildDefTest extends HTMLPurifier_Harness
|
2006-07-23 00:11:03 +00:00
|
|
|
{
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
function setUp() {
|
|
|
|
$this->obj = null;
|
|
|
|
$this->func = 'validateChildren';
|
|
|
|
$this->to_tokens = true;
|
|
|
|
$this->to_html = true;
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
function test_custom() {
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->obj = new HTMLPurifier_ChildDef_Custom('(a,b?,c*,d+,(a,b)*)');
|
2006-09-13 02:11:09 +00:00
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult('', false);
|
|
|
|
$this->assertResult('<a /><a />', false);
|
2006-09-30 18:55:17 +00:00
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult('<a /><b /><c /><d /><a /><b />');
|
|
|
|
$this->assertResult('<a /><d>Dob</d><a /><b>foo</b>'.
|
|
|
|
'<a href="moo" /><b>foo</b>');
|
2006-09-13 02:11:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-09-15 01:52:22 +00:00
|
|
|
function test_table() {
|
2006-09-13 02:11:09 +00:00
|
|
|
|
|
|
|
// the table definition
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->obj = 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-10-01 20:47:07 +00:00
|
|
|
$this->assertResult('', false);
|
2006-07-23 00:11:03 +00:00
|
|
|
|
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
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult('<tr />');
|
|
|
|
$this->assertResult('<caption /><col /><thead /><tfoot /><tbody>'.
|
|
|
|
'<tr><td>asdf</td></tr></tbody>');
|
|
|
|
$this->assertResult('<col /><col /><col /><tr />');
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-09-15 01:52:22 +00:00
|
|
|
// mixed up order
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<col /><colgroup /><tbody /><tfoot /><thead /><tr>1</tr><caption /><tr />',
|
|
|
|
'<caption /><col /><colgroup /><thead /><tfoot /><tbody /><tr>1</tr><tr />');
|
2006-09-15 01:52:22 +00:00
|
|
|
|
|
|
|
// duplicates of singles
|
|
|
|
// - first caption serves
|
|
|
|
// - trailing tfoots/theads get turned into tbodys
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<caption>1</caption><caption /><tbody /><tbody /><tfoot>1</tfoot><tfoot />',
|
|
|
|
'<caption>1</caption><tfoot>1</tfoot><tbody /><tbody /><tbody />'
|
|
|
|
);
|
2006-09-15 01:52:22 +00:00
|
|
|
|
|
|
|
// errant text dropped (until bubbling is implemented)
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult('foo', false);
|
2006-09-15 01:52:22 +00:00
|
|
|
|
2006-09-24 02:08:18 +00:00
|
|
|
// whitespace sticks to the previous element, last whitespace is
|
|
|
|
// stationary
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult("\n <tr />\n <tr />\n ");
|
|
|
|
$this->assertResult(
|
|
|
|
"\n\t<tbody />\n\t\t<tfoot />\n\t\t\t",
|
|
|
|
"\n\t\t<tfoot />\n\t<tbody />\n\t\t\t"
|
|
|
|
);
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
function testParsing() {
|
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
|
|
|
|
));
|
2006-10-01 20:47:07 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_required_pcdata_forbidden() {
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->obj = new HTMLPurifier_ChildDef_Required('dt | dd');
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult('', false);
|
|
|
|
$this->assertResult(
|
|
|
|
'<dt>Term</dt>Text in an illegal location'.
|
|
|
|
'<dd>Definition</dd><b>Illegal tag</b>',
|
|
|
|
'<dt>Term</dt><dd>Definition</dd>');
|
|
|
|
$this->assertResult('How do you do!', false);
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
// whitespace shouldn't trigger it
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult("\n<dd>Definition</dd> ");
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<dd>Definition</dd> <b></b> ',
|
|
|
|
'<dd>Definition</dd> '
|
|
|
|
);
|
|
|
|
$this->assertResult("\t ", false);
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_required_pcdata_allowed() {
|
2006-08-14 02:46:34 +00:00
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->obj = new HTMLPurifier_ChildDef_Required('#PCDATA | b');
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult('<b>Bold text</b><img />', '<b>Bold text</b>');
|
2006-08-14 02:46:34 +00:00
|
|
|
|
|
|
|
// with child escaping on
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<b>Bold text</b><img />',
|
|
|
|
'<b>Bold text</b><img />',
|
|
|
|
array(
|
|
|
|
'Core.EscapeInvalidChildren' => true
|
|
|
|
)
|
|
|
|
);
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_optional() {
|
2006-08-14 02:46:34 +00:00
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->obj = new HTMLPurifier_ChildDef_Optional('b | i');
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult('<b>Bold text</b><img />', '<b>Bold text</b>');
|
|
|
|
$this->assertResult('Not allowed text', '');
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-08-03 01:03:23 +00:00
|
|
|
function test_chameleon() {
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->obj = new HTMLPurifier_ChildDef_Chameleon(
|
|
|
|
'b | i', // allowed only when in inline context
|
2006-08-03 01:03:23 +00:00
|
|
|
'b | i | div' // allowed only when in block context
|
|
|
|
);
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<b>Allowed.</b>', true,
|
|
|
|
array(), array('ParentType' => 'inline')
|
|
|
|
);
|
2006-08-03 01:03:23 +00:00
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<div>Not allowed.</div>', '',
|
|
|
|
array(), array('ParentType' => 'inline')
|
|
|
|
);
|
2006-08-03 01:03:23 +00:00
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$this->assertResult(
|
|
|
|
'<div>Allowed.</div>', true,
|
|
|
|
array(), array('ParentType' => 'block')
|
|
|
|
);
|
2006-08-03 01:03:23 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2006-09-24 02:08:18 +00:00
|
|
|
?>
|