Skip to content

Commit 4250c18

Browse files
committed
[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. Resolves #866, #876, #878.
1 parent 1fad44a commit 4250c18

File tree

5 files changed

+113
-7
lines changed

5 files changed

+113
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Please also have a look at our
1010

1111
### Added
1212

13+
- `OutputFormat` properties for space around specific list separators (#880)
1314
- Partial support for CSS Color Module Level 4:
1415
- `rgb` and `rgba`, and `hsl` and `hsla` are now aliases (#797}
1516
- Parse color functions that use the "modern" syntax (#800)
@@ -26,6 +27,8 @@ Please also have a look at our
2627

2728
### Deprecated
2829

30+
- `OutputFormat` properties for space around list separators as an array (#880)
31+
2932
### Removed
3033

3134
- Remove `OutputFormat::level()` (#874)

src/OutputFormat.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,39 @@ class OutputFormat
9898
public $sSpaceAfterSelectorSeparator = ' ';
9999

100100
/**
101-
* This is what’s printed after the comma of value lists
101+
* This is what’s inserted before the separator in value lists, by default.
102102
*
103-
* @var string
103+
* `array` is deprecated in version 8.8.0, and will be removed in version 9.0.0.
104+
* To set the spacing for specific separators, use {@see $aSpaceBeforeListArgumentSeparators} instead.
105+
*
106+
* @var string|array<non-empty-string, string>
104107
*/
105108
public $sSpaceBeforeListArgumentSeparator = '';
106109

107110
/**
108-
* @var string
111+
* Keys are separators (e.g. `,`). Values are the space sequence to insert, or an empty string.
112+
*
113+
* @var array<non-empty-string, string>
114+
*/
115+
public $aSpaceBeforeListArgumentSeparators = [];
116+
117+
/**
118+
* This is what’s inserted after the separator in value lists, by default.
119+
*
120+
* `array` is deprecated in version 8.8.0, and will be removed in version 9.0.0.
121+
* To set the spacing for specific separators, use {@see $aSpaceAfterListArgumentSeparators} instead.
122+
*
123+
* @var string|array<non-empty-string, string>
109124
*/
110125
public $sSpaceAfterListArgumentSeparator = '';
111126

127+
/**
128+
* Keys are separators (e.g. `,`). Values are the space sequence to insert, or an empty string.
129+
*
130+
* @var array<non-empty-string, string>
131+
*/
132+
public $aSpaceAfterListArgumentSeparators = [];
133+
112134
/**
113135
* @var string
114136
*/
@@ -311,7 +333,7 @@ public static function createPretty(): self
311333
$format->set('Space*Rules', "\n")
312334
->set('Space*Blocks', "\n")
313335
->setSpaceBetweenBlocks("\n\n")
314-
->set('SpaceAfterListArgumentSeparator', ['default' => '', ',' => ' '])
336+
->set('SpaceAfterListArgumentSeparators', [',' => ' '])
315337
->setRenderComments(true);
316338
return $format;
317339
}

src/OutputFormatter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,19 @@ public function spaceAfterSelectorSeparator(): string
9191
*/
9292
public function spaceBeforeListArgumentSeparator($sSeparator): string
9393
{
94-
return $this->space('BeforeListArgumentSeparator', $sSeparator);
94+
$spaceForSeparator = $this->oFormat->getSpaceBeforeListArgumentSeparators();
95+
96+
return $spaceForSeparator[$sSeparator] ?? $this->space('BeforeListArgumentSeparator', $sSeparator);
9597
}
9698

9799
/**
98100
* @param string $sSeparator
99101
*/
100102
public function spaceAfterListArgumentSeparator($sSeparator): string
101103
{
102-
return $this->space('AfterListArgumentSeparator', $sSeparator);
104+
$spaceForSeparator = $this->oFormat->getSpaceAfterListArgumentSeparators();
105+
106+
return $spaceForSeparator[$sSeparator] ?? $this->space('AfterListArgumentSeparator', $sSeparator);
103107
}
104108

105109
public function spaceBeforeOpeningBrace(): string

tests/OutputFormatTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,11 @@ public function spaceAfterListArgumentSeparator(): void
9898

9999
/**
100100
* @test
101+
*
102+
* @deprecated since version 8.8.0; will be removed in version 9.0.
103+
* Use `setSpaceAfterListArgumentSeparators()` to set different spacing per separator.
101104
*/
102-
public function spaceAfterListArgumentSeparatorComplex(): void
105+
public function spaceAfterListArgumentSeparatorComplexDeprecated(): void
103106
{
104107
self::assertSame(
105108
'.main, .test {font: italic normal bold 16px/1.2 "Helvetica", Verdana, sans-serif;background: white;}'
@@ -113,6 +116,26 @@ public function spaceAfterListArgumentSeparatorComplex(): void
113116
);
114117
}
115118

119+
/**
120+
* @test
121+
*/
122+
public function spaceAfterListArgumentSeparatorComplex(): void
123+
{
124+
self::assertSame(
125+
'.main, .test {font: italic normal bold 16px/1.2 "Helvetica", Verdana, sans-serif;background: white;}'
126+
. "\n@media screen {.main {background-size: 100% 100%;font-size: 1.3em;background-color: #fff;}}",
127+
$this->document->render(
128+
OutputFormat::create()
129+
->setSpaceAfterListArgumentSeparator(' ')
130+
->setSpaceAfterListArgumentSeparators([
131+
',' => "\t",
132+
'/' => '',
133+
' ' => '',
134+
])
135+
)
136+
);
137+
}
138+
116139
/**
117140
* @test
118141
*/

tests/Unit/OutputFormatTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,33 @@ public function setSpaceBeforeListArgumentSeparatorProvidesFluentInterface(): vo
441441
self::assertSame($this->subject, $this->subject->setSpaceBeforeListArgumentSeparator(' '));
442442
}
443443

444+
/**
445+
* @test
446+
*/
447+
public function getSpaceBeforeListArgumentSeparatorsInitiallyReturnsEmptyArray(): void
448+
{
449+
self::assertSame([], $this->subject->getSpaceBeforeListArgumentSeparators());
450+
}
451+
452+
/**
453+
* @test
454+
*/
455+
public function setSpaceBeforeListArgumentSeparatorsSetsSpaceBeforeListArgumentSeparators(): void
456+
{
457+
$value = ['/' => ' '];
458+
$this->subject->setSpaceBeforeListArgumentSeparators($value);
459+
460+
self::assertSame($value, $this->subject->getSpaceBeforeListArgumentSeparators());
461+
}
462+
463+
/**
464+
* @test
465+
*/
466+
public function setSpaceBeforeListArgumentSeparatorsProvidesFluentInterface(): void
467+
{
468+
self::assertSame($this->subject, $this->subject->setSpaceBeforeListArgumentSeparators([]));
469+
}
470+
444471
/**
445472
* @test
446473
*/
@@ -468,6 +495,33 @@ public function setSpaceAfterListArgumentSeparatorProvidesFluentInterface(): voi
468495
self::assertSame($this->subject, $this->subject->setSpaceAfterListArgumentSeparator(' '));
469496
}
470497

498+
/**
499+
* @test
500+
*/
501+
public function getSpaceAfterListArgumentSeparatorsInitiallyReturnsEmptyArray(): void
502+
{
503+
self::assertSame([], $this->subject->getSpaceAfterListArgumentSeparators());
504+
}
505+
506+
/**
507+
* @test
508+
*/
509+
public function setSpaceAfterListArgumentSeparatorsSetsSpaceAfterListArgumentSeparators(): void
510+
{
511+
$value = [',' => ' '];
512+
$this->subject->setSpaceAfterListArgumentSeparators($value);
513+
514+
self::assertSame($value, $this->subject->getSpaceAfterListArgumentSeparators());
515+
}
516+
517+
/**
518+
* @test
519+
*/
520+
public function setSpaceAfterListArgumentSeparatorsProvidesFluentInterface(): void
521+
{
522+
self::assertSame($this->subject, $this->subject->setSpaceAfterListArgumentSeparators([]));
523+
}
524+
471525
/**
472526
* @test
473527
*/

0 commit comments

Comments
 (0)