config);
$purifier->addFilter(new HTMLPurifier_Filter_ExtractStyleBlocks());
$result = $purifier->purify('Test');
$this->assertIdentical($result, 'Test');
$this->assertIdentical($purifier->context->get('StyleBlocks'),
array(
".foo {\ntext-align:center;\n}",
"* {\nfont-size:12pt;\n}"
)
);
}
function assertExtractStyleBlocks($html, $expect = true, $styles = array()) {
$filter = new HTMLPurifier_Filter_ExtractStyleBlocks(false); // disable cleaning
if ($expect === true) $expect = $html;
$result = $filter->preFilter($html, $this->config, $this->context);
$this->assertIdentical($result, $expect);
$this->assertIdentical($this->context->get('StyleBlocks'), $styles);
}
function test_extractStyleBlocks_preserve() {
$this->assertExtractStyleBlocks('Foobar');
}
function test_extractStyleBlocks_allStyle() {
$this->assertExtractStyleBlocks('', '', array('foo'));
}
function test_extractStyleBlocks_multipleBlocks() {
$this->assertExtractStyleBlocks(
"NOP",
"NOP",
array('1', '2', '4')
);
}
function test_extractStyleBlocks_blockWithAttributes() {
$this->assertExtractStyleBlocks(
'',
'',
array('css')
);
}
function test_extractStyleBlocks_styleWithPadding() {
$this->assertExtractStyleBlocks(
"AlasAwesome\n Trendy!",
"AlasAwesome\n Trendy!",
array('foo')
);
}
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);
}
function test_cleanCSS_malformed() {
$this->assertCleanCSS('', '');
}
function test_cleanCSS_selector() {
$this->assertCleanCSS("a .foo #id div.cl#foo {\nfont-weight:700;\n}");
}
function test_cleanCSS_angledBrackets() {
$this->assertCleanCSS(
".class {\nfont-family:'';\n}",
".class {\nfont-family:'\\3C /style\\3E ';\n}"
);
}
function test_cleanCSS_angledBrackets2() {
// CSSTidy's behavior in this case is wrong, and should be fixed
//$this->assertCleanCSS(
// "span[title=\"\"] {\nfont-size:12pt;\n}",
// "span[title=\"\\3C /style\\3E \"] {\nfont-size:12pt;\n}"
//);
}
function test_cleanCSS_bogus() {
$this->assertCleanCSS("div {bogus:tree;}", "div {\n}");
}
function test_cleanCSS_escapeCodes() {
$this->assertCleanCSS(
".class {\nfont-family:'\\3C /style\\3E ';\n}"
);
}
function test_cleanCSS_noEscapeCodes() {
$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}", '');
}
function test_cleanCSS_scopeWithMultipleNaughtySelectors() {
$this->config->set('Filter', 'ExtractStyleBlocksScope', '#foo');
$this->assertCleanCSS(" ++ ++ 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;
}"
);
}
}