Add %HTML.TargetNoreferrer, which adds rel="noreferrer" when target attribute is set
[htmlpurifier.git] / tests / HTMLPurifier / GeneratorTest.php
blobe4906ab870c21d7e77f7ad3a8d3e204b0cb04f2a
1 <?php
3 class HTMLPurifier_GeneratorTest extends HTMLPurifier_Harness
6 /**
7 * Entity lookup table to help for a few tests.
8 */
9 private $_entity_lookup;
11 public function __construct()
13 parent::__construct();
14 $this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
17 public function setUp()
19 parent::setUp();
20 $this->config->set('Output.Newline', "\n");
23 /**
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.<>'),
42 'Foobar.&lt;&gt;'
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&amp;b=bar">'
56 public function test_generateFromToken_end()
58 $this->assertGenerateFromToken(
59 new HTMLPurifier_Token_End('b'),
60 '</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:&quot;Courier New&quot;;" />'
74 public function test_generateFromToken_startNoAttr()
76 $this->assertGenerateFromToken(
77 new HTMLPurifier_Token_Start('asdf'),
78 '<asdf>'
82 public function test_generateFromToken_emptyNoAttr()
84 $this->assertGenerateFromToken(
85 new HTMLPurifier_Token_Empty('br'),
86 '<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),
101 $theta_char
105 public function test_generateFromToken_backtick()
107 $this->assertGenerateFromToken(
108 new HTMLPurifier_Token_Start('img', array('alt' => '`foo')),
109 '<img 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' => '`')),
118 '<img 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&amp;b=bar"'
150 public function test_generateAttributes_doubleQuote()
152 $this->assertGenerateAttributes(
153 array('style' => 'font-family:"Courier New";'),
154 'style="font-family:&quot;Courier New&quot;;"'
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 &amp; 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(
195 array(
196 new HTMLPurifier_Token_Start('b'),
197 new HTMLPurifier_Token_Text('Foobar!'),
198 new HTMLPurifier_Token_End('b')
200 '<b>Foobar!</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(
215 array(
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(
227 array(
228 new HTMLPurifier_Token_Start('script'),
229 new HTMLPurifier_Token_Text('alert(3 < 5);'),
231 "<script>alert(3 &lt; 5);"
235 public function test_generateFromTokens_Scripting_doubleBlock()
237 $this->assertGeneration(
238 array(
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 &lt; 5);foo();</script>"
248 public function test_generateFromTokens_Scripting_disableWrapper()
250 $this->config->set('Output.CommentScriptContents', false);
251 $this->assertGeneration(
252 array(
253 new HTMLPurifier_Token_Start('script'),
254 new HTMLPurifier_Token_Text('alert(3 < 5);'),
255 new HTMLPurifier_Token_End('script')
257 "<script>alert(3 &lt; 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') ),
268 '<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')) ),
278 '<p>'
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.
289 return;
291 $this->config->set('Core.TidyFormat', true);
292 $this->config->set('Output.Newline', "\n");
294 // nice wrapping please
295 $this->assertGeneration(
296 array(
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')) ),
312 '<p a="d" b="c">'
319 // vim: et sw=4 sts=4