2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
2008-04-28 01:35:07 +00:00
|
|
|
class HTMLPurifier_GeneratorTest extends HTMLPurifier_Harness
|
2006-07-23 00:11:03 +00:00
|
|
|
{
|
|
|
|
|
2008-04-28 01:35:07 +00:00
|
|
|
/**
|
|
|
|
* Entity lookup table to help for a few tests.
|
|
|
|
*/
|
|
|
|
private $_entity_lookup;
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2008-04-21 15:24:18 +00:00
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
2006-08-01 00:29:38 +00:00
|
|
|
$this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2008-04-21 15:24:18 +00:00
|
|
|
public function setUp() {
|
2008-04-28 01:35:07 +00:00
|
|
|
parent::setUp();
|
|
|
|
$this->config->set('Output', 'Newline', "\n");
|
2006-10-01 21:55:13 +00:00
|
|
|
}
|
|
|
|
|
2008-04-28 01:35:07 +00:00
|
|
|
/**
|
|
|
|
* Creates a generator based on config and context member variables.
|
|
|
|
*/
|
|
|
|
protected function createGenerator() {
|
|
|
|
return new HTMLPurifier_Generator($this->config, $this->context);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function assertGenerateFromToken($token, $html) {
|
|
|
|
$generator = $this->createGenerator();
|
|
|
|
$result = $generator->generateFromToken($token);
|
|
|
|
$this->assertIdentical($result, $html);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateFromToken_text() {
|
|
|
|
$this->assertGenerateFromToken(
|
|
|
|
new HTMLPurifier_Token_Text('Foobar.<>'),
|
|
|
|
'Foobar.<>'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateFromToken_startWithAttr() {
|
|
|
|
$this->assertGenerateFromToken(
|
|
|
|
new HTMLPurifier_Token_Start('a',
|
|
|
|
array('href' => 'dyn?a=foo&b=bar')
|
|
|
|
),
|
|
|
|
'<a href="dyn?a=foo&b=bar">'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateFromToken_end() {
|
|
|
|
$this->assertGenerateFromToken(
|
|
|
|
new HTMLPurifier_Token_End('b'),
|
|
|
|
'</b>'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateFromToken_emptyWithAttr() {
|
|
|
|
$this->assertGenerateFromToken(
|
|
|
|
new HTMLPurifier_Token_Empty('br',
|
|
|
|
array('style' => 'font-family:"Courier New";')
|
|
|
|
),
|
|
|
|
'<br style="font-family:"Courier New";" />'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateFromToken_startNoAttr() {
|
|
|
|
$this->assertGenerateFromToken(
|
|
|
|
new HTMLPurifier_Token_Start('asdf'),
|
|
|
|
'<asdf>'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateFromToken_emptyNoAttr() {
|
|
|
|
$this->assertGenerateFromToken(
|
|
|
|
new HTMLPurifier_Token_Empty('br'),
|
|
|
|
'<br />'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateFromToken_error() {
|
|
|
|
$this->expectError('Cannot generate HTML from non-HTMLPurifier_Token object');
|
|
|
|
$this->assertGenerateFromToken( null, '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateFromToken_() {
|
2006-08-01 00:29:38 +00:00
|
|
|
$theta_char = $this->_entity_lookup->table['theta'];
|
2008-04-28 01:35:07 +00:00
|
|
|
$this->assertGenerateFromToken(
|
|
|
|
new HTMLPurifier_Token_Text($theta_char),
|
|
|
|
$theta_char
|
|
|
|
);
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2008-04-28 01:35:07 +00:00
|
|
|
function assertGenerateAttributes($attr, $expect, $element = false) {
|
|
|
|
$generator = $this->createGenerator();
|
|
|
|
$result = $generator->generateAttributes($attr, $element);
|
|
|
|
$this->assertIdentical($result, $expect);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateAttributes_blank() {
|
|
|
|
$this->assertGenerateAttributes(array(), '');
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateAttributes_basic() {
|
|
|
|
$this->assertGenerateAttributes(
|
|
|
|
array('href' => 'dyn?a=foo&b=bar'),
|
|
|
|
'href="dyn?a=foo&b=bar"'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateAttributes_doubleQuote() {
|
|
|
|
$this->assertGenerateAttributes(
|
|
|
|
array('style' => 'font-family:"Courier New";'),
|
|
|
|
'style="font-family:"Courier New";"'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateAttributes_singleQuote() {
|
|
|
|
$this->assertGenerateAttributes(
|
|
|
|
array('style' => 'font-family:\'Courier New\';'),
|
|
|
|
'style="font-family:\'Courier New\';"'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateAttributes_multiple() {
|
|
|
|
$this->assertGenerateAttributes(
|
|
|
|
array('src' => 'picture.jpg', 'alt' => 'Short & interesting'),
|
|
|
|
'src="picture.jpg" alt="Short & interesting"'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateAttributes_specialChar() {
|
2006-08-01 00:29:38 +00:00
|
|
|
$theta_char = $this->_entity_lookup->table['theta'];
|
2008-04-28 01:35:07 +00:00
|
|
|
$this->assertGenerateAttributes(
|
|
|
|
array('title' => 'Theta is ' . $theta_char),
|
|
|
|
'title="Theta is ' . $theta_char . '"'
|
|
|
|
);
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2008-04-28 01:35:07 +00:00
|
|
|
|
2007-05-21 01:36:15 +00:00
|
|
|
function test_generateAttributes_minimized() {
|
2008-04-28 01:35:07 +00:00
|
|
|
$this->config->set('HTML', 'Doctype', 'HTML 4.01 Transitional');
|
|
|
|
$this->assertGenerateAttributes(
|
|
|
|
array('compact' => 'compact'), 'compact', 'menu'
|
|
|
|
);
|
2007-05-21 01:36:15 +00:00
|
|
|
}
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
function test_generateFromTokens() {
|
|
|
|
|
2008-04-28 01:35:07 +00:00
|
|
|
$this->assertGeneration(
|
2006-10-01 21:55:13 +00:00
|
|
|
array(
|
|
|
|
new HTMLPurifier_Token_Start('b'),
|
|
|
|
new HTMLPurifier_Token_Text('Foobar!'),
|
|
|
|
new HTMLPurifier_Token_End('b')
|
|
|
|
),
|
|
|
|
'<b>Foobar!</b>'
|
|
|
|
);
|
2006-07-30 18:37:42 +00:00
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2008-04-21 15:24:18 +00:00
|
|
|
protected function assertGeneration($tokens, $expect) {
|
2008-04-28 01:35:07 +00:00
|
|
|
$generator = new HTMLPurifier_Generator($this->config, $this->context);
|
|
|
|
$result = $generator->generateFromTokens($tokens);
|
2007-05-05 20:17:04 +00:00
|
|
|
$this->assertIdentical($expect, $result);
|
2006-09-16 00:37:33 +00:00
|
|
|
}
|
|
|
|
|
2007-05-15 01:17:10 +00:00
|
|
|
function test_generateFromTokens_Scripting() {
|
|
|
|
$this->assertGeneration(
|
|
|
|
array(
|
|
|
|
new HTMLPurifier_Token_Start('script'),
|
|
|
|
new HTMLPurifier_Token_Text('alert(3 < 5);'),
|
|
|
|
new HTMLPurifier_Token_End('script')
|
|
|
|
),
|
2007-06-21 14:44:26 +00:00
|
|
|
"<script><!--//--><![CDATA[//><!--\nalert(3 < 5);\n//--><!]]></script>"
|
2007-05-15 01:17:10 +00:00
|
|
|
);
|
2008-04-28 01:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateFromTokens_Scripting_missingCloseTag() {
|
2007-06-21 14:44:26 +00:00
|
|
|
$this->assertGeneration(
|
|
|
|
array(
|
|
|
|
new HTMLPurifier_Token_Start('script'),
|
|
|
|
new HTMLPurifier_Token_Text('alert(3 < 5);'),
|
|
|
|
),
|
|
|
|
"<script>alert(3 < 5);"
|
|
|
|
);
|
2008-04-28 01:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateFromTokens_Scripting_doubleBlock() {
|
2007-06-21 14:44:26 +00:00
|
|
|
$this->assertGeneration(
|
|
|
|
array(
|
|
|
|
new HTMLPurifier_Token_Start('script'),
|
|
|
|
new HTMLPurifier_Token_Text('alert(3 < 5);'),
|
|
|
|
new HTMLPurifier_Token_Text('foo();'),
|
|
|
|
new HTMLPurifier_Token_End('script')
|
|
|
|
),
|
|
|
|
"<script>alert(3 < 5);foo();</script>"
|
|
|
|
);
|
2008-04-28 01:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateFromTokens_Scripting_disableWrapper() {
|
2008-04-03 21:53:06 +00:00
|
|
|
$this->config->set('Output', 'CommentScriptContents', false);
|
2007-05-15 01:17:10 +00:00
|
|
|
$this->assertGeneration(
|
|
|
|
array(
|
|
|
|
new HTMLPurifier_Token_Start('script'),
|
|
|
|
new HTMLPurifier_Token_Text('alert(3 < 5);'),
|
|
|
|
new HTMLPurifier_Token_End('script')
|
|
|
|
),
|
|
|
|
"<script>alert(3 < 5);</script>"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2006-09-16 00:37:33 +00:00
|
|
|
function test_generateFromTokens_XHTMLoff() {
|
2008-04-03 21:53:06 +00:00
|
|
|
$this->config->set('HTML', 'XHTML', false);
|
2006-09-16 00:37:33 +00:00
|
|
|
|
|
|
|
// omit trailing slash
|
|
|
|
$this->assertGeneration(
|
|
|
|
array( new HTMLPurifier_Token_Empty('br') ),
|
|
|
|
'<br>'
|
|
|
|
);
|
|
|
|
|
|
|
|
// there should be a test for attribute minimization, but it is
|
|
|
|
// impossible for something like that to happen due to our current
|
|
|
|
// definitions! fix it later
|
|
|
|
|
|
|
|
// namespaced attributes must be dropped
|
|
|
|
$this->assertGeneration(
|
|
|
|
array( new HTMLPurifier_Token_Start('p', array('xml:lang'=>'fr')) ),
|
|
|
|
'<p>'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-09-24 21:23:54 +00:00
|
|
|
function test_generateFromTokens_TidyFormat() {
|
|
|
|
// abort test if tidy isn't loaded
|
|
|
|
if (!extension_loaded('tidy')) return;
|
|
|
|
|
2008-05-20 17:14:08 +00:00
|
|
|
// just don't test; Tidy is exploding on me.
|
|
|
|
return;
|
|
|
|
|
2008-05-15 23:04:46 +00:00
|
|
|
$this->config->set('Core', 'TidyFormat', true);
|
2007-06-25 19:18:55 +00:00
|
|
|
$this->config->set('Output', 'Newline', "\n");
|
2006-09-24 21:23:54 +00:00
|
|
|
|
|
|
|
// nice wrapping please
|
|
|
|
$this->assertGeneration(
|
|
|
|
array(
|
|
|
|
new HTMLPurifier_Token_Start('div'),
|
|
|
|
new HTMLPurifier_Token_Text('Text'),
|
2008-05-15 23:04:46 +00:00
|
|
|
new HTMLPurifier_Token_End('div')
|
2006-09-24 21:23:54 +00:00
|
|
|
),
|
2008-05-15 23:04:46 +00:00
|
|
|
"<div>\n Text\n</div>\n"
|
2006-09-24 21:23:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-06-25 02:36:27 +00:00
|
|
|
function test_generateFromTokens_sortAttr() {
|
|
|
|
$this->config->set('Output', 'SortAttr', true);
|
|
|
|
|
|
|
|
$this->assertGeneration(
|
|
|
|
array( new HTMLPurifier_Token_Start('p', array('b'=>'c', 'a'=>'d')) ),
|
|
|
|
'<p a="d" b="c">'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|