Skip to content

Commit 8d192a6

Browse files
committed
Apply #1241
1 parent 836b5b6 commit 8d192a6

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1717

1818
### Changed
1919

20+
- Parameters for `getAllValues()` are deconflated, so it now takes three (all
21+
optional), allowing `$element` and `$ruleSearchPattern` to be specified
22+
separately (#1241)
2023
- Implement `Positionable` in the following CSS item classes:
2124
`Comment`, `CSSList`, `SourceException`, `Charset`, `CSSNamespace`, `Import`,
2225
`Rule`, `DeclarationBlock`, `RuleSet`, `CSSFunction`, `Value` (#1225)
2326

2427
### Deprecated
2528

29+
- Passing a string as the first argument to `getAllValues()` is deprecated;
30+
the search pattern should now be passed as the second argument (#1241)
31+
- Passing a Boolean as the second argument to `getAllValues()` is deprecated;
32+
the flag for searching in function arguments should now be passed as the third
33+
argument (#1241)
2634
- `getLineNo()` is deprecated in these classes (use `getLineNumber()` instead):
2735
`Comment`, `CSSList`, `SourceException`, `Charset`, `CSSNamespace`, `Import`,
2836
`Rule`, `DeclarationBlock`, `RuleSet`, `CSSFunction`, `Value` (#1225, #1233)

src/CSSList/CSSBlockList.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,44 @@ protected function allRuleSets(array &$aResult)
6262
/**
6363
* Returns all `Value` objects found recursively in `Rule`s in the tree.
6464
*
65-
* @param CSSElement|string $element
66-
* the `CSSList` or `RuleSet` to start the search from (defaults to the whole document).
67-
* If a string is given, it is used as rule name filter.
65+
* @param CSSElement|string|null $element
66+
* This is the `CSSList` or `RuleSet` to start the search from (defaults to the whole document).
67+
* If a string is given, it is used as a rule name filter.
68+
* Passing a string for this parameter is deprecated in version 8.9.0, and will not work from v9.0;
69+
* use the following parameter to pass a rule name filter instead.
70+
* @param string|bool|null $ruleSearchPatternOrSearchInFunctionArguments
71+
* This allows filtering rules by property name
72+
* (e.g. if "color" is passed, only `Value`s from `color` properties will be returned,
73+
* or if "font-" is provided, `Value`s from all font rules, like `font-size`, and including `font` itself,
74+
* will be returned).
75+
* If a Boolean is provided, it is treated as the `$searchInFunctionArguments` argument.
76+
* Passing a Boolean for this parameter is deprecated in version 8.9.0, and will not work from v9.0;
77+
* use the `$searchInFunctionArguments` parameter instead.
6878
* @param bool $searchInFunctionArguments whether to also return Value objects used as Function arguments.
6979
*
7080
* @return array<int, Value>
7181
*
7282
* @see RuleSet->getRules()
7383
*/
74-
public function getAllValues($element = null, $searchInFunctionArguments = false)
75-
{
76-
$searchString = null;
84+
public function getAllValues(
85+
$element = null,
86+
$ruleSearchPatternOrSearchInFunctionArguments = null,
87+
$searchInFunctionArguments = false
88+
) {
89+
if (\is_bool($ruleSearchPatternOrSearchInFunctionArguments)) {
90+
$searchInFunctionArguments = $ruleSearchPatternOrSearchInFunctionArguments;
91+
$searchString = null;
92+
} else {
93+
$searchString = $ruleSearchPatternOrSearchInFunctionArguments;
94+
}
95+
7796
if ($element === null) {
7897
$element = $this;
7998
} elseif (\is_string($element)) {
8099
$searchString = $element;
81100
$element = $this;
82101
}
102+
83103
$result = [];
84104
$this->allValues($element, $result, $searchString, $searchInFunctionArguments);
85105
return $result;

tests/Unit/CSSList/CSSBlockListTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,32 @@ public function getAllValuesWithSearchStringProvidedReturnsOnlyValuesFromMatchin
182182
self::assertSame([$value1], $result);
183183
}
184184

185+
/**
186+
* @test
187+
*
188+
* @return void
189+
*/
190+
public function getAllValuesWithSearchStringProvidedInNewMethodSignatureReturnsOnlyValuesFromMatchingRules()
191+
{
192+
$subject = new ConcreteCSSBlockList();
193+
194+
$value1 = new CSSString('Superfont');
195+
$value2 = new CSSString('aquamarine');
196+
197+
$declarationBlock = new DeclarationBlock();
198+
$rule1 = new Rule('font-family');
199+
$rule1->setValue($value1);
200+
$declarationBlock->addRule($rule1);
201+
$rule2 = new Rule('color');
202+
$rule2->setValue($value2);
203+
$declarationBlock->addRule($rule2);
204+
$subject->setContents([$declarationBlock]);
205+
206+
$result = $subject->getAllValues(null, 'font-');
207+
208+
self::assertSame([$value1], $result);
209+
}
210+
185211
/**
186212
* @test
187213
*
@@ -227,4 +253,27 @@ public function getAllValuesWithSearchInFunctionArgumentsReturnsValuesInFunction
227253

228254
self::assertSame([$value1, $value2], $result);
229255
}
256+
257+
/**
258+
* @test
259+
*
260+
* @return void
261+
*/
262+
public function getAllValuesWithSearchInFunctionArgumentsInNewMethodSignatureReturnsValuesInFunctionArguments()
263+
{
264+
$subject = new ConcreteCSSBlockList();
265+
266+
$value1 = new Size(10, 'px');
267+
$value2 = new Size(2, '%');
268+
269+
$declarationBlock = new DeclarationBlock();
270+
$rule = new Rule('margin');
271+
$rule->setValue(new CSSFunction('max', [$value1, $value2]));
272+
$declarationBlock->addRule($rule);
273+
$subject->setContents([$declarationBlock]);
274+
275+
$result = $subject->getAllValues(null, null, true);
276+
277+
self::assertSame([$value1, $value2], $result);
278+
}
230279
}

0 commit comments

Comments
 (0)