Don't lower-case components of background.
[htmlpurifier.git] / tests / HTMLPurifier / Filter / ExtractStyleBlocksTest.php
bloba166022a0856a42d3548d4cb88493cce17082850
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;} body.class[foo="attr"] {text-align:right;}</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 // [Content] No longer can smuggle in angled brackets using
83 // font-family; when we add support for 'content', reinstate
84 // this test.
85 //$this->assertCleanCSS(
86 // ".class {\nfont-family:'</style>';\n}",
87 // ".class {\nfont-family:\"\\3C /style\\3E \";\n}"
88 //);
91 function test_cleanCSS_angledBrackets2() {
92 // CSSTidy's behavior in this case is wrong, and should be fixed
93 //$this->assertCleanCSS(
94 // "span[title=\"</style>\"] {\nfont-size:12pt;\n}",
95 // "span[title=\"\\3C /style\\3E \"] {\nfont-size:12pt;\n}"
96 //);
99 function test_cleanCSS_bogus() {
100 $this->assertCleanCSS("div {bogus:tree;}", "div {\n}");
103 /* [CONTENT]
104 function test_cleanCSS_escapeCodes() {
105 $this->assertCleanCSS(
106 ".class {\nfont-family:\"\\3C /style\\3E \";\n}"
110 function test_cleanCSS_noEscapeCodes() {
111 $this->config->set('Filter.ExtractStyleBlocks.Escaping', false);
112 $this->assertCleanCSS(
113 ".class {\nfont-family:\"</style>\";\n}"
118 function test_cleanCSS_scope() {
119 $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
120 $this->assertCleanCSS(
121 "p {\ntext-indent:1em;\n}",
122 "#foo p {\ntext-indent:1em;\n}"
126 function test_cleanCSS_scopeWithSelectorCommas() {
127 $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
128 $this->assertCleanCSS(
129 "b, i {\ntext-decoration:underline;\n}",
130 "#foo b, #foo i {\ntext-decoration:underline;\n}"
134 function test_cleanCSS_scopeWithNaughtySelector() {
135 $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
136 $this->assertCleanCSS(" + p {\ntext-indent:1em;\n}", '');
139 function test_cleanCSS_scopeWithMultipleNaughtySelectors() {
140 $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
141 $this->assertCleanCSS(" ++ ++ p {\ntext-indent:1em;\n}", '');
144 function test_cleanCSS_scopeWithCommas() {
145 $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo, .bar');
146 $this->assertCleanCSS(
147 "p {\ntext-indent:1em;\n}",
148 "#foo p, .bar p {\ntext-indent:1em;\n}"
152 function test_cleanCSS_scopeAllWithCommas() {
153 $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo, .bar');
154 $this->assertCleanCSS(
155 "p, div {\ntext-indent:1em;\n}",
156 "#foo p, .bar p, #foo div, .bar div {\ntext-indent:1em;\n}"
160 function test_cleanCSS_scopeWithConflicts() {
161 $this->config->set('Filter.ExtractStyleBlocks.Scope', 'p');
162 $this->assertCleanCSS(
163 "div {
164 text-align:right;
167 p div {
168 text-align:left;
171 "p div {
172 text-align:right;
175 p p div {
176 text-align:left;
181 function test_removeComments() {
182 $this->assertCleanCSS(
183 "<!--
184 div {
185 text-align:right;
187 -->",
188 "div {
189 text-align:right;
194 function test_atSelector() {
195 $this->assertCleanCSS(
197 b { text-align: center; }
203 function test_selectorValidation() {
204 $this->assertCleanCSS(
205 "&, & {
206 text-align: center;
210 $this->assertCleanCSS(
211 "&, b {
212 text-align:center;
214 "b {
215 text-align:center;
218 $this->assertCleanCSS(
219 "& a #foo:hover.bar +b > i {
220 text-align:center;
222 "a #foo:hover.bar + b \\3E i {
223 text-align:center;
226 $this->assertCleanCSS("doesnt-exist { text-align:center }", "");
229 function test_cleanCSS_caseSensitive() {
230 $this->assertCleanCSS("a .foo #ID div.cl#foo {\nbackground:url(\"http://foo/BAR\");\n}");
235 // vim: et sw=4 sts=4