0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-23 00:41:52 +00:00
htmlpurifier/tests/HTML_Lexer.php

129 lines
4.0 KiB
PHP
Raw Normal View History

<?php
class TestCase_MarkupLexer extends UnitTestCase
{
var $MarkupLexer;
function setUp() {
$this->MarkupLexer =& new MarkupLexer();
}
function test_nextWhiteSpace() {
$HP =& $this->MarkupLexer;
$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(
new MF_Text('This is regular text.')
);
$input[] = 'This is <b>bold</b> text';
$expect[] = array(
new MF_Text('This is ')
,new MF_StartTag('b', array())
,new MF_Text('bold')
,new MF_EndTag('b')
,new MF_Text(' text')
);
$input[] = '<DIV>Totally rad dude. <b>asdf</b></div>';
$expect[] = array(
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')
);
$input[] = '<asdf></asdf><d></d><poOloka><poolasdf><ds></asdf></ASDF>';
$expect[] = array(
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')
);
$input[] = '<a'."\t".'href="foobar.php"'."\n".'title="foo!">Link to <b id="asdf">foobar</b></a>';
$expect[] = array(
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')
);
$input[] = '<br />';
$expect[] = array(
new MF_EmptyTag('br')
);
$input[] = '<!-- Comment --> <!-- not so well formed --->';
$expect[] = array(
new MF_Comment(' Comment ')
,new MF_Text(' ')
,new MF_Comment(' not so well formed -')
);
$input[] = '<a href=""';
$expect[] = array(
new MF_Text('<a href=""')
);
$size = count($input);
for($i = 0; $i < $size; $i++) {
$result = $this->MarkupLexer->tokenizeHTML($input[$i]);
$this->assertEqual($expect[$i], $result);
paintIf($result, $expect[$i] != $result);
}
}
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++) {
$result = $this->MarkupLexer->tokenizeAttributeString($input[$i]);
$this->assertEqual($expect[$i], $result);
paintIf($result, $expect[$i] != $result);
}
}
}
?>