0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 08:21:52 +00:00

Kick naming changes into effect.

git-svn-id: http://htmlpurifier.org/svnroot/html_purifier/trunk@20 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-04-15 01:30:26 +00:00
parent ec8d41d768
commit bf331d3a13
4 changed files with 94 additions and 42 deletions

View File

@ -64,7 +64,7 @@ class MarkupLexer
if (!$inside_tag && $position_next_lt !== false) {
// We are not inside tag and there still is another tag to parse
$array[] = new HTML_Text(substr($string, $cursor, $position_next_lt - $cursor));
$array[] = new MF_Text(substr($string, $cursor, $position_next_lt - $cursor));
$cursor = $position_next_lt + 1;
$inside_tag = true;
continue;
@ -73,7 +73,7 @@ class MarkupLexer
// If we're already at the end, break
if ($cursor === strlen($string)) break;
// Create Text of rest of string
$array[] = new HTML_Text(substr($string, $cursor));
$array[] = new MF_Text(substr($string, $cursor));
break;
} elseif ($inside_tag && $position_next_gt !== false) {
// We are in tag and it is well formed
@ -82,7 +82,7 @@ class MarkupLexer
// Check if it's a comment
if (substr($segment,0,3) == '!--' && substr($segment,strlen($segment)-2,2) == '--') {
$array[] = new HTML_Comment(substr($segment,3,strlen($segment)-5));
$array[] = new MF_Comment(substr($segment,3,strlen($segment)-5));
$inside_tag = false;
$cursor = $position_next_gt + 1;
continue;
@ -92,7 +92,7 @@ class MarkupLexer
$is_end_tag = (strpos($segment,'/') === 0);
if ($is_end_tag) {
$type = substr($segment, 1);
$array[] = new HTML_EndTag($type);
$array[] = new MF_EndTag($type);
$inside_tag = false;
$cursor = $position_next_gt + 1;
continue;
@ -108,9 +108,9 @@ class MarkupLexer
$position_first_space = $this->nextWhiteSpace($segment);
if ($position_first_space === false) {
if ($is_self_closing) {
$array[] = new HTML_EmptyTag($segment);
$array[] = new MF_EmptyTag($segment);
} else {
$array[] = new HTML_StartTag($segment, array());
$array[] = new MF_StartTag($segment, array());
}
$inside_tag = false;
$cursor = $position_next_gt + 1;
@ -122,15 +122,15 @@ class MarkupLexer
$attribute_string = trim(substr($segment, $position_first_space));
$attributes = $this->tokenizeAttributeString($attribute_string);
if ($is_self_closing) {
$array[] = new HTML_EmptyTag($type, $attributes);
$array[] = new MF_EmptyTag($type, $attributes);
} else {
$array[] = new HTML_StartTag($type, $attributes);
$array[] = new MF_StartTag($type, $attributes);
}
$cursor = $position_next_gt + 1;
$inside_tag = false;
continue;
} else {
$array[] = new HTML_Text('<' . substr($string, $cursor));
$array[] = new MF_Text('<' . substr($string, $cursor));
break;
}
break;

48
MarkupFragment.php Normal file
View File

@ -0,0 +1,48 @@
<?php
// MF = Markup Fragment
// all objects here are immutable
class MF {}
class MF_Tag extends MF
{
var $name;
function MF_Tag($name) {
$this->name = strtolower($name);
}
}
class MF_StartTag extends MF_Tag
{
var $attributes = array();
function MF_StartTag($type, $attributes = array()) {
$this->MF_Tag($type);
$this->attributes = $attributes;
}
}
class MF_EmptyTag extends MF_StartTag {}
class MF_EndTag extends MF_Tag {}
class MF_Text extends MF
{
var $name = '#PCDATA';
var $data;
function MF_Text($data) {
$this->data = trim($data); // fairly certain trimming it's okay
}
function append($mf_text) {
return new MF_Text($this->data . $mf_text->data);
}
}
class MF_Comment extends MF
{
var $data;
function MF_Comment($data) {
$this->data = $data;
}
}
?>

View File

@ -5,11 +5,15 @@ load_simpletest(); // includes all relevant simpletest files
require_once 'XML/HTMLSax3.php'; // optional PEAR class
require_once 'HTML_Purifier.php';
require_once 'HTML_Lexer.php';
require_once 'MarkupFragment.php';
$test = new GroupTest('HTML_Purifier');
chdir('tests/');
$test->addTestFile('HTML_Purifier.php');
$test->addTestFile('HTML_Lexer.php');
//$test->addTestFile('MarkupFragment.php');
chdir('../');
$test->run(new HtmlReporter());

View File

@ -1,6 +1,6 @@
<?php
class SimpleTest_MarkupLexer extends UnitTestCase
class TestCase_MarkupLexer extends UnitTestCase
{
var $MarkupLexer;
@ -26,66 +26,66 @@ class SimpleTest_MarkupLexer extends UnitTestCase
$input[] = 'This is regular text.';
$expect[] = array(
new HTML_Text('This is regular text.')
new MF_Text('This is regular text.')
);
$input[] = 'This is <b>bold</b> text';
$expect[] = array(
new HTML_Text('This is ')
,new HTML_StartTag('b', array())
,new HTML_Text('bold')
,new HTML_EndTag('b')
,new HTML_Text(' text')
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 HTML_StartTag('DIV', array())
,new HTML_Text('Totally rad dude. ')
,new HTML_StartTag('b', array())
,new HTML_Text('asdf')
,new HTML_EndTag('b')
,new HTML_EndTag('div')
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 HTML_StartTag('asdf')
,new HTML_EndTag('asdf')
,new HTML_StartTag('d')
,new HTML_EndTag('d')
,new HTML_StartTag('poOloka')
,new HTML_StartTag('poolasdf')
,new HTML_StartTag('ds')
,new HTML_EndTag('asdf')
,new HTML_EndTag('ASDF')
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 HTML_StartTag('a',array('href'=>'foobar.php','title'=>'foo!'))
,new HTML_Text('Link to ')
,new HTML_StartTag('b',array('id'=>'asdf'))
,new HTML_Text('foobar')
,new HTML_EndTag('b')
,new HTML_EndTag('a')
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 HTML_EmptyTag('br')
new MF_EmptyTag('br')
);
$input[] = '<!-- Comment --> <!-- not so well formed --->';
$expect[] = array(
new HTML_Comment(' Comment ')
,new HTML_Text(' ')
,new HTML_Comment(' not so well formed -')
new MF_Comment(' Comment ')
,new MF_Text(' ')
,new MF_Comment(' not so well formed -')
);
$input[] = '<a href=""';
$expect[] = array(
new HTML_Text('<a href=""')
new MF_Text('<a href=""')
);
$size = count($input);