Complex classes like Parser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Parser |
||
| 17 | { |
||
| 18 | /**#@+ |
||
| 19 | * Boolean rules bitfield |
||
| 20 | */ |
||
| 21 | const RULE_AUTO_CLOSE = 1 << 0; |
||
| 22 | const RULE_AUTO_REOPEN = 1 << 1; |
||
| 23 | const RULE_BREAK_PARAGRAPH = 1 << 2; |
||
| 24 | const RULE_CREATE_PARAGRAPHS = 1 << 3; |
||
| 25 | const RULE_DISABLE_AUTO_BR = 1 << 4; |
||
| 26 | const RULE_ENABLE_AUTO_BR = 1 << 5; |
||
| 27 | const RULE_IGNORE_TAGS = 1 << 6; |
||
| 28 | const RULE_IGNORE_TEXT = 1 << 7; |
||
| 29 | const RULE_IGNORE_WHITESPACE = 1 << 8; |
||
| 30 | const RULE_IS_TRANSPARENT = 1 << 9; |
||
| 31 | const RULE_PREVENT_BR = 1 << 10; |
||
| 32 | const RULE_SUSPEND_AUTO_BR = 1 << 11; |
||
| 33 | const RULE_TRIM_FIRST_LINE = 1 << 12; |
||
| 34 | /**#@-*/ |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Bitwise disjunction of rules related to automatic line breaks |
||
| 38 | */ |
||
| 39 | const RULES_AUTO_LINEBREAKS = self::RULE_DISABLE_AUTO_BR | self::RULE_ENABLE_AUTO_BR | self::RULE_SUSPEND_AUTO_BR; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Bitwise disjunction of rules that are inherited by subcontexts |
||
| 43 | */ |
||
| 44 | const RULES_INHERITANCE = self::RULE_ENABLE_AUTO_BR; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * All the characters that are considered whitespace |
||
| 48 | */ |
||
| 49 | const WHITESPACE = " \n\t"; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array Number of open tags for each tag name |
||
| 53 | */ |
||
| 54 | protected $cntOpen; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array Number of times each tag has been used |
||
| 58 | */ |
||
| 59 | protected $cntTotal; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array Current context |
||
| 63 | */ |
||
| 64 | protected $context; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var integer How hard the parser has worked on fixing bad markup so far |
||
| 68 | */ |
||
| 69 | protected $currentFixingCost; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var Tag Current tag being processed |
||
| 73 | */ |
||
| 74 | protected $currentTag; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var bool Whether the output contains "rich" tags, IOW any tag that is not <p> or <br/> |
||
| 78 | */ |
||
| 79 | protected $isRich; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var Logger This parser's logger |
||
| 83 | */ |
||
| 84 | protected $logger; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var integer How hard the parser should work on fixing bad markup |
||
| 88 | */ |
||
| 89 | public $maxFixingCost = 10000; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var array Associative array of namespace prefixes in use in document (prefixes used as key) |
||
| 93 | */ |
||
| 94 | protected $namespaces; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array Stack of open tags (instances of Tag) |
||
| 98 | */ |
||
| 99 | protected $openTags; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var string This parser's output |
||
| 103 | */ |
||
| 104 | protected $output; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var integer Position of the cursor in the original text |
||
| 108 | */ |
||
| 109 | protected $pos; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var array Array of callbacks, using plugin names as keys |
||
| 113 | */ |
||
| 114 | protected $pluginParsers = []; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var array Associative array of [pluginName => pluginConfig] |
||
| 118 | */ |
||
| 119 | protected $pluginsConfig; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var array Variables registered for use in filters |
||
| 123 | */ |
||
| 124 | public $registeredVars = []; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var array Root context, used at the root of the document |
||
| 128 | */ |
||
| 129 | protected $rootContext; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var array Tags' config |
||
| 133 | */ |
||
| 134 | protected $tagsConfig; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var array Tag storage |
||
| 138 | */ |
||
| 139 | protected $tagStack; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var bool Whether the tags in the stack are sorted |
||
| 143 | */ |
||
| 144 | protected $tagStackIsSorted; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var string Text being parsed |
||
| 148 | */ |
||
| 149 | protected $text; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var integer Length of the text being parsed |
||
| 153 | */ |
||
| 154 | protected $textLen; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var integer Counter incremented everytime the parser is reset. Used to as a canary to detect |
||
| 158 | * whether the parser was reset during execution |
||
| 159 | */ |
||
| 160 | protected $uid = 0; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @var integer Position before which we output text verbatim, without paragraphs or linebreaks |
||
| 164 | */ |
||
| 165 | protected $wsPos; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Constructor |
||
| 169 | */ |
||
| 170 | 190 | public function __construct(array $config) |
|
| 171 | { |
||
| 172 | 190 | $this->pluginsConfig = $config['plugins']; |
|
| 173 | 190 | $this->registeredVars = $config['registeredVars']; |
|
| 174 | 190 | $this->rootContext = $config['rootContext']; |
|
| 175 | 190 | $this->tagsConfig = $config['tags']; |
|
| 176 | |||
| 177 | 190 | $this->__wakeup(); |
|
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Serializer |
||
| 182 | * |
||
| 183 | * Returns the properties that need to persist through serialization. |
||
| 184 | * |
||
| 185 | * NOTE: using __sleep() is preferable to implementing Serializable because it leaves the choice |
||
| 186 | * of the serializer to the user (e.g. igbinary) |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | 2 | public function __sleep() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Unserializer |
||
| 197 | * |
||
| 198 | * @return void |
||
| 199 | */ |
||
| 200 | 190 | public function __wakeup() |
|
| 201 | { |
||
| 202 | 190 | $this->logger = new Logger; |
|
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Reset the parser for a new parsing |
||
| 207 | * |
||
| 208 | * @param string $text Text to be parsed |
||
| 209 | * @return void |
||
| 210 | */ |
||
| 211 | 182 | protected function reset($text) |
|
| 212 | { |
||
| 213 | // Reject invalid UTF-8 |
||
| 214 | 182 | if (!preg_match('//u', $text)) |
|
| 215 | { |
||
| 216 | 1 | throw new InvalidArgumentException('Invalid UTF-8 input'); |
|
| 217 | } |
||
| 218 | |||
| 219 | // Normalize CR/CRLF to LF, remove control characters that aren't allowed in XML |
||
| 220 | 181 | $text = preg_replace('/\\r\\n?/', "\n", $text); |
|
| 221 | 181 | $text = preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]+/S', '', $text); |
|
| 222 | |||
| 223 | // Clear the logs |
||
| 224 | 181 | $this->logger->clear(); |
|
| 225 | |||
| 226 | // Initialize the rest |
||
| 227 | 181 | $this->cntOpen = []; |
|
| 228 | 181 | $this->cntTotal = []; |
|
| 229 | 181 | $this->currentFixingCost = 0; |
|
| 230 | 181 | $this->currentTag = null; |
|
| 231 | 181 | $this->isRich = false; |
|
| 232 | 181 | $this->namespaces = []; |
|
| 233 | 181 | $this->openTags = []; |
|
| 234 | 181 | $this->output = ''; |
|
| 235 | 181 | $this->pos = 0; |
|
| 236 | 181 | $this->tagStack = []; |
|
| 237 | 181 | $this->tagStackIsSorted = false; |
|
| 238 | 181 | $this->text = $text; |
|
| 239 | 181 | $this->textLen = strlen($text); |
|
| 240 | 181 | $this->wsPos = 0; |
|
| 241 | |||
| 242 | // Initialize the root context |
||
| 243 | 181 | $this->context = $this->rootContext; |
|
| 244 | 181 | $this->context['inParagraph'] = false; |
|
| 245 | |||
| 246 | // Bump the UID |
||
| 247 | 181 | ++$this->uid; |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Set a tag's option |
||
| 252 | * |
||
| 253 | * This method ensures that the tag's config is a value and not a reference, to prevent |
||
| 254 | * potential side-effects. References contained *inside* the tag's config are left untouched |
||
| 255 | * |
||
| 256 | * @param string $tagName Tag's name |
||
| 257 | * @param string $optionName Option's name |
||
| 258 | * @param mixed $optionValue Option's value |
||
| 259 | * @return void |
||
| 260 | */ |
||
| 261 | 7 | protected function setTagOption($tagName, $optionName, $optionValue) |
|
| 262 | { |
||
| 263 | 7 | if (isset($this->tagsConfig[$tagName])) |
|
| 264 | { |
||
| 265 | // Copy the tag's config and remove it. That will destroy the reference |
||
| 266 | 7 | $tagConfig = $this->tagsConfig[$tagName]; |
|
| 267 | 7 | unset($this->tagsConfig[$tagName]); |
|
| 268 | |||
| 269 | // Set the new value and replace the tag's config |
||
| 270 | 7 | $tagConfig[$optionName] = $optionValue; |
|
| 271 | 7 | $this->tagsConfig[$tagName] = $tagConfig; |
|
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | //========================================================================== |
||
| 276 | // Public API |
||
| 277 | //========================================================================== |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Disable a tag |
||
| 281 | * |
||
| 282 | * @param string $tagName Name of the tag |
||
| 283 | * @return void |
||
| 284 | */ |
||
| 285 | 3 | public function disableTag($tagName) |
|
| 286 | { |
||
| 287 | 3 | $this->setTagOption($tagName, 'isDisabled', true); |
|
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Enable a tag |
||
| 292 | * |
||
| 293 | * @param string $tagName Name of the tag |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | 1 | public function enableTag($tagName) |
|
| 297 | { |
||
| 298 | 1 | if (isset($this->tagsConfig[$tagName])) |
|
| 299 | { |
||
| 300 | 1 | unset($this->tagsConfig[$tagName]['isDisabled']); |
|
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get this parser's Logger instance |
||
| 306 | * |
||
| 307 | * @return Logger |
||
| 308 | */ |
||
| 309 | 143 | public function getLogger() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Return the last text parsed |
||
| 316 | * |
||
| 317 | * This method returns the normalized text, which may be slightly different from the original |
||
| 318 | * text in that EOLs are normalized to LF and other control codes are stripped. This method is |
||
| 319 | * meant to be used in support of processing log entries, which contain offsets based on the |
||
| 320 | * normalized text |
||
| 321 | * |
||
| 322 | * @see Parser::reset() |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | 142 | public function getText() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Parse a text |
||
| 333 | * |
||
| 334 | * @param string $text Text to parse |
||
| 335 | * @return string XML representation |
||
| 336 | */ |
||
| 337 | 182 | public function parse($text) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Change a tag's tagLimit |
||
| 367 | * |
||
| 368 | * NOTE: the default tagLimit should generally be set during configuration instead |
||
| 369 | * |
||
| 370 | * @param string $tagName The tag's name, in UPPERCASE |
||
| 371 | * @param integer $tagLimit |
||
| 372 | * @return void |
||
| 373 | */ |
||
| 374 | 2 | public function setTagLimit($tagName, $tagLimit) |
|
| 375 | { |
||
| 376 | 2 | $this->setTagOption($tagName, 'tagLimit', $tagLimit); |
|
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Change a tag's nestingLimit |
||
| 381 | * |
||
| 382 | * NOTE: the default nestingLimit should generally be set during configuration instead |
||
| 383 | * |
||
| 384 | * @param string $tagName The tag's name, in UPPERCASE |
||
| 385 | * @param integer $nestingLimit |
||
| 386 | * @return void |
||
| 387 | */ |
||
| 388 | 2 | public function setNestingLimit($tagName, $nestingLimit) |
|
| 389 | { |
||
| 390 | 2 | $this->setTagOption($tagName, 'nestingLimit', $nestingLimit); |
|
| 391 | } |
||
| 392 | |||
| 393 | //========================================================================== |
||
| 394 | // Output handling |
||
| 395 | //========================================================================== |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Finalize the output by appending the rest of the unprocessed text and create the root node |
||
| 399 | * |
||
| 400 | * @return void |
||
| 401 | */ |
||
| 402 | 181 | protected function finalizeOutput() |
|
| 403 | { |
||
| 404 | // Output the rest of the text and close the last paragraph |
||
| 405 | 181 | $this->outputText($this->textLen, 0, true); |
|
| 406 | |||
| 407 | // Remove empty tag pairs, e.g. <I><U></U></I> as well as empty paragraphs |
||
| 408 | do |
||
| 409 | { |
||
| 410 | 181 | $this->output = preg_replace('(<([^ />]++)[^>]*></\\1>)', '', $this->output, -1, $cnt); |
|
| 411 | } |
||
| 412 | 181 | while ($cnt > 0); |
|
| 413 | |||
| 414 | // Merge consecutive <i> tags |
||
| 415 | 181 | if (strpos($this->output, '</i><i>') !== false) |
|
| 416 | { |
||
| 417 | 1 | $this->output = str_replace('</i><i>', '', $this->output); |
|
| 418 | } |
||
| 419 | |||
| 420 | // Remove control characters from the output to ensure it's valid XML |
||
| 421 | 181 | $this->output = preg_replace('([\\x00-\\x08\\x0B-\\x1F])', '', $this->output); |
|
| 422 | |||
| 423 | // Encode Unicode characters that are outside of the BMP |
||
| 424 | 181 | $this->output = Utils::encodeUnicodeSupplementaryCharacters($this->output); |
|
| 425 | |||
| 426 | // Use a <r> root if the text is rich, or <t> for plain text (including <p></p> and <br/>) |
||
| 427 | 181 | $tagName = ($this->isRich) ? 'r' : 't'; |
|
| 428 | |||
| 429 | // Prepare the root node with all the namespace declarations |
||
| 430 | 181 | $tmp = '<' . $tagName; |
|
| 431 | 181 | foreach (array_keys($this->namespaces) as $prefix) |
|
| 432 | { |
||
| 433 | 2 | $tmp .= ' xmlns:' . $prefix . '="urn:s9e:TextFormatter:' . $prefix . '"'; |
|
| 434 | } |
||
| 435 | |||
| 436 | 181 | $this->output = $tmp . '>' . $this->output . '</' . $tagName . '>'; |
|
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Append a tag to the output |
||
| 441 | * |
||
| 442 | * @param Tag $tag Tag to append |
||
| 443 | * @return void |
||
| 444 | */ |
||
| 445 | 138 | protected function outputTag(Tag $tag) |
|
| 446 | { |
||
| 447 | 138 | $this->isRich = true; |
|
| 448 | |||
| 449 | 138 | $tagName = $tag->getName(); |
|
| 450 | 138 | $tagPos = $tag->getPos(); |
|
| 451 | 138 | $tagLen = $tag->getLen(); |
|
| 452 | 138 | $tagFlags = $tag->getFlags(); |
|
| 453 | |||
| 454 | 138 | if ($tagFlags & self::RULE_IGNORE_WHITESPACE) |
|
| 455 | { |
||
| 456 | 11 | $skipBefore = 1; |
|
| 457 | 11 | $skipAfter = ($tag->isEndTag()) ? 2 : 1; |
|
| 458 | } |
||
| 459 | else |
||
| 460 | { |
||
| 461 | 133 | $skipBefore = $skipAfter = 0; |
|
| 462 | } |
||
| 463 | |||
| 464 | // Current paragraph must end before the tag if: |
||
| 465 | // - the tag is a start (or self-closing) tag and it breaks paragraphs, or |
||
| 466 | // - the tag is an end tag (but not self-closing) |
||
| 467 | 138 | $closeParagraph = false; |
|
| 468 | 138 | if ($tag->isStartTag()) |
|
| 469 | { |
||
| 470 | 138 | if ($tagFlags & self::RULE_BREAK_PARAGRAPH) |
|
| 471 | { |
||
| 472 | 138 | $closeParagraph = true; |
|
| 473 | } |
||
| 474 | } |
||
| 475 | else |
||
| 476 | { |
||
| 477 | 104 | $closeParagraph = true; |
|
| 478 | } |
||
| 479 | |||
| 480 | // Let the cursor catch up with this tag's position |
||
| 481 | 138 | $this->outputText($tagPos, $skipBefore, $closeParagraph); |
|
| 482 | |||
| 483 | // Capture the text consumed by the tag |
||
| 484 | 138 | $tagText = ($tagLen) |
|
| 485 | 98 | ? htmlspecialchars(substr($this->text, $tagPos, $tagLen), ENT_NOQUOTES, 'UTF-8') |
|
| 486 | 138 | : ''; |
|
| 487 | |||
| 488 | // Output current tag |
||
| 489 | 138 | if ($tag->isStartTag()) |
|
| 490 | { |
||
| 491 | // Handle paragraphs before opening the tag |
||
| 492 | 138 | if (!($tagFlags & self::RULE_BREAK_PARAGRAPH)) |
|
| 493 | { |
||
| 494 | 137 | $this->outputParagraphStart($tagPos); |
|
| 495 | } |
||
| 496 | |||
| 497 | // Record this tag's namespace, if applicable |
||
| 498 | 138 | $colonPos = strpos($tagName, ':'); |
|
| 499 | 138 | if ($colonPos) |
|
| 500 | { |
||
| 501 | 2 | $this->namespaces[substr($tagName, 0, $colonPos)] = 0; |
|
| 502 | } |
||
| 503 | |||
| 504 | // Open the start tag and add its attributes, but don't close the tag |
||
| 505 | 138 | $this->output .= '<' . $tagName; |
|
| 506 | |||
| 507 | // We output the attributes in lexical order. Helps canonicalizing the output and could |
||
| 508 | // prove useful someday |
||
| 509 | 138 | $attributes = $tag->getAttributes(); |
|
| 510 | 138 | ksort($attributes); |
|
| 511 | |||
| 512 | 138 | foreach ($attributes as $attrName => $attrValue) |
|
| 513 | { |
||
| 514 | 12 | $this->output .= ' ' . $attrName . '="' . str_replace("\n", ' ', htmlspecialchars($attrValue, ENT_COMPAT, 'UTF-8')) . '"'; |
|
| 515 | } |
||
| 516 | |||
| 517 | 138 | if ($tag->isSelfClosingTag()) |
|
| 518 | { |
||
| 519 | 47 | if ($tagLen) |
|
| 520 | { |
||
| 521 | 36 | $this->output .= '>' . $tagText . '</' . $tagName . '>'; |
|
| 522 | } |
||
| 523 | else |
||
| 524 | { |
||
| 525 | 47 | $this->output .= '/>'; |
|
| 526 | } |
||
| 527 | } |
||
| 528 | 104 | elseif ($tagLen) |
|
| 529 | { |
||
| 530 | 70 | $this->output .= '><s>' . $tagText . '</s>'; |
|
| 531 | } |
||
| 532 | else |
||
| 533 | { |
||
| 534 | 138 | $this->output .= '>'; |
|
| 535 | } |
||
| 536 | } |
||
| 537 | else |
||
| 538 | { |
||
| 539 | 104 | if ($tagLen) |
|
| 540 | { |
||
| 541 | 58 | $this->output .= '<e>' . $tagText . '</e>'; |
|
| 542 | } |
||
| 543 | |||
| 544 | 104 | $this->output .= '</' . $tagName . '>'; |
|
| 545 | } |
||
| 546 | |||
| 547 | // Move the cursor past the tag |
||
| 548 | 138 | $this->pos = $tagPos + $tagLen; |
|
| 549 | |||
| 550 | // Skip newlines (no other whitespace) after this tag |
||
| 551 | 138 | $this->wsPos = $this->pos; |
|
| 552 | 138 | while ($skipAfter && $this->wsPos < $this->textLen && $this->text[$this->wsPos] === "\n") |
|
| 553 | { |
||
| 554 | // Decrement the number of lines to skip |
||
| 555 | 9 | --$skipAfter; |
|
| 556 | |||
| 557 | // Move the cursor past the newline |
||
| 558 | 9 | ++$this->wsPos; |
|
| 559 | } |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Output the text between the cursor's position (included) and given position (not included) |
||
| 564 | * |
||
| 565 | * @param integer $catchupPos Position we're catching up to |
||
| 566 | * @param integer $maxLines Maximum number of lines to ignore at the end of the text |
||
| 567 | * @param bool $closeParagraph Whether to close the paragraph at the end, if applicable |
||
| 568 | * @return void |
||
| 569 | */ |
||
| 570 | 181 | protected function outputText($catchupPos, $maxLines, $closeParagraph) |
|
| 571 | { |
||
| 572 | 181 | if ($closeParagraph) |
|
| 573 | { |
||
| 574 | 181 | if (!($this->context['flags'] & self::RULE_CREATE_PARAGRAPHS)) |
|
| 575 | { |
||
| 576 | 170 | $closeParagraph = false; |
|
| 577 | } |
||
| 578 | else |
||
| 579 | { |
||
| 580 | // Ignore any number of lines at the end if we're closing a paragraph |
||
| 581 | 18 | $maxLines = -1; |
|
| 582 | } |
||
| 583 | } |
||
| 584 | |||
| 585 | 181 | if ($this->pos >= $catchupPos) |
|
| 586 | { |
||
| 587 | // We're already there, close the paragraph if applicable and return |
||
| 588 | 133 | if ($closeParagraph) |
|
| 589 | { |
||
| 590 | 4 | $this->outputParagraphEnd(); |
|
| 591 | } |
||
| 592 | |||
| 593 | 133 | return; |
|
| 594 | } |
||
| 595 | |||
| 596 | // Skip over previously identified whitespace if applicable |
||
| 597 | 159 | if ($this->wsPos > $this->pos) |
|
| 598 | { |
||
| 599 | 9 | $skipPos = min($catchupPos, $this->wsPos); |
|
| 600 | 9 | $this->output .= substr($this->text, $this->pos, $skipPos - $this->pos); |
|
| 601 | 9 | $this->pos = $skipPos; |
|
| 602 | |||
| 603 | 9 | if ($this->pos >= $catchupPos) |
|
| 604 | { |
||
| 605 | // Skipped everything. Close the paragraph if applicable and return |
||
| 606 | 2 | if ($closeParagraph) |
|
| 607 | { |
||
| 608 | 1 | $this->outputParagraphEnd(); |
|
| 609 | } |
||
| 610 | |||
| 611 | 2 | return; |
|
| 612 | } |
||
| 613 | } |
||
| 614 | |||
| 615 | // Test whether we're even supposed to output anything |
||
| 616 | 159 | if ($this->context['flags'] & self::RULE_IGNORE_TEXT) |
|
| 617 | { |
||
| 618 | 5 | $catchupLen = $catchupPos - $this->pos; |
|
| 619 | 5 | $catchupText = substr($this->text, $this->pos, $catchupLen); |
|
| 620 | |||
| 621 | // If the catchup text is not entirely composed of whitespace, we put it inside ignore |
||
| 622 | // tags |
||
| 623 | 5 | if (strspn($catchupText, " \n\t") < $catchupLen) |
|
| 624 | { |
||
| 625 | 5 | $catchupText = '<i>' . htmlspecialchars($catchupText, ENT_NOQUOTES, 'UTF-8') . '</i>'; |
|
| 626 | } |
||
| 627 | |||
| 628 | 5 | $this->output .= $catchupText; |
|
| 629 | 5 | $this->pos = $catchupPos; |
|
| 630 | |||
| 631 | 5 | if ($closeParagraph) |
|
| 632 | { |
||
| 633 | 1 | $this->outputParagraphEnd(); |
|
| 634 | } |
||
| 635 | |||
| 636 | 5 | return; |
|
| 637 | } |
||
| 638 | |||
| 639 | // Compute the amount of text to ignore at the end of the output |
||
| 640 | 157 | $ignorePos = $catchupPos; |
|
| 641 | 157 | $ignoreLen = 0; |
|
| 642 | |||
| 643 | // Ignore as many lines (including whitespace) as specified |
||
| 644 | 157 | while ($maxLines && --$ignorePos >= $this->pos) |
|
| 645 | { |
||
| 646 | 21 | $c = $this->text[$ignorePos]; |
|
| 647 | 21 | if (strpos(self::WHITESPACE, $c) === false) |
|
| 648 | { |
||
| 649 | 14 | break; |
|
| 650 | } |
||
| 651 | |||
| 652 | 12 | if ($c === "\n") |
|
| 653 | { |
||
| 654 | 10 | --$maxLines; |
|
| 655 | } |
||
| 656 | |||
| 657 | 12 | ++$ignoreLen; |
|
| 658 | } |
||
| 659 | |||
| 660 | // Adjust $catchupPos to ignore the text at the end |
||
| 661 | 157 | $catchupPos -= $ignoreLen; |
|
| 662 | |||
| 663 | // Break down the text in paragraphs if applicable |
||
| 664 | 157 | if ($this->context['flags'] & self::RULE_CREATE_PARAGRAPHS) |
|
| 665 | { |
||
| 666 | 15 | if (!$this->context['inParagraph']) |
|
| 667 | { |
||
| 668 | 13 | $this->outputWhitespace($catchupPos); |
|
| 669 | |||
| 670 | 13 | if ($catchupPos > $this->pos) |
|
| 671 | { |
||
| 672 | 10 | $this->outputParagraphStart($catchupPos); |
|
| 673 | } |
||
| 674 | } |
||
| 675 | |||
| 676 | // Look for a paragraph break in this text |
||
| 677 | 15 | $pbPos = strpos($this->text, "\n\n", $this->pos); |
|
| 678 | |||
| 679 | 15 | while ($pbPos !== false && $pbPos < $catchupPos) |
|
| 680 | { |
||
| 681 | 3 | $this->outputText($pbPos, 0, true); |
|
| 682 | 3 | $this->outputParagraphStart($catchupPos); |
|
| 683 | |||
| 684 | 3 | $pbPos = strpos($this->text, "\n\n", $this->pos); |
|
| 685 | } |
||
| 686 | } |
||
| 687 | |||
| 688 | // Capture, escape and output the text |
||
| 689 | 157 | if ($catchupPos > $this->pos) |
|
| 690 | { |
||
| 691 | 155 | $catchupText = htmlspecialchars( |
|
| 692 | 155 | substr($this->text, $this->pos, $catchupPos - $this->pos), |
|
| 693 | 155 | ENT_NOQUOTES, |
|
| 694 | 155 | 'UTF-8' |
|
| 695 | ); |
||
| 696 | |||
| 697 | // Format line breaks if applicable |
||
| 698 | 155 | if (($this->context['flags'] & self::RULES_AUTO_LINEBREAKS) === self::RULE_ENABLE_AUTO_BR) |
|
| 699 | { |
||
| 700 | 21 | $catchupText = str_replace("\n", "<br/>\n", $catchupText); |
|
| 701 | } |
||
| 702 | |||
| 703 | 155 | $this->output .= $catchupText; |
|
| 704 | } |
||
| 705 | |||
| 706 | // Close the paragraph if applicable |
||
| 707 | 157 | if ($closeParagraph) |
|
| 708 | { |
||
| 709 | 14 | $this->outputParagraphEnd(); |
|
| 710 | } |
||
| 711 | |||
| 712 | // Add the ignored text if applicable |
||
| 713 | 157 | if ($ignoreLen) |
|
| 714 | { |
||
| 715 | 12 | $this->output .= substr($this->text, $catchupPos, $ignoreLen); |
|
| 716 | } |
||
| 717 | |||
| 718 | // Move the cursor past the text |
||
| 719 | 157 | $this->pos = $catchupPos + $ignoreLen; |
|
| 720 | } |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Output a linebreak tag |
||
| 724 | * |
||
| 725 | * @param Tag $tag |
||
| 726 | * @return void |
||
| 727 | */ |
||
| 728 | 6 | protected function outputBrTag(Tag $tag) |
|
| 729 | { |
||
| 730 | 6 | $this->outputText($tag->getPos(), 0, false); |
|
| 731 | 6 | $this->output .= '<br/>'; |
|
| 732 | } |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Output an ignore tag |
||
| 736 | * |
||
| 737 | * @param Tag $tag |
||
| 738 | * @return void |
||
| 739 | */ |
||
| 740 | 18 | protected function outputIgnoreTag(Tag $tag) |
|
| 741 | { |
||
| 742 | 18 | $tagPos = $tag->getPos(); |
|
| 743 | 18 | $tagLen = $tag->getLen(); |
|
| 744 | |||
| 745 | // Capture the text to ignore |
||
| 746 | 18 | $ignoreText = substr($this->text, $tagPos, $tagLen); |
|
| 747 | |||
| 748 | // Catch up with the tag's position then output the tag |
||
| 749 | 18 | $this->outputText($tagPos, 0, false); |
|
| 750 | 18 | $this->output .= '<i>' . htmlspecialchars($ignoreText, ENT_NOQUOTES, 'UTF-8') . '</i>'; |
|
| 751 | 18 | $this->isRich = true; |
|
| 752 | |||
| 753 | // Move the cursor past this tag |
||
| 754 | 18 | $this->pos = $tagPos + $tagLen; |
|
| 755 | } |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Start a paragraph between current position and given position, if applicable |
||
| 759 | * |
||
| 760 | * @param integer $maxPos Rightmost position at which the paragraph can be opened |
||
| 761 | * @return void |
||
| 762 | */ |
||
| 763 | 144 | protected function outputParagraphStart($maxPos) |
|
| 764 | { |
||
| 765 | // Do nothing if we're already in a paragraph, or if we don't use paragraphs |
||
| 766 | 144 | if ($this->context['inParagraph'] |
|
| 767 | 144 | || !($this->context['flags'] & self::RULE_CREATE_PARAGRAPHS)) |
|
| 768 | { |
||
| 769 | 134 | return; |
|
| 770 | } |
||
| 771 | |||
| 772 | // Output the whitespace between $this->pos and $maxPos if applicable |
||
| 773 | 16 | $this->outputWhitespace($maxPos); |
|
| 774 | |||
| 775 | // Open the paragraph, but only if it's not at the very end of the text |
||
| 776 | 16 | if ($this->pos < $this->textLen) |
|
| 777 | { |
||
| 778 | 16 | $this->output .= '<p>'; |
|
| 779 | 16 | $this->context['inParagraph'] = true; |
|
| 780 | } |
||
| 781 | } |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Close current paragraph at current position if applicable |
||
| 785 | * |
||
| 786 | * @return void |
||
| 787 | */ |
||
| 788 | 18 | protected function outputParagraphEnd() |
|
| 789 | { |
||
| 790 | // Do nothing if we're not in a paragraph |
||
| 791 | 18 | if (!$this->context['inParagraph']) |
|
| 792 | { |
||
| 793 | 3 | return; |
|
| 794 | } |
||
| 795 | |||
| 796 | 16 | $this->output .= '</p>'; |
|
| 797 | 16 | $this->context['inParagraph'] = false; |
|
| 798 | } |
||
| 799 | |||
| 800 | /** |
||
| 801 | * Output the content of a verbatim tag |
||
| 802 | * |
||
| 803 | * @param Tag $tag |
||
| 804 | * @return void |
||
| 805 | */ |
||
| 806 | 4 | protected function outputVerbatim(Tag $tag) |
|
| 807 | { |
||
| 808 | 4 | $flags = $this->context['flags']; |
|
| 809 | 4 | $this->context['flags'] = $tag->getFlags(); |
|
| 810 | 4 | $this->outputText($this->currentTag->getPos() + $this->currentTag->getLen(), 0, false); |
|
| 811 | 4 | $this->context['flags'] = $flags; |
|
| 812 | } |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Skip as much whitespace after current position as possible |
||
| 816 | * |
||
| 817 | * @param integer $maxPos Rightmost character to be skipped |
||
| 818 | * @return void |
||
| 819 | */ |
||
| 820 | 18 | protected function outputWhitespace($maxPos) |
|
| 821 | { |
||
| 822 | 18 | if ($maxPos > $this->pos) |
|
| 823 | { |
||
| 824 | 13 | $spn = strspn($this->text, self::WHITESPACE, $this->pos, $maxPos - $this->pos); |
|
| 825 | |||
| 826 | 13 | if ($spn) |
|
| 827 | { |
||
| 828 | 6 | $this->output .= substr($this->text, $this->pos, $spn); |
|
| 829 | 6 | $this->pos += $spn; |
|
| 830 | } |
||
| 831 | } |
||
| 832 | } |
||
| 833 | |||
| 834 | //========================================================================== |
||
| 835 | // Plugins handling |
||
| 836 | //========================================================================== |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Disable a plugin |
||
| 840 | * |
||
| 841 | * @param string $pluginName Name of the plugin |
||
| 842 | * @return void |
||
| 843 | */ |
||
| 844 | 5 | public function disablePlugin($pluginName) |
|
| 845 | { |
||
| 846 | 5 | if (isset($this->pluginsConfig[$pluginName])) |
|
| 847 | { |
||
| 848 | // Copy the plugin's config to remove the reference |
||
| 849 | 4 | $pluginConfig = $this->pluginsConfig[$pluginName]; |
|
| 850 | 4 | unset($this->pluginsConfig[$pluginName]); |
|
| 851 | |||
| 852 | // Update the value and replace the plugin's config |
||
| 853 | 4 | $pluginConfig['isDisabled'] = true; |
|
| 854 | 4 | $this->pluginsConfig[$pluginName] = $pluginConfig; |
|
| 855 | } |
||
| 856 | } |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Enable a plugin |
||
| 860 | * |
||
| 861 | * @param string $pluginName Name of the plugin |
||
| 862 | * @return void |
||
| 863 | */ |
||
| 864 | 2 | public function enablePlugin($pluginName) |
|
| 865 | { |
||
| 866 | 2 | if (isset($this->pluginsConfig[$pluginName])) |
|
| 867 | { |
||
| 868 | 1 | $this->pluginsConfig[$pluginName]['isDisabled'] = false; |
|
| 869 | } |
||
| 870 | } |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Execute given plugin |
||
| 874 | * |
||
| 875 | * @param string $pluginName Plugin's name |
||
| 876 | * @return void |
||
| 877 | */ |
||
| 878 | 182 | protected function executePluginParser($pluginName) |
|
| 879 | { |
||
| 880 | 182 | $pluginConfig = $this->pluginsConfig[$pluginName]; |
|
| 881 | 182 | if (isset($pluginConfig['quickMatch']) && strpos($this->text, $pluginConfig['quickMatch']) === false) |
|
| 882 | { |
||
| 883 | 1 | return; |
|
| 884 | } |
||
| 885 | |||
| 886 | 181 | $matches = []; |
|
| 887 | 181 | if (isset($pluginConfig['regexp'])) |
|
| 888 | { |
||
| 889 | 8 | $matches = $this->getMatches($pluginConfig['regexp'], $pluginConfig['regexpLimit']); |
|
| 890 | 8 | if (empty($matches)) |
|
| 891 | { |
||
| 892 | 1 | return; |
|
| 893 | } |
||
| 894 | } |
||
| 895 | |||
| 896 | // Execute the plugin's parser, which will add tags via $this->addStartTag() and others |
||
| 897 | 180 | call_user_func($this->getPluginParser($pluginName), $this->text, $matches); |
|
| 898 | } |
||
| 899 | |||
| 900 | /** |
||
| 901 | * Execute all the plugins |
||
| 902 | * |
||
| 903 | * @return void |
||
| 904 | */ |
||
| 905 | 192 | protected function executePluginParsers() |
|
| 906 | { |
||
| 907 | 192 | foreach ($this->pluginsConfig as $pluginName => $pluginConfig) |
|
| 908 | { |
||
| 909 | 183 | if (empty($pluginConfig['isDisabled'])) |
|
| 910 | { |
||
| 911 | 182 | $this->executePluginParser($pluginName); |
|
| 912 | } |
||
| 913 | } |
||
| 914 | } |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Execute given regexp and returns as many matches as given limit |
||
| 918 | * |
||
| 919 | * @param string $regexp |
||
| 920 | * @param integer $limit |
||
| 921 | * @return array |
||
| 922 | */ |
||
| 923 | 8 | protected function getMatches($regexp, $limit) |
|
| 933 | |||
| 934 | /** |
||
| 935 | * Get the cached callback for given plugin's parser |
||
| 936 | * |
||
| 937 | * @param string $pluginName Plugin's name |
||
| 938 | * @return callable |
||
| 939 | */ |
||
| 940 | 180 | protected function getPluginParser($pluginName) |
|
| 956 | |||
| 957 | /** |
||
| 958 | * Register a parser |
||
| 959 | * |
||
| 960 | * Can be used to add a new parser with no plugin config, or pre-generate a parser for an |
||
| 961 | * existing plugin |
||
| 962 | * |
||
| 963 | * @param string $pluginName |
||
| 964 | * @param callback $parser |
||
| 965 | * @return void |
||
| 966 | */ |
||
| 967 | 175 | public function registerParser($pluginName, $parser, $regexp = null, $limit = PHP_INT_MAX) |
|
| 968 | { |
||
| 969 | 175 | if (!is_callable($parser)) |
|
| 970 | { |
||
| 971 | 1 | throw new InvalidArgumentException('Argument 1 passed to ' . __METHOD__ . ' must be a valid callback'); |
|
| 972 | } |
||
| 973 | // Create an empty config for this plugin to ensure it is executed |
||
| 974 | 174 | if (!isset($this->pluginsConfig[$pluginName])) |
|
| 975 | { |
||
| 976 | 173 | $this->pluginsConfig[$pluginName] = []; |
|
| 977 | } |
||
| 978 | 174 | if (isset($regexp)) |
|
| 979 | { |
||
| 980 | 2 | $this->pluginsConfig[$pluginName]['regexp'] = $regexp; |
|
| 981 | 2 | $this->pluginsConfig[$pluginName]['regexpLimit'] = $limit; |
|
| 982 | } |
||
| 983 | 174 | $this->pluginParsers[$pluginName] = $parser; |
|
| 984 | } |
||
| 985 | |||
| 986 | //========================================================================== |
||
| 987 | // Rules handling |
||
| 988 | //========================================================================== |
||
| 989 | |||
| 990 | /** |
||
| 991 | * Apply closeAncestor rules associated with given tag |
||
| 992 | * |
||
| 993 | * @param Tag $tag Tag |
||
| 994 | * @return bool Whether a new tag has been added |
||
| 995 | */ |
||
| 996 | 137 | protected function closeAncestor(Tag $tag) |
|
| 1030 | |||
| 1031 | /** |
||
| 1032 | * Apply closeParent rules associated with given tag |
||
| 1033 | * |
||
| 1034 | * @param Tag $tag Tag |
||
| 1035 | * @return bool Whether a new tag has been added |
||
| 1036 | */ |
||
| 1037 | 137 | protected function closeParent(Tag $tag) |
|
| 1066 | |||
| 1067 | /** |
||
| 1068 | * Apply the createChild rules associated with given tag |
||
| 1069 | * |
||
| 1070 | * @param Tag $tag Tag |
||
| 1071 | * @return void |
||
| 1072 | */ |
||
| 1073 | 138 | protected function createChild(Tag $tag) |
|
| 1074 | { |
||
| 1075 | 138 | $tagConfig = $this->tagsConfig[$tag->getName()]; |
|
| 1076 | 138 | if (isset($tagConfig['rules']['createChild'])) |
|
| 1077 | { |
||
| 1078 | 3 | $priority = -1000; |
|
| 1079 | 3 | $tagPos = $this->pos + strspn($this->text, " \n\r\t", $this->pos); |
|
| 1080 | 3 | foreach ($tagConfig['rules']['createChild'] as $tagName) |
|
| 1081 | { |
||
| 1082 | 3 | $this->addStartTag($tagName, $tagPos, 0, ++$priority); |
|
| 1083 | } |
||
| 1084 | } |
||
| 1085 | } |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Apply fosterParent rules associated with given tag |
||
| 1089 | * |
||
| 1090 | * NOTE: this rule has the potential for creating an unbounded loop, either if a tag tries to |
||
| 1091 | * foster itself or two or more tags try to foster each other in a loop. We mitigate the |
||
| 1092 | * risk by preventing a tag from creating a child of itself (the parent still gets closed) |
||
| 1093 | * and by checking and increasing the currentFixingCost so that a loop of multiple tags |
||
| 1094 | * do not run indefinitely. The default tagLimit and nestingLimit also serve to prevent the |
||
| 1095 | * loop from running indefinitely |
||
| 1096 | * |
||
| 1097 | * @param Tag $tag Tag |
||
| 1098 | * @return bool Whether a new tag has been added |
||
| 1099 | */ |
||
| 1100 | 137 | protected function fosterParent(Tag $tag) |
|
| 1136 | |||
| 1137 | /** |
||
| 1138 | * Apply requireAncestor rules associated with given tag |
||
| 1139 | * |
||
| 1140 | * @param Tag $tag Tag |
||
| 1141 | * @return bool Whether this tag has an unfulfilled requireAncestor requirement |
||
| 1142 | */ |
||
| 1143 | 140 | protected function requireAncestor(Tag $tag) |
|
| 1168 | |||
| 1169 | //========================================================================== |
||
| 1170 | // Tag processing |
||
| 1171 | //========================================================================== |
||
| 1172 | |||
| 1173 | /** |
||
| 1174 | * Create and add a copy of a tag as a child of a given tag |
||
| 1175 | * |
||
| 1176 | * @param Tag $tag Current tag |
||
| 1177 | * @param Tag $fosterTag Tag to foster |
||
| 1178 | * @return void |
||
| 1179 | */ |
||
| 1180 | 11 | protected function addFosterTag(Tag $tag, Tag $fosterTag) |
|
| 1181 | { |
||
| 1182 | 11 | list($childPos, $childPrio) = $this->getMagicStartCoords($tag->getPos() + $tag->getLen()); |
|
| 1183 | |||
| 1184 | // Add a 0-width copy of the parent tag after this tag and make it depend on this tag |
||
| 1185 | 11 | $childTag = $this->addCopyTag($fosterTag, $childPos, 0, $childPrio); |
|
| 1186 | 11 | $tag->cascadeInvalidationTo($childTag); |
|
| 1187 | } |
||
| 1188 | |||
| 1189 | /** |
||
| 1190 | * Create and add an end tag for given start tag at given position |
||
| 1191 | * |
||
| 1192 | * @param Tag $startTag Start tag |
||
| 1193 | * @param integer $tagPos End tag's position (will be adjusted for whitespace if applicable) |
||
| 1194 | * @param integer $prio End tag's priority |
||
| 1195 | * @return Tag |
||
| 1196 | */ |
||
| 1197 | 35 | protected function addMagicEndTag(Tag $startTag, $tagPos, $prio = 0) |
|
| 1213 | |||
| 1214 | /** |
||
| 1215 | * Compute the position of a magic end tag, adjusted for whitespace |
||
| 1216 | * |
||
| 1217 | * @param integer $tagPos Rightmost possible position for the tag |
||
| 1218 | * @return integer |
||
| 1219 | */ |
||
| 1220 | 5 | protected function getMagicEndPos($tagPos) |
|
| 1231 | |||
| 1232 | /** |
||
| 1233 | * Compute the position and priority of a magic start tag, adjusted for whitespace |
||
| 1234 | * |
||
| 1235 | * @param integer $tagPos Leftmost possible position for the tag |
||
| 1236 | * @return integer[] [Tag pos, priority] |
||
| 1237 | */ |
||
| 1238 | 11 | protected function getMagicStartCoords($tagPos) |
|
| 1264 | |||
| 1265 | /** |
||
| 1266 | * Test whether given start tag is immediately followed by a closing tag |
||
| 1267 | * |
||
| 1268 | * @param Tag $tag Start tag |
||
| 1269 | * @return bool |
||
| 1270 | */ |
||
| 1271 | 3 | protected function isFollowedByClosingTag(Tag $tag) |
|
| 1275 | |||
| 1276 | /** |
||
| 1277 | * Process all tags in the stack |
||
| 1278 | * |
||
| 1279 | * @return void |
||
| 1280 | */ |
||
| 1281 | 181 | protected function processTags() |
|
| 1282 | { |
||
| 1283 | 181 | if (empty($this->tagStack)) |
|
| 1284 | { |
||
| 1285 | 24 | return; |
|
| 1286 | } |
||
| 1287 | |||
| 1288 | // Initialize the count tables |
||
| 1289 | 157 | foreach (array_keys($this->tagsConfig) as $tagName) |
|
| 1290 | { |
||
| 1291 | 143 | $this->cntOpen[$tagName] = 0; |
|
| 1292 | 143 | $this->cntTotal[$tagName] = 0; |
|
| 1293 | } |
||
| 1294 | |||
| 1295 | // Process the tag stack, close tags that were left open and repeat until done |
||
| 1296 | do |
||
| 1297 | { |
||
| 1298 | 157 | while (!empty($this->tagStack)) |
|
| 1299 | { |
||
| 1320 | |||
| 1321 | /** |
||
| 1322 | * Process current tag |
||
| 1323 | * |
||
| 1324 | * @return void |
||
| 1325 | */ |
||
| 1326 | 157 | protected function processCurrentTag() |
|
| 1412 | |||
| 1413 | /** |
||
| 1414 | * Process given start tag (including self-closing tags) at current position |
||
| 1415 | * |
||
| 1416 | * @param Tag $tag Start tag (including self-closing) |
||
| 1417 | * @return void |
||
| 1418 | */ |
||
| 1419 | 140 | protected function processStartTag(Tag $tag) |
|
| 1533 | |||
| 1534 | /** |
||
| 1535 | * Process given end tag at current position |
||
| 1536 | * |
||
| 1537 | * @param Tag $tag end tag |
||
| 1538 | * @return void |
||
| 1539 | */ |
||
| 1540 | 105 | protected function processEndTag(Tag $tag) |
|
| 1698 | |||
| 1699 | /** |
||
| 1700 | * Update counters and replace current context with its parent context |
||
| 1701 | * |
||
| 1702 | * @return void |
||
| 1703 | */ |
||
| 1704 | 104 | protected function popContext() |
|
| 1710 | |||
| 1711 | /** |
||
| 1712 | * Update counters and replace current context with a new context based on given tag |
||
| 1713 | * |
||
| 1714 | * If given tag is a self-closing tag, the context won't change |
||
| 1715 | * |
||
| 1716 | * @param Tag $tag Start tag (including self-closing) |
||
| 1717 | * @return void |
||
| 1718 | */ |
||
| 1719 | 138 | protected function pushContext(Tag $tag) |
|
| 1768 | |||
| 1769 | /** |
||
| 1770 | * Return whether given tag is allowed in current context |
||
| 1771 | * |
||
| 1772 | * @param string $tagName |
||
| 1773 | * @return bool |
||
| 1774 | */ |
||
| 1775 | 140 | protected function tagIsAllowed($tagName) |
|
| 1781 | |||
| 1782 | //========================================================================== |
||
| 1783 | // Tag stack |
||
| 1784 | //========================================================================== |
||
| 1785 | |||
| 1786 | /** |
||
| 1787 | * Add a start tag |
||
| 1788 | * |
||
| 1789 | * @param string $name Name of the tag |
||
| 1790 | * @param integer $pos Position of the tag in the text |
||
| 1791 | * @param integer $len Length of text consumed by the tag |
||
| 1792 | * @param integer $prio Tag's priority |
||
| 1793 | * @return Tag |
||
| 1794 | */ |
||
| 1795 | 122 | public function addStartTag($name, $pos, $len, $prio = 0) |
|
| 1799 | |||
| 1800 | /** |
||
| 1801 | * Add an end tag |
||
| 1802 | * |
||
| 1803 | * @param string $name Name of the tag |
||
| 1804 | * @param integer $pos Position of the tag in the text |
||
| 1805 | * @param integer $len Length of text consumed by the tag |
||
| 1806 | * @param integer $prio Tag's priority |
||
| 1807 | * @return Tag |
||
| 1808 | */ |
||
| 1809 | 110 | public function addEndTag($name, $pos, $len, $prio = 0) |
|
| 1813 | |||
| 1814 | /** |
||
| 1815 | * Add a self-closing tag |
||
| 1816 | * |
||
| 1817 | * @param string $name Name of the tag |
||
| 1818 | * @param integer $pos Position of the tag in the text |
||
| 1819 | * @param integer $len Length of text consumed by the tag |
||
| 1820 | * @param integer $prio Tag's priority |
||
| 1821 | * @return Tag |
||
| 1822 | */ |
||
| 1823 | 68 | public function addSelfClosingTag($name, $pos, $len, $prio = 0) |
|
| 1827 | |||
| 1828 | /** |
||
| 1829 | * Add a 0-width "br" tag to force a line break at given position |
||
| 1830 | * |
||
| 1831 | * @param integer $pos Position of the tag in the text |
||
| 1832 | * @param integer $prio Tag's priority |
||
| 1833 | * @return Tag |
||
| 1834 | */ |
||
| 1835 | 9 | public function addBrTag($pos, $prio = 0) |
|
| 1839 | |||
| 1840 | /** |
||
| 1841 | * Add an "ignore" tag |
||
| 1842 | * |
||
| 1843 | * @param integer $pos Position of the tag in the text |
||
| 1844 | * @param integer $len Length of text consumed by the tag |
||
| 1845 | * @param integer $prio Tag's priority |
||
| 1846 | * @return Tag |
||
| 1847 | */ |
||
| 1848 | 12 | public function addIgnoreTag($pos, $len, $prio = 0) |
|
| 1852 | |||
| 1853 | /** |
||
| 1854 | * Add a paragraph break at given position |
||
| 1855 | * |
||
| 1856 | * Uses a zero-width tag that is actually never output in the result |
||
| 1857 | * |
||
| 1858 | * @param integer $pos Position of the tag in the text |
||
| 1859 | * @param integer $prio Tag's priority |
||
| 1860 | * @return Tag |
||
| 1861 | */ |
||
| 1862 | 5 | public function addParagraphBreak($pos, $prio = 0) |
|
| 1866 | |||
| 1867 | /** |
||
| 1868 | * Add a copy of given tag at given position and length |
||
| 1869 | * |
||
| 1870 | * @param Tag $tag Original tag |
||
| 1871 | * @param integer $pos Copy's position |
||
| 1872 | * @param integer $len Copy's length |
||
| 1873 | * @param integer $prio Copy's priority (same as original by default) |
||
| 1874 | * @return Tag Copy tag |
||
| 1875 | */ |
||
| 1876 | 22 | public function addCopyTag(Tag $tag, $pos, $len, $prio = null) |
|
| 1887 | |||
| 1888 | /** |
||
| 1889 | * Add a tag |
||
| 1890 | * |
||
| 1891 | * @param integer $type Tag's type |
||
| 1892 | * @param string $name Name of the tag |
||
| 1893 | * @param integer $pos Position of the tag in the text |
||
| 1894 | * @param integer $len Length of text consumed by the tag |
||
| 1895 | * @param integer $prio Tag's priority |
||
| 1896 | * @return Tag |
||
| 1897 | */ |
||
| 1898 | 185 | protected function addTag($type, $name, $pos, $len, $prio) |
|
| 1934 | |||
| 1935 | /** |
||
| 1936 | * Test whether given text span is outside text boundaries or an invalid UTF sequence |
||
| 1937 | * |
||
| 1938 | * @param integer $pos Start of text |
||
| 1939 | * @param integer $len Length of text |
||
| 1940 | * @return bool |
||
| 1941 | */ |
||
| 1942 | 183 | protected function isInvalidTextSpan($pos, $len) |
|
| 1946 | |||
| 1947 | /** |
||
| 1948 | * Insert given tag in the tag stack |
||
| 1949 | * |
||
| 1950 | * @param Tag $tag |
||
| 1951 | * @return void |
||
| 1952 | */ |
||
| 1953 | 176 | protected function insertTag(Tag $tag) |
|
| 1971 | |||
| 1972 | /** |
||
| 1973 | * Add a pair of tags |
||
| 1974 | * |
||
| 1975 | * @param string $name Name of the tags |
||
| 1976 | * @param integer $startPos Position of the start tag |
||
| 1977 | * @param integer $startLen Length of the start tag |
||
| 1978 | * @param integer $endPos Position of the start tag |
||
| 1979 | * @param integer $endLen Length of the start tag |
||
| 1980 | * @param integer $prio Start tag's priority (the end tag will be set to minus that value) |
||
| 1981 | * @return Tag Start tag |
||
| 1982 | */ |
||
| 1983 | 25 | public function addTagPair($name, $startPos, $startLen, $endPos, $endLen, $prio = 0) |
|
| 1992 | |||
| 1993 | /** |
||
| 1994 | * Add a tag that represents a verbatim copy of the original text |
||
| 1995 | * |
||
| 1996 | * @param integer $pos Position of the tag in the text |
||
| 1997 | * @param integer $len Length of text consumed by the tag |
||
| 1998 | * @param integer $prio Tag's priority |
||
| 1999 | * @return Tag |
||
| 2000 | */ |
||
| 2001 | 4 | public function addVerbatim($pos, $len, $prio = 0) |
|
| 2005 | |||
| 2006 | /** |
||
| 2007 | * Sort tags by position and precedence |
||
| 2008 | * |
||
| 2009 | * @return void |
||
| 2010 | */ |
||
| 2011 | 163 | protected function sortTags() |
|
| 2019 | |||
| 2020 | /** |
||
| 2021 | * Sort tags by position if none of them overlap |
||
| 2022 | * |
||
| 2023 | * @return bool Whether the tag stack is sorted |
||
| 2024 | */ |
||
| 2025 | 163 | protected function sortFast() |
|
| 2042 | |||
| 2043 | /** |
||
| 2044 | * sortTags() callback |
||
| 2045 | * |
||
| 2046 | * Tags are stored as a stack, in LIFO order. We sort tags by position _descending_ so that they |
||
| 2047 | * are processed in the order they appear in the text. |
||
| 2048 | * |
||
| 2049 | * @param Tag $a First tag to compare |
||
| 2050 | * @param Tag $b Second tag to compare |
||
| 2051 | * @return integer |
||
| 2052 | */ |
||
| 2053 | 57 | protected static function compareTags(Tag $a, Tag $b) |
|
| 2106 | } |