From 6389459b1e2017a40990f0cc5e30033df3f0891a Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Wed, 6 May 2020 17:36:47 +0200 Subject: [PATCH] Refactor newline logic for auto-generated sections This change does have two effects: 1. Instead of prepending a newline individually in every possible code path, we do it one time at the end. But only if there is something in the output. This does not change anything, as proven by the unchanged parser tests. 2. I removed the newline between the

and the generated element. Note that both these elements are created in the same method, next to each other. So there is no way this can influence other wikitext. Unfortunately this code path is executed only when using the *preview* function, and impossible to be covered by parser tests because of this. However, it's covered by unit tests. This refactoring is motivated by, but not required for T148701. Bug: T148701 Change-Id: I6691c70f8e3fa3f21e2d11035bed9cdc2dc87093 --- src/Cite.php | 8 ++++---- tests/phpunit/CiteIntegrationTest.php | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Cite.php b/src/Cite.php index 7ad3d62e1..2462a0404 100644 --- a/src/Cite.php +++ b/src/Cite.php @@ -520,9 +520,9 @@ class Cite { $s = ''; foreach ( $this->referenceStack->getGroups() as $group ) { if ( $group === self::DEFAULT_GROUP || $isSectionPreview ) { - $s .= "\n" . $this->formatReferences( $parser, $group ); + $s .= $this->formatReferences( $parser, $group ); } else { - $s .= "\n
" . $this->errorReporter->halfParsed( + $s .= '
' . $this->errorReporter->halfParsed( $parser, 'cite_error_group_refs_without_references', Sanitizer::safeEncodeAttribute( $group ) @@ -539,13 +539,13 @@ class Cite { ) . $s; } // provide a preview of references in its own section - $s = "\n" . Html::rawElement( + $s = Html::rawElement( 'div', [ 'class' => 'mw-ext-cite-cite_section_preview_references' ], $s ); } - return $s; + return $s !== '' ? "\n" . $s : ''; } /** diff --git a/tests/phpunit/CiteIntegrationTest.php b/tests/phpunit/CiteIntegrationTest.php index be83c0c9a..11d6ba80d 100644 --- a/tests/phpunit/CiteIntegrationTest.php +++ b/tests/phpunit/CiteIntegrationTest.php @@ -74,7 +74,7 @@ class CiteIntegrationTest extends \MediaWikiIntegrationTestCase { ] ], false, - "\n" . '' + "\n" ], 'Default group in preview' => [ [ @@ -87,7 +87,7 @@ class CiteIntegrationTest extends \MediaWikiIntegrationTestCase { true, "\n" . '
' . '

' . - '(cite_section_preview_references)

' . "\n" . '
' + '(cite_section_preview_references)

' ], 'Named group' => [ [ @@ -111,7 +111,7 @@ class CiteIntegrationTest extends \MediaWikiIntegrationTestCase { true, "\n" . '
' . '

' . - '(cite_section_preview_references)

' . "\n" . '
' + '(cite_section_preview_references)' ] ]; }