3 class HTMLPurifier_GeneratorTest
extends HTMLPurifier_Harness
7 * Entity lookup table to help for a few tests.
9 private $_entity_lookup;
11 public function __construct()
13 parent
::__construct();
14 $this->_entity_lookup
= HTMLPurifier_EntityLookup
::instance();
17 public function setUp()
20 $this->config
->set('Output.Newline', "\n");
24 * Creates a generator based on config and context member variables.
26 protected function createGenerator()
28 return new HTMLPurifier_Generator($this->config
, $this->context
);
31 protected function assertGenerateFromToken($token, $html)
33 $generator = $this->createGenerator();
34 $result = $generator->generateFromToken($token);
35 $this->assertIdentical($result, $html);
38 public function test_generateFromToken_text()
40 $this->assertGenerateFromToken(
41 new HTMLPurifier_Token_Text('Foobar.<>'),
46 public function test_generateFromToken_startWithAttr()
48 $this->assertGenerateFromToken(
49 new HTMLPurifier_Token_Start('a',
50 array('href' => 'dyn?a=foo&b=bar')
52 '<a href="dyn?a=foo&b=bar">'
56 public function test_generateFromToken_end()
58 $this->assertGenerateFromToken(
59 new HTMLPurifier_Token_End('b'),
64 public function test_generateFromToken_emptyWithAttr()
66 $this->assertGenerateFromToken(
67 new HTMLPurifier_Token_Empty('br',
68 array('style' => 'font-family:"Courier New";')
70 '<br style="font-family:"Courier New";" />'
74 public function test_generateFromToken_startNoAttr()
76 $this->assertGenerateFromToken(
77 new HTMLPurifier_Token_Start('asdf'),
82 public function test_generateFromToken_emptyNoAttr()
84 $this->assertGenerateFromToken(
85 new HTMLPurifier_Token_Empty('br'),
90 public function test_generateFromToken_error()
92 $this->expectError('Cannot generate HTML from non-HTMLPurifier_Token object');
93 $this->assertGenerateFromToken( null, '' );
96 public function test_generateFromToken_unicode()
98 $theta_char = $this->_entity_lookup
->table
['theta'];
99 $this->assertGenerateFromToken(
100 new HTMLPurifier_Token_Text($theta_char),
105 public function test_generateFromToken_backtick()
107 $this->assertGenerateFromToken(
108 new HTMLPurifier_Token_Start('img', array('alt' => '`foo')),
113 public function test_generateFromToken_backtickDisabled()
115 $this->config
->set('Output.FixInnerHTML', false);
116 $this->assertGenerateFromToken(
117 new HTMLPurifier_Token_Start('img', array('alt' => '`')),
122 public function test_generateFromToken_backtickNoChange()
124 $this->assertGenerateFromToken(
125 new HTMLPurifier_Token_Start('img', array('alt' => '`foo` bar')),
126 '<img alt="`foo` bar">'
130 public function assertGenerateAttributes($attr, $expect, $element = false)
132 $generator = $this->createGenerator();
133 $result = $generator->generateAttributes($attr, $element);
134 $this->assertIdentical($result, $expect);
137 public function test_generateAttributes_blank()
139 $this->assertGenerateAttributes(array(), '');
142 public function test_generateAttributes_basic()
144 $this->assertGenerateAttributes(
145 array('href' => 'dyn?a=foo&b=bar'),
146 'href="dyn?a=foo&b=bar"'
150 public function test_generateAttributes_doubleQuote()
152 $this->assertGenerateAttributes(
153 array('style' => 'font-family:"Courier New";'),
154 'style="font-family:"Courier New";"'
158 public function test_generateAttributes_singleQuote()
160 $this->assertGenerateAttributes(
161 array('style' => 'font-family:\'Courier New\';'),
162 'style="font-family:\'Courier New\';"'
166 public function test_generateAttributes_multiple()
168 $this->assertGenerateAttributes(
169 array('src' => 'picture.jpg', 'alt' => 'Short & interesting'),
170 'src="picture.jpg" alt="Short & interesting"'
174 public function test_generateAttributes_specialChar()
176 $theta_char = $this->_entity_lookup
->table
['theta'];
177 $this->assertGenerateAttributes(
178 array('title' => 'Theta is ' . $theta_char),
179 'title="Theta is ' . $theta_char . '"'
184 public function test_generateAttributes_minimized()
186 $this->config
->set('HTML.Doctype', 'HTML 4.01 Transitional');
187 $this->assertGenerateAttributes(
188 array('compact' => 'compact'), 'compact', 'menu'
192 public function test_generateFromTokens()
194 $this->assertGeneration(
196 new HTMLPurifier_Token_Start('b'),
197 new HTMLPurifier_Token_Text('Foobar!'),
198 new HTMLPurifier_Token_End('b')
205 protected function assertGeneration($tokens, $expect)
207 $generator = new HTMLPurifier_Generator($this->config
, $this->context
);
208 $result = $generator->generateFromTokens($tokens);
209 $this->assertIdentical($expect, $result);
212 public function test_generateFromTokens_Scripting()
214 $this->assertGeneration(
216 new HTMLPurifier_Token_Start('script'),
217 new HTMLPurifier_Token_Text('alert(3 < 5);'),
218 new HTMLPurifier_Token_End('script')
220 "<script><!--//--><![CDATA[//><!--\nalert(3 < 5);\n//--><!]]></script>"
224 public function test_generateFromTokens_Scripting_missingCloseTag()
226 $this->assertGeneration(
228 new HTMLPurifier_Token_Start('script'),
229 new HTMLPurifier_Token_Text('alert(3 < 5);'),
231 "<script>alert(3 < 5);"
235 public function test_generateFromTokens_Scripting_doubleBlock()
237 $this->assertGeneration(
239 new HTMLPurifier_Token_Start('script'),
240 new HTMLPurifier_Token_Text('alert(3 < 5);'),
241 new HTMLPurifier_Token_Text('foo();'),
242 new HTMLPurifier_Token_End('script')
244 "<script>alert(3 < 5);foo();</script>"
248 public function test_generateFromTokens_Scripting_disableWrapper()
250 $this->config
->set('Output.CommentScriptContents', false);
251 $this->assertGeneration(
253 new HTMLPurifier_Token_Start('script'),
254 new HTMLPurifier_Token_Text('alert(3 < 5);'),
255 new HTMLPurifier_Token_End('script')
257 "<script>alert(3 < 5);</script>"
261 public function test_generateFromTokens_XHTMLoff()
263 $this->config
->set('HTML.XHTML', false);
265 // omit trailing slash
266 $this->assertGeneration(
267 array( new HTMLPurifier_Token_Empty('br') ),
271 // there should be a test for attribute minimization, but it is
272 // impossible for something like that to happen due to our current
273 // definitions! fix it later
275 // namespaced attributes must be dropped
276 $this->assertGeneration(
277 array( new HTMLPurifier_Token_Start('p', array('xml:lang'=>'fr')) ),
283 public function test_generateFromTokens_TidyFormat()
285 // abort test if tidy isn't loaded
286 if (!extension_loaded('tidy')) return;
288 // just don't test; Tidy is exploding on me.
291 $this->config
->set('Core.TidyFormat', true);
292 $this->config
->set('Output.Newline', "\n");
294 // nice wrapping please
295 $this->assertGeneration(
297 new HTMLPurifier_Token_Start('div'),
298 new HTMLPurifier_Token_Text('Text'),
299 new HTMLPurifier_Token_End('div')
301 "<div>\n Text\n</div>\n"
306 public function test_generateFromTokens_sortAttr()
308 $this->config
->set('Output.SortAttr', true);
310 $this->assertGeneration(
311 array( new HTMLPurifier_Token_Start('p', array('b'=>'c', 'a'=>'d')) ),
319 // vim: et sw=4 sts=4