2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/Lexer/DirectLex.php';
|
|
|
|
|
|
|
|
class HTMLPurifier_Lexer_DirectLexTest extends UnitTestCase
|
|
|
|
{
|
|
|
|
|
|
|
|
var $DirectLex;
|
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
$this->DirectLex = new HTMLPurifier_Lexer_DirectLex();
|
|
|
|
}
|
|
|
|
|
|
|
|
// internals testing
|
2006-07-23 18:13:04 +00:00
|
|
|
function test_parseAttributeString() {
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-07-23 18:13:04 +00:00
|
|
|
$input[0] = 'href="about:blank" rel="nofollow"';
|
|
|
|
$expect[0] = array('href'=>'about:blank', 'rel'=>'nofollow');
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-07-23 18:13:04 +00:00
|
|
|
$input[1] = "href='about:blank'";
|
|
|
|
$expect[1] = array('href'=>'about:blank');
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-07-23 18:13:04 +00:00
|
|
|
// note that the single quotes aren't /really/ escaped
|
2006-07-23 00:11:03 +00:00
|
|
|
$input[2] = 'onclick="javascript:alert(\'asdf\');"';
|
|
|
|
$expect[2] = array('onclick' => "javascript:alert('asdf');");
|
|
|
|
|
|
|
|
$input[3] = 'selected';
|
|
|
|
$expect[3] = array('selected'=>'selected');
|
|
|
|
|
2006-07-23 18:13:04 +00:00
|
|
|
// [INVALID]
|
|
|
|
$input[4] = '="nokey"';
|
2006-07-23 00:11:03 +00:00
|
|
|
$expect[4] = array();
|
|
|
|
|
2006-07-23 18:13:04 +00:00
|
|
|
// [SIMPLE]
|
|
|
|
$input[5] = 'color=blue';
|
|
|
|
$expect[5] = array('color' => 'blue');
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-07-23 18:13:04 +00:00
|
|
|
// [INVALID]
|
|
|
|
$input[6] = 'href="about:blank';
|
|
|
|
$expect[6] = array('href' => 'about:blank');
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-07-23 18:13:04 +00:00
|
|
|
// [INVALID]
|
2006-07-23 00:11:03 +00:00
|
|
|
$input[7] = '"=';
|
|
|
|
$expect[7] = array('"' => '');
|
2006-07-23 18:13:04 +00:00
|
|
|
// we ought to get array()
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
$input[8] = 'href ="about:blank"rel ="nofollow"';
|
|
|
|
$expect[8] = array('href' => 'about:blank', 'rel' => 'nofollow');
|
|
|
|
|
2006-07-23 18:13:04 +00:00
|
|
|
$input[9] = 'two bool';
|
|
|
|
$expect[9] = array('two' => 'two', 'bool' => 'bool');
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2006-07-23 18:13:04 +00:00
|
|
|
$input[10] = 'name="input" selected';
|
|
|
|
$expect[10] = array('name' => 'input', 'selected' => 'selected');
|
2006-07-23 00:11:03 +00:00
|
|
|
|
2007-06-25 00:48:26 +00:00
|
|
|
$input[11] = '=""';
|
|
|
|
$expect[11] = array();
|
|
|
|
|
|
|
|
$input[12] = '="" =""';
|
|
|
|
$expect[12] = array('"' => ''); // tough to say, just don't throw a loop
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
|
|
$context = new HTMLPurifier_Context();
|
2006-07-23 00:11:03 +00:00
|
|
|
$size = count($input);
|
|
|
|
for($i = 0; $i < $size; $i++) {
|
2006-10-01 20:47:07 +00:00
|
|
|
$result = $this->DirectLex->parseAttributeString($input[$i], $config, $context);
|
2007-05-05 20:17:04 +00:00
|
|
|
$this->assertIdentical($expect[$i], $result, 'Test ' . $i . ': %s');
|
2006-07-23 00:11:03 +00:00
|
|
|
paintIf($result, $expect[$i] != $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-06-18 02:01:01 +00:00
|
|
|
function testLineNumbers() {
|
|
|
|
|
|
|
|
$html = '<b>Line 1</b>
|
|
|
|
<i>Line 2</i>
|
|
|
|
Still Line 2<br
|
|
|
|
/>Now Line 4
|
|
|
|
|
|
|
|
<br />';
|
|
|
|
|
|
|
|
$expect = array(
|
|
|
|
// line 1
|
|
|
|
0 => new HTMLPurifier_Token_Start('b')
|
|
|
|
,1 => new HTMLPurifier_Token_Text('Line 1')
|
|
|
|
,2 => new HTMLPurifier_Token_End('b')
|
|
|
|
,3 => new HTMLPurifier_Token_Text('
|
|
|
|
')
|
|
|
|
// line 2
|
|
|
|
,4 => new HTMLPurifier_Token_Start('i')
|
|
|
|
,5 => new HTMLPurifier_Token_Text('Line 2')
|
|
|
|
,6 => new HTMLPurifier_Token_End('i')
|
|
|
|
,7 => new HTMLPurifier_Token_Text('
|
|
|
|
Still Line 2')
|
|
|
|
// line 3
|
|
|
|
,8 => new HTMLPurifier_Token_Empty('br')
|
|
|
|
// line 4
|
|
|
|
,9 => new HTMLPurifier_Token_Text('Now Line 4
|
|
|
|
|
|
|
|
')
|
|
|
|
// line SIX
|
|
|
|
,10 => new HTMLPurifier_Token_Empty('br')
|
|
|
|
);
|
|
|
|
|
|
|
|
$context = new HTMLPurifier_Context();
|
|
|
|
$config = HTMLPurifier_Config::createDefault();
|
|
|
|
$output = $this->DirectLex->tokenizeHTML($html, $config, $context);
|
|
|
|
|
|
|
|
$this->assertIdentical($output, $expect);
|
|
|
|
|
|
|
|
$context = new HTMLPurifier_Context();
|
|
|
|
$config = HTMLPurifier_Config::create(array(
|
|
|
|
'Core.MaintainLineNumbers' => true
|
|
|
|
));
|
|
|
|
$expect[0]->line = 1;
|
|
|
|
$expect[1]->line = 1;
|
|
|
|
$expect[2]->line = 1;
|
|
|
|
$expect[3]->line = 1;
|
|
|
|
$expect[4]->line = 2;
|
|
|
|
$expect[5]->line = 2;
|
|
|
|
$expect[6]->line = 2;
|
|
|
|
$expect[7]->line = 2;
|
|
|
|
$expect[8]->line = 3;
|
|
|
|
$expect[9]->line = 4;
|
|
|
|
$expect[10]->line = 6;
|
|
|
|
|
|
|
|
$output = $this->DirectLex->tokenizeHTML($html, $config, $context);
|
|
|
|
$this->assertIdentical($output, $expect);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2006-07-23 00:01:11 +00:00
|
|
|
?>
|