From d86998ddf9ab072d3d70afa5fe6f882ee2621348 Mon Sep 17 00:00:00 2001 From: JakeQZ Date: Thu, 6 Feb 2025 15:57:25 +0000 Subject: [PATCH 1/2] [CLEANUP] Add separate `OutputFormat` properties for arrays `SpaceBeforeListArgumentSeparators` and `SpaceAfterListArgumentSeparators` are added to allow for different spacing for different separators. `SpaceBeforeListArgumentSeparator` and `SpaceAfterListArgumentSeparator` will specify the default spacing. Setting these as an array is deprecated. This is the backport of #880. --- CHANGELOG.md | 3 +++ src/OutputFormat.php | 30 ++++++++++++++++++++++++++---- src/OutputFormatter.php | 8 ++++++-- tests/OutputFormatTest.php | 27 ++++++++++++++++++++++++++- 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d7ac5d5..6a32053c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,10 +7,13 @@ This project adheres to [Semantic Versioning](https://semver.org/). ### Added +- `OutputFormat` properties for space around specific list separators (#880) + ### Changed ### Deprecated +- `OutputFormat` properties for space around list separators as an array (#880) - Deprecate `OutputFormat::level()` (#870) ### Removed diff --git a/src/OutputFormat.php b/src/OutputFormat.php index 2ce257fb..366fca3f 100644 --- a/src/OutputFormat.php +++ b/src/OutputFormat.php @@ -96,17 +96,39 @@ class OutputFormat public $sSpaceAfterSelectorSeparator = ' '; /** - * This is what’s printed after the comma of value lists + * This is what’s inserted before the separator in value lists, by default. * - * @var string + * `array` is deprecated in version 8.8.0, and will be removed in version 9.0.0. + * To set the spacing for specific separators, use {@see $aSpaceBeforeListArgumentSeparators} instead. + * + * @var string|array */ public $sSpaceBeforeListArgumentSeparator = ''; /** - * @var string + * Keys are separators (e.g. `,`). Values are the space sequence to insert, or an empty string. + * + * @var array + */ + public $aSpaceBeforeListArgumentSeparators = []; + + /** + * This is what’s inserted after the separator in value lists, by default. + * + * `array` is deprecated in version 8.8.0, and will be removed in version 9.0.0. + * To set the spacing for specific separators, use {@see $aSpaceAfterListArgumentSeparators} instead. + * + * @var string|array */ public $sSpaceAfterListArgumentSeparator = ''; + /** + * Keys are separators (e.g. `,`). Values are the space sequence to insert, or an empty string. + * + * @var array + */ + public $aSpaceAfterListArgumentSeparators = []; + /** * @var string */ @@ -343,7 +365,7 @@ public static function createPretty() $format->set('Space*Rules', "\n") ->set('Space*Blocks', "\n") ->setSpaceBetweenBlocks("\n\n") - ->set('SpaceAfterListArgumentSeparator', ['default' => '', ',' => ' ']) + ->set('SpaceAfterListArgumentSeparators', [',' => ' ']) ->setRenderComments(true); return $format; } diff --git a/src/OutputFormatter.php b/src/OutputFormatter.php index 7af3af05..6e9b8609 100644 --- a/src/OutputFormatter.php +++ b/src/OutputFormatter.php @@ -117,7 +117,9 @@ public function spaceAfterSelectorSeparator() */ public function spaceBeforeListArgumentSeparator($sSeparator) { - return $this->space('BeforeListArgumentSeparator', $sSeparator); + $spaceForSeparator = $this->oFormat->getSpaceBeforeListArgumentSeparators(); + + return $spaceForSeparator[$sSeparator] ?? $this->space('BeforeListArgumentSeparator', $sSeparator); } /** @@ -127,7 +129,9 @@ public function spaceBeforeListArgumentSeparator($sSeparator) */ public function spaceAfterListArgumentSeparator($sSeparator) { - return $this->space('AfterListArgumentSeparator', $sSeparator); + $spaceForSeparator = $this->oFormat->getSpaceAfterListArgumentSeparators(); + + return $spaceForSeparator[$sSeparator] ?? $this->space('AfterListArgumentSeparator', $sSeparator); } /** diff --git a/tests/OutputFormatTest.php b/tests/OutputFormatTest.php index ff543a9c..2fb6df99 100644 --- a/tests/OutputFormatTest.php +++ b/tests/OutputFormatTest.php @@ -104,8 +104,11 @@ public function spaceAfterListArgumentSeparator() /** * @test + * + * @deprecated since version 8.8.0; will be removed in version 9.0. + * Use `setSpaceAfterListArgumentSeparators()` to set different spacing per separator. */ - public function spaceAfterListArgumentSeparatorComplex() + public function spaceAfterListArgumentSeparatorComplexDeprecated() { $this->setUpTestcase(); @@ -121,6 +124,28 @@ public function spaceAfterListArgumentSeparatorComplex() ); } + /** + * @test + */ + public function spaceAfterListArgumentSeparatorComplex() + { + $this->setUpTestcase(); + + self::assertSame( + '.main, .test {font: italic normal bold 16px/1.2 "Helvetica", Verdana, sans-serif;background: white;}' + . "\n@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}", + $this->oDocument->render( + OutputFormat::create() + ->setSpaceAfterListArgumentSeparator(' ') + ->setSpaceAfterListArgumentSeparators([ + ',' => "\t", + '/' => '', + ' ' => '', + ]) + ) + ); + } + /** * @test */ From 08de488694eabc180e36fc06c20c5bcb912dd7ee Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Fri, 7 Feb 2025 00:32:59 +0000 Subject: [PATCH 2/2] Avoid use of null-coalescing operator for PHP 5.6 --- src/OutputFormatter.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/OutputFormatter.php b/src/OutputFormatter.php index 6e9b8609..3844383d 100644 --- a/src/OutputFormatter.php +++ b/src/OutputFormatter.php @@ -118,8 +118,11 @@ public function spaceAfterSelectorSeparator() public function spaceBeforeListArgumentSeparator($sSeparator) { $spaceForSeparator = $this->oFormat->getSpaceBeforeListArgumentSeparators(); + if (isset($spaceForSeparator[$sSeparator])) { + return $spaceForSeparator[$sSeparator]; + } - return $spaceForSeparator[$sSeparator] ?? $this->space('BeforeListArgumentSeparator', $sSeparator); + return $this->space('BeforeListArgumentSeparator', $sSeparator); } /** @@ -130,8 +133,11 @@ public function spaceBeforeListArgumentSeparator($sSeparator) public function spaceAfterListArgumentSeparator($sSeparator) { $spaceForSeparator = $this->oFormat->getSpaceAfterListArgumentSeparators(); + if (isset($spaceForSeparator[$sSeparator])) { + return $spaceForSeparator[$sSeparator]; + } - return $spaceForSeparator[$sSeparator] ?? $this->space('AfterListArgumentSeparator', $sSeparator); + return $this->space('AfterListArgumentSeparator', $sSeparator); } /**