From f2fedfbb0f6c6556583f36767d480b06efed762d Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 3 Nov 2024 23:23:07 +0100 Subject: [PATCH] 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. --- src/Generators/HTML.php | 6 ++++-- src/Generators/Markdown.php | 6 ++++-- .../Expectations/ExpectedOutputCodeTitleWhitespace.html | 8 ++++---- .../Expectations/ExpectedOutputCodeTitleWhitespace.md | 8 ++++---- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Generators/HTML.php b/src/Generators/HTML.php index 8e2001e2e5..ba05d072df 100644 --- a/src/Generators/HTML.php +++ b/src/Generators/HTML.php @@ -281,7 +281,8 @@ protected function printCodeComparisonBlock(DOMNode $node) { $codeBlocks = $node->getElementsByTagName('code'); - $firstTitle = $codeBlocks->item(0)->getAttribute('title'); + $firstTitle = trim($codeBlocks->item(0)->getAttribute('title')); + $firstTitle = str_replace(' ', '  ', $firstTitle); $first = trim($codeBlocks->item(0)->nodeValue); $first = str_replace('', $first); @@ -289,7 +290,8 @@ protected function printCodeComparisonBlock(DOMNode $node) $first = str_replace('', '', $first); $first = str_replace('', '', $first); - $secondTitle = $codeBlocks->item(1)->getAttribute('title'); + $secondTitle = trim($codeBlocks->item(1)->getAttribute('title')); + $secondTitle = str_replace(' ', '  ', $secondTitle); $second = trim($codeBlocks->item(1)->nodeValue); $second = str_replace('', $second); diff --git a/src/Generators/Markdown.php b/src/Generators/Markdown.php index 76dbb0605f..77807b55e0 100644 --- a/src/Generators/Markdown.php +++ b/src/Generators/Markdown.php @@ -140,13 +140,15 @@ protected function printCodeComparisonBlock(DOMNode $node) { $codeBlocks = $node->getElementsByTagName('code'); - $firstTitle = $codeBlocks->item(0)->getAttribute('title'); + $firstTitle = trim($codeBlocks->item(0)->getAttribute('title')); + $firstTitle = str_replace(' ', '  ', $firstTitle); $first = trim($codeBlocks->item(0)->nodeValue); $first = str_replace("\n", PHP_EOL.' ', $first); $first = str_replace('', '', $first); $first = str_replace('', '', $first); - $secondTitle = $codeBlocks->item(1)->getAttribute('title'); + $secondTitle = trim($codeBlocks->item(1)->getAttribute('title')); + $secondTitle = str_replace(' ', '  ', $secondTitle); $second = trim($codeBlocks->item(1)->nodeValue); $second = str_replace("\n", PHP_EOL.' ', $second); $second = str_replace('', '', $second); diff --git a/tests/Core/Generators/Expectations/ExpectedOutputCodeTitleWhitespace.html b/tests/Core/Generators/Expectations/ExpectedOutputCodeTitleWhitespace.html index a64cdb4deb..fe1c15bb47 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputCodeTitleWhitespace.html +++ b/tests/Core/Generators/Expectations/ExpectedOutputCodeTitleWhitespace.html @@ -75,8 +75,8 @@

Code Title, whitespace handling

This is a standard block.

- - + + @@ -85,8 +85,8 @@

Code Title, whitespace handling

Valid: spaces at start of description.Invalid: spaces at end making line > 46 chars. Valid: spaces at start of description.Invalid: spaces at end making line > 46 chars.
// Dummy.
- - + + diff --git a/tests/Core/Generators/Expectations/ExpectedOutputCodeTitleWhitespace.md b/tests/Core/Generators/Expectations/ExpectedOutputCodeTitleWhitespace.md index 2d04562c7e..e623ba98dd 100644 --- a/tests/Core/Generators/Expectations/ExpectedOutputCodeTitleWhitespace.md +++ b/tests/Core/Generators/Expectations/ExpectedOutputCodeTitleWhitespace.md @@ -5,8 +5,8 @@ This is a standard block.
Valid: spaces at start + end of description. Invalid: spaces ' ' in description.Valid: spaces at start + end of description.Invalid: spaces '     ' in description.
// Note: description above without the
// trailing whitespace fits in 46 chars.
- - + +
Valid: spaces at start of description.Invalid: spaces at end making line > 46 chars. Valid: spaces at start of description.Invalid: spaces at end making line > 46 chars.
@@ -23,8 +23,8 @@ This is a standard block.
- - + +
Valid: spaces at start + end of description. Invalid: spaces ' ' in description.Valid: spaces at start + end of description.Invalid: spaces '     ' in description.