|
16 | 16 | namespace PHP_CodeSniffer\Generators; |
17 | 17 |
|
18 | 18 | use DOMDocument; |
| 19 | +use DOMElement; |
19 | 20 | use DOMNode; |
20 | 21 | use PHP_CodeSniffer\Config; |
21 | 22 |
|
@@ -392,23 +393,11 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node) |
392 | 393 | return ''; |
393 | 394 | } |
394 | 395 |
|
395 | | - $firstTitle = trim($firstCodeElm->getAttribute('title')); |
396 | | - $firstTitle = str_replace(' ', ' ', $firstTitle); |
397 | | - $first = trim($firstCodeElm->nodeValue); |
398 | | - $first = str_replace('<?php', '<?php', $first); |
399 | | - $first = str_replace("\n", '</br>', $first); |
400 | | - $first = str_replace(' ', ' ', $first); |
401 | | - $first = str_replace('<em>', '<span class="code-comparison-highlight">', $first); |
402 | | - $first = str_replace('</em>', '</span>', $first); |
403 | | - |
404 | | - $secondTitle = trim($secondCodeElm->getAttribute('title')); |
405 | | - $secondTitle = str_replace(' ', ' ', $secondTitle); |
406 | | - $second = trim($secondCodeElm->nodeValue); |
407 | | - $second = str_replace('<?php', '<?php', $second); |
408 | | - $second = str_replace("\n", '</br>', $second); |
409 | | - $second = str_replace(' ', ' ', $second); |
410 | | - $second = str_replace('<em>', '<span class="code-comparison-highlight">', $second); |
411 | | - $second = str_replace('</em>', '</span>', $second); |
| 396 | + $firstTitle = $this->formatCodeTitle($firstCodeElm); |
| 397 | + $first = $this->formatCodeSample($firstCodeElm); |
| 398 | + |
| 399 | + $secondTitle = $this->formatCodeTitle($secondCodeElm); |
| 400 | + $second = $this->formatCodeSample($secondCodeElm); |
412 | 401 |
|
413 | 402 | $titleRow = ''; |
414 | 403 | if ($firstTitle !== '' || $secondTitle !== '') { |
@@ -439,4 +428,43 @@ protected function getFormattedCodeComparisonBlock(DOMNode $node) |
439 | 428 | }//end getFormattedCodeComparisonBlock() |
440 | 429 |
|
441 | 430 |
|
| 431 | + /** |
| 432 | + * Retrieve a code block title and prepare it for output as HTML. |
| 433 | + * |
| 434 | + * @param \DOMElement $codeElm The DOMElement object for a code block. |
| 435 | + * |
| 436 | + * @since 3.12.0 |
| 437 | + * |
| 438 | + * @return string |
| 439 | + */ |
| 440 | + private function formatCodeTitle(DOMElement $codeElm) |
| 441 | + { |
| 442 | + $title = trim($codeElm->getAttribute('title')); |
| 443 | + return str_replace(' ', ' ', $title); |
| 444 | + |
| 445 | + }//end formatCodeTitle() |
| 446 | + |
| 447 | + |
| 448 | + /** |
| 449 | + * Retrieve a code block contents and prepare it for output as HTML. |
| 450 | + * |
| 451 | + * @param \DOMElement $codeElm The DOMElement object for a code block. |
| 452 | + * |
| 453 | + * @since 3.12.0 |
| 454 | + * |
| 455 | + * @return string |
| 456 | + */ |
| 457 | + private function formatCodeSample(DOMElement $codeElm) |
| 458 | + { |
| 459 | + $code = (string) $codeElm->nodeValue; |
| 460 | + $code = trim($code); |
| 461 | + $code = str_replace('<?php', '<?php', $code); |
| 462 | + $code = str_replace(["\n", ' '], ['</br>', ' '], $code); |
| 463 | + $code = str_replace(['<em>', '</em>'], ['<span class="code-comparison-highlight">', '</span>'], $code); |
| 464 | + |
| 465 | + return $code; |
| 466 | + |
| 467 | + }//end formatCodeSample() |
| 468 | + |
| 469 | + |
442 | 470 | }//end class |
0 commit comments