mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-11-09 23:28:42 +00:00
Augment URISchemeRegistry with the ability to overload/register your own schemes.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@215 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
parent
10ea44932a
commit
6c3d364213
@ -46,10 +46,10 @@ class HTMLPurifier_AttrDef_URI extends HTMLPurifier_AttrDef
|
||||
// no need to validate the scheme's fmt since we do that when we
|
||||
// retrieve the specific scheme object from the registry
|
||||
$scheme = ctype_lower($scheme) ? $scheme : strtolower($scheme);
|
||||
$scheme_obj =& $registry->getScheme($scheme);
|
||||
$scheme_obj =& $registry->getScheme($scheme, $config);
|
||||
if (!$scheme_obj) return ''; // invalid scheme, clean it out
|
||||
} else {
|
||||
$scheme_obj =& $registry->getScheme($config->get('URI', 'DefaultScheme'));
|
||||
$scheme_obj =& $registry->getScheme($config->get('URI', 'DefaultScheme'), $config);
|
||||
}
|
||||
|
||||
|
||||
|
@ -52,6 +52,10 @@ class HTMLPurifier_URISchemeRegistry
|
||||
return $this->schemes[$scheme];
|
||||
}
|
||||
|
||||
function register($scheme, &$scheme_obj) {
|
||||
$this->schemes[$scheme] =& $scheme_obj;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
?>
|
||||
|
@ -195,11 +195,11 @@ class HTMLPurifier_AttrDef_URITest extends HTMLPurifier_AttrDefHarness
|
||||
$this->scheme =& new HTMLPurifier_URISchemeMock($this);
|
||||
|
||||
// here are the schemes we will support with overloaded mocks
|
||||
$registry->setReturnReference('getScheme', $this->scheme, array('http'));
|
||||
$registry->setReturnReference('getScheme', $this->scheme, array('mailto'));
|
||||
$registry->setReturnReference('getScheme', $this->scheme, array('http', $this->config));
|
||||
$registry->setReturnReference('getScheme', $this->scheme, array('mailto', $this->config));
|
||||
|
||||
// default return value is false (meaning no scheme defined: reject)
|
||||
$registry->setReturnValue('getScheme', false, array('*'));
|
||||
$registry->setReturnValue('getScheme', false, array('*', $this->config));
|
||||
|
||||
if ($this->components === false) {
|
||||
$this->scheme->expectNever('validateComponents');
|
||||
|
@ -10,9 +10,9 @@ class HTMLPurifier_AttrDefHarness extends UnitTestCase
|
||||
// cannot be used for accumulator
|
||||
function assertDef($string, $expect = true, $message = '%s') {
|
||||
// $expect can be a string or bool
|
||||
$this->setUpAssertDef();
|
||||
if (!$this->config) $this->config = HTMLPurifier_Config::createDefault();
|
||||
if (!$this->context) $this->context = new HTMLPurifier_AttrContext();
|
||||
$this->setUpAssertDef();
|
||||
$result = $this->def->validate($string, $this->config, $this->context);
|
||||
if ($expect === true) {
|
||||
$this->assertIdentical($string, $result, $message);
|
||||
|
@ -7,11 +7,32 @@ class HTMLPurifier_URISchemeRegistryTest extends UnitTestCase
|
||||
|
||||
function test() {
|
||||
|
||||
$registry =& HTMLPurifier_URISchemeRegistry::instance();
|
||||
generate_mock_once('HTMLPurifier_URIScheme');
|
||||
|
||||
$config = HTMLPurifier_Config::createDefault();
|
||||
$config->set('URI', 'AllowedSchemes', array('http' => true, 'telnet' => true));
|
||||
|
||||
$registry = new HTMLPurifier_URISchemeRegistry();
|
||||
$this->assertIsA($registry->getScheme('http'), 'HTMLPurifier_URIScheme_http');
|
||||
|
||||
// to come: overloading and custom schemes, as well as changing the
|
||||
// configuration values used by this class
|
||||
$scheme_http = new HTMLPurifier_URISchemeMock($this);
|
||||
$scheme_telnet = new HTMLPurifier_URISchemeMock($this);
|
||||
$scheme_foobar = new HTMLPurifier_URISchemeMock($this);
|
||||
|
||||
// register a new scheme
|
||||
$registry->register('telnet', $scheme_telnet);
|
||||
$this->assertIdentical($registry->getScheme('telnet', $config), $scheme_telnet);
|
||||
|
||||
// overload a scheme, this is FINAL (forget about defaults)
|
||||
$registry->register('http', $scheme_http);
|
||||
$this->assertIdentical($registry->getScheme('http', $config), $scheme_http);
|
||||
|
||||
// when we register a scheme, it's automatically allowed
|
||||
$registry->register('foobar', $scheme_foobar);
|
||||
$this->assertIdentical($registry->getScheme('foobar', $config), $scheme_foobar);
|
||||
|
||||
// however, don't try to get a scheme that isn't allowed
|
||||
$this->assertNull($registry->getScheme('ftp', $config));
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user