2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/Generator.php';
|
2006-08-01 00:29:38 +00:00
|
|
|
require_once 'HTMLPurifier/EntityLookup.php';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
require_once 'HTMLPurifier/Harness.php';
|
|
|
|
|
|
|
|
class HTMLPurifier_GeneratorTest extends HTMLPurifier_Harness
|
2006-07-23 00:11:03 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
var $gen;
|
2006-08-01 00:29:38 +00:00
|
|
|
var $_entity_lookup;
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
function HTMLPurifier_GeneratorTest() {
|
|
|
|
$this->UnitTestCase();
|
|
|
|
$this->gen = new HTMLPurifier_Generator();
|
2006-08-01 00:29:38 +00:00
|
|
|
$this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
function setUp() {
|
|
|
|
$this->obj = new HTMLPurifier_Generator();
|
|
|
|
$this->func = null;
|
|
|
|
$this->to_tokens = false;
|
|
|
|
$this->to_html = false;
|
|
|
|
}
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
function test_generateFromToken() {
|
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
$inputs = $expect = array();
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
$inputs[0] = new HTMLPurifier_Token_Text('Foobar.<>');
|
|
|
|
$expect[0] = 'Foobar.<>';
|
|
|
|
|
|
|
|
$inputs[1] = new HTMLPurifier_Token_Start('a',
|
|
|
|
array('href' => 'dyn?a=foo&b=bar')
|
|
|
|
);
|
|
|
|
$expect[1] = '<a href="dyn?a=foo&b=bar">';
|
|
|
|
|
|
|
|
$inputs[2] = new HTMLPurifier_Token_End('b');
|
|
|
|
$expect[2] = '</b>';
|
|
|
|
|
|
|
|
$inputs[3] = new HTMLPurifier_Token_Empty('br',
|
|
|
|
array('style' => 'font-family:"Courier New";')
|
|
|
|
);
|
|
|
|
$expect[3] = '<br style="font-family:"Courier New";" />';
|
|
|
|
|
|
|
|
$inputs[4] = new HTMLPurifier_Token_Start('asdf');
|
|
|
|
$expect[4] = '<asdf>';
|
|
|
|
|
|
|
|
$inputs[5] = new HTMLPurifier_Token_Empty('br');
|
|
|
|
$expect[5] = '<br />';
|
|
|
|
|
2006-08-01 00:29:38 +00:00
|
|
|
// test fault tolerance
|
|
|
|
$inputs[6] = null;
|
|
|
|
$expect[6] = '';
|
|
|
|
|
|
|
|
// don't convert non-special characters
|
|
|
|
$theta_char = $this->_entity_lookup->table['theta'];
|
|
|
|
$inputs[7] = new HTMLPurifier_Token_Text($theta_char);
|
|
|
|
$expect[7] = $theta_char;
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
foreach ($inputs as $i => $input) {
|
2006-10-01 21:55:13 +00:00
|
|
|
$result = $this->obj->generateFromToken($input);
|
2007-05-05 20:17:04 +00:00
|
|
|
$this->assertIdentical($result, $expect[$i]);
|
2006-07-23 00:11:03 +00:00
|
|
|
paintIf($result, $result != $expect[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_generateAttributes() {
|
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
$inputs = $expect = array();
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
$inputs[0] = array();
|
|
|
|
$expect[0] = '';
|
|
|
|
|
|
|
|
$inputs[1] = array('href' => 'dyn?a=foo&b=bar');
|
|
|
|
$expect[1] = 'href="dyn?a=foo&b=bar"';
|
|
|
|
|
|
|
|
$inputs[2] = array('style' => 'font-family:"Courier New";');
|
|
|
|
$expect[2] = 'style="font-family:"Courier New";"';
|
|
|
|
|
|
|
|
$inputs[3] = array('src' => 'picture.jpg', 'alt' => 'Short & interesting');
|
|
|
|
$expect[3] = 'src="picture.jpg" alt="Short & interesting"';
|
|
|
|
|
2006-08-01 00:29:38 +00:00
|
|
|
// don't escape nonspecial characters
|
|
|
|
$theta_char = $this->_entity_lookup->table['theta'];
|
|
|
|
$inputs[4] = array('title' => 'Theta is ' . $theta_char);
|
|
|
|
$expect[4] = 'title="Theta is ' . $theta_char . '"';
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
foreach ($inputs as $i => $input) {
|
2007-05-21 01:36:15 +00:00
|
|
|
$result = $this->obj->generateAttributes($input, 'irrelevant');
|
2007-05-05 20:17:04 +00:00
|
|
|
$this->assertIdentical($result, $expect[$i]);
|
2006-07-23 00:11:03 +00:00
|
|
|
paintIf($result, $result != $expect[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-05-21 01:36:15 +00:00
|
|
|
function test_generateAttributes_minimized() {
|
|
|
|
$gen = new HTMLPurifier_Generator();
|
|
|
|
$context = new HTMLPurifier_Context();
|
|
|
|
$gen->generateFromTokens(array(), HTMLPurifier_Config::create(array('HTML.Doctype' => 'HTML 4.01 Transitional')), $context);
|
|
|
|
$result = $gen->generateAttributes(array('compact' => 'compact'), 'menu');
|
|
|
|
$this->assertIdentical($result, 'compact');
|
|
|
|
}
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
function test_generateFromTokens() {
|
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->func = 'generateFromTokens';
|
2006-07-30 18:37:42 +00:00
|
|
|
|
2006-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(
|
|
|
|
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-10-01 21:55:13 +00:00
|
|
|
$this->assertResult(array(), '');
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-09-16 00:37:33 +00:00
|
|
|
var $config;
|
|
|
|
function assertGeneration($tokens, $expect) {
|
2006-10-01 21:55:13 +00:00
|
|
|
$context = new HTMLPurifier_Context();
|
|
|
|
$result = $this->gen->generateFromTokens(
|
|
|
|
$tokens, $this->config, $context);
|
2006-09-24 21:23:54 +00:00
|
|
|
// normalized newlines, this probably should be put somewhere else
|
|
|
|
$result = str_replace("\r\n", "\n", $result);
|
|
|
|
$result = str_replace("\r", "\n", $result);
|
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->config = HTMLPurifier_Config::createDefault();
|
|
|
|
|
|
|
|
$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
|
|
|
);
|
|
|
|
|
2007-06-21 14:44:26 +00:00
|
|
|
// if missing close tag, don't do anything
|
|
|
|
$this->assertGeneration(
|
|
|
|
array(
|
|
|
|
new HTMLPurifier_Token_Start('script'),
|
|
|
|
new HTMLPurifier_Token_Text('alert(3 < 5);'),
|
|
|
|
),
|
|
|
|
"<script>alert(3 < 5);"
|
|
|
|
);
|
|
|
|
|
|
|
|
// if two script blocks, don't do anything
|
|
|
|
$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>"
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-05-15 01:17:10 +00:00
|
|
|
$this->config = HTMLPurifier_Config::createDefault();
|
|
|
|
$this->config->set('Core', 'CommentScriptContents', false);
|
|
|
|
|
|
|
|
$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() {
|
|
|
|
$this->config = HTMLPurifier_Config::createDefault();
|
|
|
|
$this->config->set('Core', 'XHTML', false);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
$this->config = HTMLPurifier_Config::createDefault();
|
|
|
|
$this->config->set('Core', 'TidyFormat', true);
|
|
|
|
|
|
|
|
// nice wrapping please
|
|
|
|
$this->assertGeneration(
|
|
|
|
array(
|
|
|
|
new HTMLPurifier_Token_Start('div'),
|
|
|
|
new HTMLPurifier_Token_Text('Text'),
|
|
|
|
new HTMLPurifier_Token_End('div')
|
|
|
|
),
|
|
|
|
"<div>\n Text\n</div>\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2006-04-16 03:27:40 +00:00
|
|
|
?>
|