4 * Factory for token generation.
6 * @note Doing some benchmarking indicates that the new operator is much
7 * slower than the clone operator (even discounting the cost of the
8 * constructor). This class is for that optimization.
9 * Other then that, there's not much point as we don't
10 * maintain parallel HTMLPurifier_Token hierarchies (the main reason why
11 * you'd want to use an abstract factory).
12 * @todo Port DirectLex to use this
14 class HTMLPurifier_TokenFactory
16 // p stands for prototype
19 * @type HTMLPurifier_Token_Start
24 * @type HTMLPurifier_Token_End
29 * @type HTMLPurifier_Token_Empty
34 * @type HTMLPurifier_Token_Text
39 * @type HTMLPurifier_Token_Comment
44 * Generates blank prototypes for cloning.
46 public function __construct()
48 $this->p_start = new HTMLPurifier_Token_Start('', array());
49 $this->p_end = new HTMLPurifier_Token_End('');
50 $this->p_empty = new HTMLPurifier_Token_Empty('', array());
51 $this->p_text = new HTMLPurifier_Token_Text('');
52 $this->p_comment = new HTMLPurifier_Token_Comment('');
56 * Creates a HTMLPurifier_Token_Start.
57 * @param string $name Tag name
58 * @param array $attr Associative array of attributes
59 * @return HTMLPurifier_Token_Start Generated HTMLPurifier_Token_Start
61 public function createStart($name, $attr = array())
63 $p = clone $this->p_start;
64 $p->__construct($name, $attr);
69 * Creates a HTMLPurifier_Token_End.
70 * @param string $name Tag name
71 * @return HTMLPurifier_Token_End Generated HTMLPurifier_Token_End
73 public function createEnd($name)
75 $p = clone $this->p_end;
76 $p->__construct($name);
81 * Creates a HTMLPurifier_Token_Empty.
82 * @param string $name Tag name
83 * @param array $attr Associative array of attributes
84 * @return HTMLPurifier_Token_Empty Generated HTMLPurifier_Token_Empty
86 public function createEmpty($name, $attr = array())
88 $p = clone $this->p_empty;
89 $p->__construct($name, $attr);
94 * Creates a HTMLPurifier_Token_Text.
95 * @param string $data Data of text token
96 * @return HTMLPurifier_Token_Text Generated HTMLPurifier_Token_Text
98 public function createText($data)
100 $p = clone $this->p_text;
101 $p->__construct($data);
106 * Creates a HTMLPurifier_Token_Comment.
107 * @param string $data Data of comment token
108 * @return HTMLPurifier_Token_Comment Generated HTMLPurifier_Token_Comment
110 public function createComment($data)
112 $p = clone $this->p_comment;
113 $p->__construct($data);
118 // vim: et sw=4 sts=4