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

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.

diff --git a/tests/Core/Generators/Expectations/ExpectedOutputStandardEncoding.html b/tests/Core/Generators/Expectations/ExpectedOutputStandardEncoding.html index 5b1c8ca3c3..106e835a74 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputStandardEncoding.html +++ b/tests/Core/Generators/Expectations/ExpectedOutputStandardEncoding.html @@ -72,8 +72,8 @@

GeneratorTest Coding Standards

Standard Element, handling of HTML tags

-

The use of tags in standard descriptions is allowed and their handling should be safeguarded. - Other tags, like <a href="example.com">link</a>, <b>bold</bold>, <script></script> are not allowed and will be encoded for display when the HTML or Markdown report is used.

+

The use of tags in standard descriptions is allowed and their handling should be safeguarded.
+Other tags, like <a href="example.com">link</a>, <b>bold</bold>, <script></script> are not allowed and will be encoded for display when the HTML or Markdown report is used.

diff --git a/tests/Core/Generators/Expectations/ExpectedOutputStandardIndent.html b/tests/Core/Generators/Expectations/ExpectedOutputStandardIndent.html index 4c4bcb54ac..b3aad7f45c 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputStandardIndent.html +++ b/tests/Core/Generators/Expectations/ExpectedOutputStandardIndent.html @@ -72,10 +72,10 @@

GeneratorTest Coding Standards

Standard Element, indentation should be ignored

-

This line has no indentation. - This line has 4 spaces indentation. - This line has 8 spaces indentation. - This line has 4 spaces indentation.

+

This line has no indentation.
+This line has 4 spaces indentation.
+This line has 8 spaces indentation.
+This line has 4 spaces indentation.

diff --git a/tests/Core/Generators/Expectations/ExpectedOutputStandardLineWrapping.html b/tests/Core/Generators/Expectations/ExpectedOutputStandardLineWrapping.html index 8ca982efc1..d54d94c38f 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputStandardLineWrapping.html +++ b/tests/Core/Generators/Expectations/ExpectedOutputStandardLineWrapping.html @@ -72,9 +72,9 @@

GeneratorTest Coding Standards

Standard Element, line wrapping handling

-

This line has to be exactly 99 chars to test part of the logic.------------------------------------ - And this line has to be exactly 100 chars.---------------------------------------------------------- - 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.

+

This line has to be exactly 99 chars to test part of the logic.------------------------------------
+And this line has to be exactly 100 chars.----------------------------------------------------------
+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.

Valid: contains line which is too long.