Whoops, forgot to edit WHATSNEW
[htmlpurifier.git] / tests / HTMLPurifier / TagTransformTest.php
blobe6d2d5b95ee7e0ff0d60a167a0c6ae9e5c5b4077
1 <?php
3 // needs to be seperated into files
4 class HTMLPurifier_TagTransformTest extends HTMLPurifier_Harness
7 /**
8 * Asserts that a transformation happens
10 * This assertion performs several tests on the transform:
12 * -# Transforms a start tag with only $name and no attributes
13 * -# Transforms a start tag with $name and $attributes
14 * -# Transform an end tag
15 * -# Transform an empty tag with only $name and no attributes
16 * -# Transform an empty tag with $name and $attributes
18 * In its current form, it assumes that start and empty tags would be
19 * treated the same, and is really ensuring that the tag transform doesn't
20 * do anything wonky to the tag type.
22 * @param $transformer HTMLPurifier_TagTransform class to test
23 * @param $name Name of the original tag
24 * @param $attributes Attributes of the original tag
25 * @param $expect_name Name of output tag
26 * @param $expect_attributes Attributes of output tag when $attributes
27 * is included.
28 * @param $expect_added_attributes Attributes of output tag when $attributes
29 * are omitted.
30 * @param $config_array Configuration array for HTMLPurifier_Config
31 * @param $context_array Context array for HTMLPurifier_Context
33 protected function assertTransformation($transformer,
34 $name, $attributes,
35 $expect_name, $expect_attributes,
36 $expect_added_attributes = array(),
37 $config_array = array(), $context_array = array()) {
39 $config = HTMLPurifier_Config::createDefault();
40 $config->loadArray($config_array);
42 $context = new HTMLPurifier_Context();
43 $context->loadArray($context_array);
45 // start tag transform
46 $this->assertIdentical(
47 new HTMLPurifier_Token_Start($expect_name, $expect_added_attributes),
48 $transformer->transform(
49 new HTMLPurifier_Token_Start($name), $config, $context)
52 // start tag transform with attributes
53 $this->assertIdentical(
54 new HTMLPurifier_Token_Start($expect_name, $expect_attributes),
55 $transformer->transform(
56 new HTMLPurifier_Token_Start($name, $attributes),
57 $config, $context
61 // end tag transform
62 $this->assertIdentical(
63 new HTMLPurifier_Token_End($expect_name),
64 $transformer->transform(
65 new HTMLPurifier_Token_End($name), $config, $context
69 // empty tag transform
70 $this->assertIdentical(
71 new HTMLPurifier_Token_Empty($expect_name, $expect_added_attributes),
72 $transformer->transform(
73 new HTMLPurifier_Token_Empty($name), $config, $context
77 // empty tag transform with attributes
78 $this->assertIdentical(
79 new HTMLPurifier_Token_Empty($expect_name, $expect_attributes),
80 $transformer->transform(
81 new HTMLPurifier_Token_Empty($name, $attributes),
82 $config, $context
89 public function testSimple()
91 $transformer = new HTMLPurifier_TagTransform_Simple('ul');
93 $this->assertTransformation(
94 $transformer,
95 'menu', array('class' => 'boom'),
96 'ul', array('class' => 'boom')
101 public function testSimpleWithCSS()
103 $transformer = new HTMLPurifier_TagTransform_Simple('div', 'text-align:center;');
105 $this->assertTransformation(
106 $transformer,
107 'center', array('class' => 'boom', 'style'=>'font-weight:bold;'),
108 'div', array('class' => 'boom', 'style'=>'text-align:center;font-weight:bold;'),
109 array('style'=>'text-align:center;')
112 // test special case, uppercase attribute key
113 $this->assertTransformation(
114 $transformer,
115 'center', array('STYLE'=>'font-weight:bold;'),
116 'div', array('style'=>'text-align:center;font-weight:bold;'),
117 array('style'=>'text-align:center;')
122 protected function assertSizeToStyle($transformer, $size, $style)
124 $this->assertTransformation(
125 $transformer,
126 'font', array('size' => $size),
127 'span', array('style' => 'font-size:' . $style . ';')
131 public function testFont()
133 $transformer = new HTMLPurifier_TagTransform_Font();
135 // test a font-face transformation
136 $this->assertTransformation(
137 $transformer,
138 'font', array('face' => 'Arial'),
139 'span', array('style' => 'font-family:Arial;')
142 // test a color transformation
143 $this->assertTransformation(
144 $transformer,
145 'font', array('color' => 'red'),
146 'span', array('style' => 'color:red;')
149 // test the size transforms
150 $this->assertSizeToStyle($transformer, '0', 'xx-small');
151 $this->assertSizeToStyle($transformer, '1', 'xx-small');
152 $this->assertSizeToStyle($transformer, '2', 'small');
153 $this->assertSizeToStyle($transformer, '3', 'medium');
154 $this->assertSizeToStyle($transformer, '4', 'large');
155 $this->assertSizeToStyle($transformer, '5', 'x-large');
156 $this->assertSizeToStyle($transformer, '6', 'xx-large');
157 $this->assertSizeToStyle($transformer, '7', '300%');
158 $this->assertSizeToStyle($transformer, '-1', 'smaller');
159 $this->assertSizeToStyle($transformer, '-2', '60%');
160 $this->assertSizeToStyle($transformer, '-3', '60%');
161 $this->assertSizeToStyle($transformer, '+1', 'larger');
162 $this->assertSizeToStyle($transformer, '+2', '150%');
163 $this->assertSizeToStyle($transformer, '+3', '200%');
164 $this->assertSizeToStyle($transformer, '+4', '300%');
165 $this->assertSizeToStyle($transformer, '+5', '300%');
166 $this->assertTransformation(
167 $transformer, 'font', array('size' => ''),
168 'span', array()
171 // test multiple transforms, the alphabetical ordering is important
172 $this->assertTransformation(
173 $transformer,
174 'font', array('color' => 'red', 'face' => 'Arial', 'size' => '6'),
175 'span', array('style' => 'color:red;font-family:Arial;font-size:xx-large;')
180 // vim: et sw=4 sts=4