0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-01-03 05:11:52 +00:00

[3.1.0] Convert tokens to use instanceof, reducing memory footprint and improving comparison speed.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1509 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-01-19 20:23:01 +00:00
parent dd8ef4d3f5
commit 5eee08c548
16 changed files with 78 additions and 70 deletions

2
NEWS
View File

@ -13,6 +13,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
- Autoclose now operates iteratively, i.e. <span><span><div> now has - Autoclose now operates iteratively, i.e. <span><span><div> now has
both span tags closed. both span tags closed.
. Plugins now get their own changelogs according to project conventions. . Plugins now get their own changelogs according to project conventions.
. Convert tokens to use instanceof, reducing memory footprint and
improving comparison speed.
3.0.0, released 2008-01-06 3.0.0, released 2008-01-06
# HTML Purifier is PHP 5 only! The 2.1.x branch will be maintained # HTML Purifier is PHP 5 only! The 2.1.x branch will be maintained

View File

@ -34,7 +34,10 @@ class HTMLPurifier_AttrValidator
$current_token =& $context->get('CurrentToken', true); $current_token =& $context->get('CurrentToken', true);
if (!$current_token) $context->register('CurrentToken', $token); if (!$current_token) $context->register('CurrentToken', $token);
if ($token->type !== 'start' && $token->type !== 'empty') return $token; if (
!$token instanceof HTMLPurifier_Token_Start &&
!$token instanceof HTMLPurifier_Token_Empty
) return $token;
// create alias to global definition array, see also $defs // create alias to global definition array, see also $defs
// DEFINITION CALL // DEFINITION CALL

View File

@ -69,9 +69,9 @@ class HTMLPurifier_ChildDef_Custom extends HTMLPurifier_ChildDef
$is_child = ($nesting == 0); // direct $is_child = ($nesting == 0); // direct
if ($token->type == 'start') { if ($token instanceof HTMLPurifier_Token_Start) {
$nesting++; $nesting++;
} elseif ($token->type == 'end') { } elseif ($token instanceof HTMLPurifier_Token_End) {
$nesting--; $nesting--;
} }

View File

@ -71,9 +71,9 @@ class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
$is_child = ($nesting == 0); $is_child = ($nesting == 0);
if ($token->type == 'start') { if ($token instanceof HTMLPurifier_Token_Start) {
$nesting++; $nesting++;
} elseif ($token->type == 'end') { } elseif ($token instanceof HTMLPurifier_Token_End) {
$nesting--; $nesting--;
} }
@ -81,7 +81,7 @@ class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
$is_deleting = false; $is_deleting = false;
if (!isset($this->elements[$token->name])) { if (!isset($this->elements[$token->name])) {
$is_deleting = true; $is_deleting = true;
if ($pcdata_allowed && $token->type == 'text') { if ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text) {
$result[] = $token; $result[] = $token;
} elseif ($pcdata_allowed && $escape_invalid_children) { } elseif ($pcdata_allowed && $escape_invalid_children) {
$result[] = new HTMLPurifier_Token_Text( $result[] = new HTMLPurifier_Token_Text(
@ -91,7 +91,7 @@ class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
continue; continue;
} }
} }
if (!$is_deleting || ($pcdata_allowed && $token->type == 'text')) { if (!$is_deleting || ($pcdata_allowed && $token instanceof HTMLPurifier_Token_Text)) {
$result[] = $token; $result[] = $token;
} elseif ($pcdata_allowed && $escape_invalid_children) { } elseif ($pcdata_allowed && $escape_invalid_children) {
$result[] = $result[] =

View File

@ -45,8 +45,8 @@ extends HTMLPurifier_ChildDef_Required
if (!$is_inline) { if (!$is_inline) {
if (!$depth) { if (!$depth) {
if ( if (
($token->type == 'text' && !$token->is_whitespace) || ($token instanceof HTMLPurifier_Token_Text && !$token->is_whitespace) ||
($token->type != 'text' && !isset($this->elements[$token->name])) (!$token instanceof HTMLPurifier_Token_Text && !isset($this->elements[$token->name]))
) { ) {
$is_inline = true; $is_inline = true;
$ret[] = $block_wrap_start; $ret[] = $block_wrap_start;
@ -55,7 +55,7 @@ extends HTMLPurifier_ChildDef_Required
} else { } else {
if (!$depth) { if (!$depth) {
// starting tokens have been inline text / empty // starting tokens have been inline text / empty
if ($token->type == 'start' || $token->type == 'empty') { if ($token instanceof HTMLPurifier_Token_Start || $token instanceof HTMLPurifier_Token_Empty) {
if (isset($this->elements[$token->name])) { if (isset($this->elements[$token->name])) {
// ended // ended
$ret[] = $block_wrap_end; $ret[] = $block_wrap_end;
@ -65,8 +65,8 @@ extends HTMLPurifier_ChildDef_Required
} }
} }
$ret[] = $token; $ret[] = $token;
if ($token->type == 'start') $depth++; if ($token instanceof HTMLPurifier_Token_Start) $depth++;
if ($token->type == 'end') $depth--; if ($token instanceof HTMLPurifier_Token_End) $depth--;
} }
if ($is_inline) $ret[] = $block_wrap_end; if ($is_inline) $ret[] = $block_wrap_end;
return $ret; return $ret;

View File

@ -41,9 +41,9 @@ class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef
if ($token === false) { if ($token === false) {
// terminating sequence started // terminating sequence started
} elseif ($token->type == 'start') { } elseif ($token instanceof HTMLPurifier_Token_Start) {
$nesting++; $nesting++;
} elseif ($token->type == 'end') { } elseif ($token instanceof HTMLPurifier_Token_End) {
$nesting--; $nesting--;
} }
@ -112,7 +112,7 @@ class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef
$collection[] = $token; $collection[] = $token;
continue; continue;
default: default:
if ($token->type == 'text' && $token->is_whitespace) { if ($token instanceof HTMLPurifier_Token_Text && $token->is_whitespace) {
$collection[] = $token; $collection[] = $token;
$tag_index++; $tag_index++;
} }

View File

@ -89,7 +89,7 @@ class HTMLPurifier_Generator
if (!$tokens) return ''; if (!$tokens) return '';
for ($i = 0, $size = count($tokens); $i < $size; $i++) { for ($i = 0, $size = count($tokens); $i < $size; $i++) {
if ($this->_scriptFix && $tokens[$i]->name === 'script' if ($this->_scriptFix && $tokens[$i]->name === 'script'
&& $i + 2 < $size && $tokens[$i+2]->type == 'end') { && $i + 2 < $size && $tokens[$i+2] instanceof HTMLPurifier_Token_End) {
// script special case // script special case
// the contents of the script block must be ONE token // the contents of the script block must be ONE token
// for this to work // for this to work
@ -139,21 +139,21 @@ class HTMLPurifier_Generator
* @return Generated HTML * @return Generated HTML
*/ */
public function generateFromToken($token) { public function generateFromToken($token) {
if (!isset($token->type)) return ''; if (!$token instanceof HTMLPurifier_Token) return '';
if ($token->type == 'start') { if ($token instanceof HTMLPurifier_Token_Start) {
$attr = $this->generateAttributes($token->attr, $token->name); $attr = $this->generateAttributes($token->attr, $token->name);
return '<' . $token->name . ($attr ? ' ' : '') . $attr . '>'; return '<' . $token->name . ($attr ? ' ' : '') . $attr . '>';
} elseif ($token->type == 'end') { } elseif ($token instanceof HTMLPurifier_Token_End) {
return '</' . $token->name . '>'; return '</' . $token->name . '>';
} elseif ($token->type == 'empty') { } elseif ($token instanceof HTMLPurifier_Token_Empty) {
$attr = $this->generateAttributes($token->attr, $token->name); $attr = $this->generateAttributes($token->attr, $token->name);
return '<' . $token->name . ($attr ? ' ' : '') . $attr . return '<' . $token->name . ($attr ? ' ' : '') . $attr .
( $this->_xhtml ? ' /': '' ) ( $this->_xhtml ? ' /': '' )
. '>'; . '>';
} elseif ($token->type == 'text') { } elseif ($token instanceof HTMLPurifier_Token_Text) {
return $this->escape($token->data); return $this->escape($token->data);
} else { } else {
@ -168,7 +168,7 @@ class HTMLPurifier_Generator
* --> somewhere inside the script contents. * --> somewhere inside the script contents.
*/ */
public function generateScriptFromToken($token) { public function generateScriptFromToken($token) {
if ($token->type != 'text') return $this->generateFromToken($token); if (!$token instanceof HTMLPurifier_Token_Text) return $this->generateFromToken($token);
// return '<!--' . "\n" . trim($token->data) . "\n" . '// -->'; // return '<!--' . "\n" . trim($token->data) . "\n" . '// -->';
// more advanced version: // more advanced version:
// thanks <http://lachy.id.au/log/2005/05/script-comments> // thanks <http://lachy.id.au/log/2005/05/script-comments>

View File

@ -72,7 +72,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
// a double newline in them // a double newline in them
$nesting = 0; $nesting = 0;
for ($i = $this->inputIndex + 1; isset($this->inputTokens[$i]); $i++) { for ($i = $this->inputIndex + 1; isset($this->inputTokens[$i]); $i++) {
if ($this->inputTokens[$i]->type == 'start'){ if ($this->inputTokens[$i] instanceof HTMLPurifier_Token_Start){
if (!$this->_isInline($this->inputTokens[$i])) { if (!$this->_isInline($this->inputTokens[$i])) {
// we haven't found a double-newline, and // we haven't found a double-newline, and
// we've hit a block element, so don't paragraph // we've hit a block element, so don't paragraph
@ -81,11 +81,11 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
} }
$nesting++; $nesting++;
} }
if ($this->inputTokens[$i]->type == 'end') { if ($this->inputTokens[$i] instanceof HTMLPurifier_Token_End) {
if ($nesting <= 0) break; if ($nesting <= 0) break;
$nesting--; $nesting--;
} }
if ($this->inputTokens[$i]->type == 'text') { if ($this->inputTokens[$i] instanceof HTMLPurifier_Token_Text) {
// found it! // found it!
if (strpos($this->inputTokens[$i]->data, "\n\n") !== false) { if (strpos($this->inputTokens[$i]->data, "\n\n") !== false) {
$ok = true; $ok = true;
@ -117,12 +117,12 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
// check if this token is adjacent to the parent token // check if this token is adjacent to the parent token
$prev = $this->inputTokens[$this->inputIndex - 1]; $prev = $this->inputTokens[$this->inputIndex - 1];
if ($prev->type != 'start') { if (!$prev instanceof HTMLPurifier_Token_Start) {
// not adjacent, we can abort early // not adjacent, we can abort early
// add lead paragraph tag if our token is inline // add lead paragraph tag if our token is inline
// and the previous tag was an end paragraph // and the previous tag was an end paragraph
if ( if (
$prev->name == 'p' && $prev->type == 'end' && $prev->name == 'p' && $prev instanceof HTMLPurifier_Token_End &&
$this->_isInline($token) $this->_isInline($token)
) { ) {
$token = array($this->_pStart(), $token); $token = array($this->_pStart(), $token);
@ -139,9 +139,9 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
// early if possible // early if possible
$j = 1; // current nesting, one is due to parent (we recalculate current token) $j = 1; // current nesting, one is due to parent (we recalculate current token)
for ($i = $this->inputIndex; isset($this->inputTokens[$i]); $i++) { for ($i = $this->inputIndex; isset($this->inputTokens[$i]); $i++) {
if ($this->inputTokens[$i]->type == 'start') $j++; if ($this->inputTokens[$i] instanceof HTMLPurifier_Token_Start) $j++;
if ($this->inputTokens[$i]->type == 'end') $j--; if ($this->inputTokens[$i] instanceof HTMLPurifier_Token_End) $j--;
if ($this->inputTokens[$i]->type == 'text') { if ($this->inputTokens[$i] instanceof HTMLPurifier_Token_Text) {
if (strpos($this->inputTokens[$i]->data, "\n\n") !== false) { if (strpos($this->inputTokens[$i]->data, "\n\n") !== false) {
$ok = true; $ok = true;
break; break;
@ -248,14 +248,14 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
if (!$needs_end) { if (!$needs_end) {
// Start of the checks one after the current token's index // Start of the checks one after the current token's index
for ($i = $this->inputIndex + 1; isset($this->inputTokens[$i]); $i++) { for ($i = $this->inputIndex + 1; isset($this->inputTokens[$i]); $i++) {
if ($this->inputTokens[$i]->type == 'start' || $this->inputTokens[$i]->type == 'empty') { if ($this->inputTokens[$i] instanceof HTMLPurifier_Token_Start || $this->inputTokens[$i] instanceof HTMLPurifier_Token_Empty) {
$remove_paragraph_end = $this->_isInline($this->inputTokens[$i]); $remove_paragraph_end = $this->_isInline($this->inputTokens[$i]);
} }
// check if we can abort early (whitespace means we carry-on!) // check if we can abort early (whitespace means we carry-on!)
if ($this->inputTokens[$i]->type == 'text' && !$this->inputTokens[$i]->is_whitespace) break; if ($this->inputTokens[$i] instanceof HTMLPurifier_Token_Text && !$this->inputTokens[$i]->is_whitespace) break;
// end tags will automatically be handled by MakeWellFormed, // end tags will automatically be handled by MakeWellFormed,
// so we don't have to worry about them // so we don't have to worry about them
if ($this->inputTokens[$i]->type == 'end') break; if ($this->inputTokens[$i] instanceof HTMLPurifier_Token_End) break;
} }
} else { } else {
$remove_paragraph_end = false; $remove_paragraph_end = false;

View File

@ -74,7 +74,7 @@ class HTMLPurifier_Lexer_PEARSax3 extends HTMLPurifier_Lexer
// HTMLSax3 seems to always send empty tags an extra close tag // HTMLSax3 seems to always send empty tags an extra close tag
// check and ignore if you see it: // check and ignore if you see it:
// [TESTME] to make sure it doesn't overreach // [TESTME] to make sure it doesn't overreach
if ($this->tokens[count($this->tokens)-1]->type == 'empty') { if ($this->tokens[count($this->tokens)-1] instanceof HTMLPurifier_Token_Empty) {
return true; return true;
} }
$this->tokens[] = new HTMLPurifier_Token_End($name); $this->tokens[] = new HTMLPurifier_Token_End($name);

View File

@ -91,12 +91,12 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
// scroll to the end of this node, report number, and collect // scroll to the end of this node, report number, and collect
// all children // all children
for ($j = $i, $depth = 0; ; $j++) { for ($j = $i, $depth = 0; ; $j++) {
if ($tokens[$j]->type == 'start') { if ($tokens[$j] instanceof HTMLPurifier_Token_Start) {
$depth++; $depth++;
// skip token assignment on first iteration, this is the // skip token assignment on first iteration, this is the
// token we currently are on // token we currently are on
if ($depth == 1) continue; if ($depth == 1) continue;
} elseif ($tokens[$j]->type == 'end') { } elseif ($tokens[$j] instanceof HTMLPurifier_Token_End) {
$depth--; $depth--;
// skip token assignment on last iteration, this is the // skip token assignment on last iteration, this is the
// end token of the token we're currently on // end token of the token we're currently on
@ -287,8 +287,8 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
// Test if the token indeed is a start tag, if not, move forward // Test if the token indeed is a start tag, if not, move forward
// and test again. // and test again.
$size = count($tokens); $size = count($tokens);
while ($i < $size and $tokens[$i]->type != 'start') { while ($i < $size and !$tokens[$i] instanceof HTMLPurifier_Token_Start) {
if ($tokens[$i]->type == 'end') { if ($tokens[$i] instanceof HTMLPurifier_Token_End) {
// pop a token index off the stack if we ended a node // pop a token index off the stack if we ended a node
array_pop($stack); array_pop($stack);
// pop an exclusion lookup off exclusion stack if // pop an exclusion lookup off exclusion stack if

View File

@ -110,7 +110,7 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
// quick-check: if it's not a tag, no need to process // quick-check: if it's not a tag, no need to process
if (empty( $token->is_tag )) { if (empty( $token->is_tag )) {
if ($token->type === 'text') { if ($token instanceof HTMLPurifier_Token_Text) {
// injector handler code; duplicated for performance reasons // injector handler code; duplicated for performance reasons
foreach ($this->injectors as $i => $injector) { foreach ($this->injectors as $i => $injector) {
if (!$injector->skip) $injector->handleText($token); if (!$injector->skip) $injector->handleText($token);
@ -128,21 +128,21 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
// quick tag checks: anything that's *not* an end tag // quick tag checks: anything that's *not* an end tag
$ok = false; $ok = false;
if ($info->type == 'empty' && $token->type == 'start') { if ($info->type === 'empty' && $token instanceof HTMLPurifier_Token_Start) {
// test if it claims to be a start tag but is empty // test if it claims to be a start tag but is empty
$token = new HTMLPurifier_Token_Empty($token->name, $token->attr); $token = new HTMLPurifier_Token_Empty($token->name, $token->attr);
$ok = true; $ok = true;
} elseif ($info->type != 'empty' && $token->type == 'empty' ) { } elseif ($info->type !== 'empty' && $token instanceof HTMLPurifier_Token_Empty) {
// claims to be empty but really is a start tag // claims to be empty but really is a start tag
$token = array( $token = array(
new HTMLPurifier_Token_Start($token->name, $token->attr), new HTMLPurifier_Token_Start($token->name, $token->attr),
new HTMLPurifier_Token_End($token->name) new HTMLPurifier_Token_End($token->name)
); );
$ok = true; $ok = true;
} elseif ($token->type == 'empty') { } elseif ($token instanceof HTMLPurifier_Token_Empty) {
// real empty token // real empty token
$ok = true; $ok = true;
} elseif ($token->type == 'start') { } elseif ($token instanceof HTMLPurifier_Token_Start) {
// start tag // start tag
// ...unless they also have to close their parent // ...unless they also have to close their parent
@ -181,7 +181,7 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
} }
// sanity check: we should be dealing with a closing tag // sanity check: we should be dealing with a closing tag
if ($token->type != 'end') continue; if (!$token instanceof HTMLPurifier_Token_End) continue;
// make sure that we have something open // make sure that we have something open
if (empty($this->currentNesting)) { if (empty($this->currentNesting)) {
@ -304,9 +304,9 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
} elseif ($token) { } elseif ($token) {
// regular case // regular case
$this->outputTokens[] = $token; $this->outputTokens[] = $token;
if ($token->type == 'start') { if ($token instanceof HTMLPurifier_Token_Start) {
$this->currentNesting[] = $token; $this->currentNesting[] = $token;
} elseif ($token->type == 'end') { } elseif ($token instanceof HTMLPurifier_Token_End) {
array_pop($this->currentNesting); // not actually used array_pop($this->currentNesting); // not actually used
} }
} }

View File

@ -116,7 +116,7 @@ class HTMLPurifier_Strategy_RemoveForeignElements extends HTMLPurifier_Strategy
// mostly everything's good, but // mostly everything's good, but
// we need to make sure required attributes are in order // we need to make sure required attributes are in order
if ( if (
($token->type === 'start' || $token->type === 'empty') && ($token instanceof HTMLPurifier_Token_Start || $token instanceof HTMLPurifier_Token_Empty) &&
$definition->info[$token->name]->required_attr && $definition->info[$token->name]->required_attr &&
($token->name != 'img' || $remove_invalid_img) // ensure config option still works ($token->name != 'img' || $remove_invalid_img) // ensure config option still works
) { ) {
@ -135,9 +135,9 @@ class HTMLPurifier_Strategy_RemoveForeignElements extends HTMLPurifier_Strategy
$token->armor['ValidateAttributes'] = true; $token->armor['ValidateAttributes'] = true;
} }
if (isset($hidden_elements[$token->name]) && $token->type == 'start') { if (isset($hidden_elements[$token->name]) && $token instanceof HTMLPurifier_Token_Start) {
$textify_comments = $token->name; $textify_comments = $token->name;
} elseif ($token->name === $textify_comments && $token->type == 'end') { } elseif ($token->name === $textify_comments && $token instanceof HTMLPurifier_Token_End) {
$textify_comments = false; $textify_comments = false;
} }
@ -151,9 +151,9 @@ class HTMLPurifier_Strategy_RemoveForeignElements extends HTMLPurifier_Strategy
// check if we need to destroy all of the tag's children // check if we need to destroy all of the tag's children
// CAN BE GENERICIZED // CAN BE GENERICIZED
if (isset($hidden_elements[$token->name])) { if (isset($hidden_elements[$token->name])) {
if ($token->type == 'start') { if ($token instanceof HTMLPurifier_Token_Start) {
$remove_until = $token->name; $remove_until = $token->name;
} elseif ($token->type == 'empty') { } elseif ($token instanceof HTMLPurifier_Token_Empty) {
// do nothing: we're still looking // do nothing: we're still looking
} else { } else {
$remove_until = false; $remove_until = false;
@ -164,7 +164,7 @@ class HTMLPurifier_Strategy_RemoveForeignElements extends HTMLPurifier_Strategy
} }
continue; continue;
} }
} elseif ($token->type == 'comment') { } elseif ($token instanceof HTMLPurifier_Token_Comment) {
// textify comments in script tags when they are allowed // textify comments in script tags when they are allowed
if ($textify_comments !== false) { if ($textify_comments !== false) {
$data = $token->data; $data = $token->data;
@ -174,7 +174,7 @@ class HTMLPurifier_Strategy_RemoveForeignElements extends HTMLPurifier_Strategy
if ($e) $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed'); if ($e) $e->send(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed');
continue; continue;
} }
} elseif ($token->type == 'text') { } elseif ($token instanceof HTMLPurifier_Token_Text) {
} else { } else {
continue; continue;
} }

View File

@ -25,7 +25,7 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy
// only process tokens that have attributes, // only process tokens that have attributes,
// namely start and empty tags // namely start and empty tags
if ($token->type !== 'start' && $token->type !== 'empty') continue; if (!$token instanceof HTMLPurifier_Token_Start && !$token instanceof HTMLPurifier_Token_Empty) continue;
// skip tokens that are armored // skip tokens that are armored
if (!empty($token->armor['ValidateAttributes'])) continue; if (!empty($token->armor['ValidateAttributes'])) continue;

View File

@ -38,7 +38,7 @@ class HTMLPurifier_TagTransform_Font extends HTMLPurifier_TagTransform
public function transform($tag, $config, $context) { public function transform($tag, $config, $context) {
if ($tag->type == 'end') { if ($tag instanceof HTMLPurifier_Token_End) {
$new_tag = $tag->copy(); $new_tag = $tag->copy();
$new_tag->name = $this->transform_to; $new_tag->name = $this->transform_to;
return $new_tag; return $new_tag;

View File

@ -25,7 +25,7 @@ class HTMLPurifier_TagTransform_Simple extends HTMLPurifier_TagTransform
$new_tag = $tag->copy(); $new_tag = $tag->copy();
$new_tag->name = $this->transform_to; $new_tag->name = $this->transform_to;
if (!is_null($this->style) && if (!is_null($this->style) &&
($new_tag->type == 'start' || $new_tag->type == 'empty') ($new_tag instanceof HTMLPurifier_Token_Start || $new_tag instanceof HTMLPurifier_Token_Empty)
) { ) {
$this->prependCSS($new_tag->attr, $this->style); $this->prependCSS($new_tag->attr, $this->style);
} }

View File

@ -27,6 +27,20 @@ class HTMLPurifier_Token {
public function copy() { public function copy() {
return unserialize(serialize($this)); return unserialize(serialize($this));
} }
public function __get($n) {
if ($n === 'type') {
trigger_error('Deprecated type property called; use instanceof', E_USER_NOTICE);
switch (get_class($this)) {
case 'HTMLPurifier_Token_Start': return 'start';
case 'HTMLPurifier_Token_Empty': return 'empty';
case 'HTMLPurifier_Token_End': return 'end';
case 'HTMLPurifier_Token_Text': return 'text';
case 'HTMLPurifier_Token_Comment': return 'comment';
default: return null;
}
}
}
} }
/** /**
@ -84,18 +98,12 @@ class HTMLPurifier_Token_Tag extends HTMLPurifier_Token // abstract
/** /**
* Concrete start token class. * Concrete start token class.
*/ */
class HTMLPurifier_Token_Start extends HTMLPurifier_Token_Tag class HTMLPurifier_Token_Start extends HTMLPurifier_Token_Tag {}
{
public $type = 'start';
}
/** /**
* Concrete empty token class. * Concrete empty token class.
*/ */
class HTMLPurifier_Token_Empty extends HTMLPurifier_Token_Tag class HTMLPurifier_Token_Empty extends HTMLPurifier_Token_Tag {}
{
public $type = 'empty';
}
/** /**
* Concrete end token class. * Concrete end token class.
@ -104,10 +112,7 @@ class HTMLPurifier_Token_Empty extends HTMLPurifier_Token_Tag
* is for optimization reasons, as under normal circumstances, the Lexers * is for optimization reasons, as under normal circumstances, the Lexers
* do not pass attributes. * do not pass attributes.
*/ */
class HTMLPurifier_Token_End extends HTMLPurifier_Token_Tag class HTMLPurifier_Token_End extends HTMLPurifier_Token_Tag {}
{
public $type = 'end';
}
/** /**
* Concrete text token class. * Concrete text token class.
@ -122,7 +127,6 @@ class HTMLPurifier_Token_Text extends HTMLPurifier_Token
{ {
public $name = '#PCDATA'; /**< PCDATA tag name compatible with DTD. */ public $name = '#PCDATA'; /**< PCDATA tag name compatible with DTD. */
public $type = 'text';
public $data; /**< Parsed character data of text. */ public $data; /**< Parsed character data of text. */
public $is_whitespace; /**< Bool indicating if node is whitespace. */ public $is_whitespace; /**< Bool indicating if node is whitespace. */
@ -145,7 +149,6 @@ class HTMLPurifier_Token_Text extends HTMLPurifier_Token
class HTMLPurifier_Token_Comment extends HTMLPurifier_Token class HTMLPurifier_Token_Comment extends HTMLPurifier_Token
{ {
public $data; /**< Character data within comment. */ public $data; /**< Character data within comment. */
public $type = 'comment';
/** /**
* Transparent constructor. * Transparent constructor.
* *