2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/Definition.php';
|
2006-07-24 01:22:51 +00:00
|
|
|
require_once 'HTMLPurifier/Lexer/DirectLex.php';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
class HTMLPurifier_DefinitionTest extends UnitTestCase
|
|
|
|
{
|
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
var $def, $lex, $gen;
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
function HTMLPurifier_DefinitionTest() {
|
|
|
|
$this->UnitTestCase();
|
|
|
|
$this->def = new HTMLPurifier_Definition();
|
|
|
|
$this->def->loadData();
|
2006-07-24 01:22:51 +00:00
|
|
|
|
|
|
|
// we can't use the DOM lexer since it does too much stuff
|
|
|
|
// automatically, however, we should be able to use it
|
|
|
|
// interchangeably if we wanted to...
|
|
|
|
|
|
|
|
if (true) {
|
|
|
|
$this->lex = new HTMLPurifier_Lexer_DirectLex();
|
|
|
|
} else {
|
|
|
|
require_once 'HTMLPurifier/Lexer/DOMLex.php';
|
|
|
|
$this->lex = new HTMLPurifier_Lexer_DOMLex();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->gen = new HTMLPurifier_Generator();
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_removeForeignElements() {
|
|
|
|
|
|
|
|
$inputs = array();
|
|
|
|
$expect = array();
|
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[0] = '';
|
2006-07-23 00:11:03 +00:00
|
|
|
$expect[0] = $inputs[0];
|
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[1] = 'This is <b>bold text</b>.';
|
2006-07-23 00:11:03 +00:00
|
|
|
$expect[1] = $inputs[1];
|
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
// [INVALID]
|
|
|
|
$inputs[2] = '<asdf>Bling</asdf><d href="bang">Bong</d><foobar />';
|
|
|
|
$expect[2] = htmlspecialchars($inputs[2]);
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
foreach ($inputs as $i => $input) {
|
2006-07-24 01:22:51 +00:00
|
|
|
$tokens = $this->lex->tokenizeHTML($input);
|
|
|
|
$result_tokens = $this->def->removeForeignElements($tokens);
|
|
|
|
$result = $this->gen->generateFromTokens($result_tokens);
|
|
|
|
$this->assertEqual($expect[$i], $result, "Test $i: %s");
|
2006-07-23 00:11:03 +00:00
|
|
|
paintIf($result, $result != $expect[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_makeWellFormed() {
|
|
|
|
|
|
|
|
$inputs = array();
|
|
|
|
$expect = array();
|
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[0] = '';
|
2006-07-23 00:11:03 +00:00
|
|
|
$expect[0] = $inputs[0];
|
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[1] = 'This is <b>bold text</b>.';
|
2006-07-23 00:11:03 +00:00
|
|
|
$expect[1] = $inputs[1];
|
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[2] = '<b>Unclosed tag, gasp!';
|
|
|
|
$expect[2] = '<b>Unclosed tag, gasp!</b>';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[3] = '<b><i>Bold and italic?</b>';
|
|
|
|
$expect[3] = '<b><i>Bold and italic?</i></b>';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
// CHANGE THIS BEHAVIOR!
|
|
|
|
$inputs[4] = 'Unused end tags... recycle!</b>';
|
|
|
|
$expect[4] = 'Unused end tags... recycle!</b>';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[5] = '<br style="clear:both;">';
|
|
|
|
$expect[5] = '<br style="clear:both;" />';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[6] = '<div style="clear:both;" />';
|
|
|
|
$expect[6] = '<div style="clear:both;"></div>';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
// test automatic paragraph closing
|
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[7] = '<p>Paragraph 1<p>Paragraph 2';
|
|
|
|
$expect[7] = '<p>Paragraph 1</p><p>Paragraph 2</p>';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[8] = '<div><p>Paragraphs<p>In<p>A<p>Div</div>';
|
|
|
|
$expect[8] = '<div><p>Paragraphs</p><p>In</p><p>A</p><p>Div</p></div>';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
// automatic list closing
|
|
|
|
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[9] = '<ol><li>Item 1<li>Item 2</ol>';
|
|
|
|
$expect[9] = '<ol><li>Item 1</li><li>Item 2</li></ol>';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
foreach ($inputs as $i => $input) {
|
2006-07-24 01:22:51 +00:00
|
|
|
$tokens = $this->lex->tokenizeHTML($input);
|
|
|
|
$result_tokens = $this->def->makeWellFormed($tokens);
|
|
|
|
$result = $this->gen->generateFromTokens($result_tokens);
|
|
|
|
$this->assertEqual($expect[$i], $result, "Test $i: %s");
|
2006-07-23 00:11:03 +00:00
|
|
|
paintIf($result, $result != $expect[$i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_fixNesting() {
|
|
|
|
$inputs = array();
|
|
|
|
$expect = array();
|
|
|
|
|
|
|
|
// next id = 4
|
|
|
|
|
|
|
|
// legal inline nesting
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[0] = '<b>Bold text</b>';
|
2006-07-23 00:11:03 +00:00
|
|
|
$expect[0] = $inputs[0];
|
|
|
|
|
|
|
|
// legal inline and block
|
|
|
|
// as the parent element is considered FLOW
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[1] = '<a href="about:blank">Blank</a><div>Block</div>';
|
2006-07-23 00:11:03 +00:00
|
|
|
$expect[1] = $inputs[1];
|
|
|
|
|
|
|
|
// illegal block in inline, element -> text
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[2] = '<b><div>Illegal div.</div></b>';
|
|
|
|
$expect[2] = '<b><div>Illegal div.</div></b>';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
// test of empty set that's required, resulting in removal of node
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[3] = '<ul></ul>';
|
|
|
|
$expect[3] = '';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
// test illegal text which gets removed
|
2006-07-24 01:22:51 +00:00
|
|
|
$inputs[4] = '<ul>Illegal text<li>Legal item</li></ul>';
|
|
|
|
$expect[4] = '<ul><li>Legal item</li></ul>';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
foreach ($inputs as $i => $input) {
|
2006-07-24 01:22:51 +00:00
|
|
|
$tokens = $this->lex->tokenizeHTML($input);
|
|
|
|
$result_tokens = $this->def->fixNesting($tokens);
|
|
|
|
$result = $this->gen->generateFromTokens($result_tokens);
|
|
|
|
$this->assertEqual($expect[$i], $result, "Test $i: %s");
|
2006-07-23 00:11:03 +00:00
|
|
|
paintIf($result, $result != $expect[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-04-16 03:00:05 +00:00
|
|
|
?>
|