2007-02-17 19:37:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/HTMLModuleManager.php';
|
|
|
|
|
|
|
|
class HTMLPurifier_HTMLModuleManagerTest extends UnitTestCase
|
|
|
|
{
|
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
function test_addModule() {
|
|
|
|
$manager = new HTMLPurifier_HTMLModuleManager();
|
|
|
|
$manager->doctypes->register('Blank'); // doctype normally is blank...
|
2007-02-18 05:29:19 +00:00
|
|
|
|
2007-06-20 21:39:28 +00:00
|
|
|
$attrdef_nmtokens = new HTMLPurifier_AttrDef();
|
|
|
|
$attrdef_nmtokens->_name = 'nmtokens'; // for testing only
|
2007-05-23 00:39:07 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
generate_mock_once('HTMLPurifier_AttrDef');
|
2007-06-26 15:40:04 +00:00
|
|
|
$attrdef = new HTMLPurifier_AttrDefMock();
|
2007-05-23 00:39:07 +00:00
|
|
|
$attrdef->setReturnValue('make', $attrdef_nmtokens);
|
|
|
|
$manager->attrTypes->info['NMTOKENS'] =& $attrdef;
|
2007-02-17 19:37:48 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
// ...but we add user modules
|
2007-02-17 19:37:48 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$common_module = new HTMLPurifier_HTMLModule();
|
|
|
|
$common_module->name = 'Common';
|
|
|
|
$common_module->attr_collections['Common'] = array('class' => 'NMTOKENS');
|
|
|
|
$common_module->content_sets['Flow'] = 'Block | Inline';
|
|
|
|
$manager->addModule($common_module);
|
2007-02-27 23:57:54 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$structural_module = new HTMLPurifier_HTMLModule();
|
|
|
|
$structural_module->name = 'Structural';
|
|
|
|
$structural_module->addElement('p', true, 'Block', 'Inline', 'Common');
|
|
|
|
$structural_module->addElement('div', false, 'Block', 'Flow');
|
|
|
|
$manager->addModule($structural_module);
|
2007-02-18 05:29:19 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$formatting_module = new HTMLPurifier_HTMLModule();
|
|
|
|
$formatting_module->name = 'Formatting';
|
|
|
|
$formatting_module->addElement('em', true, 'Inline', 'Inline', 'Common');
|
|
|
|
$manager->addModule($formatting_module);
|
2007-02-18 05:29:19 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
|
|
$config->set('HTML', 'Trusted', false);
|
2007-06-25 18:38:39 +00:00
|
|
|
$config->set('HTML', 'CustomDoctype', 'Blank');
|
2007-02-18 05:29:19 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$manager->setup($config);
|
2007-02-18 05:29:19 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$p = new HTMLPurifier_ElementDef();
|
|
|
|
$p->attr['class'] = $attrdef_nmtokens;
|
|
|
|
$p->child = new HTMLPurifier_ChildDef_Optional(array('em', '#PCDATA'));
|
|
|
|
$p->content_model = 'em | #PCDATA';
|
|
|
|
$p->content_model_type = 'optional';
|
|
|
|
$p->descendants_are_inline = true;
|
|
|
|
$p->safe = true;
|
2007-02-18 05:29:19 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$em = new HTMLPurifier_ElementDef();
|
|
|
|
$em->attr['class'] = $attrdef_nmtokens;
|
|
|
|
$em->child = new HTMLPurifier_ChildDef_Optional(array('em', '#PCDATA'));
|
|
|
|
$em->content_model = 'em | #PCDATA';
|
|
|
|
$em->content_model_type = 'optional';
|
|
|
|
$em->descendants_are_inline = true;
|
|
|
|
$em->safe = true;
|
2007-02-17 19:37:48 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$this->assertEqual(
|
|
|
|
array('p' => $p, 'em' => $em),
|
|
|
|
$manager->getElements()
|
2007-02-17 19:37:48 +00:00
|
|
|
);
|
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
// test trusted parameter override
|
2007-02-17 19:37:48 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$div = new HTMLPurifier_ElementDef();
|
|
|
|
$div->child = new HTMLPurifier_ChildDef_Optional(array('p', 'div', 'em', '#PCDATA'));
|
|
|
|
$div->content_model = 'p | div | em | #PCDATA';
|
|
|
|
$div->content_model_type = 'optional';
|
|
|
|
$div->descendants_are_inline = false;
|
|
|
|
$div->safe = false;
|
2007-02-17 19:37:48 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$this->assertEqual($div, $manager->getElement('div', true));
|
2007-02-18 05:29:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-05-20 22:29:31 +00:00
|
|
|
function testAllowedModules() {
|
|
|
|
|
|
|
|
$manager = new HTMLPurifier_HTMLModuleManager();
|
|
|
|
$manager->doctypes->register(
|
|
|
|
'Fantasy Inventory 1.0', true,
|
|
|
|
array('Weapons', 'Magic')
|
|
|
|
);
|
|
|
|
|
|
|
|
// register these modules so it doesn't blow up
|
|
|
|
$weapons_module = new HTMLPurifier_HTMLModule();
|
|
|
|
$weapons_module->name = 'Weapons';
|
|
|
|
$manager->registerModule($weapons_module);
|
|
|
|
|
|
|
|
$magic_module = new HTMLPurifier_HTMLModule();
|
|
|
|
$magic_module->name = 'Magic';
|
|
|
|
$manager->registerModule($magic_module);
|
|
|
|
|
|
|
|
$config = HTMLPurifier_Config::create(array(
|
2007-06-25 18:38:39 +00:00
|
|
|
'HTML.CustomDoctype' => 'Fantasy Inventory 1.0',
|
2007-05-20 22:29:31 +00:00
|
|
|
'HTML.AllowedModules' => 'Weapons'
|
|
|
|
));
|
|
|
|
$manager->setup($config);
|
|
|
|
|
|
|
|
$this->assertTrue( isset($manager->modules['Weapons']));
|
|
|
|
$this->assertFalse(isset($manager->modules['Magic']));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-02-17 19:37:48 +00:00
|
|
|
}
|
|
|
|
|