Fix Internet Explorer innerHTML bug.
[htmlpurifier.git] / tests / HTMLPurifier / GeneratorTest.php
blob46b1dcf6ba45659b12bf8ceb959e6a9e2b278498
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_unicode() {
86 $theta_char = $this->_entity_lookup->table['theta'];
87 $this->assertGenerateFromToken(
88 new HTMLPurifier_Token_Text($theta_char),
89 $theta_char
93 function test_generateFromToken_backtick() {
94 $this->assertGenerateFromToken(
95 new HTMLPurifier_Token_Start('img', array('alt' => '`foo')),
96 '<img alt="`foo ">'
100 function test_generateFromToken_backtickDisabled() {
101 $this->config->set('Output.FixInnerHTML', false);
102 $this->assertGenerateFromToken(
103 new HTMLPurifier_Token_Start('img', array('alt' => '`')),
104 '<img alt="`">'
108 function test_generateFromToken_backtickNoChange() {
109 $this->assertGenerateFromToken(
110 new HTMLPurifier_Token_Start('img', array('alt' => '`foo` bar')),
111 '<img alt="`foo` bar">'
115 function assertGenerateAttributes($attr, $expect, $element = false) {
116 $generator = $this->createGenerator();
117 $result = $generator->generateAttributes($attr, $element);
118 $this->assertIdentical($result, $expect);
121 function test_generateAttributes_blank() {
122 $this->assertGenerateAttributes(array(), '');
125 function test_generateAttributes_basic() {
126 $this->assertGenerateAttributes(
127 array('href' => 'dyn?a=foo&b=bar'),
128 'href="dyn?a=foo&amp;b=bar"'
132 function test_generateAttributes_doubleQuote() {
133 $this->assertGenerateAttributes(
134 array('style' => 'font-family:"Courier New";'),
135 'style="font-family:&quot;Courier New&quot;;"'
139 function test_generateAttributes_singleQuote() {
140 $this->assertGenerateAttributes(
141 array('style' => 'font-family:\'Courier New\';'),
142 'style="font-family:\'Courier New\';"'
146 function test_generateAttributes_multiple() {
147 $this->assertGenerateAttributes(
148 array('src' => 'picture.jpg', 'alt' => 'Short & interesting'),
149 'src="picture.jpg" alt="Short &amp; interesting"'
153 function test_generateAttributes_specialChar() {
154 $theta_char = $this->_entity_lookup->table['theta'];
155 $this->assertGenerateAttributes(
156 array('title' => 'Theta is ' . $theta_char),
157 'title="Theta is ' . $theta_char . '"'
162 function test_generateAttributes_minimized() {
163 $this->config->set('HTML.Doctype', 'HTML 4.01 Transitional');
164 $this->assertGenerateAttributes(
165 array('compact' => 'compact'), 'compact', 'menu'
169 function test_generateFromTokens() {
171 $this->assertGeneration(
172 array(
173 new HTMLPurifier_Token_Start('b'),
174 new HTMLPurifier_Token_Text('Foobar!'),
175 new HTMLPurifier_Token_End('b')
177 '<b>Foobar!</b>'
182 protected function assertGeneration($tokens, $expect) {
183 $generator = new HTMLPurifier_Generator($this->config, $this->context);
184 $result = $generator->generateFromTokens($tokens);
185 $this->assertIdentical($expect, $result);
188 function test_generateFromTokens_Scripting() {
189 $this->assertGeneration(
190 array(
191 new HTMLPurifier_Token_Start('script'),
192 new HTMLPurifier_Token_Text('alert(3 < 5);'),
193 new HTMLPurifier_Token_End('script')
195 "<script><!--//--><![CDATA[//><!--\nalert(3 < 5);\n//--><!]]></script>"
199 function test_generateFromTokens_Scripting_missingCloseTag() {
200 $this->assertGeneration(
201 array(
202 new HTMLPurifier_Token_Start('script'),
203 new HTMLPurifier_Token_Text('alert(3 < 5);'),
205 "<script>alert(3 &lt; 5);"
209 function test_generateFromTokens_Scripting_doubleBlock() {
210 $this->assertGeneration(
211 array(
212 new HTMLPurifier_Token_Start('script'),
213 new HTMLPurifier_Token_Text('alert(3 < 5);'),
214 new HTMLPurifier_Token_Text('foo();'),
215 new HTMLPurifier_Token_End('script')
217 "<script>alert(3 &lt; 5);foo();</script>"
221 function test_generateFromTokens_Scripting_disableWrapper() {
222 $this->config->set('Output.CommentScriptContents', false);
223 $this->assertGeneration(
224 array(
225 new HTMLPurifier_Token_Start('script'),
226 new HTMLPurifier_Token_Text('alert(3 < 5);'),
227 new HTMLPurifier_Token_End('script')
229 "<script>alert(3 &lt; 5);</script>"
233 function test_generateFromTokens_XHTMLoff() {
234 $this->config->set('HTML.XHTML', false);
236 // omit trailing slash
237 $this->assertGeneration(
238 array( new HTMLPurifier_Token_Empty('br') ),
239 '<br>'
242 // there should be a test for attribute minimization, but it is
243 // impossible for something like that to happen due to our current
244 // definitions! fix it later
246 // namespaced attributes must be dropped
247 $this->assertGeneration(
248 array( new HTMLPurifier_Token_Start('p', array('xml:lang'=>'fr')) ),
249 '<p>'
254 function test_generateFromTokens_TidyFormat() {
255 // abort test if tidy isn't loaded
256 if (!extension_loaded('tidy')) return;
258 // just don't test; Tidy is exploding on me.
259 return;
261 $this->config->set('Core.TidyFormat', true);
262 $this->config->set('Output.Newline', "\n");
264 // nice wrapping please
265 $this->assertGeneration(
266 array(
267 new HTMLPurifier_Token_Start('div'),
268 new HTMLPurifier_Token_Text('Text'),
269 new HTMLPurifier_Token_End('div')
271 "<div>\n Text\n</div>\n"
276 function test_generateFromTokens_sortAttr() {
277 $this->config->set('Output.SortAttr', true);
279 $this->assertGeneration(
280 array( new HTMLPurifier_Token_Start('p', array('b'=>'c', 'a'=>'d')) ),
281 '<p a="d" b="c">'
288 // vim: et sw=4 sts=4