2006-08-15 20:19:16 +00:00
|
|
|
<?php
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Factory for token generation (PHP 5 only).
|
|
|
|
*
|
|
|
|
* @note Doing some benchmarking indicates that the new operator is much
|
|
|
|
* slower than the clone operator (even discounting the cost of the
|
|
|
|
* constructor). This class is for that optimization. We may want to
|
|
|
|
* consider porting this to PHP 4 by virtue of the fact it makes the code
|
|
|
|
* easier to read. Other then that, there's not much point as we don't
|
|
|
|
* maintain parallel HTMLPurifier_Token hierarchies (the main reason why
|
|
|
|
* you'd want to use an abstract factory).
|
|
|
|
*/
|
2006-08-15 20:19:16 +00:00
|
|
|
class HTMLPurifier_TokenFactory
|
|
|
|
{
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Prototypes that will be cloned.
|
|
|
|
* @private
|
|
|
|
*/
|
2006-08-15 20:19:16 +00:00
|
|
|
// p stands for prototype
|
|
|
|
private $p_start, $p_end, $p_empty, $p_text, $p_comment;
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Generates blank prototypes for cloning.
|
|
|
|
*/
|
2006-08-15 20:19:16 +00:00
|
|
|
public function __construct() {
|
|
|
|
$this->p_start = new HTMLPurifier_Token_Start('', array());
|
|
|
|
$this->p_end = new HTMLPurifier_Token_End('');
|
|
|
|
$this->p_empty = new HTMLPurifier_Token_Empty('', array());
|
|
|
|
$this->p_text = new HTMLPurifier_Token_Text('');
|
|
|
|
$this->p_comment= new HTMLPurifier_Token_Comment('');
|
|
|
|
}
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Creates a HTMLPurifier_Token_Start.
|
|
|
|
* @param $name Tag name
|
2006-12-06 22:29:08 +00:00
|
|
|
* @param $attr Associative array of attributes
|
2006-08-20 20:59:13 +00:00
|
|
|
* @return Generated HTMLPurifier_Token_Start
|
|
|
|
*/
|
2006-12-06 22:29:08 +00:00
|
|
|
public function createStart($name, $attr = array()) {
|
2006-08-15 20:19:16 +00:00
|
|
|
$p = clone $this->p_start;
|
2007-11-29 04:29:51 +00:00
|
|
|
$p->__construct($name, $attr);
|
2006-08-15 20:19:16 +00:00
|
|
|
return $p;
|
|
|
|
}
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Creates a HTMLPurifier_Token_End.
|
|
|
|
* @param $name Tag name
|
|
|
|
* @return Generated HTMLPurifier_Token_End
|
|
|
|
*/
|
2006-08-15 20:19:16 +00:00
|
|
|
public function createEnd($name) {
|
|
|
|
$p = clone $this->p_end;
|
2007-11-29 04:29:51 +00:00
|
|
|
$p->__construct($name);
|
2006-08-15 20:19:16 +00:00
|
|
|
return $p;
|
|
|
|
}
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Creates a HTMLPurifier_Token_Empty.
|
|
|
|
* @param $name Tag name
|
2006-12-06 22:29:08 +00:00
|
|
|
* @param $attr Associative array of attributes
|
2006-08-20 20:59:13 +00:00
|
|
|
* @return Generated HTMLPurifier_Token_Empty
|
2006-08-20 21:39:22 +00:00
|
|
|
*/
|
2006-12-06 22:29:08 +00:00
|
|
|
public function createEmpty($name, $attr = array()) {
|
2006-08-15 20:19:16 +00:00
|
|
|
$p = clone $this->p_empty;
|
2007-11-29 04:29:51 +00:00
|
|
|
$p->__construct($name, $attr);
|
2006-08-15 20:19:16 +00:00
|
|
|
return $p;
|
|
|
|
}
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Creates a HTMLPurifier_Token_Text.
|
|
|
|
* @param $data Data of text token
|
|
|
|
* @return Generated HTMLPurifier_Token_Text
|
|
|
|
*/
|
2006-08-15 20:19:16 +00:00
|
|
|
public function createText($data) {
|
|
|
|
$p = clone $this->p_text;
|
2007-11-29 04:29:51 +00:00
|
|
|
$p->__construct($data);
|
2006-08-15 20:19:16 +00:00
|
|
|
return $p;
|
|
|
|
}
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Creates a HTMLPurifier_Token_Comment.
|
|
|
|
* @param $data Data of comment token
|
|
|
|
* @return Generated HTMLPurifier_Token_Comment
|
|
|
|
*/
|
2006-08-15 20:19:16 +00:00
|
|
|
public function createComment($data) {
|
|
|
|
$p = clone $this->p_comment;
|
2007-11-29 04:29:51 +00:00
|
|
|
$p->__construct($data);
|
2006-08-15 20:19:16 +00:00
|
|
|
return $p;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|