Skip to content

Commit becac94

Browse files
authored
Merge pull request #723 from PHPCSStandards/feature/generators-html-fix-line-break-handling
Generators/HTML: fix line break handling
2 parents 205425b + baec97e commit becac94

File tree

6 files changed

+40
-21
lines changed

6 files changed

+40
-21
lines changed

src/Generators/HTML.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,35 @@ protected function printTextBlock(DOMNode $node)
237237
$content = trim($node->nodeValue);
238238
$content = htmlspecialchars($content, (ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401));
239239

240-
// Use the correct line endings based on the OS.
241-
$content = str_replace("\n", PHP_EOL, $content);
242-
243-
// Allow em tags only.
240+
// Allow only em tags.
244241
$content = str_replace('&lt;em&gt;', '<em>', $content);
245242
$content = str_replace('&lt;/em&gt;', '</em>', $content);
246243

247-
echo " <p class=\"text\">$content</p>".PHP_EOL;
244+
$nodeLines = explode("\n", $content);
245+
$lineCount = count($nodeLines);
246+
$lines = [];
247+
248+
for ($i = 0; $i < $lineCount; $i++) {
249+
$currentLine = trim($nodeLines[$i]);
250+
251+
if (isset($nodeLines[($i + 1)]) === false) {
252+
// We're at the end of the text, just add the line.
253+
$lines[] = $currentLine;
254+
} else {
255+
$nextLine = trim($nodeLines[($i + 1)]);
256+
if ($nextLine === '') {
257+
// Next line is a blank line, end the paragraph and start a new one.
258+
// Also skip over the blank line.
259+
$lines[] = $currentLine.'</p>'.PHP_EOL.' <p class="text">';
260+
++$i;
261+
} else {
262+
// Next line is not blank, so just add a line break.
263+
$lines[] = $currentLine.'<br/>'.PHP_EOL;
264+
}
265+
}
266+
}
267+
268+
echo ' <p class="text">'.implode('', $lines).'</p>'.PHP_EOL;
248269

249270
}//end printTextBlock()
250271

tests/Core/Generators/Expectations/ExpectedOutputCodeComparisonLineLength.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@
7272
<h1>GeneratorTest Coding Standards</h1>
7373
<a name="Code-Comparison,-line-length" />
7474
<h2>Code Comparison, line length</h2>
75-
<p class="text">Ensure there is no PHP &quot;Warning: str_repeat(): Second argument has to be greater than or equal to 0&quot;.
76-
Ref: squizlabs/PHP_CodeSniffer#2522</p>
75+
<p class="text">Ensure there is no PHP &quot;Warning: str_repeat(): Second argument has to be greater than or equal to 0&quot;.<br/>
76+
Ref: squizlabs/PHP_CodeSniffer#2522</p>
7777
<table class="code-comparison">
7878
<tr>
7979
<td class="code-comparison-title">Valid: contains line which is too long.</td>

tests/Core/Generators/Expectations/ExpectedOutputStandardBlankLines.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,9 @@
7272
<h1>GeneratorTest Coding Standards</h1>
7373
<a name="Standard-Element,-blank-line-handling" />
7474
<h2>Standard Element, blank line handling</h2>
75-
<p class="text">There is a blank line at the start of this standard.
76-
77-
And the above blank line is also deliberate to test part of the logic.
78-
79-
Let&#039;s also end on a blank line to test that too.</p>
75+
<p class="text">There is a blank line at the start of this standard.</p>
76+
<p class="text">And the above blank line is also deliberate to test part of the logic.</p>
77+
<p class="text">Let&#039;s also end on a blank line to test that too.</p>
8078
<div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
8179
</body>
8280
</html>

tests/Core/Generators/Expectations/ExpectedOutputStandardEncoding.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@
7272
<h1>GeneratorTest Coding Standards</h1>
7373
<a name="Standard-Element,-handling-of-HTML-tags" />
7474
<h2>Standard Element, handling of HTML tags</h2>
75-
<p class="text">The use of <em>tags</em> in standard descriptions is allowed and their handling should be <em>safeguarded</em>.
76-
Other tags, like &lt;a href=&quot;example.com&quot;&gt;link&lt;/a&gt;, &lt;b&gt;bold&lt;/bold&gt;, &lt;script&gt;&lt;/script&gt; are not allowed and will be encoded for display when the HTML or Markdown report is used.</p>
75+
<p class="text">The use of <em>tags</em> in standard descriptions is allowed and their handling should be <em>safeguarded</em>.<br/>
76+
Other tags, like &lt;a href=&quot;example.com&quot;&gt;link&lt;/a&gt;, &lt;b&gt;bold&lt;/bold&gt;, &lt;script&gt;&lt;/script&gt; are not allowed and will be encoded for display when the HTML or Markdown report is used.</p>
7777
<div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
7878
</body>
7979
</html>

tests/Core/Generators/Expectations/ExpectedOutputStandardIndent.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@
7272
<h1>GeneratorTest Coding Standards</h1>
7373
<a name="Standard-Element,-indentation-should-be-ignored" />
7474
<h2>Standard Element, indentation should be ignored</h2>
75-
<p class="text">This line has no indentation.
76-
This line has 4 spaces indentation.
77-
This line has 8 spaces indentation.
78-
This line has 4 spaces indentation.</p>
75+
<p class="text">This line has no indentation.<br/>
76+
This line has 4 spaces indentation.<br/>
77+
This line has 8 spaces indentation.<br/>
78+
This line has 4 spaces indentation.</p>
7979
<div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
8080
</body>
8181
</html>

tests/Core/Generators/Expectations/ExpectedOutputStandardLineWrapping.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@
7272
<h1>GeneratorTest Coding Standards</h1>
7373
<a name="Standard-Element,-line-wrapping-handling" />
7474
<h2>Standard Element, line wrapping handling</h2>
75-
<p class="text">This line has to be exactly 99 chars to test part of the logic.------------------------------------
76-
And this line has to be exactly 100 chars.----------------------------------------------------------
77-
And here we have a line which should start wrapping as it is longer than 100 chars. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean pellentesque iaculis enim quis hendrerit. Morbi ultrices in odio pharetra commodo.</p>
75+
<p class="text">This line has to be exactly 99 chars to test part of the logic.------------------------------------<br/>
76+
And this line has to be exactly 100 chars.----------------------------------------------------------<br/>
77+
And here we have a line which should start wrapping as it is longer than 100 chars. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean pellentesque iaculis enim quis hendrerit. Morbi ultrices in odio pharetra commodo.</p>
7878
<div class="tag-line">Documentation generated on #REDACTED# by <a href="https://github.com/PHPCSStandards/PHP_CodeSniffer">PHP_CodeSniffer #VERSION#</a></div>
7979
</body>
8080
</html>

0 commit comments

Comments
 (0)