2007-02-17 19:37:48 +00:00
|
|
|
<?php
|
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
class HTMLPurifier_HTMLModuleManagerTest extends HTMLPurifier_Harness
|
2007-02-17 19:37:48 +00:00
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-09 01:23:05 +00:00
|
|
|
protected function createManager() {
|
2007-05-15 02:33:19 +00:00
|
|
|
$manager = new HTMLPurifier_HTMLModuleManager();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-09 01:23:05 +00:00
|
|
|
$this->config->set('HTML', 'CustomDoctype', 'Blank');
|
|
|
|
$manager->doctypes->register('Blank');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
$attrdef_nmtokens = new HTMLPurifier_AttrDef_HTML_Nmtokens();
|
2008-12-06 07:28:20 +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);
|
2008-04-21 23:28:52 +00:00
|
|
|
$manager->attrTypes->set('NMTOKENS', $attrdef);
|
2008-06-09 01:23:05 +00:00
|
|
|
return $manager;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-09 01:23:05 +00:00
|
|
|
function test_addModule() {
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-09 01:23:05 +00:00
|
|
|
$manager = $this->createManager();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
// ...but we add user modules
|
2008-12-06 07:28:20 +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);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$structural_module = new HTMLPurifier_HTMLModule();
|
|
|
|
$structural_module->name = 'Structural';
|
2008-04-21 23:28:52 +00:00
|
|
|
$structural_module->addElement('p', 'Block', 'Inline', 'Common');
|
2007-05-15 02:33:19 +00:00
|
|
|
$manager->addModule($structural_module);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$formatting_module = new HTMLPurifier_HTMLModule();
|
|
|
|
$formatting_module->name = 'Formatting';
|
2008-04-21 23:28:52 +00:00
|
|
|
$formatting_module->addElement('em', 'Inline', 'Inline', 'Common');
|
2007-05-15 02:33:19 +00:00
|
|
|
$manager->addModule($formatting_module);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-21 23:28:52 +00:00
|
|
|
$unsafe_module = new HTMLPurifier_HTMLModule();
|
|
|
|
$unsafe_module->name = 'Unsafe';
|
|
|
|
$unsafe_module->safe = false;
|
|
|
|
$unsafe_module->addElement('div', 'Block', 'Flow');
|
|
|
|
$manager->addModule($unsafe_module);
|
2008-12-06 07:28:20 +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');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$manager->setup($config);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-09 01:23:05 +00:00
|
|
|
$attrdef_nmtokens = new HTMLPurifier_AttrDef_HTML_Nmtokens();
|
2008-12-06 07:28:20 +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;
|
2008-12-06 07:28:20 +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;
|
2008-12-06 07:28:20 +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
|
|
|
);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
// test trusted parameter override
|
2008-12-06 07:28:20 +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;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-15 02:33:19 +00:00
|
|
|
$this->assertEqual($div, $manager->getElement('div', true));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-02-18 05:29:19 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-20 22:29:31 +00:00
|
|
|
function testAllowedModules() {
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-20 22:29:31 +00:00
|
|
|
$manager = new HTMLPurifier_HTMLModuleManager();
|
|
|
|
$manager->doctypes->register(
|
|
|
|
'Fantasy Inventory 1.0', true,
|
|
|
|
array('Weapons', 'Magic')
|
|
|
|
);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-20 22:29:31 +00:00
|
|
|
// register these modules so it doesn't blow up
|
|
|
|
$weapons_module = new HTMLPurifier_HTMLModule();
|
|
|
|
$weapons_module->name = 'Weapons';
|
|
|
|
$manager->registerModule($weapons_module);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-20 22:29:31 +00:00
|
|
|
$magic_module = new HTMLPurifier_HTMLModule();
|
|
|
|
$magic_module->name = 'Magic';
|
|
|
|
$manager->registerModule($magic_module);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-20 22:29:31 +00:00
|
|
|
$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);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-20 22:29:31 +00:00
|
|
|
$this->assertTrue( isset($manager->modules['Weapons']));
|
|
|
|
$this->assertFalse(isset($manager->modules['Magic']));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-20 22:29:31 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-02-17 19:37:48 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|