+++ /dev/null
-<?php\r
-\r
-/**\r
- * Injector that auto paragraphs text in the root node based on\r
- * double-spacing.\r
- * @todo Ensure all states are unit tested, including variations as well.\r
- * @todo Make a graph of the flow control for this Injector.\r
- */\r
-class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector\r
-{\r
- /**\r
- * @type string\r
- */\r
- public $name = 'AutoParagraph';\r
-\r
- /**\r
- * @type array\r
- */\r
- public $needed = array('p');\r
-\r
- /**\r
- * @return HTMLPurifier_Token_Start\r
- */\r
- private function _pStart()\r
- {\r
- $par = new HTMLPurifier_Token_Start('p');\r
- $par->armor['MakeWellFormed_TagClosedError'] = true;\r
- return $par;\r
- }\r
-\r
- /**\r
- * @param HTMLPurifier_Token_Text $token\r
- */\r
- public function handleText(&$token)\r
- {\r
- $text = $token->data;\r
- // Does the current parent allow <p> tags?\r
- if ($this->allowsElement('p')) {\r
- if (empty($this->currentNesting) || strpos($text, "\n\n") !== false) {\r
- // Note that we have differing behavior when dealing with text\r
- // in the anonymous root node, or a node inside the document.\r
- // If the text as a double-newline, the treatment is the same;\r
- // if it doesn't, see the next if-block if you're in the document.\r
-\r
- $i = $nesting = null;\r
- if (!$this->forwardUntilEndToken($i, $current, $nesting) && $token->is_whitespace) {\r
- // State 1.1: ... ^ (whitespace, then document end)\r
- // ----\r
- // This is a degenerate case\r
- } else {\r
- if (!$token->is_whitespace || $this->_isInline($current)) {\r
- // State 1.2: PAR1\r
- // ----\r
-\r
- // State 1.3: PAR1\n\nPAR2\r
- // ------------\r
-\r
- // State 1.4: <div>PAR1\n\nPAR2 (see State 2)\r
- // ------------\r
- $token = array($this->_pStart());\r
- $this->_splitText($text, $token);\r
- } else {\r
- // State 1.5: \n<hr />\r
- // --\r
- }\r
- }\r
- } else {\r
- // State 2: <div>PAR1... (similar to 1.4)\r
- // ----\r
-\r
- // We're in an element that allows paragraph tags, but we're not\r
- // sure if we're going to need them.\r
- if ($this->_pLookAhead()) {\r
- // State 2.1: <div>PAR1<b>PAR1\n\nPAR2\r
- // ----\r
- // Note: This will always be the first child, since any\r
- // previous inline element would have triggered this very\r
- // same routine, and found the double newline. One possible\r
- // exception would be a comment.\r
- $token = array($this->_pStart(), $token);\r
- } else {\r
- // State 2.2.1: <div>PAR1<div>\r
- // ----\r
-\r
- // State 2.2.2: <div>PAR1<b>PAR1</b></div>\r
- // ----\r
- }\r
- }\r
- // Is the current parent a <p> tag?\r
- } elseif (!empty($this->currentNesting) &&\r
- $this->currentNesting[count($this->currentNesting) - 1]->name == 'p') {\r
- // State 3.1: ...<p>PAR1\r
- // ----\r
-\r
- // State 3.2: ...<p>PAR1\n\nPAR2\r
- // ------------\r
- $token = array();\r
- $this->_splitText($text, $token);\r
- // Abort!\r
- } else {\r
- // State 4.1: ...<b>PAR1\r
- // ----\r
-\r
- // State 4.2: ...<b>PAR1\n\nPAR2\r
- // ------------\r
- }\r
- }\r
-\r
- /**\r
- * @param HTMLPurifier_Token $token\r
- */\r
- public function handleElement(&$token)\r
- {\r
- // We don't have to check if we're already in a <p> tag for block\r
- // tokens, because the tag would have been autoclosed by MakeWellFormed.\r
- if ($this->allowsElement('p')) {\r
- if (!empty($this->currentNesting)) {\r
- if ($this->_isInline($token)) {\r
- // State 1: <div>...<b>\r
- // ---\r
- // Check if this token is adjacent to the parent token\r
- // (seek backwards until token isn't whitespace)\r
- $i = null;\r
- $this->backward($i, $prev);\r
-\r
- if (!$prev instanceof HTMLPurifier_Token_Start) {\r
- // Token wasn't adjacent\r
- if ($prev instanceof HTMLPurifier_Token_Text &&\r
- substr($prev->data, -2) === "\n\n"\r
- ) {\r
- // State 1.1.4: <div><p>PAR1</p>\n\n<b>\r
- // ---\r
- // Quite frankly, this should be handled by splitText\r
- $token = array($this->_pStart(), $token);\r
- } else {\r
- // State 1.1.1: <div><p>PAR1</p><b>\r
- // ---\r
- // State 1.1.2: <div><br /><b>\r
- // ---\r
- // State 1.1.3: <div>PAR<b>\r
- // ---\r
- }\r
- } else {\r
- // State 1.2.1: <div><b>\r
- // ---\r
- // Lookahead to see if <p> is needed.\r
- if ($this->_pLookAhead()) {\r
- // State 1.3.1: <div><b>PAR1\n\nPAR2\r
- // ---\r
- $token = array($this->_pStart(), $token);\r
- } else {\r
- // State 1.3.2: <div><b>PAR1</b></div>\r
- // ---\r
-\r
- // State 1.3.3: <div><b>PAR1</b><div></div>\n\n</div>\r
- // ---\r
- }\r
- }\r
- } else {\r
- // State 2.3: ...<div>\r
- // -----\r
- }\r
- } else {\r
- if ($this->_isInline($token)) {\r
- // State 3.1: <b>\r
- // ---\r
- // This is where the {p} tag is inserted, not reflected in\r
- // inputTokens yet, however.\r
- $token = array($this->_pStart(), $token);\r
- } else {\r
- // State 3.2: <div>\r
- // -----\r
- }\r
-\r
- $i = null;\r
- if ($this->backward($i, $prev)) {\r
- if (!$prev instanceof HTMLPurifier_Token_Text) {\r
- // State 3.1.1: ...</p>{p}<b>\r
- // ---\r
- // State 3.2.1: ...</p><div>\r
- // -----\r
- if (!is_array($token)) {\r
- $token = array($token);\r
- }\r
- array_unshift($token, new HTMLPurifier_Token_Text("\n\n"));\r
- } else {\r
- // State 3.1.2: ...</p>\n\n{p}<b>\r
- // ---\r
- // State 3.2.2: ...</p>\n\n<div>\r
- // -----\r
- // Note: PAR<ELEM> cannot occur because PAR would have been\r
- // wrapped in <p> tags.\r
- }\r
- }\r
- }\r
- } else {\r
- // State 2.2: <ul><li>\r
- // ----\r
- // State 2.4: <p><b>\r
- // ---\r
- }\r
- }\r
-\r
- /**\r
- * Splits up a text in paragraph tokens and appends them\r
- * to the result stream that will replace the original\r
- * @param string $data String text data that will be processed\r
- * into paragraphs\r
- * @param HTMLPurifier_Token[] $result Reference to array of tokens that the\r
- * tags will be appended onto\r
- */\r
- private function _splitText($data, &$result)\r
- {\r
- $raw_paragraphs = explode("\n\n", $data);\r
- $paragraphs = array(); // without empty paragraphs\r
- $needs_start = false;\r
- $needs_end = false;\r
-\r
- $c = count($raw_paragraphs);\r
- if ($c == 1) {\r
- // There were no double-newlines, abort quickly. In theory this\r
- // should never happen.\r
- $result[] = new HTMLPurifier_Token_Text($data);\r
- return;\r
- }\r
- for ($i = 0; $i < $c; $i++) {\r
- $par = $raw_paragraphs[$i];\r
- if (trim($par) !== '') {\r
- $paragraphs[] = $par;\r
- } else {\r
- if ($i == 0) {\r
- // Double newline at the front\r
- if (empty($result)) {\r
- // The empty result indicates that the AutoParagraph\r
- // injector did not add any start paragraph tokens.\r
- // This means that we have been in a paragraph for\r
- // a while, and the newline means we should start a new one.\r
- $result[] = new HTMLPurifier_Token_End('p');\r
- $result[] = new HTMLPurifier_Token_Text("\n\n");\r
- // However, the start token should only be added if\r
- // there is more processing to be done (i.e. there are\r
- // real paragraphs in here). If there are none, the\r
- // next start paragraph tag will be handled by the\r
- // next call to the injector\r
- $needs_start = true;\r
- } else {\r
- // We just started a new paragraph!\r
- // Reinstate a double-newline for presentation's sake, since\r
- // it was in the source code.\r
- array_unshift($result, new HTMLPurifier_Token_Text("\n\n"));\r
- }\r
- } elseif ($i + 1 == $c) {\r
- // Double newline at the end\r
- // There should be a trailing </p> when we're finally done.\r
- $needs_end = true;\r
- }\r
- }\r
- }\r
-\r
- // Check if this was just a giant blob of whitespace. Move this earlier,\r
- // perhaps?\r
- if (empty($paragraphs)) {\r
- return;\r
- }\r
-\r
- // Add the start tag indicated by \n\n at the beginning of $data\r
- if ($needs_start) {\r
- $result[] = $this->_pStart();\r
- }\r
-\r
- // Append the paragraphs onto the result\r
- foreach ($paragraphs as $par) {\r
- $result[] = new HTMLPurifier_Token_Text($par);\r
- $result[] = new HTMLPurifier_Token_End('p');\r
- $result[] = new HTMLPurifier_Token_Text("\n\n");\r
- $result[] = $this->_pStart();\r
- }\r
-\r
- // Remove trailing start token; Injector will handle this later if\r
- // it was indeed needed. This prevents from needing to do a lookahead,\r
- // at the cost of a lookbehind later.\r
- array_pop($result);\r
-\r
- // If there is no need for an end tag, remove all of it and let\r
- // MakeWellFormed close it later.\r
- if (!$needs_end) {\r
- array_pop($result); // removes \n\n\r
- array_pop($result); // removes </p>\r
- }\r
- }\r
-\r
- /**\r
- * Returns true if passed token is inline (and, ergo, allowed in\r
- * paragraph tags)\r
- * @param HTMLPurifier_Token $token\r
- * @return bool\r
- */\r
- private function _isInline($token)\r
- {\r
- return isset($this->htmlDefinition->info['p']->child->elements[$token->name]);\r
- }\r
-\r
- /**\r
- * Looks ahead in the token list and determines whether or not we need\r
- * to insert a <p> tag.\r
- * @return bool\r
- */\r
- private function _pLookAhead()\r
- {\r
- if ($this->currentToken instanceof HTMLPurifier_Token_Start) {\r
- $nesting = 1;\r
- } else {\r
- $nesting = 0;\r
- }\r
- $ok = false;\r
- $i = null;\r
- while ($this->forwardUntilEndToken($i, $current, $nesting)) {\r
- $result = $this->_checkNeedsP($current);\r
- if ($result !== null) {\r
- $ok = $result;\r
- break;\r
- }\r
- }\r
- return $ok;\r
- }\r
-\r
- /**\r
- * Determines if a particular token requires an earlier inline token\r
- * to get a paragraph. This should be used with _forwardUntilEndToken\r
- * @param HTMLPurifier_Token $current\r
- * @return bool\r
- */\r
- private function _checkNeedsP($current)\r
- {\r
- if ($current instanceof HTMLPurifier_Token_Start) {\r
- if (!$this->_isInline($current)) {\r
- // <div>PAR1<div>\r
- // ----\r
- // Terminate early, since we hit a block element\r
- return false;\r
- }\r
- } elseif ($current instanceof HTMLPurifier_Token_Text) {\r
- if (strpos($current->data, "\n\n") !== false) {\r
- // <div>PAR1<b>PAR1\n\nPAR2\r
- // ----\r
- return true;\r
- } else {\r
- // <div>PAR1<b>PAR1...\r
- // ----\r
- }\r
- }\r
- return null;\r
- }\r
-}\r
-\r
-// vim: et sw=4 sts=4\r