mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-08-05 05:37:49 +02:00
svn:eol-style = native
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@97 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
@@ -1,132 +1,132 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/ChildDef.php';
|
||||
require_once 'HTMLPurifier/Lexer.php';
|
||||
require_once 'HTMLPurifier/Generator.php';
|
||||
|
||||
class HTMLPurifier_ChildDefTest extends UnitTestCase
|
||||
{
|
||||
|
||||
var $lex;
|
||||
var $gen;
|
||||
|
||||
function HTMLPurifier_ChildDefTest() {
|
||||
$this->lex = HTMLPurifier_Lexer::create();
|
||||
$this->gen = new HTMLPurifier_Generator();
|
||||
parent::UnitTestCase();
|
||||
}
|
||||
|
||||
function assertSeries($inputs, $expect, $def) {
|
||||
foreach ($inputs as $i => $input) {
|
||||
$tokens = $this->lex->tokenizeHTML($input);
|
||||
$result = $def->validateChildren($tokens);
|
||||
if (is_bool($expect[$i])) {
|
||||
$this->assertIdentical($expect[$i], $result);
|
||||
} else {
|
||||
$result_html = $this->gen->generateFromTokens($result);
|
||||
$this->assertEqual($expect[$i], $result_html);
|
||||
paintIf($result_html, $result_html != $expect[$i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function test_complex() {
|
||||
|
||||
// the table definition
|
||||
$def = new HTMLPurifier_ChildDef(
|
||||
'(caption?, (col*|colgroup*), thead?, tfoot?, (tbody+|tr+))');
|
||||
|
||||
$inputs[0] = '';
|
||||
$expect[0] = false;
|
||||
|
||||
// we really don't care what's inside, because if it turns out
|
||||
// this tr is illegal, we'll end up re-evaluating the parent node
|
||||
// anyway.
|
||||
$inputs[1] = '<tr></tr>';
|
||||
$expect[1] = true;
|
||||
|
||||
$inputs[2] = '<caption></caption><col></col><thead></thead>' .
|
||||
'<tfoot></tfoot><tbody></tbody>';
|
||||
$expect[2] = true;
|
||||
|
||||
$inputs[3] = '<col></col><col></col><col></col><tr></tr>';
|
||||
$expect[3] = true;
|
||||
|
||||
$this->assertSeries($inputs, $expect, $def);
|
||||
|
||||
}
|
||||
|
||||
function test_simple() {
|
||||
|
||||
// simple is actually an abstract class
|
||||
// but we're unit testing some of the conv. functions it gives
|
||||
|
||||
$def = new HTMLPurifier_ChildDef_Simple('foobar | bang |gizmo');
|
||||
$this->assertEqual($def->elements,
|
||||
array(
|
||||
'foobar' => true
|
||||
,'bang' => true
|
||||
,'gizmo' => true
|
||||
));
|
||||
|
||||
$def = new HTMLPurifier_ChildDef_Simple(array('href', 'src'));
|
||||
$this->assertEqual($def->elements,
|
||||
array(
|
||||
'href' => true
|
||||
,'src' => true
|
||||
));
|
||||
}
|
||||
|
||||
function test_required_pcdata_forbidden() {
|
||||
|
||||
$def = new HTMLPurifier_ChildDef_Required('dt | dd');
|
||||
|
||||
$inputs[0] = '';
|
||||
$expect[0] = false;
|
||||
|
||||
$inputs[1] = '<dt>Term</dt>Text in an illegal location'.
|
||||
'<dd>Definition</dd><b>Illegal tag</b>';
|
||||
|
||||
$expect[1] = '<dt>Term</dt><dd>Definition</dd>';
|
||||
|
||||
$inputs[2] = 'How do you do!';
|
||||
$expect[2] = false;
|
||||
|
||||
// whitespace shouldn't trigger it
|
||||
$inputs[3] = "\n<dd>Definition</dd> ";
|
||||
$expect[3] = true;
|
||||
|
||||
$inputs[4] ='<dd>Definition</dd> <b></b> ';
|
||||
$expect[4] = '<dd>Definition</dd> ';
|
||||
|
||||
$inputs[5] = "\t ";
|
||||
$expect[5] = false;
|
||||
|
||||
$this->assertSeries($inputs, $expect, $def);
|
||||
|
||||
}
|
||||
|
||||
function test_required_pcdata_allowed() {
|
||||
$def = new HTMLPurifier_ChildDef_Required('#PCDATA | b');
|
||||
|
||||
$inputs[0] = '<b>Bold text</b><img />';
|
||||
$expect[0] = '<b>Bold text</b><img />';
|
||||
|
||||
$this->assertSeries($inputs, $expect, $def);
|
||||
}
|
||||
|
||||
function test_optional() {
|
||||
$def = new HTMLPurifier_ChildDef_Optional('b | i');
|
||||
|
||||
$inputs[0] = '<b>Bold text</b><img />';
|
||||
$expect[0] = '<b>Bold text</b>';
|
||||
|
||||
$inputs[1] = 'Not allowed text';
|
||||
$expect[1] = '';
|
||||
|
||||
$this->assertSeries($inputs, $expect, $def);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/ChildDef.php';
|
||||
require_once 'HTMLPurifier/Lexer.php';
|
||||
require_once 'HTMLPurifier/Generator.php';
|
||||
|
||||
class HTMLPurifier_ChildDefTest extends UnitTestCase
|
||||
{
|
||||
|
||||
var $lex;
|
||||
var $gen;
|
||||
|
||||
function HTMLPurifier_ChildDefTest() {
|
||||
$this->lex = HTMLPurifier_Lexer::create();
|
||||
$this->gen = new HTMLPurifier_Generator();
|
||||
parent::UnitTestCase();
|
||||
}
|
||||
|
||||
function assertSeries($inputs, $expect, $def) {
|
||||
foreach ($inputs as $i => $input) {
|
||||
$tokens = $this->lex->tokenizeHTML($input);
|
||||
$result = $def->validateChildren($tokens);
|
||||
if (is_bool($expect[$i])) {
|
||||
$this->assertIdentical($expect[$i], $result);
|
||||
} else {
|
||||
$result_html = $this->gen->generateFromTokens($result);
|
||||
$this->assertEqual($expect[$i], $result_html);
|
||||
paintIf($result_html, $result_html != $expect[$i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function test_complex() {
|
||||
|
||||
// the table definition
|
||||
$def = new HTMLPurifier_ChildDef(
|
||||
'(caption?, (col*|colgroup*), thead?, tfoot?, (tbody+|tr+))');
|
||||
|
||||
$inputs[0] = '';
|
||||
$expect[0] = false;
|
||||
|
||||
// we really don't care what's inside, because if it turns out
|
||||
// this tr is illegal, we'll end up re-evaluating the parent node
|
||||
// anyway.
|
||||
$inputs[1] = '<tr></tr>';
|
||||
$expect[1] = true;
|
||||
|
||||
$inputs[2] = '<caption></caption><col></col><thead></thead>' .
|
||||
'<tfoot></tfoot><tbody></tbody>';
|
||||
$expect[2] = true;
|
||||
|
||||
$inputs[3] = '<col></col><col></col><col></col><tr></tr>';
|
||||
$expect[3] = true;
|
||||
|
||||
$this->assertSeries($inputs, $expect, $def);
|
||||
|
||||
}
|
||||
|
||||
function test_simple() {
|
||||
|
||||
// simple is actually an abstract class
|
||||
// but we're unit testing some of the conv. functions it gives
|
||||
|
||||
$def = new HTMLPurifier_ChildDef_Simple('foobar | bang |gizmo');
|
||||
$this->assertEqual($def->elements,
|
||||
array(
|
||||
'foobar' => true
|
||||
,'bang' => true
|
||||
,'gizmo' => true
|
||||
));
|
||||
|
||||
$def = new HTMLPurifier_ChildDef_Simple(array('href', 'src'));
|
||||
$this->assertEqual($def->elements,
|
||||
array(
|
||||
'href' => true
|
||||
,'src' => true
|
||||
));
|
||||
}
|
||||
|
||||
function test_required_pcdata_forbidden() {
|
||||
|
||||
$def = new HTMLPurifier_ChildDef_Required('dt | dd');
|
||||
|
||||
$inputs[0] = '';
|
||||
$expect[0] = false;
|
||||
|
||||
$inputs[1] = '<dt>Term</dt>Text in an illegal location'.
|
||||
'<dd>Definition</dd><b>Illegal tag</b>';
|
||||
|
||||
$expect[1] = '<dt>Term</dt><dd>Definition</dd>';
|
||||
|
||||
$inputs[2] = 'How do you do!';
|
||||
$expect[2] = false;
|
||||
|
||||
// whitespace shouldn't trigger it
|
||||
$inputs[3] = "\n<dd>Definition</dd> ";
|
||||
$expect[3] = true;
|
||||
|
||||
$inputs[4] ='<dd>Definition</dd> <b></b> ';
|
||||
$expect[4] = '<dd>Definition</dd> ';
|
||||
|
||||
$inputs[5] = "\t ";
|
||||
$expect[5] = false;
|
||||
|
||||
$this->assertSeries($inputs, $expect, $def);
|
||||
|
||||
}
|
||||
|
||||
function test_required_pcdata_allowed() {
|
||||
$def = new HTMLPurifier_ChildDef_Required('#PCDATA | b');
|
||||
|
||||
$inputs[0] = '<b>Bold text</b><img />';
|
||||
$expect[0] = '<b>Bold text</b><img />';
|
||||
|
||||
$this->assertSeries($inputs, $expect, $def);
|
||||
}
|
||||
|
||||
function test_optional() {
|
||||
$def = new HTMLPurifier_ChildDef_Optional('b | i');
|
||||
|
||||
$inputs[0] = '<b>Bold text</b><img />';
|
||||
$expect[0] = '<b>Bold text</b>';
|
||||
|
||||
$inputs[1] = 'Not allowed text';
|
||||
$expect[1] = '';
|
||||
|
||||
$this->assertSeries($inputs, $expect, $def);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@@ -1,267 +1,267 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Definition.php';
|
||||
require_once 'HTMLPurifier/Lexer.php';
|
||||
|
||||
class HTMLPurifier_DefinitionTest extends UnitTestCase
|
||||
{
|
||||
|
||||
var $def, $lex;
|
||||
|
||||
function HTMLPurifier_DefinitionTest() {
|
||||
$this->UnitTestCase();
|
||||
$this->def = new HTMLPurifier_Definition();
|
||||
$this->def->loadData();
|
||||
$this->lex = new HTMLPurifier_Lexer();
|
||||
}
|
||||
|
||||
function test_removeForeignElements() {
|
||||
|
||||
$inputs = array();
|
||||
$expect = array();
|
||||
|
||||
$inputs[0] = array();
|
||||
$expect[0] = $inputs[0];
|
||||
|
||||
$inputs[1] = array(
|
||||
new HTMLPurifier_Token_Text('This is ')
|
||||
,new HTMLPurifier_Token_Start('b', array())
|
||||
,new HTMLPurifier_Token_Text('bold')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
,new HTMLPurifier_Token_Text(' text')
|
||||
);
|
||||
$expect[1] = $inputs[1];
|
||||
|
||||
$inputs[2] = array(
|
||||
new HTMLPurifier_Token_Start('asdf')
|
||||
,new HTMLPurifier_Token_End('asdf')
|
||||
,new HTMLPurifier_Token_Start('d', array('href' => 'bang!'))
|
||||
,new HTMLPurifier_Token_End('d')
|
||||
,new HTMLPurifier_Token_Start('pooloka')
|
||||
,new HTMLPurifier_Token_Start('poolasdf')
|
||||
,new HTMLPurifier_Token_Start('ds', array('moogle' => '&'))
|
||||
,new HTMLPurifier_Token_End('asdf')
|
||||
,new HTMLPurifier_Token_End('asdf')
|
||||
);
|
||||
$expect[2] = array(
|
||||
new HTMLPurifier_Token_Text('<asdf>')
|
||||
,new HTMLPurifier_Token_Text('</asdf>')
|
||||
,new HTMLPurifier_Token_Text('<d href="bang!">')
|
||||
,new HTMLPurifier_Token_Text('</d>')
|
||||
,new HTMLPurifier_Token_Text('<pooloka>')
|
||||
,new HTMLPurifier_Token_Text('<poolasdf>')
|
||||
,new HTMLPurifier_Token_Text('<ds moogle="&">')
|
||||
,new HTMLPurifier_Token_Text('</asdf>')
|
||||
,new HTMLPurifier_Token_Text('</asdf>')
|
||||
);
|
||||
|
||||
foreach ($inputs as $i => $input) {
|
||||
$result = $this->def->removeForeignElements($input);
|
||||
$this->assertEqual($expect[$i], $result);
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function test_makeWellFormed() {
|
||||
|
||||
$inputs = array();
|
||||
$expect = array();
|
||||
|
||||
$inputs[0] = array();
|
||||
$expect[0] = $inputs[0];
|
||||
|
||||
$inputs[1] = array(
|
||||
new HTMLPurifier_Token_Text('This is ')
|
||||
,new HTMLPurifier_Token_Start('b')
|
||||
,new HTMLPurifier_Token_Text('bold')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
,new HTMLPurifier_Token_Text(' text')
|
||||
,new HTMLPurifier_Token_Empty('br')
|
||||
);
|
||||
$expect[1] = $inputs[1];
|
||||
|
||||
$inputs[2] = array(
|
||||
new HTMLPurifier_Token_Start('b')
|
||||
,new HTMLPurifier_Token_Text('Unclosed tag, gasp!')
|
||||
);
|
||||
$expect[2] = array(
|
||||
new HTMLPurifier_Token_Start('b')
|
||||
,new HTMLPurifier_Token_Text('Unclosed tag, gasp!')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
);
|
||||
|
||||
$inputs[3] = array(
|
||||
new HTMLPurifier_Token_Start('b')
|
||||
,new HTMLPurifier_Token_Start('i')
|
||||
,new HTMLPurifier_Token_Text('The b is closed, but the i is not')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
);
|
||||
$expect[3] = array(
|
||||
new HTMLPurifier_Token_Start('b')
|
||||
,new HTMLPurifier_Token_Start('i')
|
||||
,new HTMLPurifier_Token_Text('The b is closed, but the i is not')
|
||||
,new HTMLPurifier_Token_End('i')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
);
|
||||
|
||||
$inputs[4] = array(
|
||||
new HTMLPurifier_Token_Text('Hey, recycle unused end tags!')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
);
|
||||
$expect[4] = array(
|
||||
new HTMLPurifier_Token_Text('Hey, recycle unused end tags!')
|
||||
,new HTMLPurifier_Token_Text('</b>')
|
||||
);
|
||||
|
||||
$inputs[5] = array(new HTMLPurifier_Token_Start('br', array('style' => 'clear:both;')));
|
||||
$expect[5] = array(new HTMLPurifier_Token_Empty('br', array('style' => 'clear:both;')));
|
||||
|
||||
$inputs[6] = array(new HTMLPurifier_Token_Empty('div', array('style' => 'clear:both;')));
|
||||
$expect[6] = array(
|
||||
new HTMLPurifier_Token_Start('div', array('style' => 'clear:both;'))
|
||||
,new HTMLPurifier_Token_End('div')
|
||||
);
|
||||
|
||||
// test automatic paragraph closing
|
||||
|
||||
$inputs[7] = array(
|
||||
new HTMLPurifier_Token_Start('p')
|
||||
,new HTMLPurifier_Token_Text('Paragraph 1')
|
||||
,new HTMLPurifier_Token_Start('p')
|
||||
,new HTMLPurifier_Token_Text('Paragraph 2')
|
||||
);
|
||||
$expect[7] = array(
|
||||
new HTMLPurifier_Token_Start('p')
|
||||
,new HTMLPurifier_Token_Text('Paragraph 1')
|
||||
,new HTMLPurifier_Token_End('p')
|
||||
,new HTMLPurifier_Token_Start('p')
|
||||
,new HTMLPurifier_Token_Text('Paragraph 2')
|
||||
,new HTMLPurifier_Token_End('p')
|
||||
);
|
||||
|
||||
$inputs[8] = array(
|
||||
new HTMLPurifier_Token_Start('div')
|
||||
,new HTMLPurifier_Token_Start('p')
|
||||
,new HTMLPurifier_Token_Text('Paragraph 1 in a div')
|
||||
,new HTMLPurifier_Token_End('div')
|
||||
);
|
||||
$expect[8] = array(
|
||||
new HTMLPurifier_Token_Start('div')
|
||||
,new HTMLPurifier_Token_Start('p')
|
||||
,new HTMLPurifier_Token_Text('Paragraph 1 in a div')
|
||||
,new HTMLPurifier_Token_End('p')
|
||||
,new HTMLPurifier_Token_End('div')
|
||||
);
|
||||
|
||||
// automatic list closing
|
||||
|
||||
$inputs[9] = array(
|
||||
new HTMLPurifier_Token_Start('ol')
|
||||
|
||||
,new HTMLPurifier_Token_Start('li')
|
||||
,new HTMLPurifier_Token_Text('Item 1')
|
||||
|
||||
,new HTMLPurifier_Token_Start('li')
|
||||
,new HTMLPurifier_Token_Text('Item 2')
|
||||
|
||||
,new HTMLPurifier_Token_End('ol')
|
||||
);
|
||||
$expect[9] = array(
|
||||
new HTMLPurifier_Token_Start('ol')
|
||||
|
||||
,new HTMLPurifier_Token_Start('li')
|
||||
,new HTMLPurifier_Token_Text('Item 1')
|
||||
,new HTMLPurifier_Token_End('li')
|
||||
|
||||
,new HTMLPurifier_Token_Start('li')
|
||||
,new HTMLPurifier_Token_Text('Item 2')
|
||||
,new HTMLPurifier_Token_End('li')
|
||||
|
||||
,new HTMLPurifier_Token_End('ol')
|
||||
);
|
||||
|
||||
foreach ($inputs as $i => $input) {
|
||||
$result = $this->def->makeWellFormed($input);
|
||||
$this->assertEqual($expect[$i], $result);
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function test_fixNesting() {
|
||||
$inputs = array();
|
||||
$expect = array();
|
||||
|
||||
// next id = 4
|
||||
|
||||
// legal inline nesting
|
||||
$inputs[0] = array(
|
||||
new HTMLPurifier_Token_Start('b'),
|
||||
new HTMLPurifier_Token_Text('Bold text'),
|
||||
new HTMLPurifier_Token_End('b'),
|
||||
);
|
||||
$expect[0] = $inputs[0];
|
||||
|
||||
// legal inline and block
|
||||
// as the parent element is considered FLOW
|
||||
$inputs[1] = array(
|
||||
new HTMLPurifier_Token_Start('a', array('href' => 'http://www.example.com/')),
|
||||
new HTMLPurifier_Token_Text('Linky'),
|
||||
new HTMLPurifier_Token_End('a'),
|
||||
new HTMLPurifier_Token_Start('div'),
|
||||
new HTMLPurifier_Token_Text('Block element'),
|
||||
new HTMLPurifier_Token_End('div'),
|
||||
);
|
||||
$expect[1] = $inputs[1];
|
||||
|
||||
// illegal block in inline, element -> text
|
||||
$inputs[2] = array(
|
||||
new HTMLPurifier_Token_Start('b'),
|
||||
new HTMLPurifier_Token_Start('div'),
|
||||
new HTMLPurifier_Token_Text('Illegal Div'),
|
||||
new HTMLPurifier_Token_End('div'),
|
||||
new HTMLPurifier_Token_End('b'),
|
||||
);
|
||||
$expect[2] = array(
|
||||
new HTMLPurifier_Token_Start('b'),
|
||||
new HTMLPurifier_Token_Text('<div>'),
|
||||
new HTMLPurifier_Token_Text('Illegal Div'),
|
||||
new HTMLPurifier_Token_Text('</div>'),
|
||||
new HTMLPurifier_Token_End('b'),
|
||||
);
|
||||
|
||||
// test of empty set that's required, resulting in removal of node
|
||||
$inputs[3] = array(
|
||||
new HTMLPurifier_Token_Start('ul'),
|
||||
new HTMLPurifier_Token_End('ul')
|
||||
);
|
||||
$expect[3] = array();
|
||||
|
||||
// test illegal text which gets removed
|
||||
$inputs[4] = array(
|
||||
new HTMLPurifier_Token_Start('ul'),
|
||||
new HTMLPurifier_Token_Text('Illegal Text'),
|
||||
new HTMLPurifier_Token_Start('li'),
|
||||
new HTMLPurifier_Token_Text('Legal item'),
|
||||
new HTMLPurifier_Token_End('li'),
|
||||
new HTMLPurifier_Token_End('ul')
|
||||
);
|
||||
$expect[4] = array(
|
||||
new HTMLPurifier_Token_Start('ul'),
|
||||
new HTMLPurifier_Token_Start('li'),
|
||||
new HTMLPurifier_Token_Text('Legal item'),
|
||||
new HTMLPurifier_Token_End('li'),
|
||||
new HTMLPurifier_Token_End('ul')
|
||||
);
|
||||
|
||||
foreach ($inputs as $i => $input) {
|
||||
$result = $this->def->fixNesting($input);
|
||||
$this->assertEqual($expect[$i], $result);
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Definition.php';
|
||||
require_once 'HTMLPurifier/Lexer.php';
|
||||
|
||||
class HTMLPurifier_DefinitionTest extends UnitTestCase
|
||||
{
|
||||
|
||||
var $def, $lex;
|
||||
|
||||
function HTMLPurifier_DefinitionTest() {
|
||||
$this->UnitTestCase();
|
||||
$this->def = new HTMLPurifier_Definition();
|
||||
$this->def->loadData();
|
||||
$this->lex = new HTMLPurifier_Lexer();
|
||||
}
|
||||
|
||||
function test_removeForeignElements() {
|
||||
|
||||
$inputs = array();
|
||||
$expect = array();
|
||||
|
||||
$inputs[0] = array();
|
||||
$expect[0] = $inputs[0];
|
||||
|
||||
$inputs[1] = array(
|
||||
new HTMLPurifier_Token_Text('This is ')
|
||||
,new HTMLPurifier_Token_Start('b', array())
|
||||
,new HTMLPurifier_Token_Text('bold')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
,new HTMLPurifier_Token_Text(' text')
|
||||
);
|
||||
$expect[1] = $inputs[1];
|
||||
|
||||
$inputs[2] = array(
|
||||
new HTMLPurifier_Token_Start('asdf')
|
||||
,new HTMLPurifier_Token_End('asdf')
|
||||
,new HTMLPurifier_Token_Start('d', array('href' => 'bang!'))
|
||||
,new HTMLPurifier_Token_End('d')
|
||||
,new HTMLPurifier_Token_Start('pooloka')
|
||||
,new HTMLPurifier_Token_Start('poolasdf')
|
||||
,new HTMLPurifier_Token_Start('ds', array('moogle' => '&'))
|
||||
,new HTMLPurifier_Token_End('asdf')
|
||||
,new HTMLPurifier_Token_End('asdf')
|
||||
);
|
||||
$expect[2] = array(
|
||||
new HTMLPurifier_Token_Text('<asdf>')
|
||||
,new HTMLPurifier_Token_Text('</asdf>')
|
||||
,new HTMLPurifier_Token_Text('<d href="bang!">')
|
||||
,new HTMLPurifier_Token_Text('</d>')
|
||||
,new HTMLPurifier_Token_Text('<pooloka>')
|
||||
,new HTMLPurifier_Token_Text('<poolasdf>')
|
||||
,new HTMLPurifier_Token_Text('<ds moogle="&">')
|
||||
,new HTMLPurifier_Token_Text('</asdf>')
|
||||
,new HTMLPurifier_Token_Text('</asdf>')
|
||||
);
|
||||
|
||||
foreach ($inputs as $i => $input) {
|
||||
$result = $this->def->removeForeignElements($input);
|
||||
$this->assertEqual($expect[$i], $result);
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function test_makeWellFormed() {
|
||||
|
||||
$inputs = array();
|
||||
$expect = array();
|
||||
|
||||
$inputs[0] = array();
|
||||
$expect[0] = $inputs[0];
|
||||
|
||||
$inputs[1] = array(
|
||||
new HTMLPurifier_Token_Text('This is ')
|
||||
,new HTMLPurifier_Token_Start('b')
|
||||
,new HTMLPurifier_Token_Text('bold')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
,new HTMLPurifier_Token_Text(' text')
|
||||
,new HTMLPurifier_Token_Empty('br')
|
||||
);
|
||||
$expect[1] = $inputs[1];
|
||||
|
||||
$inputs[2] = array(
|
||||
new HTMLPurifier_Token_Start('b')
|
||||
,new HTMLPurifier_Token_Text('Unclosed tag, gasp!')
|
||||
);
|
||||
$expect[2] = array(
|
||||
new HTMLPurifier_Token_Start('b')
|
||||
,new HTMLPurifier_Token_Text('Unclosed tag, gasp!')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
);
|
||||
|
||||
$inputs[3] = array(
|
||||
new HTMLPurifier_Token_Start('b')
|
||||
,new HTMLPurifier_Token_Start('i')
|
||||
,new HTMLPurifier_Token_Text('The b is closed, but the i is not')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
);
|
||||
$expect[3] = array(
|
||||
new HTMLPurifier_Token_Start('b')
|
||||
,new HTMLPurifier_Token_Start('i')
|
||||
,new HTMLPurifier_Token_Text('The b is closed, but the i is not')
|
||||
,new HTMLPurifier_Token_End('i')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
);
|
||||
|
||||
$inputs[4] = array(
|
||||
new HTMLPurifier_Token_Text('Hey, recycle unused end tags!')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
);
|
||||
$expect[4] = array(
|
||||
new HTMLPurifier_Token_Text('Hey, recycle unused end tags!')
|
||||
,new HTMLPurifier_Token_Text('</b>')
|
||||
);
|
||||
|
||||
$inputs[5] = array(new HTMLPurifier_Token_Start('br', array('style' => 'clear:both;')));
|
||||
$expect[5] = array(new HTMLPurifier_Token_Empty('br', array('style' => 'clear:both;')));
|
||||
|
||||
$inputs[6] = array(new HTMLPurifier_Token_Empty('div', array('style' => 'clear:both;')));
|
||||
$expect[6] = array(
|
||||
new HTMLPurifier_Token_Start('div', array('style' => 'clear:both;'))
|
||||
,new HTMLPurifier_Token_End('div')
|
||||
);
|
||||
|
||||
// test automatic paragraph closing
|
||||
|
||||
$inputs[7] = array(
|
||||
new HTMLPurifier_Token_Start('p')
|
||||
,new HTMLPurifier_Token_Text('Paragraph 1')
|
||||
,new HTMLPurifier_Token_Start('p')
|
||||
,new HTMLPurifier_Token_Text('Paragraph 2')
|
||||
);
|
||||
$expect[7] = array(
|
||||
new HTMLPurifier_Token_Start('p')
|
||||
,new HTMLPurifier_Token_Text('Paragraph 1')
|
||||
,new HTMLPurifier_Token_End('p')
|
||||
,new HTMLPurifier_Token_Start('p')
|
||||
,new HTMLPurifier_Token_Text('Paragraph 2')
|
||||
,new HTMLPurifier_Token_End('p')
|
||||
);
|
||||
|
||||
$inputs[8] = array(
|
||||
new HTMLPurifier_Token_Start('div')
|
||||
,new HTMLPurifier_Token_Start('p')
|
||||
,new HTMLPurifier_Token_Text('Paragraph 1 in a div')
|
||||
,new HTMLPurifier_Token_End('div')
|
||||
);
|
||||
$expect[8] = array(
|
||||
new HTMLPurifier_Token_Start('div')
|
||||
,new HTMLPurifier_Token_Start('p')
|
||||
,new HTMLPurifier_Token_Text('Paragraph 1 in a div')
|
||||
,new HTMLPurifier_Token_End('p')
|
||||
,new HTMLPurifier_Token_End('div')
|
||||
);
|
||||
|
||||
// automatic list closing
|
||||
|
||||
$inputs[9] = array(
|
||||
new HTMLPurifier_Token_Start('ol')
|
||||
|
||||
,new HTMLPurifier_Token_Start('li')
|
||||
,new HTMLPurifier_Token_Text('Item 1')
|
||||
|
||||
,new HTMLPurifier_Token_Start('li')
|
||||
,new HTMLPurifier_Token_Text('Item 2')
|
||||
|
||||
,new HTMLPurifier_Token_End('ol')
|
||||
);
|
||||
$expect[9] = array(
|
||||
new HTMLPurifier_Token_Start('ol')
|
||||
|
||||
,new HTMLPurifier_Token_Start('li')
|
||||
,new HTMLPurifier_Token_Text('Item 1')
|
||||
,new HTMLPurifier_Token_End('li')
|
||||
|
||||
,new HTMLPurifier_Token_Start('li')
|
||||
,new HTMLPurifier_Token_Text('Item 2')
|
||||
,new HTMLPurifier_Token_End('li')
|
||||
|
||||
,new HTMLPurifier_Token_End('ol')
|
||||
);
|
||||
|
||||
foreach ($inputs as $i => $input) {
|
||||
$result = $this->def->makeWellFormed($input);
|
||||
$this->assertEqual($expect[$i], $result);
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function test_fixNesting() {
|
||||
$inputs = array();
|
||||
$expect = array();
|
||||
|
||||
// next id = 4
|
||||
|
||||
// legal inline nesting
|
||||
$inputs[0] = array(
|
||||
new HTMLPurifier_Token_Start('b'),
|
||||
new HTMLPurifier_Token_Text('Bold text'),
|
||||
new HTMLPurifier_Token_End('b'),
|
||||
);
|
||||
$expect[0] = $inputs[0];
|
||||
|
||||
// legal inline and block
|
||||
// as the parent element is considered FLOW
|
||||
$inputs[1] = array(
|
||||
new HTMLPurifier_Token_Start('a', array('href' => 'http://www.example.com/')),
|
||||
new HTMLPurifier_Token_Text('Linky'),
|
||||
new HTMLPurifier_Token_End('a'),
|
||||
new HTMLPurifier_Token_Start('div'),
|
||||
new HTMLPurifier_Token_Text('Block element'),
|
||||
new HTMLPurifier_Token_End('div'),
|
||||
);
|
||||
$expect[1] = $inputs[1];
|
||||
|
||||
// illegal block in inline, element -> text
|
||||
$inputs[2] = array(
|
||||
new HTMLPurifier_Token_Start('b'),
|
||||
new HTMLPurifier_Token_Start('div'),
|
||||
new HTMLPurifier_Token_Text('Illegal Div'),
|
||||
new HTMLPurifier_Token_End('div'),
|
||||
new HTMLPurifier_Token_End('b'),
|
||||
);
|
||||
$expect[2] = array(
|
||||
new HTMLPurifier_Token_Start('b'),
|
||||
new HTMLPurifier_Token_Text('<div>'),
|
||||
new HTMLPurifier_Token_Text('Illegal Div'),
|
||||
new HTMLPurifier_Token_Text('</div>'),
|
||||
new HTMLPurifier_Token_End('b'),
|
||||
);
|
||||
|
||||
// test of empty set that's required, resulting in removal of node
|
||||
$inputs[3] = array(
|
||||
new HTMLPurifier_Token_Start('ul'),
|
||||
new HTMLPurifier_Token_End('ul')
|
||||
);
|
||||
$expect[3] = array();
|
||||
|
||||
// test illegal text which gets removed
|
||||
$inputs[4] = array(
|
||||
new HTMLPurifier_Token_Start('ul'),
|
||||
new HTMLPurifier_Token_Text('Illegal Text'),
|
||||
new HTMLPurifier_Token_Start('li'),
|
||||
new HTMLPurifier_Token_Text('Legal item'),
|
||||
new HTMLPurifier_Token_End('li'),
|
||||
new HTMLPurifier_Token_End('ul')
|
||||
);
|
||||
$expect[4] = array(
|
||||
new HTMLPurifier_Token_Start('ul'),
|
||||
new HTMLPurifier_Token_Start('li'),
|
||||
new HTMLPurifier_Token_Text('Legal item'),
|
||||
new HTMLPurifier_Token_End('li'),
|
||||
new HTMLPurifier_Token_End('ul')
|
||||
);
|
||||
|
||||
foreach ($inputs as $i => $input) {
|
||||
$result = $this->def->fixNesting($input);
|
||||
$this->assertEqual($expect[$i], $result);
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@@ -1,89 +1,89 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Generator.php';
|
||||
|
||||
class HTMLPurifier_GeneratorTest extends UnitTestCase
|
||||
{
|
||||
|
||||
var $gen;
|
||||
|
||||
function HTMLPurifier_GeneratorTest() {
|
||||
$this->UnitTestCase();
|
||||
$this->gen = new HTMLPurifier_Generator();
|
||||
}
|
||||
|
||||
function test_generateFromToken() {
|
||||
|
||||
$inputs = array();
|
||||
$expect = array();
|
||||
|
||||
$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 />';
|
||||
|
||||
foreach ($inputs as $i => $input) {
|
||||
$result = $this->gen->generateFromToken($input);
|
||||
$this->assertEqual($result, $expect[$i]);
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function test_generateAttributes() {
|
||||
|
||||
$inputs = array();
|
||||
$expect = array();
|
||||
|
||||
$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"';
|
||||
|
||||
foreach ($inputs as $i => $input) {
|
||||
$result = $this->gen->generateAttributes($input);
|
||||
$this->assertEqual($result, $expect[$i]);
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function test_generateFromTokens() {
|
||||
|
||||
$tokens = array(
|
||||
new HTMLPurifier_Token_Start('b'),
|
||||
new HTMLPurifier_Token_Text('Foobar!'),
|
||||
new HTMLPurifier_Token_End('b')
|
||||
);
|
||||
$expect = '<b>Foobar!</b>';
|
||||
$this->assertEqual($expect, $this->gen->generateFromTokens($tokens));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Generator.php';
|
||||
|
||||
class HTMLPurifier_GeneratorTest extends UnitTestCase
|
||||
{
|
||||
|
||||
var $gen;
|
||||
|
||||
function HTMLPurifier_GeneratorTest() {
|
||||
$this->UnitTestCase();
|
||||
$this->gen = new HTMLPurifier_Generator();
|
||||
}
|
||||
|
||||
function test_generateFromToken() {
|
||||
|
||||
$inputs = array();
|
||||
$expect = array();
|
||||
|
||||
$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 />';
|
||||
|
||||
foreach ($inputs as $i => $input) {
|
||||
$result = $this->gen->generateFromToken($input);
|
||||
$this->assertEqual($result, $expect[$i]);
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function test_generateAttributes() {
|
||||
|
||||
$inputs = array();
|
||||
$expect = array();
|
||||
|
||||
$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"';
|
||||
|
||||
foreach ($inputs as $i => $input) {
|
||||
$result = $this->gen->generateAttributes($input);
|
||||
$this->assertEqual($result, $expect[$i]);
|
||||
paintIf($result, $result != $expect[$i]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function test_generateFromTokens() {
|
||||
|
||||
$tokens = array(
|
||||
new HTMLPurifier_Token_Start('b'),
|
||||
new HTMLPurifier_Token_Text('Foobar!'),
|
||||
new HTMLPurifier_Token_End('b')
|
||||
);
|
||||
$expect = '<b>Foobar!</b>';
|
||||
$this->assertEqual($expect, $this->gen->generateFromTokens($tokens));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@@ -1,83 +1,83 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Lexer/DirectLex.php';
|
||||
|
||||
class HTMLPurifier_Lexer_DirectLexTest extends UnitTestCase
|
||||
{
|
||||
|
||||
var $DirectLex;
|
||||
|
||||
function setUp() {
|
||||
$this->DirectLex = new HTMLPurifier_Lexer_DirectLex();
|
||||
}
|
||||
|
||||
function test_nextWhiteSpace() {
|
||||
$HP =& $this->DirectLex;
|
||||
$this->assertIdentical(false, $HP->nextWhiteSpace('asdf'));
|
||||
$this->assertIdentical(0, $HP->nextWhiteSpace(' asdf'));
|
||||
$this->assertIdentical(0, $HP->nextWhiteSpace("\nasdf"));
|
||||
$this->assertIdentical(1, $HP->nextWhiteSpace("a\tsdf"));
|
||||
$this->assertIdentical(4, $HP->nextWhiteSpace("asdf\r"));
|
||||
$this->assertIdentical(2, $HP->nextWhiteSpace("as\t\r\nasdf as"));
|
||||
$this->assertIdentical(3, $HP->nextWhiteSpace('a a ', 2));
|
||||
}
|
||||
|
||||
function test_parseData() {
|
||||
$HP =& $this->DirectLex;
|
||||
$this->assertIdentical('asdf', $HP->parseData('asdf'));
|
||||
$this->assertIdentical('&', $HP->parseData('&'));
|
||||
$this->assertIdentical('"', $HP->parseData('"'));
|
||||
$this->assertIdentical("'", $HP->parseData('''));
|
||||
$this->assertIdentical('-', $HP->parseData('-'));
|
||||
// UTF-8 needed!!!
|
||||
}
|
||||
|
||||
// internals testing
|
||||
function test_tokenizeAttributeString() {
|
||||
|
||||
$input[0] = 'href="asdf" boom="assdf"';
|
||||
$expect[0] = array('href'=>'asdf', 'boom'=>'assdf');
|
||||
|
||||
$input[1] = "href='r'";
|
||||
$expect[1] = array('href'=>'r');
|
||||
|
||||
$input[2] = 'onclick="javascript:alert(\'asdf\');"';
|
||||
$expect[2] = array('onclick' => "javascript:alert('asdf');");
|
||||
|
||||
$input[3] = 'selected';
|
||||
$expect[3] = array('selected'=>'selected');
|
||||
|
||||
$input[4] = '="asdf"';
|
||||
$expect[4] = array();
|
||||
|
||||
$input[5] = 'missile=launch';
|
||||
$expect[5] = array('missile' => 'launch');
|
||||
|
||||
$input[6] = 'href="foo';
|
||||
$expect[6] = array('href' => 'foo');
|
||||
|
||||
$input[7] = '"=';
|
||||
$expect[7] = array('"' => '');
|
||||
|
||||
$input[8] = 'href ="about:blank"rel ="nofollow"';
|
||||
$expect[8] = array('href' => 'about:blank', 'rel' => 'nofollow');
|
||||
|
||||
$input[9] = 'foo bar';
|
||||
$expect[9] = array('foo' => 'foo', 'bar' => 'bar');
|
||||
|
||||
$input[10] = 'foo="bar" blue';
|
||||
$expect[10] = array('foo' => 'bar', 'blue' => 'blue');
|
||||
|
||||
$size = count($input);
|
||||
for($i = 0; $i < $size; $i++) {
|
||||
$result = $this->DirectLex->tokenizeAttributeString($input[$i]);
|
||||
$this->assertEqual($expect[$i], $result, 'Test ' . $i . ': %s');
|
||||
paintIf($result, $expect[$i] != $result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Lexer/DirectLex.php';
|
||||
|
||||
class HTMLPurifier_Lexer_DirectLexTest extends UnitTestCase
|
||||
{
|
||||
|
||||
var $DirectLex;
|
||||
|
||||
function setUp() {
|
||||
$this->DirectLex = new HTMLPurifier_Lexer_DirectLex();
|
||||
}
|
||||
|
||||
function test_nextWhiteSpace() {
|
||||
$HP =& $this->DirectLex;
|
||||
$this->assertIdentical(false, $HP->nextWhiteSpace('asdf'));
|
||||
$this->assertIdentical(0, $HP->nextWhiteSpace(' asdf'));
|
||||
$this->assertIdentical(0, $HP->nextWhiteSpace("\nasdf"));
|
||||
$this->assertIdentical(1, $HP->nextWhiteSpace("a\tsdf"));
|
||||
$this->assertIdentical(4, $HP->nextWhiteSpace("asdf\r"));
|
||||
$this->assertIdentical(2, $HP->nextWhiteSpace("as\t\r\nasdf as"));
|
||||
$this->assertIdentical(3, $HP->nextWhiteSpace('a a ', 2));
|
||||
}
|
||||
|
||||
function test_parseData() {
|
||||
$HP =& $this->DirectLex;
|
||||
$this->assertIdentical('asdf', $HP->parseData('asdf'));
|
||||
$this->assertIdentical('&', $HP->parseData('&'));
|
||||
$this->assertIdentical('"', $HP->parseData('"'));
|
||||
$this->assertIdentical("'", $HP->parseData('''));
|
||||
$this->assertIdentical('-', $HP->parseData('-'));
|
||||
// UTF-8 needed!!!
|
||||
}
|
||||
|
||||
// internals testing
|
||||
function test_tokenizeAttributeString() {
|
||||
|
||||
$input[0] = 'href="asdf" boom="assdf"';
|
||||
$expect[0] = array('href'=>'asdf', 'boom'=>'assdf');
|
||||
|
||||
$input[1] = "href='r'";
|
||||
$expect[1] = array('href'=>'r');
|
||||
|
||||
$input[2] = 'onclick="javascript:alert(\'asdf\');"';
|
||||
$expect[2] = array('onclick' => "javascript:alert('asdf');");
|
||||
|
||||
$input[3] = 'selected';
|
||||
$expect[3] = array('selected'=>'selected');
|
||||
|
||||
$input[4] = '="asdf"';
|
||||
$expect[4] = array();
|
||||
|
||||
$input[5] = 'missile=launch';
|
||||
$expect[5] = array('missile' => 'launch');
|
||||
|
||||
$input[6] = 'href="foo';
|
||||
$expect[6] = array('href' => 'foo');
|
||||
|
||||
$input[7] = '"=';
|
||||
$expect[7] = array('"' => '');
|
||||
|
||||
$input[8] = 'href ="about:blank"rel ="nofollow"';
|
||||
$expect[8] = array('href' => 'about:blank', 'rel' => 'nofollow');
|
||||
|
||||
$input[9] = 'foo bar';
|
||||
$expect[9] = array('foo' => 'foo', 'bar' => 'bar');
|
||||
|
||||
$input[10] = 'foo="bar" blue';
|
||||
$expect[10] = array('foo' => 'bar', 'blue' => 'blue');
|
||||
|
||||
$size = count($input);
|
||||
for($i = 0; $i < $size; $i++) {
|
||||
$result = $this->DirectLex->tokenizeAttributeString($input[$i]);
|
||||
$this->assertEqual($expect[$i], $result, 'Test ' . $i . ': %s');
|
||||
paintIf($result, $expect[$i] != $result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
@@ -1,197 +1,197 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Lexer/DirectLex.php';
|
||||
require_once 'HTMLPurifier/Lexer/PEARSax3.php';
|
||||
|
||||
class HTMLPurifier_LexerTest extends UnitTestCase
|
||||
{
|
||||
|
||||
var $DirectLex, $PEARSax3, $DOMLex;
|
||||
var $_has_dom;
|
||||
|
||||
function setUp() {
|
||||
$this->DirectLex = new HTMLPurifier_Lexer_DirectLex();
|
||||
$this->PEARSax3 = new HTMLPurifier_Lexer_PEARSax3();
|
||||
|
||||
$this->_has_dom = version_compare(PHP_VERSION, '5', '>=');
|
||||
|
||||
if ($this->_has_dom) {
|
||||
require_once 'HTMLPurifier/Lexer/DOMLex.php';
|
||||
$this->DOMLex = new HTMLPurifier_Lexer_DOMLex();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function test_tokenizeHTML() {
|
||||
|
||||
$input = array();
|
||||
$expect = array();
|
||||
$sax_expect = array();
|
||||
|
||||
$input[0] = '';
|
||||
$expect[0] = array();
|
||||
|
||||
$input[1] = 'This is regular text.';
|
||||
$expect[1] = array(
|
||||
new HTMLPurifier_Token_Text('This is regular text.')
|
||||
);
|
||||
|
||||
$input[2] = 'This is <b>bold</b> text';
|
||||
$expect[2] = array(
|
||||
new HTMLPurifier_Token_Text('This is ')
|
||||
,new HTMLPurifier_Token_Start('b', array())
|
||||
,new HTMLPurifier_Token_Text('bold')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
,new HTMLPurifier_Token_Text(' text')
|
||||
);
|
||||
|
||||
$input[3] = '<DIV>Totally rad dude. <b>asdf</b></div>';
|
||||
$expect[3] = array(
|
||||
new HTMLPurifier_Token_Start('DIV', array())
|
||||
,new HTMLPurifier_Token_Text('Totally rad dude. ')
|
||||
,new HTMLPurifier_Token_Start('b', array())
|
||||
,new HTMLPurifier_Token_Text('asdf')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
,new HTMLPurifier_Token_End('div')
|
||||
);
|
||||
|
||||
// [XML-INVALID]
|
||||
$input[4] = '<asdf></asdf><d></d><poOloka><poolasdf><ds></asdf></ASDF>';
|
||||
$expect[4] = array(
|
||||
new HTMLPurifier_Token_Start('asdf')
|
||||
,new HTMLPurifier_Token_End('asdf')
|
||||
,new HTMLPurifier_Token_Start('d')
|
||||
,new HTMLPurifier_Token_End('d')
|
||||
,new HTMLPurifier_Token_Start('poOloka')
|
||||
,new HTMLPurifier_Token_Start('poolasdf')
|
||||
,new HTMLPurifier_Token_Start('ds')
|
||||
,new HTMLPurifier_Token_End('asdf')
|
||||
,new HTMLPurifier_Token_End('ASDF')
|
||||
);
|
||||
// DOM is different because it condenses empty tags into REAL empty ones
|
||||
// as well as makes it well-formed
|
||||
$dom_expect[4] = array(
|
||||
new HTMLPurifier_Token_Empty('asdf')
|
||||
,new HTMLPurifier_Token_Empty('d')
|
||||
,new HTMLPurifier_Token_Start('pooloka')
|
||||
,new HTMLPurifier_Token_Start('poolasdf')
|
||||
,new HTMLPurifier_Token_Empty('ds')
|
||||
,new HTMLPurifier_Token_End('poolasdf')
|
||||
,new HTMLPurifier_Token_End('pooloka')
|
||||
);
|
||||
|
||||
$input[5] = '<a'."\t".'href="foobar.php"'."\n".'title="foo!">Link to <b id="asdf">foobar</b></a>';
|
||||
$expect[5] = array(
|
||||
new HTMLPurifier_Token_Start('a',array('href'=>'foobar.php','title'=>'foo!'))
|
||||
,new HTMLPurifier_Token_Text('Link to ')
|
||||
,new HTMLPurifier_Token_Start('b',array('id'=>'asdf'))
|
||||
,new HTMLPurifier_Token_Text('foobar')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
,new HTMLPurifier_Token_End('a')
|
||||
);
|
||||
|
||||
$input[6] = '<br />';
|
||||
$expect[6] = array(
|
||||
new HTMLPurifier_Token_Empty('br')
|
||||
);
|
||||
|
||||
// [SGML-INVALID] [RECOVERABLE]
|
||||
$input[7] = '<!-- Comment --> <!-- not so well formed --->';
|
||||
$expect[7] = array(
|
||||
new HTMLPurifier_Token_Comment(' Comment ')
|
||||
,new HTMLPurifier_Token_Text(' ')
|
||||
,new HTMLPurifier_Token_Comment(' not so well formed -')
|
||||
);
|
||||
$sax_expect[7] = false; // we need to figure out proper comment output
|
||||
|
||||
// [SGML-INVALID]
|
||||
$input[8] = '<a href=""';
|
||||
$expect[8] = array(
|
||||
new HTMLPurifier_Token_Text('<a href=""')
|
||||
);
|
||||
// SAX parses it into a tag
|
||||
$sax_expect[8] = array(
|
||||
new HTMLPurifier_Token_Start('a', array('href'=>''))
|
||||
);
|
||||
// DOM parses it into an empty tag
|
||||
$dom_expect[8] = array(
|
||||
new HTMLPurifier_Token_Empty('a', array('href'=>''))
|
||||
);
|
||||
|
||||
$input[9] = '<b>';
|
||||
$expect[9] = array(
|
||||
new HTMLPurifier_Token_Text('<b>')
|
||||
);
|
||||
$sax_expect[9] = array(
|
||||
new HTMLPurifier_Token_Text('<')
|
||||
,new HTMLPurifier_Token_Text('b')
|
||||
,new HTMLPurifier_Token_Text('>')
|
||||
);
|
||||
// note that SAX can clump text nodes together. We won't be
|
||||
// too picky though
|
||||
|
||||
// [SGML-INVALID]
|
||||
$input[10] = '<a "=>';
|
||||
// We barf on this, aim for no attributes
|
||||
$expect[10] = array(
|
||||
new HTMLPurifier_Token_Start('a', array('"' => ''))
|
||||
);
|
||||
// DOM correctly has no attributes, but also closes the tag
|
||||
$dom_expect[10] = array(
|
||||
new HTMLPurifier_Token_Empty('a')
|
||||
);
|
||||
// SAX barfs on this
|
||||
$sax_expect[10] = array(
|
||||
new HTMLPurifier_Token_Start('a', array('"' => ''))
|
||||
);
|
||||
|
||||
// [INVALID] [RECOVERABLE]
|
||||
$input[11] = '"';
|
||||
$expect[11] = array( new HTMLPurifier_Token_Text('"') );
|
||||
|
||||
// compare with this valid one:
|
||||
$input[12] = '"';
|
||||
$expect[12] = array( new HTMLPurifier_Token_Text('"') );
|
||||
$sax_expect[12] = false;
|
||||
// SAX chokes on this? We do have entity parsing on, so it should work!
|
||||
|
||||
foreach($input as $i => $discard) {
|
||||
$result = $this->DirectLex->tokenizeHTML($input[$i]);
|
||||
$this->assertEqual($expect[$i], $result, 'Test '.$i.': %s');
|
||||
paintIf($result, $expect[$i] != $result);
|
||||
|
||||
// assert unless I say otherwise
|
||||
$sax_result = $this->PEARSax3->tokenizeHTML($input[$i]);
|
||||
if (!isset($sax_expect[$i])) {
|
||||
// by default, assert with normal result
|
||||
$this->assertEqual($expect[$i], $sax_result, 'Test '.$i.': %s');
|
||||
paintIf($sax_result, $expect[$i] != $sax_result);
|
||||
} elseif ($sax_expect[$i] === false) {
|
||||
// assertions were turned off, optionally dump
|
||||
// paintIf($sax_expect, $i == NUMBER);
|
||||
} else {
|
||||
// match with a custom SAX result array
|
||||
$this->assertEqual($sax_expect[$i], $sax_result, 'Test '.$i.': %s');
|
||||
paintIf($sax_result, $sax_expect[$i] != $sax_result);
|
||||
}
|
||||
if ($this->_has_dom) {
|
||||
$dom_result = $this->DOMLex->tokenizeHTML($input[$i]);
|
||||
// same structure as SAX
|
||||
if (!isset($dom_expect[$i])) {
|
||||
$this->assertEqual($expect[$i], $dom_result, 'Test '.$i.': %s');
|
||||
paintIf($dom_result, $expect[$i] != $dom_result);
|
||||
} elseif ($dom_expect[$i] === false) {
|
||||
// paintIf($dom_result, $i == NUMBER);
|
||||
} else {
|
||||
$this->assertEqual($dom_expect[$i], $dom_result, 'Test '.$i.': %s');
|
||||
paintIf($dom_result, $dom_expect[$i] != $dom_result);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Lexer/DirectLex.php';
|
||||
require_once 'HTMLPurifier/Lexer/PEARSax3.php';
|
||||
|
||||
class HTMLPurifier_LexerTest extends UnitTestCase
|
||||
{
|
||||
|
||||
var $DirectLex, $PEARSax3, $DOMLex;
|
||||
var $_has_dom;
|
||||
|
||||
function setUp() {
|
||||
$this->DirectLex = new HTMLPurifier_Lexer_DirectLex();
|
||||
$this->PEARSax3 = new HTMLPurifier_Lexer_PEARSax3();
|
||||
|
||||
$this->_has_dom = version_compare(PHP_VERSION, '5', '>=');
|
||||
|
||||
if ($this->_has_dom) {
|
||||
require_once 'HTMLPurifier/Lexer/DOMLex.php';
|
||||
$this->DOMLex = new HTMLPurifier_Lexer_DOMLex();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function test_tokenizeHTML() {
|
||||
|
||||
$input = array();
|
||||
$expect = array();
|
||||
$sax_expect = array();
|
||||
|
||||
$input[0] = '';
|
||||
$expect[0] = array();
|
||||
|
||||
$input[1] = 'This is regular text.';
|
||||
$expect[1] = array(
|
||||
new HTMLPurifier_Token_Text('This is regular text.')
|
||||
);
|
||||
|
||||
$input[2] = 'This is <b>bold</b> text';
|
||||
$expect[2] = array(
|
||||
new HTMLPurifier_Token_Text('This is ')
|
||||
,new HTMLPurifier_Token_Start('b', array())
|
||||
,new HTMLPurifier_Token_Text('bold')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
,new HTMLPurifier_Token_Text(' text')
|
||||
);
|
||||
|
||||
$input[3] = '<DIV>Totally rad dude. <b>asdf</b></div>';
|
||||
$expect[3] = array(
|
||||
new HTMLPurifier_Token_Start('DIV', array())
|
||||
,new HTMLPurifier_Token_Text('Totally rad dude. ')
|
||||
,new HTMLPurifier_Token_Start('b', array())
|
||||
,new HTMLPurifier_Token_Text('asdf')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
,new HTMLPurifier_Token_End('div')
|
||||
);
|
||||
|
||||
// [XML-INVALID]
|
||||
$input[4] = '<asdf></asdf><d></d><poOloka><poolasdf><ds></asdf></ASDF>';
|
||||
$expect[4] = array(
|
||||
new HTMLPurifier_Token_Start('asdf')
|
||||
,new HTMLPurifier_Token_End('asdf')
|
||||
,new HTMLPurifier_Token_Start('d')
|
||||
,new HTMLPurifier_Token_End('d')
|
||||
,new HTMLPurifier_Token_Start('poOloka')
|
||||
,new HTMLPurifier_Token_Start('poolasdf')
|
||||
,new HTMLPurifier_Token_Start('ds')
|
||||
,new HTMLPurifier_Token_End('asdf')
|
||||
,new HTMLPurifier_Token_End('ASDF')
|
||||
);
|
||||
// DOM is different because it condenses empty tags into REAL empty ones
|
||||
// as well as makes it well-formed
|
||||
$dom_expect[4] = array(
|
||||
new HTMLPurifier_Token_Empty('asdf')
|
||||
,new HTMLPurifier_Token_Empty('d')
|
||||
,new HTMLPurifier_Token_Start('pooloka')
|
||||
,new HTMLPurifier_Token_Start('poolasdf')
|
||||
,new HTMLPurifier_Token_Empty('ds')
|
||||
,new HTMLPurifier_Token_End('poolasdf')
|
||||
,new HTMLPurifier_Token_End('pooloka')
|
||||
);
|
||||
|
||||
$input[5] = '<a'."\t".'href="foobar.php"'."\n".'title="foo!">Link to <b id="asdf">foobar</b></a>';
|
||||
$expect[5] = array(
|
||||
new HTMLPurifier_Token_Start('a',array('href'=>'foobar.php','title'=>'foo!'))
|
||||
,new HTMLPurifier_Token_Text('Link to ')
|
||||
,new HTMLPurifier_Token_Start('b',array('id'=>'asdf'))
|
||||
,new HTMLPurifier_Token_Text('foobar')
|
||||
,new HTMLPurifier_Token_End('b')
|
||||
,new HTMLPurifier_Token_End('a')
|
||||
);
|
||||
|
||||
$input[6] = '<br />';
|
||||
$expect[6] = array(
|
||||
new HTMLPurifier_Token_Empty('br')
|
||||
);
|
||||
|
||||
// [SGML-INVALID] [RECOVERABLE]
|
||||
$input[7] = '<!-- Comment --> <!-- not so well formed --->';
|
||||
$expect[7] = array(
|
||||
new HTMLPurifier_Token_Comment(' Comment ')
|
||||
,new HTMLPurifier_Token_Text(' ')
|
||||
,new HTMLPurifier_Token_Comment(' not so well formed -')
|
||||
);
|
||||
$sax_expect[7] = false; // we need to figure out proper comment output
|
||||
|
||||
// [SGML-INVALID]
|
||||
$input[8] = '<a href=""';
|
||||
$expect[8] = array(
|
||||
new HTMLPurifier_Token_Text('<a href=""')
|
||||
);
|
||||
// SAX parses it into a tag
|
||||
$sax_expect[8] = array(
|
||||
new HTMLPurifier_Token_Start('a', array('href'=>''))
|
||||
);
|
||||
// DOM parses it into an empty tag
|
||||
$dom_expect[8] = array(
|
||||
new HTMLPurifier_Token_Empty('a', array('href'=>''))
|
||||
);
|
||||
|
||||
$input[9] = '<b>';
|
||||
$expect[9] = array(
|
||||
new HTMLPurifier_Token_Text('<b>')
|
||||
);
|
||||
$sax_expect[9] = array(
|
||||
new HTMLPurifier_Token_Text('<')
|
||||
,new HTMLPurifier_Token_Text('b')
|
||||
,new HTMLPurifier_Token_Text('>')
|
||||
);
|
||||
// note that SAX can clump text nodes together. We won't be
|
||||
// too picky though
|
||||
|
||||
// [SGML-INVALID]
|
||||
$input[10] = '<a "=>';
|
||||
// We barf on this, aim for no attributes
|
||||
$expect[10] = array(
|
||||
new HTMLPurifier_Token_Start('a', array('"' => ''))
|
||||
);
|
||||
// DOM correctly has no attributes, but also closes the tag
|
||||
$dom_expect[10] = array(
|
||||
new HTMLPurifier_Token_Empty('a')
|
||||
);
|
||||
// SAX barfs on this
|
||||
$sax_expect[10] = array(
|
||||
new HTMLPurifier_Token_Start('a', array('"' => ''))
|
||||
);
|
||||
|
||||
// [INVALID] [RECOVERABLE]
|
||||
$input[11] = '"';
|
||||
$expect[11] = array( new HTMLPurifier_Token_Text('"') );
|
||||
|
||||
// compare with this valid one:
|
||||
$input[12] = '"';
|
||||
$expect[12] = array( new HTMLPurifier_Token_Text('"') );
|
||||
$sax_expect[12] = false;
|
||||
// SAX chokes on this? We do have entity parsing on, so it should work!
|
||||
|
||||
foreach($input as $i => $discard) {
|
||||
$result = $this->DirectLex->tokenizeHTML($input[$i]);
|
||||
$this->assertEqual($expect[$i], $result, 'Test '.$i.': %s');
|
||||
paintIf($result, $expect[$i] != $result);
|
||||
|
||||
// assert unless I say otherwise
|
||||
$sax_result = $this->PEARSax3->tokenizeHTML($input[$i]);
|
||||
if (!isset($sax_expect[$i])) {
|
||||
// by default, assert with normal result
|
||||
$this->assertEqual($expect[$i], $sax_result, 'Test '.$i.': %s');
|
||||
paintIf($sax_result, $expect[$i] != $sax_result);
|
||||
} elseif ($sax_expect[$i] === false) {
|
||||
// assertions were turned off, optionally dump
|
||||
// paintIf($sax_expect, $i == NUMBER);
|
||||
} else {
|
||||
// match with a custom SAX result array
|
||||
$this->assertEqual($sax_expect[$i], $sax_result, 'Test '.$i.': %s');
|
||||
paintIf($sax_result, $sax_expect[$i] != $sax_result);
|
||||
}
|
||||
if ($this->_has_dom) {
|
||||
$dom_result = $this->DOMLex->tokenizeHTML($input[$i]);
|
||||
// same structure as SAX
|
||||
if (!isset($dom_expect[$i])) {
|
||||
$this->assertEqual($expect[$i], $dom_result, 'Test '.$i.': %s');
|
||||
paintIf($dom_result, $expect[$i] != $dom_result);
|
||||
} elseif ($dom_expect[$i] === false) {
|
||||
// paintIf($dom_result, $i == NUMBER);
|
||||
} else {
|
||||
$this->assertEqual($dom_expect[$i], $dom_result, 'Test '.$i.': %s');
|
||||
paintIf($dom_result, $dom_expect[$i] != $dom_result);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user