diff --git a/docs/ref-loose-vs-strict.txt b/docs/ref-loose-vs-strict.txt index 664ceacd..110bda37 100644 --- a/docs/ref-loose-vs-strict.txt +++ b/docs/ref-loose-vs-strict.txt @@ -12,7 +12,7 @@ BLOCKQUOTE changes from 'flow' to 'block' U, S, STRIKE cut behavior: replace with appropriate inline span + CSS ADDRESS from potpourri to Inline (removes p tags) - behavior: p tags silently dropped or replaced with something + behavior: p tags silently dropped or replaced with something (
) == Things we can loosen up == diff --git a/library/HTMLPurifier/HTMLDefinition.php b/library/HTMLPurifier/HTMLDefinition.php index 44e16dbc..c2a4078d 100644 --- a/library/HTMLPurifier/HTMLDefinition.php +++ b/library/HTMLPurifier/HTMLDefinition.php @@ -35,6 +35,11 @@ HTMLPurifier_ConfigSchema::define( 'versions.' ); +HTMLPurifier_ConfigSchema::define( + 'HTML', 'Strict', false, 'bool', + 'Determines whether or not to use Transitional (loose) or Strict rulesets.' +); + /** * Defines the purified HTML type with large amounts of objects. * @@ -111,13 +116,19 @@ class HTMLPurifier_HTMLDefinition array( 'ins', 'del', 'blockquote', 'dd', 'li', 'div', 'em', 'strong', 'dfn', 'code', 'samp', 'kbd', 'var', 'cite', 'abbr', 'acronym', - 'q', 'sub', 'tt', 'sup', 'i', 'b', 'big', 'small', 'u', 's', - 'strike', 'bdo', 'span', 'dt', 'p', 'h1', 'h2', 'h3', 'h4', + 'q', 'sub', 'tt', 'sup', 'i', 'b', 'big', 'small', + 'bdo', 'span', 'dt', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ol', 'ul', 'dl', 'address', 'img', 'br', 'hr', 'pre', 'a', 'table', 'caption', 'thead', 'tfoot', 'tbody', 'colgroup', 'col', 'td', 'th', 'tr' ); + if (!$config->get('HTML', 'Strict')) { + $allowed_tags[] = 'u'; + $allowed_tags[] = 's'; + $allowed_tags[] = 'strike'; + } + foreach ($allowed_tags as $tag) { $this->info[$tag] = new HTMLPurifier_ElementDef(); } @@ -161,6 +172,7 @@ class HTMLPurifier_HTMLDefinition $e_lists = 'ul | ol | dl'; $e_blocktext = 'pre | hr | blockquote | address'; $e_block = "p | $e_heading | div | $e_lists | $e_blocktext | table"; + $e_Block = new HTMLPurifier_ChildDef_Optional($e_block); $e__flow = "#PCDATA | $e_block | $e_inline | $e_misc"; $e_Flow = new HTMLPurifier_ChildDef_Optional($e__flow); $e_a_content = new HTMLPurifier_ChildDef_Optional("#PCDATA". @@ -176,11 +188,16 @@ class HTMLPurifier_HTMLDefinition $this->info['del']->child = new HTMLPurifier_ChildDef_Chameleon($e__inline, $e__flow); - $this->info['blockquote']->child= $this->info['dd']->child = $this->info['li']->child = $this->info['div']->child = $e_Flow; + if ($config->get('HTML', 'Strict')) { + $this->info['blockquote']->child = $e_Block; + } else { + $this->info['blockquote']->child = $e_Flow; + } + $this->info['caption']->child = $this->info['em']->child = $this->info['strong']->child = @@ -220,9 +237,13 @@ class HTMLPurifier_HTMLDefinition $this->info['dl']->child = new HTMLPurifier_ChildDef_Required('dt|dd'); - $this->info['address']->child = - new HTMLPurifier_ChildDef_Optional("#PCDATA | p | $e_inline". - " | $e_misc_inline"); + if ($config->get('HTML', 'Strict')) { + $this->info['address']->child = $e_Inline + } else { + $this->info['address']->child = + new HTMLPurifier_ChildDef_Optional("#PCDATA | p | $e_inline". + " | $e_misc_inline"); + } $this->info['img']->child = $this->info['br']->child = @@ -254,7 +275,6 @@ class HTMLPurifier_HTMLDefinition $this->info[$name]->type = 'inline'; } - $e_Block = new HTMLPurifier_ChildDef_Optional($e_block); foreach ($e_Block->elements as $name => $bool) { $this->info[$name]->type = 'block'; } diff --git a/tests/HTMLPurifier/Test.php b/tests/HTMLPurifier/Test.php index ccb7af43..bbb8fada 100644 --- a/tests/HTMLPurifier/Test.php +++ b/tests/HTMLPurifier/Test.php @@ -14,11 +14,28 @@ class HTMLPurifier_Test extends UnitTestCase $this->assertIdentical($expect, $result); } - function test() { - $config = HTMLPurifier_Config::createDefault(); - $this->purifier = new HTMLPurifier($config); + function testNull() { + $this->purifier = new HTMLPurifier(); $this->assertPurification("Null byte\0", "Null byte"); } + + function testStrict() { + $config = HTMLPurifier_Config::createDefault(); + $config->set('HTML', 'Strict', true); + $this->purifier = new HTMLPurifier($config); + + $this->assertPurification( + 'Illegal underline', + 'Illegal underline' + ); + + $this->assertPurification( + '
Illegal contents
', + '
' + ); + + } + } ?> \ No newline at end of file