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
37 if (!$token->is_whitespace || $this->_isInline($current)) {
41 // State 1.3: PAR1\n\nPAR2
44 // State 1.4: <div>PAR1\n\nPAR2 (see State 2)
46 $token = array($this->_pStart());
47 $this->_splitText($text, $token);
49 // State 1.5: \n<hr />
54 // State 2: <div>PAR1... (similar to 1.4)
57 // We're in an element that allows paragraph tags, but we're not
58 // sure if we're going to need them.
59 if ($this->_pLookAhead()) {
60 // State 2.1: <div>PAR1<b>PAR1\n\nPAR2
62 // Note: This will always be the first child, since any
63 // previous inline element would have triggered this very
64 // same routine, and found the double newline. One possible
65 // exception would be a comment.
66 $token = array($this->_pStart(), $token);
68 // State 2.2.1: <div>PAR1<div>
71 // State 2.2.2: <div>PAR1<b>PAR1</b></div>
75 // Is the current parent a <p> tag?
77 !empty($this->currentNesting) &&
78 $this->currentNesting[count($this->currentNesting)-1]->name == 'p'
80 // State 3.1: ...<p>PAR1
83 // State 3.2: ...<p>PAR1\n\nPAR2
86 $this->_splitText($text, $token);
89 // State 4.1: ...<b>PAR1
92 // State 4.2: ...<b>PAR1\n\nPAR2
97 public function handleElement(&$token) {
98 // We don't have to check if we're already in a <p> tag for block
99 // tokens, because the tag would have been autoclosed by MakeWellFormed.
100 if ($this->allowsElement('p')) {
101 if (!empty($this->currentNesting)) {
102 if ($this->_isInline($token)) {
103 // State 1: <div>...<b>
106 // Check if this token is adjacent to the parent token
107 // (seek backwards until token isn't whitespace)
109 $this->backward($i, $prev);
111 if (!$prev instanceof HTMLPurifier_Token_Start) {
112 // Token wasn't adjacent
115 $prev instanceof HTMLPurifier_Token_Text &&
116 substr($prev->data, -2) === "\n\n"
118 // State 1.1.4: <div><p>PAR1</p>\n\n<b>
121 // Quite frankly, this should be handled by splitText
122 $token = array($this->_pStart(), $token);
124 // State 1.1.1: <div><p>PAR1</p><b>
127 // State 1.1.2: <div><br /><b>
130 // State 1.1.3: <div>PAR<b>
135 // State 1.2.1: <div><b>
138 // Lookahead to see if <p> is needed.
139 if ($this->_pLookAhead()) {
140 // State 1.3.1: <div><b>PAR1\n\nPAR2
142 $token = array($this->_pStart(), $token);
144 // State 1.3.2: <div><b>PAR1</b></div>
147 // State 1.3.3: <div><b>PAR1</b><div></div>\n\n</div>
152 // State 2.3: ...<div>
156 if ($this->_isInline($token)) {
159 // This is where the {p} tag is inserted, not reflected in
160 // inputTokens yet, however.
161 $token = array($this->_pStart(), $token);
168 if ($this->backward($i, $prev)) {
170 !$prev instanceof HTMLPurifier_Token_Text
172 // State 3.1.1: ...</p>{p}<b>
175 // State 3.2.1: ...</p><div>
178 if (!is_array($token)) $token = array($token);
179 array_unshift($token, new HTMLPurifier_Token_Text("\n\n"));
181 // State 3.1.2: ...</p>\n\n{p}<b>
184 // State 3.2.2: ...</p>\n\n<div>
187 // Note: PAR<ELEM> cannot occur because PAR would have been
188 // wrapped in <p> tags.
193 // State 2.2: <ul><li>
202 * Splits up a text in paragraph tokens and appends them
203 * to the result stream that will replace the original
204 * @param $data String text data that will be processed
206 * @param $result Reference to array of tokens that the
207 * tags will be appended onto
208 * @param $config Instance of HTMLPurifier_Config
209 * @param $context Instance of HTMLPurifier_Context
211 private function _splitText($data, &$result) {
212 $raw_paragraphs = explode("\n\n", $data);
213 $paragraphs = array(); // without empty paragraphs
214 $needs_start = false;
217 $c = count($raw_paragraphs);
219 // There were no double-newlines, abort quickly. In theory this
220 // should never happen.
221 $result[] = new HTMLPurifier_Token_Text($data);
224 for ($i = 0; $i < $c; $i++) {
225 $par = $raw_paragraphs[$i];
226 if (trim($par) !== '') {
227 $paragraphs[] = $par;
230 // Double newline at the front
231 if (empty($result)) {
232 // The empty result indicates that the AutoParagraph
233 // injector did not add any start paragraph tokens.
234 // This means that we have been in a paragraph for
235 // a while, and the newline means we should start a new one.
236 $result[] = new HTMLPurifier_Token_End('p');
237 $result[] = new HTMLPurifier_Token_Text("\n\n");
238 // However, the start token should only be added if
239 // there is more processing to be done (i.e. there are
240 // real paragraphs in here). If there are none, the
241 // next start paragraph tag will be handled by the
242 // next call to the injector
245 // We just started a new paragraph!
246 // Reinstate a double-newline for presentation's sake, since
247 // it was in the source code.
248 array_unshift($result, new HTMLPurifier_Token_Text("\n\n"));
250 } elseif ($i + 1 == $c) {
251 // Double newline at the end
252 // There should be a trailing </p> when we're finally done.
258 // Check if this was just a giant blob of whitespace. Move this earlier,
260 if (empty($paragraphs)) {
264 // Add the start tag indicated by \n\n at the beginning of $data
266 $result[] = $this->_pStart();
269 // Append the paragraphs onto the result
270 foreach ($paragraphs as $par) {
271 $result[] = new HTMLPurifier_Token_Text($par);
272 $result[] = new HTMLPurifier_Token_End('p');
273 $result[] = new HTMLPurifier_Token_Text("\n\n");
274 $result[] = $this->_pStart();
277 // Remove trailing start token; Injector will handle this later if
278 // it was indeed needed. This prevents from needing to do a lookahead,
279 // at the cost of a lookbehind later.
282 // If there is no need for an end tag, remove all of it and let
283 // MakeWellFormed close it later.
285 array_pop($result); // removes \n\n
286 array_pop($result); // removes </p>
292 * Returns true if passed token is inline (and, ergo, allowed in
295 private function _isInline($token) {
296 return isset($this->htmlDefinition->info['p']->child->elements[$token->name]);
300 * Looks ahead in the token list and determines whether or not we need
301 * to insert a <p> tag.
303 private function _pLookAhead() {
304 $this->current($i, $current);
305 if ($current instanceof HTMLPurifier_Token_Start) $nesting = 1;
308 while ($this->forwardUntilEndToken($i, $current, $nesting)) {
309 $result = $this->_checkNeedsP($current);
310 if ($result !== null) {
319 * Determines if a particular token requires an earlier inline token
320 * to get a paragraph. This should be used with _forwardUntilEndToken
322 private function _checkNeedsP($current) {
323 if ($current instanceof HTMLPurifier_Token_Start){
324 if (!$this->_isInline($current)) {
327 // Terminate early, since we hit a block element
330 } elseif ($current instanceof HTMLPurifier_Token_Text) {
331 if (strpos($current->data, "\n\n") !== false) {
332 // <div>PAR1<b>PAR1\n\nPAR2
336 // <div>PAR1<b>PAR1...
345 // vim: et sw=4 sts=4