2006-04-15 01:17:13 +00:00
|
|
|
<?php
|
|
|
|
|
2006-04-15 01:47:12 +00:00
|
|
|
class TestCase_HTML_Lexer extends UnitTestCase
|
2006-04-15 01:17:13 +00:00
|
|
|
{
|
|
|
|
|
2006-04-15 01:47:12 +00:00
|
|
|
var $HTML_Lexer;
|
|
|
|
var $HTML_Lexer_Sax;
|
2006-04-15 01:17:13 +00:00
|
|
|
|
|
|
|
function setUp() {
|
2006-04-15 01:47:12 +00:00
|
|
|
$this->HTML_Lexer =& new HTML_Lexer();
|
|
|
|
$this->HTML_Lexer_Sax =& new HTML_Lexer_Sax();
|
2006-04-15 01:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function test_nextWhiteSpace() {
|
2006-04-15 01:47:12 +00:00
|
|
|
$HP =& $this->HTML_Lexer;
|
2006-04-15 01:17:13 +00:00
|
|
|
$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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_tokenizeHTML() {
|
|
|
|
|
|
|
|
$input[] = '';
|
|
|
|
$expect[] = array();
|
|
|
|
|
|
|
|
$input[] = 'This is regular text.';
|
|
|
|
$expect[] = array(
|
2006-04-15 01:30:26 +00:00
|
|
|
new MF_Text('This is regular text.')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$input[] = 'This is <b>bold</b> text';
|
|
|
|
$expect[] = array(
|
2006-04-15 01:30:26 +00:00
|
|
|
new MF_Text('This is ')
|
|
|
|
,new MF_StartTag('b', array())
|
|
|
|
,new MF_Text('bold')
|
|
|
|
,new MF_EndTag('b')
|
|
|
|
,new MF_Text(' text')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$input[] = '<DIV>Totally rad dude. <b>asdf</b></div>';
|
|
|
|
$expect[] = array(
|
2006-04-15 01:30:26 +00:00
|
|
|
new MF_StartTag('DIV', array())
|
|
|
|
,new MF_Text('Totally rad dude. ')
|
|
|
|
,new MF_StartTag('b', array())
|
|
|
|
,new MF_Text('asdf')
|
|
|
|
,new MF_EndTag('b')
|
|
|
|
,new MF_EndTag('div')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$input[] = '<asdf></asdf><d></d><poOloka><poolasdf><ds></asdf></ASDF>';
|
|
|
|
$expect[] = array(
|
2006-04-15 01:30:26 +00:00
|
|
|
new MF_StartTag('asdf')
|
|
|
|
,new MF_EndTag('asdf')
|
|
|
|
,new MF_StartTag('d')
|
|
|
|
,new MF_EndTag('d')
|
|
|
|
,new MF_StartTag('poOloka')
|
|
|
|
,new MF_StartTag('poolasdf')
|
|
|
|
,new MF_StartTag('ds')
|
|
|
|
,new MF_EndTag('asdf')
|
|
|
|
,new MF_EndTag('ASDF')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$input[] = '<a'."\t".'href="foobar.php"'."\n".'title="foo!">Link to <b id="asdf">foobar</b></a>';
|
|
|
|
$expect[] = array(
|
2006-04-15 01:30:26 +00:00
|
|
|
new MF_StartTag('a',array('href'=>'foobar.php','title'=>'foo!'))
|
|
|
|
,new MF_Text('Link to ')
|
|
|
|
,new MF_StartTag('b',array('id'=>'asdf'))
|
|
|
|
,new MF_Text('foobar')
|
|
|
|
,new MF_EndTag('b')
|
|
|
|
,new MF_EndTag('a')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$input[] = '<br />';
|
|
|
|
$expect[] = array(
|
2006-04-15 01:30:26 +00:00
|
|
|
new MF_EmptyTag('br')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$input[] = '<!-- Comment --> <!-- not so well formed --->';
|
|
|
|
$expect[] = array(
|
2006-04-15 01:30:26 +00:00
|
|
|
new MF_Comment(' Comment ')
|
|
|
|
,new MF_Text(' ')
|
|
|
|
,new MF_Comment(' not so well formed -')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$input[] = '<a href=""';
|
|
|
|
$expect[] = array(
|
2006-04-15 01:30:26 +00:00
|
|
|
new MF_Text('<a href=""')
|
2006-04-15 01:17:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$size = count($input);
|
|
|
|
for($i = 0; $i < $size; $i++) {
|
2006-04-15 01:47:12 +00:00
|
|
|
$result = $this->HTML_Lexer->tokenizeHTML($input[$i]);
|
2006-04-15 01:17:13 +00:00
|
|
|
$this->assertEqual($expect[$i], $result);
|
|
|
|
paintIf($result, $expect[$i] != $result);
|
2006-04-15 01:47:12 +00:00
|
|
|
|
|
|
|
// since I didn't write the parser, I can't define its behavior
|
|
|
|
// however, make sure that the class runs without any errors
|
|
|
|
$exp_result = $this->HTML_Lexer_Sax->tokenizeHTML($input[$i]);
|
2006-04-15 01:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_tokenizeAttributeString() {
|
|
|
|
|
|
|
|
$input[] = 'href="asdf" boom="assdf"';
|
|
|
|
$expect[] = array('href'=>'asdf', 'boom'=>'assdf');
|
|
|
|
|
|
|
|
$input[] = "href='r'";
|
|
|
|
$expect[] = array('href'=>'r');
|
|
|
|
|
|
|
|
$input[] = 'onclick="javascript:alert(\'asdf\');"';
|
|
|
|
$expect[] = array('onclick' => "javascript:alert('asdf');");
|
|
|
|
|
|
|
|
$input[] = 'selected';
|
|
|
|
$expect[] = array('selected'=>'selected');
|
|
|
|
|
|
|
|
$input[] = '="asdf"';
|
|
|
|
$expect[] = array();
|
|
|
|
|
|
|
|
$size = count($input);
|
|
|
|
for($i = 0; $i < $size; $i++) {
|
2006-04-15 01:47:12 +00:00
|
|
|
$result = $this->HTML_Lexer->tokenizeAttributeString($input[$i]);
|
2006-04-15 01:17:13 +00:00
|
|
|
$this->assertEqual($expect[$i], $result);
|
|
|
|
paintIf($result, $expect[$i] != $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|