diff --git a/TODO b/TODO index dd359c0e..aff5e526 100644 --- a/TODO +++ b/TODO @@ -46,7 +46,7 @@ TODO List # Formatters for plaintext - Smileys - Linkification for HTML Purifier docs: notably configuration and classes - - Standardize token armor for all areas of armor + - Standardize token armor for all areas of processing - Fixes for Firefox's inability to handle COL alignment props (Bug 915) - Automatically add non-breaking spaces to empty table cells when empty-cells:show is applied to have compatibility with Internet Explorer diff --git a/library/HTMLPurifier/ConfigSchema.php b/library/HTMLPurifier/ConfigSchema.php index eb5d3b2c..27b45c16 100644 --- a/library/HTMLPurifier/ConfigSchema.php +++ b/library/HTMLPurifier/ConfigSchema.php @@ -67,7 +67,8 @@ class HTMLPurifier_ConfigSchema { $this->defineNamespace('URI', 'Features regarding Uniform Resource Identifiers.'); $this->defineNamespace('HTML', 'Configuration regarding allowed HTML.'); $this->defineNamespace('CSS', 'Configuration regarding allowed CSS.'); - $this->defineNamespace('AutoFormat', 'Configuration regarding auto-formatting functionality such as auto-paragraphing or linkification.'); + $this->defineNamespace('AutoFormat', 'Configuration for activating auto-formatting functionality (also known as Injectors)'); + $this->defineNamespace('AutoFormatParam', 'Configuration for customizing auto-formatting functionality'); $this->defineNamespace('Output', 'Configuration relating to the generation of (X)HTML.'); $this->defineNamespace('Cache', 'Configuration for DefinitionCache and related subclasses.'); $this->defineNamespace('Test', 'Developer testing configuration for our unit tests.'); diff --git a/library/HTMLPurifier/Strategy/MakeWellFormed.php b/library/HTMLPurifier/Strategy/MakeWellFormed.php index dda2b9e1..884632cd 100644 --- a/library/HTMLPurifier/Strategy/MakeWellFormed.php +++ b/library/HTMLPurifier/Strategy/MakeWellFormed.php @@ -7,6 +7,17 @@ require_once 'HTMLPurifier/Generator.php'; require_once 'HTMLPurifier/Injector/AutoParagraph.php'; require_once 'HTMLPurifier/Injector/Linkify.php'; +HTMLPurifier_ConfigSchema::define( + 'AutoFormat', 'Custom', array(), 'list', ' +

+ This directive can be used to add custom auto-format injectors. + Specify an array of injector names (class name minus the prefix) + or concrete implementations. Injector class must exist. This directive + has been available since 2.0.1. +

+' +); + /** * Takes tokens makes them well-formed (balance end tags, etc.) */ @@ -48,14 +59,19 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy $this->injectors = array(); - // we need a generic way of adding injectors, and also its own - // configuration namespace - if ($config->get('AutoFormat', 'AutoParagraph')) { - $this->injectors[] = new HTMLPurifier_Injector_AutoParagraph(); + $injectors = $config->getBatch('AutoFormat'); + $custom_injectors = $injectors['Custom']; + unset($injectors['Custom']); // special case + foreach ($injectors as $injector => $b) { + $injector = "HTMLPurifier_Injector_$injector"; + if ($b) $this->injectors[] = new $injector; } - - if ($config->get('AutoFormat', 'Linkify')) { - $this->injectors[] = new HTMLPurifier_Injector_Linkify(); + foreach ($custom_injectors as $injector) { + if (is_string($injector)) { + $injector = "HTMLPurifier_Injector_$injector"; + $injector = new $injector; + } + $this->injectors[] = $injector; } // array index of the injector that resulted in an array diff --git a/tests/HTMLPurifier/Injector/AutoParagraphTest.php b/tests/HTMLPurifier/Injector/AutoParagraphTest.php new file mode 100644 index 00000000..0c64d54f --- /dev/null +++ b/tests/HTMLPurifier/Injector/AutoParagraphTest.php @@ -0,0 +1,247 @@ +config = array('AutoFormat.AutoParagraph' => true); + } + + function test() { + $this->assertResult( + 'Foobar', + '

Foobar

' + ); + + $this->assertResult( +'Par 1 +Par 1 still', +'

Par 1 +Par 1 still

' + ); + + $this->assertResult( +'Par1 + +Par2', + '

Par1

Par2

' + ); + + $this->assertResult( +'Par1 + + + +Par2', + '

Par1

Par2

' + ); + + $this->assertResult( +'Par1 + +Par2', + '

Par1

Par2

' + ); + + + $this->assertResult( +'Par1 + +Par2', +'

Par1 + +Par2

' + ); + + $this->assertResult( + 'Par1

Par2

', + '

Par1

Par2

' + ); + + $this->assertResult( + 'Par1', + '

Par1

' + ); + + $this->assertResult( +'
Par1
+
+Par1
' + ); + + $this->assertResult( +'Par1 + + ', +'

Par1

' + ); + $this->assertResult( +'Par1 + +
Par2
+ +Par3', +'

Par1

Par2

Par3

' + ); + + $this->assertResult( +'Par1', + '

Par1

' + ); + + $this->assertResult( +' + +Par', + '

Par

' + ); + + $this->assertResult( +' + +Par + +', + '

Par

' + ); + + $this->assertResult( +'
Par1 + +Par2
', + '

Par1

Par2

' + ); + + $this->assertResult( +'
Par1 + +Par2
', + '

Par1

Par2

' + ); + + $this->assertResult('
Par1
'); + + $this->assertResult( +'
Par1 + +Par2
', + '

Par1

Par2

' + ); + + $this->assertResult( +'
Par1
+
+Par2
', + true + ); + + $this->assertResult( +'

Foo + +Bar

', + '

Foo

Bar

' + ); + + $this->assertResult( +'

Foo + +Bar

', + '

Foo

Bar

' + ); + + $this->assertResult( +'
Foo
', + '
Foo
' + ); + + $this->assertResult( +'
Par1 + +Par2
', + '

Par1

Par2

' + ); + + $this->assertResult( +'', true + ); + + $this->assertResult( +'
+ +Bar + +
', + '

Bar

' + ); + + $this->assertResult( +'Par1a + + + +Par2', + '

Par1a

Par2

' + ); + + $this->assertResult( +'Par1 + +Par2

', + '

Par1

Par2

' + ); + + $this->assertResult( +'Par1 + +Par2', + '

Par1

Par2

' + ); + + $this->assertResult( +'
+Par1 +
', true + ); + + $this->assertResult( +'
Par1 + +
Par2
', +'

Par1

Par2
' + ); + + $this->assertResult( +'
Par1 +
Par2
', +'

Par1 +

Par2
' + ); + + $this->assertResult( +'Par1 +
Par2
', +'

Par1 +

Par2
' + ); + + } + + function testInlineRootNode() { + $this->assertResult( +'Par + +Par2', + true, + array('AutoFormat.AutoParagraph' => true, 'HTML.Parent' => 'span') + ); + } + +} + +?> \ No newline at end of file diff --git a/tests/HTMLPurifier/Injector/LinkifyTest.php b/tests/HTMLPurifier/Injector/LinkifyTest.php new file mode 100644 index 00000000..030a47e8 --- /dev/null +++ b/tests/HTMLPurifier/Injector/LinkifyTest.php @@ -0,0 +1,39 @@ +config = array('AutoFormat.Linkify' => true); + } + + function testLinkify() { + + $this->assertResult( + 'http://example.com', + 'http://example.com' + ); + + $this->assertResult( + 'http://example.com', + 'http://example.com' + ); + + $this->assertResult( + 'This URL http://example.com is what you need', + 'This URL http://example.com is what you need' + ); + + $this->assertResult( + 'http://example.com/' + ); + + } + +} + +?> \ No newline at end of file diff --git a/tests/HTMLPurifier/InjectorHarness.php b/tests/HTMLPurifier/InjectorHarness.php new file mode 100644 index 00000000..5b337e3b --- /dev/null +++ b/tests/HTMLPurifier/InjectorHarness.php @@ -0,0 +1,16 @@ +obj = new HTMLPurifier_Strategy_MakeWellFormed(); + } + +} + +?> \ No newline at end of file diff --git a/tests/HTMLPurifier/Strategy/MakeWellFormedTest.php b/tests/HTMLPurifier/Strategy/MakeWellFormedTest.php index 52d7395a..08c3197c 100644 --- a/tests/HTMLPurifier/Strategy/MakeWellFormedTest.php +++ b/tests/HTMLPurifier/Strategy/MakeWellFormedTest.php @@ -75,262 +75,6 @@ class HTMLPurifier_Strategy_MakeWellFormedTest extends HTMLPurifier_StrategyHarn } - function testAutoParagraph() { - $this->config = array('AutoFormat.AutoParagraph' => true); - - $this->assertResult( - 'Foobar', - '

Foobar

' - ); - - $this->assertResult( -'Par 1 -Par 1 still', -'

Par 1 -Par 1 still

' - ); - - $this->assertResult( -'Par1 - -Par2', - '

Par1

Par2

' - ); - - $this->assertResult( -'Par1 - - - -Par2', - '

Par1

Par2

' - ); - - $this->assertResult( -'Par1 - -Par2', - '

Par1

Par2

' - ); - - - $this->assertResult( -'Par1 - -Par2', -'

Par1 - -Par2

' - ); - - $this->assertResult( - 'Par1

Par2

', - '

Par1

Par2

' - ); - - $this->assertResult( - 'Par1', - '

Par1

' - ); - - $this->assertResult( -'
Par1
-
-Par1
' - ); - - $this->assertResult( -'Par1 - - ', -'

Par1

' - ); - $this->assertResult( -'Par1 - -
Par2
- -Par3', -'

Par1

Par2

Par3

' - ); - - $this->assertResult( -'Par1', - '

Par1

' - ); - - $this->assertResult( -' - -Par', - '

Par

' - ); - - $this->assertResult( -' - -Par - -', - '

Par

' - ); - - $this->assertResult( -'
Par1 - -Par2
', - '

Par1

Par2

' - ); - - $this->assertResult( -'
Par1 - -Par2
', - '

Par1

Par2

' - ); - - $this->assertResult('
Par1
'); - - $this->assertResult( -'
Par1 - -Par2
', - '

Par1

Par2

' - ); - - $this->assertResult( -'
Par1
-
-Par2
', - true - ); - - $this->assertResult( -'

Foo - -Bar

', - '

Foo

Bar

' - ); - - $this->assertResult( -'

Foo - -Bar

', - '

Foo

Bar

' - ); - - $this->assertResult( -'
Foo
', - '
Foo
' - ); - - $this->assertResult( -'
Par1 - -Par2
', - '

Par1

Par2

' - ); - - $this->assertResult( -'', true - ); - - $this->assertResult( -'
- -Bar - -
', - '

Bar

' - ); - - $this->assertResult( -'Par1a - - - -Par2', - '

Par1a

Par2

' - ); - - $this->assertResult( -'Par1 - -Par2

', - '

Par1

Par2

' - ); - - $this->assertResult( -'Par1 - -Par2', - '

Par1

Par2

' - ); - - $this->assertResult( -'
-Par1 -
', true - ); - - $this->assertResult( -'
Par1 - -
Par2
', -'

Par1

Par2
' - ); - - $this->assertResult( -'
Par1 -
Par2
', -'

Par1 -

Par2
' - ); - - $this->assertResult( -'Par1 -
Par2
', -'

Par1 -

Par2
' - ); - - $this->assertResult( -'Par - -Par2', - true, - array('AutoFormat.AutoParagraph' => true, 'HTML.Parent' => 'span') - ); - - } - - function testLinkify() { - - $this->config = array('AutoFormat.Linkify' => true); - - $this->assertResult( - 'http://example.com', - 'http://example.com' - ); - - $this->assertResult( - 'http://example.com', - 'http://example.com' - ); - - $this->assertResult( - 'This URL http://example.com is what you need', - 'This URL http://example.com is what you need' - ); - - $this->assertResult( - 'http://example.com/' - ); - - } - function testMultipleInjectors() { $this->config = array('AutoFormat.AutoParagraph' => true, 'AutoFormat.Linkify' => true); diff --git a/tests/test_files.php b/tests/test_files.php index c2c8bada..a42191d9 100644 --- a/tests/test_files.php +++ b/tests/test_files.php @@ -81,6 +81,8 @@ $test_files[] = 'HTMLPurifier/HTMLModuleTest.php'; $test_files[] = 'HTMLPurifier/HTMLModule/ScriptingTest.php'; $test_files[] = 'HTMLPurifier/HTMLModule/TidyTest.php'; $test_files[] = 'HTMLPurifier/IDAccumulatorTest.php'; +$test_files[] = 'HTMLPurifier/Injector/AutoParagraphTest.php'; +$test_files[] = 'HTMLPurifier/Injector/LinkifyTest.php'; $test_files[] = 'HTMLPurifier/LanguageFactoryTest.php'; $test_files[] = 'HTMLPurifier/LanguageTest.php'; $test_files[] = 'HTMLPurifier/Lexer/DirectLexTest.php';