UnitTestCase();
$this->def = new HTMLPurifier_Definition();
$this->def->loadData();
// 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();
}
function test_removeForeignElements() {
$inputs = array();
$expect = array();
$inputs[0] = '';
$expect[0] = $inputs[0];
$inputs[1] = 'This is bold text.';
$expect[1] = $inputs[1];
// [INVALID]
$inputs[2] = 'BlingBong';
$expect[2] = htmlspecialchars($inputs[2]);
foreach ($inputs as $i => $input) {
$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");
paintIf($result, $result != $expect[$i]);
}
}
function test_makeWellFormed() {
$inputs = array();
$expect = array();
$inputs[0] = '';
$expect[0] = $inputs[0];
$inputs[1] = 'This is bold text.';
$expect[1] = $inputs[1];
$inputs[2] = 'Unclosed tag, gasp!';
$expect[2] = 'Unclosed tag, gasp!';
$inputs[3] = 'Bold and italic?';
$expect[3] = 'Bold and italic?';
// CHANGE THIS BEHAVIOR!
$inputs[4] = 'Unused end tags... recycle!';
$expect[4] = 'Unused end tags... recycle!</b>';
$inputs[5] = '
';
$expect[5] = '
';
$inputs[6] = '
';
$expect[6] = '';
// test automatic paragraph closing
$inputs[7] = 'Paragraph 1
Paragraph 2';
$expect[7] = '
Paragraph 1
Paragraph 2
';
$inputs[8] = '';
$expect[8] = '';
// automatic list closing
$inputs[9] = '- Item 1
- Item 2
';
$expect[9] = '- Item 1
- Item 2
';
foreach ($inputs as $i => $input) {
$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");
paintIf($result, $result != $expect[$i]);
}
}
function test_fixNesting() {
$inputs = array();
$expect = array();
// next id = 4
// legal inline nesting
$inputs[0] = 'Bold text';
$expect[0] = $inputs[0];
// legal inline and block
// as the parent element is considered FLOW
$inputs[1] = 'BlankBlock
';
$expect[1] = $inputs[1];
// illegal block in inline, element -> text
$inputs[2] = 'Illegal div.
';
$expect[2] = '<div>Illegal div.</div>';
// test of empty set that's required, resulting in removal of node
$inputs[3] = '';
$expect[3] = '';
// test illegal text which gets removed
$inputs[4] = '';
$expect[4] = '';
foreach ($inputs as $i => $input) {
$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");
paintIf($result, $result != $expect[$i]);
}
}
}
?>