File: ExtractStyleBlocksTest.php

package info (click to toggle)
php-htmlpurifier 4.1.1%2Bdfsg1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,556 kB
  • ctags: 6,547
  • sloc: php: 26,336; xml: 1,630; makefile: 70; sh: 14
file content (191 lines) | stat: -rw-r--r-- 5,736 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php

/**
 * @todo Assimilate CSSTidy into our library
 */
class HTMLPurifier_Filter_ExtractStyleBlocksTest extends HTMLPurifier_Harness
{

    // usual use case:
    function test_tokenizeHTML_extractStyleBlocks() {
        $this->config->set('Filter.ExtractStyleBlocks', true);
        $purifier = new HTMLPurifier($this->config);
        $result = $purifier->purify('<style type="text/css">.foo {text-align:center;bogus:remove-me;}</style>Test<style>* {font-size:12pt;}</style>');
        $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(); // disable cleaning
        if ($expect === true) $expect = $html;
        $this->config->set('Filter.ExtractStyleBlocks.TidyImpl', false);
        $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('<style>foo</style>', '', array('foo'));
    }

    function test_extractStyleBlocks_multipleBlocks() {
        $this->assertExtractStyleBlocks(
          "<style>1</style><style>2</style>NOP<style>4</style>",
          "NOP",
          array('1', '2', '4')
        );
    }

    function test_extractStyleBlocks_blockWithAttributes() {
        $this->assertExtractStyleBlocks(
          '<style type="text/css">css</style>',
          '',
          array('css')
        );
    }

    function test_extractStyleBlocks_styleWithPadding() {
        $this->assertExtractStyleBlocks(
          "Alas<styled>Awesome</styled>\n<style>foo</style> Trendy!",
          "Alas<styled>Awesome</styled>\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('</style>', '');
    }

    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:'</style>';\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=\"</style>\"] {\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.ExtractStyleBlocks.Escaping', false);
        $this->assertCleanCSS(
            ".class {\nfont-family:\"</style>\";\n}"
        );
    }

    function test_cleanCSS_scope() {
        $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
        $this->assertCleanCSS(
            "p {\ntext-indent:1em;\n}",
            "#foo p {\ntext-indent:1em;\n}"
        );
    }

    function test_cleanCSS_scopeWithSelectorCommas() {
        $this->config->set('Filter.ExtractStyleBlocks.Scope', '#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.ExtractStyleBlocks.Scope', '#foo');
        $this->assertCleanCSS("  + p {\ntext-indent:1em;\n}", '');
    }

    function test_cleanCSS_scopeWithMultipleNaughtySelectors() {
        $this->config->set('Filter.ExtractStyleBlocks.Scope', '#foo');
        $this->assertCleanCSS("  ++ ++ p {\ntext-indent:1em;\n}", '');
    }

    function test_cleanCSS_scopeWithCommas() {
        $this->config->set('Filter.ExtractStyleBlocks.Scope', '#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.ExtractStyleBlocks.Scope', '#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.ExtractStyleBlocks.Scope', 'p');
        $this->assertCleanCSS(
"div {
text-align:right;
}

p div {
text-align:left;
}",

"p div {
text-align:right;
}

p p div {
text-align:left;
}"
        );
    }

    function test_removeComments() {
        $this->assertCleanCSS(
"<!--
div {
text-align:right;
}
-->",
"div {
text-align:right;
}"
        );
    }

}

// vim: et sw=4 sts=4