Skip to content

Commit daf63d9

Browse files
authored
Merge pull request #739 from PHPCSStandards/feature/generator-html-markdown-fix-whitespace-handling-code-titles
Generators HTML/Markdown: fix whitespace handling in code title
2 parents d77d651 + f2fedfb commit daf63d9

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
@@ -160,13 +160,15 @@ protected function printCodeComparisonBlock(DOMNode $node)
160160
{
161161
$codeBlocks = $node->getElementsByTagName('code');
162162

163-
$firstTitle = $codeBlocks->item(0)->getAttribute('title');
163+
$firstTitle = trim($codeBlocks->item(0)->getAttribute('title'));
164+
$firstTitle = str_replace(' ', '&nbsp;&nbsp;', $firstTitle);
164165
$first = trim($codeBlocks->item(0)->nodeValue);
165166
$first = str_replace("\n", PHP_EOL.' ', $first);
166167
$first = str_replace('<em>', '', $first);
167168
$first = str_replace('</em>', '', $first);
168169

169-
$secondTitle = $codeBlocks->item(1)->getAttribute('title');
170+
$secondTitle = trim($codeBlocks->item(1)->getAttribute('title'));
171+
$secondTitle = str_replace(' ', '&nbsp;&nbsp;', $secondTitle);
170172
$second = trim($codeBlocks->item(1)->nodeValue);
171173
$second = str_replace("\n", PHP_EOL.' ', $second);
172174
$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)