4 * Injector that auto paragraphs text in the root node based on
6 * @todo Ensure all states are unit tested, including variations as well.
7 * @todo Make a graph of the flow control for this Injector.
9 class HTMLPurifier_Injector_AutoParagraph
extends HTMLPurifier_Injector
12 public $name = 'AutoParagraph';
13 public $needed = array('p');
15 private function _pStart() {
16 $par = new HTMLPurifier_Token_Start('p');
17 $par->armor
['MakeWellFormed_TagClosedError'] = true;
21 public function handleText(&$token) {
23 // Does the current parent allow <p> tags?
24 if ($this->allowsElement('p')) {
25 if (empty($this->currentNesting
) ||
strpos($text, "\n\n") !== false) {
26 // Note that we have differing behavior when dealing with text
27 // in the anonymous root node, or a node inside the document.
28 // If the text as a double-newline, the treatment is the same;
29 // if it doesn't, see the next if-block if you're in the document.
32 if (!$this->forwardUntilEndToken($i, $current, $nesting) && $token->is_whitespace
) {
33 // State 1.1: ... ^ (whitespace, then document end)
35 // This is a degenerate case
40 // State 1.3: PAR1\n\nPAR2
43 // State 1.4: <div>PAR1\n\nPAR2 (see State 2)
45 $token = array($this->_pStart());
46 $this->_splitText($text, $token);
49 // State 2: <div>PAR1... (similar to 1.4)
52 // We're in an element that allows paragraph tags, but we're not
53 // sure if we're going to need them.
54 if ($this->_pLookAhead()) {
55 // State 2.1: <div>PAR1<b>PAR1\n\nPAR2
57 // Note: This will always be the first child, since any
58 // previous inline element would have triggered this very
59 // same routine, and found the double newline. One possible
60 // exception would be a comment.
61 $token = array($this->_pStart(), $token);
63 // State 2.2.1: <div>PAR1<div>
66 // State 2.2.2: <div>PAR1<b>PAR1</b></div>
70 // Is the current parent a <p> tag?
72 !empty($this->currentNesting
) &&
73 $this->currentNesting
[count($this->currentNesting
)-1]->name
== 'p'
75 // State 3.1: ...<p>PAR1
78 // State 3.2: ...<p>PAR1\n\nPAR2
81 $this->_splitText($text, $token);
84 // State 4.1: ...<b>PAR1
87 // State 4.2: ...<b>PAR1\n\nPAR2
92 public function handleElement(&$token) {
93 // We don't have to check if we're already in a <p> tag for block
94 // tokens, because the tag would have been autoclosed by MakeWellFormed.
95 if ($this->allowsElement('p')) {
96 if (!empty($this->currentNesting
)) {
97 if ($this->_isInline($token)) {
98 // State 1: <div>...<b>
101 // Check if this token is adjacent to the parent token
102 // (seek backwards until token isn't whitespace)
104 $this->backward($i, $prev);
106 if (!$prev instanceof HTMLPurifier_Token_Start
) {
107 // Token wasn't adjacent
110 $prev instanceof HTMLPurifier_Token_Text
&&
111 substr($prev->data
, -2) === "\n\n"
113 // State 1.1.4: <div><p>PAR1</p>\n\n<b>
116 // Quite frankly, this should be handled by splitText
117 $token = array($this->_pStart(), $token);
119 // State 1.1.1: <div><p>PAR1</p><b>
122 // State 1.1.2: <div><br /><b>
125 // State 1.1.3: <div>PAR<b>
130 // State 1.2.1: <div><b>
133 // Lookahead to see if <p> is needed.
134 if ($this->_pLookAhead()) {
135 // State 1.3.1: <div><b>PAR1\n\nPAR2
137 $token = array($this->_pStart(), $token);
139 // State 1.3.2: <div><b>PAR1</b></div>
142 // State 1.3.3: <div><b>PAR1</b><div></div>\n\n</div>
147 // State 2.3: ...<div>
151 if ($this->_isInline($token)) {
154 // This is where the {p} tag is inserted, not reflected in
155 // inputTokens yet, however.
156 $token = array($this->_pStart(), $token);
163 if ($this->backward($i, $prev)) {
165 !$prev instanceof HTMLPurifier_Token_Text
167 // State 3.1.1: ...</p>{p}<b>
170 // State 3.2.1: ...</p><div>
173 if (!is_array($token)) $token = array($token);
174 array_unshift($token, new HTMLPurifier_Token_Text("\n\n"));
176 // State 3.1.2: ...</p>\n\n{p}<b>
179 // State 3.2.2: ...</p>\n\n<div>
182 // Note: PAR<ELEM> cannot occur because PAR would have been
183 // wrapped in <p> tags.
188 // State 2.2: <ul><li>
197 * Splits up a text in paragraph tokens and appends them
198 * to the result stream that will replace the original
199 * @param $data String text data that will be processed
201 * @param $result Reference to array of tokens that the
202 * tags will be appended onto
203 * @param $config Instance of HTMLPurifier_Config
204 * @param $context Instance of HTMLPurifier_Context
206 private function _splitText($data, &$result) {
207 $raw_paragraphs = explode("\n\n", $data);
208 $paragraphs = array(); // without empty paragraphs
209 $needs_start = false;
212 $c = count($raw_paragraphs);
214 // There were no double-newlines, abort quickly. In theory this
215 // should never happen.
216 $result[] = new HTMLPurifier_Token_Text($data);
219 for ($i = 0; $i < $c; $i++
) {
220 $par = $raw_paragraphs[$i];
221 if (trim($par) !== '') {
222 $paragraphs[] = $par;
225 // Double newline at the front
226 if (empty($result)) {
227 // The empty result indicates that the AutoParagraph
228 // injector did not add any start paragraph tokens.
229 // This means that we have been in a paragraph for
230 // a while, and the newline means we should start a new one.
231 $result[] = new HTMLPurifier_Token_End('p');
232 $result[] = new HTMLPurifier_Token_Text("\n\n");
233 // However, the start token should only be added if
234 // there is more processing to be done (i.e. there are
235 // real paragraphs in here). If there are none, the
236 // next start paragraph tag will be handled by the
237 // next call to the injector
240 // We just started a new paragraph!
241 // Reinstate a double-newline for presentation's sake, since
242 // it was in the source code.
243 array_unshift($result, new HTMLPurifier_Token_Text("\n\n"));
245 } elseif ($i +
1 == $c) {
246 // Double newline at the end
247 // There should be a trailing </p> when we're finally done.
253 // Check if this was just a giant blob of whitespace. Move this earlier,
255 if (empty($paragraphs)) {
259 // Add the start tag indicated by \n\n at the beginning of $data
261 $result[] = $this->_pStart();
264 // Append the paragraphs onto the result
265 foreach ($paragraphs as $par) {
266 $result[] = new HTMLPurifier_Token_Text($par);
267 $result[] = new HTMLPurifier_Token_End('p');
268 $result[] = new HTMLPurifier_Token_Text("\n\n");
269 $result[] = $this->_pStart();
272 // Remove trailing start token; Injector will handle this later if
273 // it was indeed needed. This prevents from needing to do a lookahead,
274 // at the cost of a lookbehind later.
277 // If there is no need for an end tag, remove all of it and let
278 // MakeWellFormed close it later.
280 array_pop($result); // removes \n\n
281 array_pop($result); // removes </p>
287 * Returns true if passed token is inline (and, ergo, allowed in
290 private function _isInline($token) {
291 return isset($this->htmlDefinition
->info
['p']->child
->elements
[$token->name
]);
295 * Looks ahead in the token list and determines whether or not we need
296 * to insert a <p> tag.
298 private function _pLookAhead() {
299 $this->current($i, $current);
300 if ($current instanceof HTMLPurifier_Token_Start
) $nesting = 1;
303 while ($this->forwardUntilEndToken($i, $current, $nesting)) {
304 $result = $this->_checkNeedsP($current);
305 if ($result !== null) {
314 * Determines if a particular token requires an earlier inline token
315 * to get a paragraph. This should be used with _forwardUntilEndToken
317 private function _checkNeedsP($current) {
318 if ($current instanceof HTMLPurifier_Token_Start
){
319 if (!$this->_isInline($current)) {
322 // Terminate early, since we hit a block element
325 } elseif ($current instanceof HTMLPurifier_Token_Text
) {
326 if (strpos($current->data
, "\n\n") !== false) {
327 // <div>PAR1<b>PAR1\n\nPAR2
331 // <div>PAR1<b>PAR1...
340 // vim: et sw=4 sts=4