Skip to content

Commit f2fedfb

Browse files
committed
Generators HTML/Markdown: fix whitespace handling in code title
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.
1 parent 941a00e commit f2fedfb

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

src/Generators/HTML.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,17 @@ protected function printCodeComparisonBlock(DOMNode $node)
281281
{
282282
$codeBlocks = $node->getElementsByTagName('code');
283283

284-
$firstTitle = $codeBlocks->item(0)->getAttribute('title');
284+
$firstTitle = trim($codeBlocks->item(0)->getAttribute('title'));
285+
$firstTitle = str_replace(' ', '  ', $firstTitle);
285286
$first = trim($codeBlocks->item(0)->nodeValue);
286287
$first = str_replace('<?php', '&lt;?php', $first);
287288
$first = str_replace("\n", '</br>', $first);
288289
$first = str_replace(' ', '&nbsp;', $first);
289290
$first = str_replace('<em>', '<span class="code-comparison-highlight">', $first);
290291
$first = str_replace('</em>', '</span>', $first);
291292

292-
$secondTitle = $codeBlocks->item(1)->getAttribute('title');
293+
$secondTitle = trim($codeBlocks->item(1)->getAttribute('title'));
294+
$secondTitle = str_replace(' ', '&nbsp;&nbsp;', $secondTitle);
293295
$second = trim($codeBlocks->item(1)->nodeValue);
294296
$second = str_replace('<?php', '&lt;?php', $second);
295297
$second = str_replace("\n", '</br>', $second);

src/Generators/Markdown.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,15 @@ protected function printCodeComparisonBlock(DOMNode $node)
140140
{
141141
$codeBlocks = $node->getElementsByTagName('code');
142142

143-
$firstTitle = $codeBlocks->item(0)->getAttribute('title');
143+
$firstTitle = trim($codeBlocks->item(0)->getAttribute('title'));
144+
$firstTitle = str_replace(' ', '&nbsp;&nbsp;', $firstTitle);
144145
$first = trim($codeBlocks->item(0)->nodeValue);
145146
$first = str_replace("\n", PHP_EOL.' ', $first);
146147
$first = str_replace('<em>', '', $first);
147148
$first = str_replace('</em>', '', $first);
148149

149-
$secondTitle = $codeBlocks->item(1)->getAttribute('title');
150+
$secondTitle = trim($codeBlocks->item(1)->getAttribute('title'));
151+
$secondTitle = str_replace(' ', '&nbsp;&nbsp;', $secondTitle);
150152
$second = trim($codeBlocks->item(1)->nodeValue);
151153
$second = str_replace("\n", PHP_EOL.' ', $second);
152154
$second = str_replace('<em>', '', $second);

tests/Core/Generators/Expectations/ExpectedOutputCodeTitleWhitespace.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ <h2>Code Title, whitespace handling</h2>
7575
<p class="text">This is a standard block.</p>
7676
<table class="code-comparison">
7777
<tr>
78-
<td class="code-comparison-title"> Valid: spaces at start of description.</td>
79-
<td class="code-comparison-title">Invalid: spaces at end making line > 46 chars. </td>
78+
<td class="code-comparison-title">Valid: spaces at start of description.</td>
79+
<td class="code-comparison-title">Invalid: spaces at end making line > 46 chars.</td>
8080
</tr>
8181
<tr>
8282
<td class="code-comparison-code">//&nbsp;Dummy.</td>
@@ -85,8 +85,8 @@ <h2>Code Title, whitespace handling</h2>
8585
</table>
8686
<table class="code-comparison">
8787
<tr>
88-
<td class="code-comparison-title"> Valid: spaces at start + end of description. </td>
89-
<td class="code-comparison-title">Invalid: spaces ' ' in description.</td>
88+
<td class="code-comparison-title">Valid: spaces at start + end of description.</td>
89+
<td class="code-comparison-title">Invalid: spaces '&nbsp;&nbsp;&nbsp;&nbsp; ' in description.</td>
9090
</tr>
9191
<tr>
9292
<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>

tests/Core/Generators/Expectations/ExpectedOutputCodeTitleWhitespace.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
This is a standard block.
66
<table>
77
<tr>
8-
<th> Valid: spaces at start of description.</th>
9-
<th>Invalid: spaces at end making line > 46 chars. </th>
8+
<th>Valid: spaces at start of description.</th>
9+
<th>Invalid: spaces at end making line > 46 chars.</th>
1010
</tr>
1111
<tr>
1212
<td>
@@ -23,8 +23,8 @@ This is a standard block.
2323
</table>
2424
<table>
2525
<tr>
26-
<th> Valid: spaces at start + end of description. </th>
27-
<th>Invalid: spaces ' ' in description.</th>
26+
<th>Valid: spaces at start + end of description.</th>
27+
<th>Invalid: spaces '&nbsp;&nbsp;&nbsp;&nbsp; ' in description.</th>
2828
</tr>
2929
<tr>
3030
<td>

0 commit comments

Comments
 (0)