2007-05-29 21:26:43 +00:00
|
|
|
<?php
|
|
|
|
|
2007-08-01 14:06:59 +00:00
|
|
|
class HTMLPurifier_HTMLDefinitionTest extends HTMLPurifier_Harness
|
2007-05-29 21:26:43 +00:00
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function expectError($error = false, $message = '%s')
|
|
|
|
{
|
2008-04-26 19:28:14 +00:00
|
|
|
// Because we're testing a definition, it's vital that the cache
|
|
|
|
// is turned off for tests that expect errors.
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('Cache.DefinitionImpl', null);
|
2008-04-26 19:28:14 +00:00
|
|
|
parent::expectError($error);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_parseTinyMCEAllowedList()
|
|
|
|
{
|
2007-05-29 21:26:43 +00:00
|
|
|
$def = new HTMLPurifier_HTMLDefinition();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
|
|
|
// note: this is case-sensitive, but its config schema
|
2007-06-29 01:54:48 +00:00
|
|
|
// counterpart is not. This is generally a good thing for users,
|
|
|
|
// but it's a slight internal inconsistency
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-08-02 15:13:12 +00:00
|
|
|
$this->assertEqual(
|
|
|
|
$def->parseTinyMCEAllowedList(''),
|
|
|
|
array(array(), array())
|
|
|
|
);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 21:26:43 +00:00
|
|
|
$this->assertEqual(
|
|
|
|
$def->parseTinyMCEAllowedList('a,b,c'),
|
|
|
|
array(array('a' => true, 'b' => true, 'c' => true), array())
|
|
|
|
);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 21:26:43 +00:00
|
|
|
$this->assertEqual(
|
|
|
|
$def->parseTinyMCEAllowedList('a[x|y|z]'),
|
|
|
|
array(array('a' => true), array('a.x' => true, 'a.y' => true, 'a.z' => true))
|
|
|
|
);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 21:26:43 +00:00
|
|
|
$this->assertEqual(
|
|
|
|
$def->parseTinyMCEAllowedList('*[id]'),
|
|
|
|
array(array(), array('*.id' => true))
|
|
|
|
);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 21:26:43 +00:00
|
|
|
$this->assertEqual(
|
|
|
|
$def->parseTinyMCEAllowedList('a[*]'),
|
|
|
|
array(array('a' => true), array('a.*' => true))
|
|
|
|
);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 21:26:43 +00:00
|
|
|
$this->assertEqual(
|
|
|
|
$def->parseTinyMCEAllowedList('span[style],strong,a[href|title]'),
|
|
|
|
array(array('span' => true, 'strong' => true, 'a' => true),
|
|
|
|
array('span.style' => true, 'a.href' => true, 'a.title' => true))
|
|
|
|
);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-29 01:54:48 +00:00
|
|
|
$this->assertEqual(
|
|
|
|
// alternate form:
|
|
|
|
$def->parseTinyMCEAllowedList(
|
|
|
|
'span[style]
|
|
|
|
strong
|
|
|
|
a[href|title]
|
|
|
|
'),
|
2008-05-13 02:02:27 +00:00
|
|
|
$val = array(array('span' => true, 'strong' => true, 'a' => true),
|
2007-06-29 01:54:48 +00:00
|
|
|
array('span.style' => true, 'a.href' => true, 'a.title' => true))
|
|
|
|
);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-05-13 02:02:27 +00:00
|
|
|
$this->assertEqual(
|
|
|
|
$def->parseTinyMCEAllowedList(' span [ style ], strong'."\n\t".'a[href | title]'),
|
|
|
|
$val
|
|
|
|
);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 21:26:43 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_Allowed()
|
|
|
|
{
|
2007-05-29 21:26:43 +00:00
|
|
|
$config1 = HTMLPurifier_Config::create(array(
|
|
|
|
'HTML.AllowedElements' => array('b', 'i', 'p', 'a'),
|
2008-04-26 03:14:01 +00:00
|
|
|
'HTML.AllowedAttributes' => array('a@href', '*@id')
|
2007-05-29 21:26:43 +00:00
|
|
|
));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 21:26:43 +00:00
|
|
|
$config2 = HTMLPurifier_Config::create(array(
|
|
|
|
'HTML.Allowed' => 'b,i,p,a[href],*[id]'
|
|
|
|
));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 21:26:43 +00:00
|
|
|
$this->assertEqual($config1->getHTMLDefinition(), $config2->getHTMLDefinition());
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 21:26:43 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function assertPurification_AllowedElements_p()
|
|
|
|
{
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification('<p><b>Jelly</b></p>', '<p>Jelly</p>');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedElements()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', 'p');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification_AllowedElements_p();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedElements_multiple()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', 'p,div');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification('<div><p><b>Jelly</b></p></div>', '<div><p>Jelly</p></div>');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedElements_invalidElement()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', 'obviously_invalid,p');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->expectError(new PatternExpectation("/Element 'obviously_invalid' is not supported/"));
|
|
|
|
$this->assertPurification_AllowedElements_p();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedElements_invalidElement_xssAttempt()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', '<script>,p');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->expectError(new PatternExpectation("/Element '<script>' is not supported/"));
|
|
|
|
$this->assertPurification_AllowedElements_p();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedElements_multipleInvalidElements()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', 'dr-wiggles,dr-pepper,p');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->expectError(new PatternExpectation("/Element 'dr-wiggles' is not supported/"));
|
|
|
|
$this->expectError(new PatternExpectation("/Element 'dr-pepper' is not supported/"));
|
|
|
|
$this->assertPurification_AllowedElements_p();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function assertPurification_AllowedAttributes_global_style()
|
|
|
|
{
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification(
|
|
|
|
'<p style="font-weight:bold;" class="foo">Jelly</p><br style="clear:both;" />',
|
|
|
|
'<p style="font-weight:bold;">Jelly</p><br style="clear:both;" />');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedAttributes_global_preferredSyntax()
|
|
|
|
{
|
2010-09-04 05:30:32 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', array('p', 'br'));
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedAttributes', 'style');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification_AllowedAttributes_global_style();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedAttributes_global_verboseSyntax()
|
|
|
|
{
|
2010-09-04 05:30:32 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', array('p', 'br'));
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedAttributes', '*@style');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification_AllowedAttributes_global_style();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedAttributes_global_discouragedSyntax()
|
|
|
|
{
|
2008-04-26 03:14:01 +00:00
|
|
|
// Emit errors eventually
|
2010-09-04 05:30:32 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', array('p', 'br'));
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedAttributes', '*.style');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification_AllowedAttributes_global_style();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function assertPurification_AllowedAttributes_local_p_style()
|
|
|
|
{
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification(
|
|
|
|
'<p style="font-weight:bold;" class="foo">Jelly</p><br style="clear:both;" />',
|
|
|
|
'<p style="font-weight:bold;">Jelly</p><br />');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedAttributes_local_preferredSyntax()
|
|
|
|
{
|
2010-09-04 05:30:32 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', array('p', 'br'));
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedAttributes', 'p@style');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification_AllowedAttributes_local_p_style();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedAttributes_local_discouragedSyntax()
|
|
|
|
{
|
2010-09-04 05:30:32 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', array('p', 'br'));
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedAttributes', 'p.style');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification_AllowedAttributes_local_p_style();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedAttributes_multiple()
|
|
|
|
{
|
2010-09-04 05:30:32 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', array('p', 'br'));
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedAttributes', 'p@style,br@class,title');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification(
|
|
|
|
'<p style="font-weight:bold;" class="foo" title="foo">Jelly</p><br style="clear:both;" class="foo" title="foo" />',
|
|
|
|
'<p style="font-weight:bold;" title="foo">Jelly</p><br class="foo" title="foo" />'
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedAttributes_local_invalidAttribute()
|
|
|
|
{
|
2010-09-04 05:30:32 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', array('p', 'br'));
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedAttributes', array('p@style', 'p@<foo>'));
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->expectError(new PatternExpectation("/Attribute '<foo>' in element 'p' not supported/"));
|
|
|
|
$this->assertPurification_AllowedAttributes_local_p_style();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedAttributes_global_invalidAttribute()
|
|
|
|
{
|
2010-09-04 05:30:32 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', array('p', 'br'));
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedAttributes', array('style', '<foo>'));
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->expectError(new PatternExpectation("/Global attribute '<foo>' is not supported in any elements/"));
|
|
|
|
$this->assertPurification_AllowedAttributes_global_style();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedAttributes_local_invalidAttributeDueToMissingElement()
|
|
|
|
{
|
2010-09-04 05:30:32 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', array('p', 'br'));
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedAttributes', 'p.style,foo.style');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->expectError(new PatternExpectation("/Cannot allow attribute 'style' if element 'foo' is not allowed\/supported/"));
|
|
|
|
$this->assertPurification_AllowedAttributes_local_p_style();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedAttributes_duplicate()
|
|
|
|
{
|
2010-09-04 05:30:32 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', array('p', 'br'));
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedAttributes', 'p.style,p@style');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification_AllowedAttributes_local_p_style();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_AllowedAttributes_multipleErrors()
|
|
|
|
{
|
2010-09-04 05:30:32 +00:00
|
|
|
$this->config->set('HTML.AllowedElements', array('p', 'br'));
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.AllowedAttributes', 'p.style,foo.style,<foo>');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->expectError(new PatternExpectation("/Cannot allow attribute 'style' if element 'foo' is not allowed\/supported/"));
|
|
|
|
$this->expectError(new PatternExpectation("/Global attribute '<foo>' is not supported in any elements/"));
|
|
|
|
$this->assertPurification_AllowedAttributes_local_p_style();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2024-07-31 02:06:23 +00:00
|
|
|
public function test_AllowedAttributes_invalidAttributeDueToConsistingOfNumbers_UsingDirectLex()
|
|
|
|
{
|
|
|
|
$this->config->set('HTML.AllowedElements', array('a'));
|
|
|
|
$this->config->set('HTML.AllowedAttributes', 'href');
|
|
|
|
$this->config->set('Core.LexerImpl', 'DirectLex');
|
|
|
|
$this->assertPurification(
|
|
|
|
'<a href="https://example.com/" 10="hoge">Test</a>',
|
|
|
|
'<a href="https://example.com/">Test</a>'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_ForbiddenElements()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.ForbiddenElements', 'b');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification('<b>b</b><i>i</i>', 'b<i>i</i>');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_ForbiddenElements_invalidElement()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.ForbiddenElements', 'obviously_incorrect');
|
2008-04-26 03:14:01 +00:00
|
|
|
// no error!
|
|
|
|
$this->assertPurification('<i>i</i>');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function assertPurification_ForbiddenAttributes_b_style()
|
|
|
|
{
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification(
|
|
|
|
'<b style="float:left;">b</b><i style="float:left;">i</i>',
|
|
|
|
'<b>b</b><i style="float:left;">i</i>');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_ForbiddenAttributes()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.ForbiddenAttributes', 'b@style');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->assertPurification_ForbiddenAttributes_b_style();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_ForbiddenAttributes_incorrectSyntax()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.ForbiddenAttributes', 'b.style');
|
2008-04-26 03:14:01 +00:00
|
|
|
$this->expectError("Error with b.style: tag.attr syntax not supported for HTML.ForbiddenAttributes; use tag@attr instead");
|
|
|
|
$this->assertPurification('<b style="float:left;">Test</b>');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_ForbiddenAttributes_incorrectGlobalSyntax()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.ForbiddenAttributes', '*.style');
|
2008-05-13 01:41:25 +00:00
|
|
|
$this->expectError("Error with *.style: *.attr syntax not supported for HTML.ForbiddenAttributes; use attr instead");
|
|
|
|
$this->assertPurification('<b style="float:left;">Test</b>');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function assertPurification_ForbiddenAttributes_style()
|
|
|
|
{
|
2008-05-13 01:41:25 +00:00
|
|
|
$this->assertPurification(
|
|
|
|
'<b class="foo" style="float:left;">b</b><i style="float:left;">i</i>',
|
|
|
|
'<b class="foo">b</b><i>i</i>');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_ForbiddenAttributes_global()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.ForbiddenAttributes', 'style');
|
2008-05-13 01:41:25 +00:00
|
|
|
$this->assertPurification_ForbiddenAttributes_style();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_ForbiddenAttributes_globalVerboseFormat()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.ForbiddenAttributes', '*@style');
|
2008-05-13 01:41:25 +00:00
|
|
|
$this->assertPurification_ForbiddenAttributes_style();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_addAttribute()
|
|
|
|
{
|
2010-12-30 23:51:53 +00:00
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
2008-04-23 02:40:17 +00:00
|
|
|
$def = $config->getHTMLDefinition(true);
|
2007-06-19 01:29:50 +00:00
|
|
|
$def->addAttribute('span', 'custom', 'Enum#attribute');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-19 01:29:50 +00:00
|
|
|
$purifier = new HTMLPurifier($config);
|
|
|
|
$input = '<span custom="attribute">Custom!</span>';
|
|
|
|
$output = $purifier->purify($input);
|
|
|
|
$this->assertIdentical($input, $output);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-19 01:29:50 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_addAttribute_multiple()
|
|
|
|
{
|
2010-12-30 23:51:53 +00:00
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
2008-04-23 02:40:17 +00:00
|
|
|
$def = $config->getHTMLDefinition(true);
|
2008-03-26 04:31:04 +00:00
|
|
|
$def->addAttribute('span', 'custom', 'Enum#attribute');
|
|
|
|
$def->addAttribute('span', 'foo', 'Text');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-26 04:31:04 +00:00
|
|
|
$purifier = new HTMLPurifier($config);
|
|
|
|
$input = '<span custom="attribute" foo="asdf">Custom!</span>';
|
|
|
|
$output = $purifier->purify($input);
|
|
|
|
$this->assertIdentical($input, $output);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-26 04:31:04 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_addElement()
|
|
|
|
{
|
2010-12-30 23:51:53 +00:00
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
2008-04-23 02:40:17 +00:00
|
|
|
$def = $config->getHTMLDefinition(true);
|
2007-06-19 01:55:31 +00:00
|
|
|
$def->addElement('marquee', 'Inline', 'Inline', 'Common', array('width' => 'Length'));
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-19 01:55:31 +00:00
|
|
|
$purifier = new HTMLPurifier($config);
|
|
|
|
$input = '<span><marquee width="50">Foobar</marquee></span>';
|
|
|
|
$output = $purifier->purify($input);
|
|
|
|
$this->assertIdentical($input, $output);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-19 01:55:31 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_injector()
|
|
|
|
{
|
2008-06-09 01:23:05 +00:00
|
|
|
generate_mock_once('HTMLPurifier_Injector');
|
|
|
|
$injector = new HTMLPurifier_InjectorMock();
|
|
|
|
$injector->name = 'MyInjector';
|
2016-03-24 07:08:03 +00:00
|
|
|
$injector->returns('checkNeeded', false);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-09 01:23:05 +00:00
|
|
|
$module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
|
|
|
|
$module->info_injector[] = $injector;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-09 01:23:05 +00:00
|
|
|
$this->assertIdentical($this->config->getHTMLDefinition()->info_injector,
|
|
|
|
array(
|
|
|
|
'MyInjector' => $injector,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_injectorMissingNeeded()
|
|
|
|
{
|
2008-06-09 01:23:05 +00:00
|
|
|
generate_mock_once('HTMLPurifier_Injector');
|
|
|
|
$injector = new HTMLPurifier_InjectorMock();
|
|
|
|
$injector->name = 'MyInjector';
|
2016-03-24 07:08:03 +00:00
|
|
|
$injector->returns('checkNeeded', 'a');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-09 01:23:05 +00:00
|
|
|
$module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
|
|
|
|
$module->info_injector[] = $injector;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-09 01:23:05 +00:00
|
|
|
$this->assertIdentical($this->config->getHTMLDefinition()->info_injector,
|
|
|
|
array()
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_injectorIntegration()
|
|
|
|
{
|
2008-06-09 01:23:05 +00:00
|
|
|
$module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
|
|
|
|
$module->info_injector[] = 'Linkify';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-09 01:23:05 +00:00
|
|
|
$this->assertIdentical(
|
|
|
|
$this->config->getHTMLDefinition()->info_injector,
|
|
|
|
array('Linkify' => new HTMLPurifier_Injector_Linkify())
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_injectorIntegrationFail()
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$this->config->set('HTML.Allowed', 'p');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-09 01:23:05 +00:00
|
|
|
$module = $this->config->getHTMLDefinition(true)->getAnonymousModule();
|
|
|
|
$module->info_injector[] = 'Linkify';
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-06-09 01:23:05 +00:00
|
|
|
$this->assertIdentical(
|
|
|
|
$this->config->getHTMLDefinition()->info_injector,
|
|
|
|
array()
|
|
|
|
);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function test_notAllowedRequiredAttributeError()
|
|
|
|
{
|
2010-09-04 05:30:32 +00:00
|
|
|
$this->expectError("Required attribute 'src' in element 'img' was not allowed, which means 'img' will not be allowed either");
|
|
|
|
$this->config->set('HTML.Allowed', 'img[alt]');
|
|
|
|
$this->config->getHTMLDefinition();
|
|
|
|
}
|
|
|
|
|
2019-07-14 17:15:31 +00:00
|
|
|
public function test_manyNestedTags()
|
|
|
|
{
|
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
|
|
$config->set('Core.AllowParseManyTags', true);
|
|
|
|
$purifier = new HTMLPurifier($config);
|
|
|
|
|
|
|
|
$input = 'I am inside a lot of tags';
|
|
|
|
for ($i = 0; $i < 300; $i++) {
|
|
|
|
$input = '<div>' . $input . '</div>';
|
|
|
|
}
|
|
|
|
$output = $purifier->purify($input);
|
|
|
|
|
|
|
|
$this->assertIdentical($input, $output);
|
|
|
|
}
|
|
|
|
|
2024-04-12 00:52:45 +00:00
|
|
|
public function test_removeBlanks()
|
|
|
|
{
|
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
|
|
$config->set('Core.RemoveBlanks', true);
|
|
|
|
|
|
|
|
$input = file_get_contents(__DIR__ . '/FixtureData/RemoveBlankTestCaseInput.html');
|
|
|
|
$expected = file_get_contents(__DIR__ . '/FixtureData/RemoveBlankTestCaseOutput.html');
|
|
|
|
|
|
|
|
$purifier = new HTMLPurifier($config);
|
|
|
|
$actual = $purifier->purify($input);
|
|
|
|
$this->assertIdentical($expected, $actual);
|
|
|
|
}
|
|
|
|
|
2007-05-29 21:26:43 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|