From baec97ea62e91f51d881636ca2f73e368855935b Mon Sep 17 00:00:00 2001
From: jrfnl
Date: Sun, 3 Nov 2024 19:20:44 +0100
Subject: [PATCH] Generators/HTML: fix line break handling
As things were, line breaks in a `` block were not respected for proper display in HTML.
This has now been fixed by:
* Recognizing a blank line after a text line as an indicator that the next line should be a new paragraph.
* In all other cases, a line break will be translated to an HTML line break.
Includes updated test expectations.
---
src/Generators/HTML.php | 31 ++++++++++++++++---
...xpectedOutputCodeComparisonLineLength.html | 4 +--
.../ExpectedOutputStandardBlankLines.html | 8 ++---
.../ExpectedOutputStandardEncoding.html | 4 +--
.../ExpectedOutputStandardIndent.html | 8 ++---
.../ExpectedOutputStandardLineWrapping.html | 6 ++--
6 files changed, 40 insertions(+), 21 deletions(-)
diff --git a/src/Generators/HTML.php b/src/Generators/HTML.php
index 0ca4496963..8e2001e2e5 100644
--- a/src/Generators/HTML.php
+++ b/src/Generators/HTML.php
@@ -237,14 +237,35 @@ protected function printTextBlock(DOMNode $node)
$content = trim($node->nodeValue);
$content = htmlspecialchars($content, (ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401));
- // Use the correct line endings based on the OS.
- $content = str_replace("\n", PHP_EOL, $content);
-
- // Allow em tags only.
+ // Allow only em tags.
$content = str_replace('<em>', '', $content);
$content = str_replace('</em>', '', $content);
- echo " $content
".PHP_EOL;
+ $nodeLines = explode("\n", $content);
+ $lineCount = count($nodeLines);
+ $lines = [];
+
+ for ($i = 0; $i < $lineCount; $i++) {
+ $currentLine = trim($nodeLines[$i]);
+
+ if (isset($nodeLines[($i + 1)]) === false) {
+ // We're at the end of the text, just add the line.
+ $lines[] = $currentLine;
+ } else {
+ $nextLine = trim($nodeLines[($i + 1)]);
+ if ($nextLine === '') {
+ // Next line is a blank line, end the paragraph and start a new one.
+ // Also skip over the blank line.
+ $lines[] = $currentLine.'
'.PHP_EOL.' ';
+ ++$i;
+ } else {
+ // Next line is not blank, so just add a line break.
+ $lines[] = $currentLine.'
'.PHP_EOL;
+ }
+ }
+ }
+
+ echo '
'.implode('', $lines).'
'.PHP_EOL;
}//end printTextBlock()
diff --git a/tests/Core/Generators/Expectations/ExpectedOutputCodeComparisonLineLength.html b/tests/Core/Generators/Expectations/ExpectedOutputCodeComparisonLineLength.html
index 2f7abc69a4..b876e6b1e9 100644
--- a/tests/Core/Generators/Expectations/ExpectedOutputCodeComparisonLineLength.html
+++ b/tests/Core/Generators/Expectations/ExpectedOutputCodeComparisonLineLength.html
@@ -72,8 +72,8 @@
GeneratorTest Coding Standards
Code Comparison, line length
- Ensure there is no PHP "Warning: str_repeat(): Second argument has to be greater than or equal to 0".
- Ref: squizlabs/PHP_CodeSniffer#2522
+ Ensure there is no PHP "Warning: str_repeat(): Second argument has to be greater than or equal to 0".
+Ref: squizlabs/PHP_CodeSniffer#2522
Valid: contains line which is too long. |
diff --git a/tests/Core/Generators/Expectations/ExpectedOutputStandardBlankLines.html b/tests/Core/Generators/Expectations/ExpectedOutputStandardBlankLines.html
index 5c6539d0ca..4c45757b57 100644
--- a/tests/Core/Generators/Expectations/ExpectedOutputStandardBlankLines.html
+++ b/tests/Core/Generators/Expectations/ExpectedOutputStandardBlankLines.html
@@ -72,11 +72,9 @@
GeneratorTest Coding Standards
Standard Element, blank line handling
- There is a blank line at the start of this standard.
-
- And the above blank line is also deliberate to test part of the logic.
-
- Let's also end on a blank line to test that too.
+ There is a blank line at the start of this standard.
+ And the above blank line is also deliberate to test part of the logic.
+ Let's also end on a blank line to test that too.