diff --git a/NEWS b/NEWS index 0cf48194..8e247f58 100644 --- a/NEWS +++ b/NEWS @@ -21,7 +21,9 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier ! New HTMLPurifier_Filter_ExtractStyleBlocks for extracting in a font-family prop). - if (!$this->_disableCharacterEscaping) { + if ($config->get('Filter', 'ExtractStyleBlocksEscaping')) { $css = str_replace( array('<', '>', '&'), array('\3C ', '\3E ', '\26 '), diff --git a/tests/HTMLPurifier/Filter/ExtractStyleBlocksTest.php b/tests/HTMLPurifier/Filter/ExtractStyleBlocksTest.php index 7ba464e3..51e789d5 100644 --- a/tests/HTMLPurifier/Filter/ExtractStyleBlocksTest.php +++ b/tests/HTMLPurifier/Filter/ExtractStyleBlocksTest.php @@ -65,6 +65,8 @@ class HTMLPurifier_Filter_ExtractStyleBlocksTest extends HTMLPurifier_Harness function assertCleanCSS($input, $expect = true) { $filter = new HTMLPurifier_Filter_ExtractStyleBlocks(); if ($expect === true) $expect = $input; + $this->normalize($input); + $this->normalize($expect); $result = $filter->cleanCSS($input, $this->config, $this->context); $this->assertIdentical($result, $expect); } @@ -103,10 +105,79 @@ class HTMLPurifier_Filter_ExtractStyleBlocksTest extends HTMLPurifier_Harness } function test_cleanCSS_noEscapeCodes() { - $filter = new HTMLPurifier_Filter_ExtractStyleBlocks(null, true); - $input = ".class {\nfont-family:'';\n}"; - $result = $filter->cleanCSS($input, $this->config, $this->context); - $this->assertIdentical($result, $input); + $this->config->set('Filter', 'ExtractStyleBlocksEscaping', false); + $this->assertCleanCSS( + ".class {\nfont-family:'';\n}" + ); + } + + function test_cleanCSS_scope() { + $this->config->set('Filter', 'ExtractStyleBlocksScope', '#foo'); + $this->assertCleanCSS( + "p {\ntext-indent:1em;\n}", + "#foo p {\ntext-indent:1em;\n}" + ); + } + + function test_cleanCSS_scopeWithSelectorCommas() { + $this->config->set('Filter', 'ExtractStyleBlocksScope', '#foo'); + $this->assertCleanCSS( + "b, i {\ntext-decoration:underline;\n}", + "#foo b, #foo i {\ntext-decoration:underline;\n}" + ); + } + + function test_cleanCSS_scopeWithNaughtySelector() { + $this->config->set('Filter', 'ExtractStyleBlocksScope', '#foo'); + $this->assertCleanCSS( + " + p {\ntext-indent:1em;\n}", + "#foo p {\ntext-indent:1em;\n}" + ); + } + + function test_cleanCSS_scopeWithMultipleNaughtySelectors() { + $this->config->set('Filter', 'ExtractStyleBlocksScope', '#foo'); + $this->assertCleanCSS( + " ++ ++ p {\ntext-indent:1em;\n}", + "#foo p {\ntext-indent:1em;\n}" + ); + } + + function test_cleanCSS_scopeWithCommas() { + $this->config->set('Filter', 'ExtractStyleBlocksScope', '#foo, .bar'); + $this->assertCleanCSS( + "p {\ntext-indent:1em;\n}", + "#foo p, .bar p {\ntext-indent:1em;\n}" + ); + } + + function test_cleanCSS_scopeAllWithCommas() { + $this->config->set('Filter', 'ExtractStyleBlocksScope', '#foo, .bar'); + $this->assertCleanCSS( + "p, div {\ntext-indent:1em;\n}", + "#foo p, #foo div, .bar p, .bar div {\ntext-indent:1em;\n}" + ); + } + + function test_cleanCSS_scopeWithConflicts() { + $this->config->set('Filter', 'ExtractStyleBlocksScope', 'p'); + $this->assertCleanCSS( +"div { +text-align:right; +} + +p div { +text-align:left; +}", + +"p div { +text-align:right; +} + +p p div { +text-align:left; +}" + ); } } \ No newline at end of file diff --git a/tests/HTMLPurifier/Harness.php b/tests/HTMLPurifier/Harness.php index 5e1f2a1c..34164bc7 100644 --- a/tests/HTMLPurifier/Harness.php +++ b/tests/HTMLPurifier/Harness.php @@ -39,6 +39,13 @@ class HTMLPurifier_Harness extends UnitTestCase return array(HTMLPurifier_Config::createDefault(), new HTMLPurifier_Context); } + /** + * Normalizes a string to Unix (\n) endings + */ + function normalize(&$string) { + $string = str_replace(array("\r\n", "\r"), "\n", $string); + } + /** * If $expect is false, ignore $result and check if status failed. * Otherwise, check if $status if true and $result === $expect.