diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e499ea0d..a46c9d04a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Please also have a look at our ### Added +- `OutputFormat` properties for space around specific list separators (#880) - Partial support for CSS Color Module Level 4: - `rgb` and `rgba`, and `hsl` and `hsla` are now aliases (#797} - Parse color functions that use the "modern" syntax (#800) @@ -26,6 +27,8 @@ Please also have a look at our ### Deprecated +- `OutputFormat` properties for space around list separators as an array (#880) + ### Removed - Remove `OutputFormat::level()` (#874) diff --git a/src/OutputFormat.php b/src/OutputFormat.php index 92608df8f..1722b7384 100644 --- a/src/OutputFormat.php +++ b/src/OutputFormat.php @@ -98,17 +98,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 */ @@ -311,7 +333,7 @@ public static function createPretty(): self $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 0dfb59dc9..6740d8220 100644 --- a/src/OutputFormatter.php +++ b/src/OutputFormatter.php @@ -91,7 +91,9 @@ public function spaceAfterSelectorSeparator(): string */ public function spaceBeforeListArgumentSeparator($sSeparator): string { - return $this->space('BeforeListArgumentSeparator', $sSeparator); + $spaceForSeparator = $this->oFormat->getSpaceBeforeListArgumentSeparators(); + + return $spaceForSeparator[$sSeparator] ?? $this->space('BeforeListArgumentSeparator', $sSeparator); } /** @@ -99,7 +101,9 @@ public function spaceBeforeListArgumentSeparator($sSeparator): string */ public function spaceAfterListArgumentSeparator($sSeparator): string { - return $this->space('AfterListArgumentSeparator', $sSeparator); + $spaceForSeparator = $this->oFormat->getSpaceAfterListArgumentSeparators(); + + return $spaceForSeparator[$sSeparator] ?? $this->space('AfterListArgumentSeparator', $sSeparator); } public function spaceBeforeOpeningBrace(): string diff --git a/tests/OutputFormatTest.php b/tests/OutputFormatTest.php index f308806e9..d34b08fca 100644 --- a/tests/OutputFormatTest.php +++ b/tests/OutputFormatTest.php @@ -98,8 +98,11 @@ public function spaceAfterListArgumentSeparator(): void /** * @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(): void + public function spaceAfterListArgumentSeparatorComplexDeprecated(): void { self::assertSame( '.main, .test {font: italic normal bold 16px/1.2 "Helvetica", Verdana, sans-serif;background: white;}' @@ -113,6 +116,26 @@ public function spaceAfterListArgumentSeparatorComplex(): void ); } + /** + * @test + */ + public function spaceAfterListArgumentSeparatorComplex(): void + { + 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->document->render( + OutputFormat::create() + ->setSpaceAfterListArgumentSeparator(' ') + ->setSpaceAfterListArgumentSeparators([ + ',' => "\t", + '/' => '', + ' ' => '', + ]) + ) + ); + } + /** * @test */ diff --git a/tests/Unit/OutputFormatTest.php b/tests/Unit/OutputFormatTest.php index 2538bbe2e..2e5abce92 100644 --- a/tests/Unit/OutputFormatTest.php +++ b/tests/Unit/OutputFormatTest.php @@ -441,6 +441,33 @@ public function setSpaceBeforeListArgumentSeparatorProvidesFluentInterface(): vo self::assertSame($this->subject, $this->subject->setSpaceBeforeListArgumentSeparator(' ')); } + /** + * @test + */ + public function getSpaceBeforeListArgumentSeparatorsInitiallyReturnsEmptyArray(): void + { + self::assertSame([], $this->subject->getSpaceBeforeListArgumentSeparators()); + } + + /** + * @test + */ + public function setSpaceBeforeListArgumentSeparatorsSetsSpaceBeforeListArgumentSeparators(): void + { + $value = ['/' => ' ']; + $this->subject->setSpaceBeforeListArgumentSeparators($value); + + self::assertSame($value, $this->subject->getSpaceBeforeListArgumentSeparators()); + } + + /** + * @test + */ + public function setSpaceBeforeListArgumentSeparatorsProvidesFluentInterface(): void + { + self::assertSame($this->subject, $this->subject->setSpaceBeforeListArgumentSeparators([])); + } + /** * @test */ @@ -468,6 +495,33 @@ public function setSpaceAfterListArgumentSeparatorProvidesFluentInterface(): voi self::assertSame($this->subject, $this->subject->setSpaceAfterListArgumentSeparator(' ')); } + /** + * @test + */ + public function getSpaceAfterListArgumentSeparatorsInitiallyReturnsEmptyArray(): void + { + self::assertSame([], $this->subject->getSpaceAfterListArgumentSeparators()); + } + + /** + * @test + */ + public function setSpaceAfterListArgumentSeparatorsSetsSpaceAfterListArgumentSeparators(): void + { + $value = [',' => ' ']; + $this->subject->setSpaceAfterListArgumentSeparators($value); + + self::assertSame($value, $this->subject->getSpaceAfterListArgumentSeparators()); + } + + /** + * @test + */ + public function setSpaceAfterListArgumentSeparatorsProvidesFluentInterface(): void + { + self::assertSame($this->subject, $this->subject->setSpaceAfterListArgumentSeparators([])); + } + /** * @test */