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

Optimize files by removing is_a() calls in favor of static type member variables.

git-svn-id: http://htmlpurifier.org/svnroot/html_purifier/trunk@56 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-07-18 23:40:20 +00:00
parent 5391195170
commit 2a2d8cbd76
2 changed files with 44 additions and 18 deletions

View File

@ -5,7 +5,7 @@
class MF {} class MF {}
class MF_Tag extends MF class MF_Tag extends MF // abstract
{ {
var $name; var $name;
function MF_Tag($name) { function MF_Tag($name) {
@ -14,7 +14,7 @@ class MF_Tag extends MF
} }
} }
class MF_TagWithAttributes extends MF_Tag class MF_TagWithAttributes extends MF_Tag // abstract
{ {
var $attributes = array(); var $attributes = array();
function MF_TagWithAttributes($name, $attributes = array()) { function MF_TagWithAttributes($name, $attributes = array()) {
@ -23,13 +23,25 @@ class MF_TagWithAttributes extends MF_Tag
} }
} }
class MF_StartTag extends MF_TagWithAttributes {} class MF_StartTag extends MF_TagWithAttributes
class MF_EmptyTag extends MF_TagWithAttributes {} {
class MF_EndTag extends MF_Tag {} var $type = 'start';
}
class MF_EmptyTag extends MF_TagWithAttributes
{
var $type = 'empty';
}
class MF_EndTag extends MF_Tag
{
var $type = 'end';
}
class MF_Text extends MF class MF_Text extends MF
{ {
var $name = '#PCDATA'; var $name = '#PCDATA';
var $type = 'text';
var $data; var $data;
var $is_whitespace = false; var $is_whitespace = false;
function MF_Text($data) { function MF_Text($data) {
@ -44,6 +56,7 @@ class MF_Text extends MF
class MF_Comment extends MF class MF_Comment extends MF
{ {
var $data; var $data;
var $type = 'comment';
function MF_Comment($data) { function MF_Comment($data) {
$this->data = $data; $this->data = $data;
} }

View File

@ -165,10 +165,10 @@ class PureHTMLDefinition
$this->generator->generateFromToken($token) $this->generator->generateFromToken($token)
); );
} }
} elseif (is_a($token, 'MF_Comment')) { } elseif ($token->type == 'comment') {
// strip comments // strip comments
continue; continue;
} elseif (is_a($token, 'MF_Text')) { } elseif ($token->type == 'text') {
} else { } else {
continue; continue;
} }
@ -189,16 +189,16 @@ class PureHTMLDefinition
$info = $this->info[$token->name]; // assumption but valid $info = $this->info[$token->name]; // assumption but valid
// test if it claims to be a start tag but is empty // test if it claims to be a start tag but is empty
if (is_a($info->child_def, 'HTMLDTD_ChildDef_Empty') && if ($info->child_def->type == 'empty' &&
is_a($token, 'MF_StartTag') ) { $token->type == 'start' ) {
$result[] = new MF_EmptyTag($token->name, $token->attributes); $result[] = new MF_EmptyTag($token->name, $token->attributes);
continue; continue;
} }
// test if it claims to be empty but really is a start tag // test if it claims to be empty but really is a start tag
if (!is_a($info->child_def, 'HTMLDTD_ChildDef_Empty') && if ($info->child_def->type != 'empty' &&
is_a($token, 'MF_EmptyTag') ) { $token->type == 'empty' ) {
$result[] = new MF_StartTag($token->name, $token->attributes); $result[] = new MF_StartTag($token->name, $token->attributes);
$result[] = new MF_EndTag($token->name); $result[] = new MF_EndTag($token->name);
@ -207,14 +207,14 @@ class PureHTMLDefinition
} }
// automatically insert empty tags // automatically insert empty tags
if (is_a($token, 'MF_EmptyTag')) { if ($token->type == 'empty') {
$result[] = $token; $result[] = $token;
continue; continue;
} }
// we give start tags precedence, so automatically accept unless... // we give start tags precedence, so automatically accept unless...
// it's one of those special cases // it's one of those special cases
if (is_a($token, 'MF_StartTag')) { if ($token->type == 'start') {
// if there's a parent, check for special case // if there's a parent, check for special case
if (!empty($current_nesting)) { if (!empty($current_nesting)) {
@ -253,7 +253,7 @@ class PureHTMLDefinition
} }
// sanity check // sanity check
if (!is_a($token, 'MF_EndTag')) continue; if ($token->type != 'end') continue;
// okay, we're dealing with a closing tag // okay, we're dealing with a closing tag
@ -326,6 +326,16 @@ class PureHTMLDefinition
function fixNesting($tokens) { function fixNesting($tokens) {
if (empty($this->info)) $this->loadData(); if (empty($this->info)) $this->loadData();
/*$to_next_node = 0; // defines how much further to scroll to get
// to next node.
for ($i = 0, $size = count($tokens) ; $i < $size; $i += $to_next_node) {
// scroll to the end of this node, and report number
for ($j = $i, $depth = 0; ; $j++) {
}
}*/
} }
function validateAttributes($tokens) { function validateAttributes($tokens) {
@ -384,9 +394,9 @@ class HTMLDTD_ChildDef
$is_child = ($nesting == 0); // direct $is_child = ($nesting == 0); // direct
if (is_a($token, 'MF_StartTag')) { if ($token->type == 'start') {
$nesting++; $nesting++;
} elseif (is_a($token, 'MF_EndTag')) { } elseif ($token->type == 'end') {
$nesting--; $nesting--;
} }
@ -424,6 +434,7 @@ class HTMLDTD_ChildDef_Simple extends HTMLDTD_ChildDef
} }
class HTMLDTD_ChildDef_Required extends HTMLDTD_ChildDef_Simple class HTMLDTD_ChildDef_Required extends HTMLDTD_ChildDef_Simple
{ {
var $type = 'required';
function validateChildren($tokens_of_children) { function validateChildren($tokens_of_children) {
// if there are no tokens, delete parent node // if there are no tokens, delete parent node
if (empty($tokens_of_children)) return false; if (empty($tokens_of_children)) return false;
@ -454,9 +465,9 @@ class HTMLDTD_ChildDef_Required extends HTMLDTD_ChildDef_Simple
$is_child = ($nesting == 0); $is_child = ($nesting == 0);
if (is_a($token, 'MF_StartTag')) { if ($token->type == 'start') {
$nesting++; $nesting++;
} elseif (is_a($token, 'MF_EndTag')) { } elseif ($token->type == 'end') {
$nesting--; $nesting--;
} }
@ -491,6 +502,7 @@ class HTMLDTD_ChildDef_Required extends HTMLDTD_ChildDef_Simple
// instead of a false (to delete the node) // instead of a false (to delete the node)
class HTMLDTD_ChildDef_Optional extends HTMLDTD_ChildDef_Required class HTMLDTD_ChildDef_Optional extends HTMLDTD_ChildDef_Required
{ {
var $type = 'optional';
function validateChildren($tokens_of_children) { function validateChildren($tokens_of_children) {
$result = parent::validateChildren($tokens_of_children); $result = parent::validateChildren($tokens_of_children);
if ($result === false) return array(); if ($result === false) return array();
@ -501,6 +513,7 @@ class HTMLDTD_ChildDef_Optional extends HTMLDTD_ChildDef_Required
// placeholder // placeholder
class HTMLDTD_ChildDef_Empty extends HTMLDTD_ChildDef class HTMLDTD_ChildDef_Empty extends HTMLDTD_ChildDef
{ {
var $type = 'empty';
function HTMLDTD_ChildDef_Empty() {} function HTMLDTD_ChildDef_Empty() {}
function validateChildren() { function validateChildren() {
return false; return false;