Rewrite CSS url() and font-family output logic.
[htmlpurifier.git] / tests / HTMLPurifier / Filter / ExtractStyleBlocksTest.php
blobe93d65e6f6451c98c8c2696410f10d06ff364de2
1 <?php
3 /**
4 * @todo Assimilate CSSTidy into our library
5 */
6 class HTMLPurifier_Filter_ExtractStyleBlocksTest extends HTMLPurifier_Harness
9 // usual use case:
10 function test_tokenizeHTML_extractStyleBlocks() {
11 $this->config->set('Filter.ExtractStyleBlocks', true);
12 $purifier = new HTMLPurifier($this->config);
13 $result = $purifier->purify('<style type="text/css">.foo {text-align:center;bogus:remove-me;}</style>Test<style>* {font-size:12pt;}</style>');
14 $this->assertIdentical($result, 'Test');
15 $this->assertIdentical($purifier->context->get('StyleBlocks'),
16 array(
17 ".foo {\ntext-align:center;\n}",
18 "* {\nfont-size:12pt;\n}"
23 function assertExtractStyleBlocks($html, $expect = true, $styles = array()) {
24 $filter = new HTMLPurifier_Filter_ExtractStyleBlocks(); // disable cleaning
25 if ($expect === true) $expect = $html;
26 $this->config->set('Filter.ExtractStyleBlocks.TidyImpl', false);
27 $result = $filter->preFilter($html, $this->config, $this->context);
28 $this->assertIdentical($result, $expect);
29 $this->assertIdentical($this->context->get('StyleBlocks'), $styles);
32 function test_extractStyleBlocks_preserve() {
33 $this->assertExtractStyleBlocks('Foobar');
36 function test_extractStyleBlocks_allStyle() {
37 $this->assertExtractStyleBlocks('<style>foo</style>', '', array('foo'));
40 function test_extractStyleBlocks_multipleBlocks() {
41 $this->assertExtractStyleBlocks(
42 "<style>1</style><style>2</style>NOP<style>4</style>",
43 "NOP",
44 array('1', '2', '4')
48 function test_extractStyleBlocks_blockWithAttributes() {
49 $this->assertExtractStyleBlocks(
50 '<style type="text/css">css</style>',
51 '',
52 array('css')
56 function test_extractStyleBlocks_styleWithPadding() {
57 $this->assertExtractStyleBlocks(
58 "Alas<styled>Awesome</styled>\n<style>foo</style> Trendy!",
59 "Alas<styled>Awesome</styled>\n Trendy!",
60 array('foo')
64 function assertCleanCSS($input, $expect = true) {
65 $filter = new HTMLPurifier_Filter_ExtractStyleBlocks();
66 if ($expect === true) $expect = $input;
67 $this->normalize($input);
68 $this->normalize($expect);
69 $result = $filter->cleanCSS($input, $this->config, $this->context);
70 $this->assertIdentical($result, $expect);
73 function test_cleanCSS_malformed() {
74 $this->assertCleanCSS('</style>', '');
77 function test_cleanCSS_selector() {
78 $this->assertCleanCSS("a .foo #id div.cl#foo {\nfont-weight:700;\n}");
81 function test_cleanCSS_angledBrackets() {
82 $this->assertCleanCSS(
83 ".class {\nfont-family:'</style>';\n}",
84 ".class {\nfont-family:\"\\3C /style\\3E \";\n}"
88 function test_cleanCSS_angledBrackets2() {
89 // CSSTidy's behavior in this case is wrong, and should be fixed
90 //$this->assertCleanCSS(
91 // "span[title=\"</style>\"] {\nfont-size:12pt;\n}",
92 // "span[title=\"\\3C /style\\3E \"] {\nfont-size:12pt;\n}"
93 //);
96 function test_cleanCSS_bogus() {
97 $this->assertCleanCSS("div {bogus:tree;}", "div {\n}");
100 function test_cleanCSS_escapeCodes() {
101 $this->assertCleanCSS(
102 ".class {\nfont-family:\"\\3C /style\\3E \";\n}"
106 function test_cleanCSS_noEscapeCodes() {
107 $this->config->set('Filter.ExtractStyleBlocks.Escaping', false);
108 $this->assertCleanCSS(
109 ".class {\nfont-family:\"</style>\";\n}"
113 function test_cleanCSS_scope() {
114 $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
115 $this->assertCleanCSS(
116 "p {\ntext-indent:1em;\n}",
117 "#foo p {\ntext-indent:1em;\n}"
121 function test_cleanCSS_scopeWithSelectorCommas() {
122 $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
123 $this->assertCleanCSS(
124 "b, i {\ntext-decoration:underline;\n}",
125 "#foo b, #foo i {\ntext-decoration:underline;\n}"
129 function test_cleanCSS_scopeWithNaughtySelector() {
130 $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
131 $this->assertCleanCSS(" + p {\ntext-indent:1em;\n}", '');
134 function test_cleanCSS_scopeWithMultipleNaughtySelectors() {
135 $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
136 $this->assertCleanCSS(" ++ ++ p {\ntext-indent:1em;\n}", '');
139 function test_cleanCSS_scopeWithCommas() {
140 $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo, .bar');
141 $this->assertCleanCSS(
142 "p {\ntext-indent:1em;\n}",
143 "#foo p, .bar p {\ntext-indent:1em;\n}"
147 function test_cleanCSS_scopeAllWithCommas() {
148 $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo, .bar');
149 $this->assertCleanCSS(
150 "p, div {\ntext-indent:1em;\n}",
151 "#foo p, #foo div, .bar p, .bar div {\ntext-indent:1em;\n}"
155 function test_cleanCSS_scopeWithConflicts() {
156 $this->config->set('Filter.ExtractStyleBlocks.Scope', 'p');
157 $this->assertCleanCSS(
158 "div {
159 text-align:right;
162 p div {
163 text-align:left;
166 "p div {
167 text-align:right;
170 p p div {
171 text-align:left;
176 function test_removeComments() {
177 $this->assertCleanCSS(
178 "<!--
179 div {
180 text-align:right;
182 -->",
183 "div {
184 text-align:right;
191 // vim: et sw=4 sts=4