aboutsummaryrefslogtreecommitdiffstats
path: root/lib/htmlpurifier/tests/HTMLPurifier/TagTransformTest.php
blob: 8669d5b91731f0ab2a8b55aaa007e9a96b97be88 (plain) (blame)
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
<?php

// needs to be seperated into files
class HTMLPurifier_TagTransformTest extends HTMLPurifier_Harness
{

    /**
     * Asserts that a transformation happens
     *
     * This assertion performs several tests on the transform:
     *
     * -# Transforms a start tag with only $name and no attributes
     * -# Transforms a start tag with $name and $attributes
     * -# Transform an end tag
     * -# Transform an empty tag with only $name and no attributes
     * -# Transform an empty tag with $name and $attributes
     *
     * In its current form, it assumes that start and empty tags would be
     * treated the same, and is really ensuring that the tag transform doesn't
     * do anything wonky to the tag type.
     *
     * @param $transformer      HTMLPurifier_TagTransform class to test
     * @param $name             Name of the original tag
     * @param $attributes       Attributes of the original tag
     * @param $expect_name      Name of output tag
     * @param $expect_attributes Attributes of output tag when $attributes
     *                          is included.
     * @param $expect_added_attributes Attributes of output tag when $attributes
     *                          are omitted.
     * @param $config_array     Configuration array for HTMLPurifier_Config
     * @param $context_array    Context array for HTMLPurifier_Context
     */
    protected function assertTransformation($transformer,
                                         $name,        $attributes,
                                  $expect_name, $expect_attributes,
                                  $expect_added_attributes = array(),
                                  $config_array = array(), $context_array = array()) {

        $config = HTMLPurifier_Config::createDefault();
        $config->loadArray($config_array);

        $context = new HTMLPurifier_Context();
        $context->loadArray($context_array);

        // start tag transform
        $this->assertIdentical(
                new HTMLPurifier_Token_Start($expect_name, $expect_added_attributes),
                $transformer->transform(
                    new HTMLPurifier_Token_Start($name), $config, $context)
            );

        // start tag transform with attributes
        $this->assertIdentical(
                new HTMLPurifier_Token_Start($expect_name, $expect_attributes),
                $transformer->transform(
                    new HTMLPurifier_Token_Start($name, $attributes),
                    $config, $context
                )
            );

        // end tag transform
        $this->assertIdentical(
                new HTMLPurifier_Token_End($expect_name),
                $transformer->transform(
                    new HTMLPurifier_Token_End($name), $config, $context
                )
            );

        // empty tag transform
        $this->assertIdentical(
                new HTMLPurifier_Token_Empty($expect_name, $expect_added_attributes),
                $transformer->transform(
                    new HTMLPurifier_Token_Empty($name), $config, $context
                )
            );

        // empty tag transform with attributes
        $this->assertIdentical(
                new HTMLPurifier_Token_Empty($expect_name, $expect_attributes),
                $transformer->transform(
                    new HTMLPurifier_Token_Empty($name, $attributes),
                    $config, $context
                )
            );


    }

    function testSimple() {

        $transformer = new HTMLPurifier_TagTransform_Simple('ul');

        $this->assertTransformation(
            $transformer,
            'menu', array('class' => 'boom'),
            'ul', array('class' => 'boom')
        );

    }

    function testSimpleWithCSS() {

        $transformer = new HTMLPurifier_TagTransform_Simple('div', 'text-align:center;');

        $this->assertTransformation(
            $transformer,
            'center', array('class' => 'boom', 'style'=>'font-weight:bold;'),
            'div', array('class' => 'boom', 'style'=>'text-align:center;font-weight:bold;'),
            array('style'=>'text-align:center;')
        );

        // test special case, uppercase attribute key
        $this->assertTransformation(
            $transformer,
            'center', array('STYLE'=>'font-weight:bold;'),
            'div', array('style'=>'text-align:center;font-weight:bold;'),
            array('style'=>'text-align:center;')
        );

    }

    protected function assertSizeToStyle($transformer, $size, $style) {
        $this->assertTransformation(
            $transformer,
            'font', array('size' => $size),
            'span', array('style' => 'font-size:' . $style . ';')
        );
    }

    function testFont() {

        $transformer = new HTMLPurifier_TagTransform_Font();

        // test a font-face transformation
        $this->assertTransformation(
            $transformer,
            'font', array('face' => 'Arial'),
            'span', array('style' => 'font-family:Arial;')
        );

        // test a color transformation
        $this->assertTransformation(
            $transformer,
            'font', array('color' => 'red'),
            'span', array('style' => 'color:red;')
        );

        // test the size transforms
        $this->assertSizeToStyle($transformer, '0', 'xx-small');
        $this->assertSizeToStyle($transformer, '1', 'xx-small');
        $this->assertSizeToStyle($transformer, '2', 'small');
        $this->assertSizeToStyle($transformer, '3', 'medium');
        $this->assertSizeToStyle($transformer, '4', 'large');
        $this->assertSizeToStyle($transformer, '5', 'x-large');
        $this->assertSizeToStyle($transformer, '6', 'xx-large');
        $this->assertSizeToStyle($transformer, '7', '300%');
        $this->assertSizeToStyle($transformer, '-1', 'smaller');
        $this->assertSizeToStyle($transformer, '-2', '60%');
        $this->assertSizeToStyle($transformer, '-3', '60%');
        $this->assertSizeToStyle($transformer, '+1', 'larger');
        $this->assertSizeToStyle($transformer, '+2', '150%');
        $this->assertSizeToStyle($transformer, '+3', '200%');
        $this->assertSizeToStyle($transformer, '+4', '300%');
        $this->assertSizeToStyle($transformer, '+5', '300%');
        $this->assertTransformation(
            $transformer, 'font', array('size' => ''),
            'span', array()
        );

        // test multiple transforms, the alphabetical ordering is important
        $this->assertTransformation(
            $transformer,
            'font', array('color' => 'red', 'face' => 'Arial', 'size' => '6'),
            'span', array('style' => 'color:red;font-family:Arial;font-size:xx-large;')
        );
    }
}

// vim: et sw=4 sts=4