From 6c3d364213420be23fe8b8eb64164fcd48b75ca9 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sat, 12 Aug 2006 17:06:14 +0000 Subject: [PATCH] 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 --- library/HTMLPurifier/AttrDef/URI.php | 4 +-- library/HTMLPurifier/URISchemeRegistry.php | 6 ++++- tests/HTMLPurifier/AttrDef/URITest.php | 6 ++--- tests/HTMLPurifier/AttrDefHarness.php | 2 +- tests/HTMLPurifier/URISchemeRegistryTest.php | 27 +++++++++++++++++--- 5 files changed, 35 insertions(+), 10 deletions(-) diff --git a/library/HTMLPurifier/AttrDef/URI.php b/library/HTMLPurifier/AttrDef/URI.php index 1f5cd816..850e953b 100644 --- a/library/HTMLPurifier/AttrDef/URI.php +++ b/library/HTMLPurifier/AttrDef/URI.php @@ -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); } diff --git a/library/HTMLPurifier/URISchemeRegistry.php b/library/HTMLPurifier/URISchemeRegistry.php index fb2540f9..f925d009 100644 --- a/library/HTMLPurifier/URISchemeRegistry.php +++ b/library/HTMLPurifier/URISchemeRegistry.php @@ -52,6 +52,10 @@ class HTMLPurifier_URISchemeRegistry return $this->schemes[$scheme]; } + function register($scheme, &$scheme_obj) { + $this->schemes[$scheme] =& $scheme_obj; + } + } -?> \ No newline at end of file +?> diff --git a/tests/HTMLPurifier/AttrDef/URITest.php b/tests/HTMLPurifier/AttrDef/URITest.php index 5b4d1a58..13c3153d 100644 --- a/tests/HTMLPurifier/AttrDef/URITest.php +++ b/tests/HTMLPurifier/AttrDef/URITest.php @@ -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'); diff --git a/tests/HTMLPurifier/AttrDefHarness.php b/tests/HTMLPurifier/AttrDefHarness.php index 893877d6..8c130ef9 100644 --- a/tests/HTMLPurifier/AttrDefHarness.php +++ b/tests/HTMLPurifier/AttrDefHarness.php @@ -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); diff --git a/tests/HTMLPurifier/URISchemeRegistryTest.php b/tests/HTMLPurifier/URISchemeRegistryTest.php index 7b46b3bd..b736fd30 100644 --- a/tests/HTMLPurifier/URISchemeRegistryTest.php +++ b/tests/HTMLPurifier/URISchemeRegistryTest.php @@ -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)); }