Convert all to new configuration get/set format.
[htmlpurifier/darkodev.git] / tests / HTMLPurifier / GeneratorTest.php
blob67016e40458729dfa417d5df9ae9c77765691e7d
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() {
12 parent::__construct();
13 $this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
16 public function setUp() {
17 parent::setUp();
18 $this->config->set('Output.Newline', "\n");
21 /**
22 * Creates a generator based on config and context member variables.
24 protected function createGenerator() {
25 return new HTMLPurifier_Generator($this->config, $this->context);
28 protected function assertGenerateFromToken($token, $html) {
29 $generator = $this->createGenerator();
30 $result = $generator->generateFromToken($token);
31 $this->assertIdentical($result, $html);
34 function test_generateFromToken_text() {
35 $this->assertGenerateFromToken(
36 new HTMLPurifier_Token_Text('Foobar.<>'),
37 'Foobar.&lt;&gt;'
41 function test_generateFromToken_startWithAttr() {
42 $this->assertGenerateFromToken(
43 new HTMLPurifier_Token_Start('a',
44 array('href' => 'dyn?a=foo&b=bar')
46 '<a href="dyn?a=foo&amp;b=bar">'
50 function test_generateFromToken_end() {
51 $this->assertGenerateFromToken(
52 new HTMLPurifier_Token_End('b'),
53 '</b>'
57 function test_generateFromToken_emptyWithAttr() {
58 $this->assertGenerateFromToken(
59 new HTMLPurifier_Token_Empty('br',
60 array('style' => 'font-family:"Courier New";')
62 '<br style="font-family:&quot;Courier New&quot;;" />'
66 function test_generateFromToken_startNoAttr() {
67 $this->assertGenerateFromToken(
68 new HTMLPurifier_Token_Start('asdf'),
69 '<asdf>'
73 function test_generateFromToken_emptyNoAttr() {
74 $this->assertGenerateFromToken(
75 new HTMLPurifier_Token_Empty('br'),
76 '<br />'
80 function test_generateFromToken_error() {
81 $this->expectError('Cannot generate HTML from non-HTMLPurifier_Token object');
82 $this->assertGenerateFromToken( null, '' );
85 function test_generateFromToken_() {
86 $theta_char = $this->_entity_lookup->table['theta'];
87 $this->assertGenerateFromToken(
88 new HTMLPurifier_Token_Text($theta_char),
89 $theta_char
93 function assertGenerateAttributes($attr, $expect, $element = false) {
94 $generator = $this->createGenerator();
95 $result = $generator->generateAttributes($attr, $element);
96 $this->assertIdentical($result, $expect);
99 function test_generateAttributes_blank() {
100 $this->assertGenerateAttributes(array(), '');
103 function test_generateAttributes_basic() {
104 $this->assertGenerateAttributes(
105 array('href' => 'dyn?a=foo&b=bar'),
106 'href="dyn?a=foo&amp;b=bar"'
110 function test_generateAttributes_doubleQuote() {
111 $this->assertGenerateAttributes(
112 array('style' => 'font-family:"Courier New";'),
113 'style="font-family:&quot;Courier New&quot;;"'
117 function test_generateAttributes_singleQuote() {
118 $this->assertGenerateAttributes(
119 array('style' => 'font-family:\'Courier New\';'),
120 'style="font-family:\'Courier New\';"'
124 function test_generateAttributes_multiple() {
125 $this->assertGenerateAttributes(
126 array('src' => 'picture.jpg', 'alt' => 'Short & interesting'),
127 'src="picture.jpg" alt="Short &amp; interesting"'
131 function test_generateAttributes_specialChar() {
132 $theta_char = $this->_entity_lookup->table['theta'];
133 $this->assertGenerateAttributes(
134 array('title' => 'Theta is ' . $theta_char),
135 'title="Theta is ' . $theta_char . '"'
140 function test_generateAttributes_minimized() {
141 $this->config->set('HTML.Doctype', 'HTML 4.01 Transitional');
142 $this->assertGenerateAttributes(
143 array('compact' => 'compact'), 'compact', 'menu'
147 function test_generateFromTokens() {
149 $this->assertGeneration(
150 array(
151 new HTMLPurifier_Token_Start('b'),
152 new HTMLPurifier_Token_Text('Foobar!'),
153 new HTMLPurifier_Token_End('b')
155 '<b>Foobar!</b>'
160 protected function assertGeneration($tokens, $expect) {
161 $generator = new HTMLPurifier_Generator($this->config, $this->context);
162 $result = $generator->generateFromTokens($tokens);
163 $this->assertIdentical($expect, $result);
166 function test_generateFromTokens_Scripting() {
167 $this->assertGeneration(
168 array(
169 new HTMLPurifier_Token_Start('script'),
170 new HTMLPurifier_Token_Text('alert(3 < 5);'),
171 new HTMLPurifier_Token_End('script')
173 "<script><!--//--><![CDATA[//><!--\nalert(3 < 5);\n//--><!]]></script>"
177 function test_generateFromTokens_Scripting_missingCloseTag() {
178 $this->assertGeneration(
179 array(
180 new HTMLPurifier_Token_Start('script'),
181 new HTMLPurifier_Token_Text('alert(3 < 5);'),
183 "<script>alert(3 &lt; 5);"
187 function test_generateFromTokens_Scripting_doubleBlock() {
188 $this->assertGeneration(
189 array(
190 new HTMLPurifier_Token_Start('script'),
191 new HTMLPurifier_Token_Text('alert(3 < 5);'),
192 new HTMLPurifier_Token_Text('foo();'),
193 new HTMLPurifier_Token_End('script')
195 "<script>alert(3 &lt; 5);foo();</script>"
199 function test_generateFromTokens_Scripting_disableWrapper() {
200 $this->config->set('Output.CommentScriptContents', false);
201 $this->assertGeneration(
202 array(
203 new HTMLPurifier_Token_Start('script'),
204 new HTMLPurifier_Token_Text('alert(3 < 5);'),
205 new HTMLPurifier_Token_End('script')
207 "<script>alert(3 &lt; 5);</script>"
211 function test_generateFromTokens_XHTMLoff() {
212 $this->config->set('HTML.XHTML', false);
214 // omit trailing slash
215 $this->assertGeneration(
216 array( new HTMLPurifier_Token_Empty('br') ),
217 '<br>'
220 // there should be a test for attribute minimization, but it is
221 // impossible for something like that to happen due to our current
222 // definitions! fix it later
224 // namespaced attributes must be dropped
225 $this->assertGeneration(
226 array( new HTMLPurifier_Token_Start('p', array('xml:lang'=>'fr')) ),
227 '<p>'
232 function test_generateFromTokens_TidyFormat() {
233 // abort test if tidy isn't loaded
234 if (!extension_loaded('tidy')) return;
236 // just don't test; Tidy is exploding on me.
237 return;
239 $this->config->set('Core.TidyFormat', true);
240 $this->config->set('Output.Newline', "\n");
242 // nice wrapping please
243 $this->assertGeneration(
244 array(
245 new HTMLPurifier_Token_Start('div'),
246 new HTMLPurifier_Token_Text('Text'),
247 new HTMLPurifier_Token_End('div')
249 "<div>\n Text\n</div>\n"
254 function test_generateFromTokens_sortAttr() {
255 $this->config->set('Output.SortAttr', true);
257 $this->assertGeneration(
258 array( new HTMLPurifier_Token_Start('p', array('b'=>'c', 'a'=>'d')) ),
259 '<p a="d" b="c">'
266 // vim: et sw=4 sts=4