Skip to content

Commit

Permalink
Generators HTML/Markdown: fix whitespace handling in code title
Browse files Browse the repository at this point in the history
If there were multiple spaces next to each other in the title, this would be folded into one space for the display in HTML and Markdown.

This could mangle the code title, so fixed now.

Includes updated test expectations.
  • Loading branch information
jrfnl committed Dec 1, 2024
1 parent 96187ca commit 164657d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/Generators/HTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,17 @@ protected function printCodeComparisonBlock(DOMNode $node)
{
$codeBlocks = $node->getElementsByTagName('code');

$firstTitle = $codeBlocks->item(0)->getAttribute('title');
$firstTitle = trim($codeBlocks->item(0)->getAttribute('title'));
$firstTitle = str_replace(' ', '  ', $firstTitle);
$first = trim($codeBlocks->item(0)->nodeValue);
$first = str_replace('<?php', '&lt;?php', $first);
$first = str_replace("\n", '</br>', $first);
$first = str_replace(' ', '&nbsp;', $first);
$first = str_replace('<em>', '<span class="code-comparison-highlight">', $first);
$first = str_replace('</em>', '</span>', $first);

$secondTitle = $codeBlocks->item(1)->getAttribute('title');
$secondTitle = trim($codeBlocks->item(1)->getAttribute('title'));
$secondTitle = str_replace(' ', '&nbsp;&nbsp;', $secondTitle);
$second = trim($codeBlocks->item(1)->nodeValue);
$second = str_replace('<?php', '&lt;?php', $second);
$second = str_replace("\n", '</br>', $second);
Expand Down
6 changes: 4 additions & 2 deletions src/Generators/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@ protected function printCodeComparisonBlock(DOMNode $node)
{
$codeBlocks = $node->getElementsByTagName('code');

$firstTitle = $codeBlocks->item(0)->getAttribute('title');
$firstTitle = trim($codeBlocks->item(0)->getAttribute('title'));
$firstTitle = str_replace(' ', '&nbsp;&nbsp;', $firstTitle);
$first = trim($codeBlocks->item(0)->nodeValue);
$first = str_replace("\n", PHP_EOL.' ', $first);
$first = str_replace('<em>', '', $first);
$first = str_replace('</em>', '', $first);

$secondTitle = $codeBlocks->item(1)->getAttribute('title');
$secondTitle = trim($codeBlocks->item(1)->getAttribute('title'));
$secondTitle = str_replace(' ', '&nbsp;&nbsp;', $secondTitle);
$second = trim($codeBlocks->item(1)->nodeValue);
$second = str_replace("\n", PHP_EOL.' ', $second);
$second = str_replace('<em>', '', $second);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ <h2>Code Title, whitespace handling</h2>
<p class="text">This is a standard block.</p>
<table class="code-comparison">
<tr>
<td class="code-comparison-title"> Valid: spaces at start of description.</td>
<td class="code-comparison-title">Invalid: spaces at end making line > 46 chars. </td>
<td class="code-comparison-title">Valid: spaces at start of description.</td>
<td class="code-comparison-title">Invalid: spaces at end making line > 46 chars.</td>
</tr>
<tr>
<td class="code-comparison-code">//&nbsp;Dummy.</td>
Expand All @@ -85,8 +85,8 @@ <h2>Code Title, whitespace handling</h2>
</table>
<table class="code-comparison">
<tr>
<td class="code-comparison-title"> Valid: spaces at start + end of description. </td>
<td class="code-comparison-title">Invalid: spaces ' ' in description.</td>
<td class="code-comparison-title">Valid: spaces at start + end of description.</td>
<td class="code-comparison-title">Invalid: spaces '&nbsp;&nbsp;&nbsp;&nbsp; ' in description.</td>
</tr>
<tr>
<td class="code-comparison-code">//&nbsp;Note:&nbsp;description&nbsp;above&nbsp;without&nbsp;the</br>//&nbsp;trailing&nbsp;whitespace&nbsp;fits&nbsp;in&nbsp;46&nbsp;chars.</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
This is a standard block.
<table>
<tr>
<th> Valid: spaces at start of description.</th>
<th>Invalid: spaces at end making line > 46 chars. </th>
<th>Valid: spaces at start of description.</th>
<th>Invalid: spaces at end making line > 46 chars.</th>
</tr>
<tr>
<td>
Expand All @@ -23,8 +23,8 @@ This is a standard block.
</table>
<table>
<tr>
<th> Valid: spaces at start + end of description. </th>
<th>Invalid: spaces ' ' in description.</th>
<th>Valid: spaces at start + end of description.</th>
<th>Invalid: spaces '&nbsp;&nbsp;&nbsp;&nbsp; ' in description.</th>
</tr>
<tr>
<td>
Expand Down

0 comments on commit 164657d

Please sign in to comment.