From bf331d3a1353648078e819a3073444b19182f221 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sat, 15 Apr 2006 01:30:26 +0000 Subject: [PATCH] Kick naming changes into effect. git-svn-id: http://htmlpurifier.org/svnroot/html_purifier/trunk@20 48356398-32a2-884e-a903-53898d9a118a --- MarkupLexer.php => HTML_Lexer.php | 18 +++---- MarkupFragment.php | 48 +++++++++++++++++ tester.php | 4 ++ tests/{MarkupLexer.php => HTML_Lexer.php} | 66 +++++++++++------------ 4 files changed, 94 insertions(+), 42 deletions(-) rename MarkupLexer.php => HTML_Lexer.php (88%) create mode 100644 MarkupFragment.php rename tests/{MarkupLexer.php => HTML_Lexer.php} (64%) diff --git a/MarkupLexer.php b/HTML_Lexer.php similarity index 88% rename from MarkupLexer.php rename to HTML_Lexer.php index c093bd8c..d69629d4 100644 --- a/MarkupLexer.php +++ b/HTML_Lexer.php @@ -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; diff --git a/MarkupFragment.php b/MarkupFragment.php new file mode 100644 index 00000000..5dd403a3 --- /dev/null +++ b/MarkupFragment.php @@ -0,0 +1,48 @@ +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; + } +} + +?> \ No newline at end of file diff --git a/tester.php b/tester.php index 3b676a28..2b219341 100644 --- a/tester.php +++ b/tester.php @@ -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()); diff --git a/tests/MarkupLexer.php b/tests/HTML_Lexer.php similarity index 64% rename from tests/MarkupLexer.php rename to tests/HTML_Lexer.php index d2dd8a83..a4d9c35e 100644 --- a/tests/MarkupLexer.php +++ b/tests/HTML_Lexer.php @@ -1,6 +1,6 @@ bold 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[] = '
Totally rad dude. asdf
'; $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[] = ''; $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[] = 'Link to foobar'; $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[] = '
'; $expect[] = array( - new HTML_EmptyTag('br') + new MF_EmptyTag('br') ); $input[] = ' '; $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[] = '