00001 <?php 00002 00009 class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector 00010 { 00011 00012 public $name = 'AutoParagraph'; 00013 public $needed = array('p'); 00014 00015 private function _pStart() { 00016 $par = new HTMLPurifier_Token_Start('p'); 00017 $par->armor['MakeWellFormed_TagClosedError'] = true; 00018 return $par; 00019 } 00020 00021 public function handleText(&$token) { 00022 $text = $token->data; 00023 // Does the current parent allow <p> tags? 00024 if ($this->allowsElement('p')) { 00025 if (empty($this->currentNesting) || strpos($text, "\n\n") !== false) { 00026 // Note that we have differing behavior when dealing with text 00027 // in the anonymous root node, or a node inside the document. 00028 // If the text as a double-newline, the treatment is the same; 00029 // if it doesn't, see the next if-block if you're in the document. 00030 00031 $i = $nesting = null; 00032 if (!$this->forwardUntilEndToken($i, $current, $nesting) && $token->is_whitespace) { 00033 // State 1.1: ... ^ (whitespace, then document end) 00034 // ---- 00035 // This is a degenerate case 00036 } else { 00037 // State 1.2: PAR1 00038 // ---- 00039 00040 // State 1.3: PAR1\n\nPAR2 00041 // ------------ 00042 00043 // State 1.4: <div>PAR1\n\nPAR2 (see State 2) 00044 // ------------ 00045 $token = array($this->_pStart()); 00046 $this->_splitText($text, $token); 00047 } 00048 } else { 00049 // State 2: <div>PAR1... (similar to 1.4) 00050 // ---- 00051 00052 // We're in an element that allows paragraph tags, but we're not 00053 // sure if we're going to need them. 00054 if ($this->_pLookAhead()) { 00055 // State 2.1: <div>PAR1<b>PAR1\n\nPAR2 00056 // ---- 00057 // Note: This will always be the first child, since any 00058 // previous inline element would have triggered this very 00059 // same routine, and found the double newline. One possible 00060 // exception would be a comment. 00061 $token = array($this->_pStart(), $token); 00062 } else { 00063 // State 2.2.1: <div>PAR1<div> 00064 // ---- 00065 00066 // State 2.2.2: <div>PAR1<b>PAR1</b></div> 00067 // ---- 00068 } 00069 } 00070 // Is the current parent a <p> tag? 00071 } elseif ( 00072 !empty($this->currentNesting) && 00073 $this->currentNesting[count($this->currentNesting)-1]->name == 'p' 00074 ) { 00075 // State 3.1: ...<p>PAR1 00076 // ---- 00077 00078 // State 3.2: ...<p>PAR1\n\nPAR2 00079 // ------------ 00080 $token = array(); 00081 $this->_splitText($text, $token); 00082 // Abort! 00083 } else { 00084 // State 4.1: ...<b>PAR1 00085 // ---- 00086 00087 // State 4.2: ...<b>PAR1\n\nPAR2 00088 // ------------ 00089 } 00090 } 00091 00092 public function handleElement(&$token) { 00093 // We don't have to check if we're already in a <p> tag for block 00094 // tokens, because the tag would have been autoclosed by MakeWellFormed. 00095 if ($this->allowsElement('p')) { 00096 if (!empty($this->currentNesting)) { 00097 if ($this->_isInline($token)) { 00098 // State 1: <div>...<b> 00099 // --- 00100 00101 // Check if this token is adjacent to the parent token 00102 // (seek backwards until token isn't whitespace) 00103 $i = null; 00104 $this->backward($i, $prev); 00105 00106 if (!$prev instanceof HTMLPurifier_Token_Start) { 00107 // Token wasn't adjacent 00108 00109 if ( 00110 $prev instanceof HTMLPurifier_Token_Text && 00111 substr($prev->data, -2) === "\n\n" 00112 ) { 00113 // State 1.1.4: <div><p>PAR1</p>\n\n<b> 00114 // --- 00115 00116 // Quite frankly, this should be handled by splitText 00117 $token = array($this->_pStart(), $token); 00118 } else { 00119 // State 1.1.1: <div><p>PAR1</p><b> 00120 // --- 00121 00122 // State 1.1.2: <div><br /><b> 00123 // --- 00124 00125 // State 1.1.3: <div>PAR<b> 00126 // --- 00127 } 00128 00129 } else { 00130 // State 1.2.1: <div><b> 00131 // --- 00132 00133 // Lookahead to see if <p> is needed. 00134 if ($this->_pLookAhead()) { 00135 // State 1.3.1: <div><b>PAR1\n\nPAR2 00136 // --- 00137 $token = array($this->_pStart(), $token); 00138 } else { 00139 // State 1.3.2: <div><b>PAR1</b></div> 00140 // --- 00141 00142 // State 1.3.3: <div><b>PAR1</b><div></div>\n\n</div> 00143 // --- 00144 } 00145 } 00146 } else { 00147 // State 2.3: ...<div> 00148 // ----- 00149 } 00150 } else { 00151 if ($this->_isInline($token)) { 00152 // State 3.1: <b> 00153 // --- 00154 // This is where the {p} tag is inserted, not reflected in 00155 // inputTokens yet, however. 00156 $token = array($this->_pStart(), $token); 00157 } else { 00158 // State 3.2: <div> 00159 // ----- 00160 } 00161 00162 $i = null; 00163 if ($this->backward($i, $prev)) { 00164 if ( 00165 !$prev instanceof HTMLPurifier_Token_Text 00166 ) { 00167 // State 3.1.1: ...</p>{p}<b> 00168 // --- 00169 00170 // State 3.2.1: ...</p><div> 00171 // ----- 00172 00173 if (!is_array($token)) $token = array($token); 00174 array_unshift($token, new HTMLPurifier_Token_Text("\n\n")); 00175 } else { 00176 // State 3.1.2: ...</p>\n\n{p}<b> 00177 // --- 00178 00179 // State 3.2.2: ...</p>\n\n<div> 00180 // ----- 00181 00182 // Note: PAR<ELEM> cannot occur because PAR would have been 00183 // wrapped in <p> tags. 00184 } 00185 } 00186 } 00187 } else { 00188 // State 2.2: <ul><li> 00189 // ---- 00190 00191 // State 2.4: <p><b> 00192 // --- 00193 } 00194 } 00195 00206 private function _splitText($data, &$result) { 00207 $raw_paragraphs = explode("\n\n", $data); 00208 $paragraphs = array(); // without empty paragraphs 00209 $needs_start = false; 00210 $needs_end = false; 00211 00212 $c = count($raw_paragraphs); 00213 if ($c == 1) { 00214 // There were no double-newlines, abort quickly. In theory this 00215 // should never happen. 00216 $result[] = new HTMLPurifier_Token_Text($data); 00217 return; 00218 } 00219 for ($i = 0; $i < $c; $i++) { 00220 $par = $raw_paragraphs[$i]; 00221 if (trim($par) !== '') { 00222 $paragraphs[] = $par; 00223 } else { 00224 if ($i == 0) { 00225 // Double newline at the front 00226 if (empty($result)) { 00227 // The empty result indicates that the AutoParagraph 00228 // injector did not add any start paragraph tokens. 00229 // This means that we have been in a paragraph for 00230 // a while, and the newline means we should start a new one. 00231 $result[] = new HTMLPurifier_Token_End('p'); 00232 $result[] = new HTMLPurifier_Token_Text("\n\n"); 00233 // However, the start token should only be added if 00234 // there is more processing to be done (i.e. there are 00235 // real paragraphs in here). If there are none, the 00236 // next start paragraph tag will be handled by the 00237 // next call to the injector 00238 $needs_start = true; 00239 } else { 00240 // We just started a new paragraph! 00241 // Reinstate a double-newline for presentation's sake, since 00242 // it was in the source code. 00243 array_unshift($result, new HTMLPurifier_Token_Text("\n\n")); 00244 } 00245 } elseif ($i + 1 == $c) { 00246 // Double newline at the end 00247 // There should be a trailing </p> when we're finally done. 00248 $needs_end = true; 00249 } 00250 } 00251 } 00252 00253 // Check if this was just a giant blob of whitespace. Move this earlier, 00254 // perhaps? 00255 if (empty($paragraphs)) { 00256 return; 00257 } 00258 00259 // Add the start tag indicated by \n\n at the beginning of $data 00260 if ($needs_start) { 00261 $result[] = $this->_pStart(); 00262 } 00263 00264 // Append the paragraphs onto the result 00265 foreach ($paragraphs as $par) { 00266 $result[] = new HTMLPurifier_Token_Text($par); 00267 $result[] = new HTMLPurifier_Token_End('p'); 00268 $result[] = new HTMLPurifier_Token_Text("\n\n"); 00269 $result[] = $this->_pStart(); 00270 } 00271 00272 // Remove trailing start token; Injector will handle this later if 00273 // it was indeed needed. This prevents from needing to do a lookahead, 00274 // at the cost of a lookbehind later. 00275 array_pop($result); 00276 00277 // If there is no need for an end tag, remove all of it and let 00278 // MakeWellFormed close it later. 00279 if (!$needs_end) { 00280 array_pop($result); // removes \n\n 00281 array_pop($result); // removes </p> 00282 } 00283 00284 } 00285 00290 private function _isInline($token) { 00291 return isset($this->htmlDefinition->info['p']->child->elements[$token->name]); 00292 } 00293 00298 private function _pLookAhead() { 00299 $this->current($i, $current); 00300 if ($current instanceof HTMLPurifier_Token_Start) $nesting = 1; 00301 else $nesting = 0; 00302 $ok = false; 00303 while ($this->forwardUntilEndToken($i, $current, $nesting)) { 00304 $result = $this->_checkNeedsP($current); 00305 if ($result !== null) { 00306 $ok = $result; 00307 break; 00308 } 00309 } 00310 return $ok; 00311 } 00312 00317 private function _checkNeedsP($current) { 00318 if ($current instanceof HTMLPurifier_Token_Start){ 00319 if (!$this->_isInline($current)) { 00320 // <div>PAR1<div> 00321 // ---- 00322 // Terminate early, since we hit a block element 00323 return false; 00324 } 00325 } elseif ($current instanceof HTMLPurifier_Token_Text) { 00326 if (strpos($current->data, "\n\n") !== false) { 00327 // <div>PAR1<b>PAR1\n\nPAR2 00328 // ---- 00329 return true; 00330 } else { 00331 // <div>PAR1<b>PAR1... 00332 // ---- 00333 } 00334 } 00335 return null; 00336 } 00337 00338 } 00339 00340 // vim: et sw=4 sts=4
| Copyright © 2003 - 2009 MyOOS [Shopsystem]. All rights reserved. MyOOS [Shopsystem] is Free Software released under the GNU/GPL License. Webmaster: info@r23.de (Impressum) |
|
