Skip to content

Commit f5fc39a

Browse files
authored
[TASK] Deconflate getAllValues() parameters (#1242)
This is the backport of #1231, #1240 and #1241.
1 parent e100b80 commit f5fc39a

File tree

17 files changed

+564
-35
lines changed

17 files changed

+564
-35
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
77

88
### Added
99

10+
- Add Interface `CSSElement` (#1231)
1011
- Methods `getLineNumber` and `getColumnNumber` which return a nullable `int`
1112
for the following classes:
1213
`Comment`, `CSSList`, `SourceException`, `Charset`, `CSSNamespace`, `Import`,
@@ -16,12 +17,20 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1617

1718
### Changed
1819

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

2327
### Deprecated
2428

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)
2534
- `getLineNo()` is deprecated in these classes (use `getLineNumber()` instead):
2635
`Comment`, `CSSList`, `SourceException`, `Charset`, `CSSNamespace`, `Import`,
2736
`Rule`, `DeclarationBlock`, `RuleSet`, `CSSFunction`, `Value` (#1225, #1233)

src/CSSElement.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sabberworm\CSS;
6+
7+
/**
8+
* Represents any entity in the CSS that is encapsulated by a class.
9+
*
10+
* Its primary purpose is to provide a type for use with `Document::getAllValues()`
11+
* when a subset of values from a particular part of the document is required.
12+
*
13+
* Thus, elements which don't contain `Value`s (such as statement at-rules) don't need to implement this.
14+
*
15+
* It extends `Renderable` because every element is renderable.
16+
*/
17+
interface CSSElement extends Renderable {}

src/CSSList/CSSBlockList.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Sabberworm\CSS\CSSList;
44

5+
use Sabberworm\CSS\CSSElement;
56
use Sabberworm\CSS\Property\Selector;
67
use Sabberworm\CSS\Rule\Rule;
78
use Sabberworm\CSS\RuleSet\DeclarationBlock;
@@ -59,7 +60,53 @@ protected function allRuleSets(array &$aResult)
5960
}
6061

6162
/**
62-
* @param CSSList|Rule|RuleSet|Value $oElement
63+
* Returns all `Value` objects found recursively in `Rule`s in the tree.
64+
*
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.
78+
* @param bool $searchInFunctionArguments whether to also return Value objects used as Function arguments.
79+
*
80+
* @return array<int, Value>
81+
*
82+
* @see RuleSet->getRules()
83+
*/
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+
96+
if ($element === null) {
97+
$element = $this;
98+
} elseif (\is_string($element)) {
99+
$searchString = $element;
100+
$element = $this;
101+
}
102+
103+
$result = [];
104+
$this->allValues($element, $result, $searchString, $searchInFunctionArguments);
105+
return $result;
106+
}
107+
108+
/**
109+
* @param CSSElement|string $oElement
63110
* @param array<int, Value> $aResult
64111
* @param string|null $sSearchString
65112
* @param bool $bSearchInFunctionArguments

src/CSSList/CSSList.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Sabberworm\CSS\Comment\Comment;
66
use Sabberworm\CSS\Comment\Commentable;
7+
use Sabberworm\CSS\CSSElement;
78
use Sabberworm\CSS\OutputFormat;
89
use Sabberworm\CSS\Parsing\ParserState;
910
use Sabberworm\CSS\Parsing\SourceException;
@@ -31,7 +32,7 @@
3132
*
3233
* It can also contain `Import` and `Charset` objects stemming from at-rules.
3334
*/
34-
abstract class CSSList implements Commentable, Positionable, Renderable
35+
abstract class CSSList implements Commentable, CSSElement, Positionable
3536
{
3637
use Position;
3738

src/CSSList/Document.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Sabberworm\CSS\Property\Selector;
99
use Sabberworm\CSS\RuleSet\DeclarationBlock;
1010
use Sabberworm\CSS\RuleSet\RuleSet;
11-
use Sabberworm\CSS\Value\Value;
1211

1312
/**
1413
* This class represents the root of a parsed CSS file. It contains all top-level CSS contents: mostly declaration
@@ -77,33 +76,6 @@ public function getAllRuleSets()
7776
return $aResult;
7877
}
7978

80-
/**
81-
* Returns all `Value` objects found recursively in `Rule`s in the tree.
82-
*
83-
* @param CSSList|RuleSet|string $mElement
84-
* the `CSSList` or `RuleSet` to start the search from (defaults to the whole document).
85-
* If a string is given, it is used as rule name filter.
86-
* @param bool $bSearchInFunctionArguments whether to also return Value objects used as Function arguments.
87-
*
88-
* @return array<int, Value>
89-
*
90-
* @see RuleSet->getRules()
91-
*/
92-
public function getAllValues($mElement = null, $bSearchInFunctionArguments = false)
93-
{
94-
$sSearchString = null;
95-
if ($mElement === null) {
96-
$mElement = $this;
97-
} elseif (is_string($mElement)) {
98-
$sSearchString = $mElement;
99-
$mElement = $this;
100-
}
101-
/** @var array<int, Value> $aResult */
102-
$aResult = [];
103-
$this->allValues($mElement, $aResult, $sSearchString, $bSearchInFunctionArguments);
104-
return $aResult;
105-
}
106-
10779
/**
10880
* Returns all `Selector` objects with the requested specificity found recursively in the tree.
10981
*

src/Rule/Rule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
use Sabberworm\CSS\Comment\Comment;
66
use Sabberworm\CSS\Comment\Commentable;
7+
use Sabberworm\CSS\CSSElement;
78
use Sabberworm\CSS\OutputFormat;
89
use Sabberworm\CSS\Parsing\ParserState;
910
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
1011
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
1112
use Sabberworm\CSS\Position\Position;
1213
use Sabberworm\CSS\Position\Positionable;
13-
use Sabberworm\CSS\Renderable;
1414
use Sabberworm\CSS\Value\RuleValueList;
1515
use Sabberworm\CSS\Value\Value;
1616

@@ -19,7 +19,7 @@
1919
*
2020
* In CSS, `Rule`s are expressed as follows: “key: value[0][0] value[0][1], value[1][0] value[1][1];”
2121
*/
22-
class Rule implements Commentable, Positionable, Renderable
22+
class Rule implements Commentable, CSSElement, Positionable
2323
{
2424
use Position;
2525

src/RuleSet/RuleSet.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Sabberworm\CSS\Comment\Comment;
66
use Sabberworm\CSS\Comment\Commentable;
7+
use Sabberworm\CSS\CSSElement;
78
use Sabberworm\CSS\OutputFormat;
89
use Sabberworm\CSS\Parsing\ParserState;
910
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
@@ -22,7 +23,7 @@
2223
* If you want to manipulate a `RuleSet`, use the methods `addRule(Rule $rule)`, `getRules()` and `removeRule($rule)`
2324
* (which accepts either a `Rule` or a rule name; optionally suffixed by a dash to remove all related rules).
2425
*/
25-
abstract class RuleSet implements Commentable, Positionable, Renderable
26+
abstract class RuleSet implements CSSElement, Commentable, Positionable
2627
{
2728
use Position;
2829

src/Value/Value.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
namespace Sabberworm\CSS\Value;
44

5+
use Sabberworm\CSS\CSSElement;
56
use Sabberworm\CSS\Parsing\ParserState;
67
use Sabberworm\CSS\Parsing\SourceException;
78
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
89
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
910
use Sabberworm\CSS\Position\Position;
1011
use Sabberworm\CSS\Position\Positionable;
11-
use Sabberworm\CSS\Renderable;
1212

1313
/**
1414
* Abstract base class for specific classes of CSS values: `Size`, `Color`, `CSSString` and `URL`, and another
1515
* abstract subclass `ValueList`.
1616
*/
17-
abstract class Value implements Positionable, Renderable
17+
abstract class Value implements CSSElement, Positionable
1818
{
1919
use Position;
2020

0 commit comments

Comments
 (0)