Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f82fd72

Browse files
committedApr 6, 2025·
Apply #1225.
1 parent ddd999e commit f82fd72

File tree

12 files changed

+76
-163
lines changed

12 files changed

+76
-163
lines changed
 

‎CHANGELOG.md

+18
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,28 @@ This project adheres to [Semantic Versioning](https://semver.org/).
77

88
### Added
99

10+
- Methods `getLineNumber` and `getColumnNumber` which return a nullable `int`
11+
for the following classes:
12+
`Comment`, `CSSList`, `SourceException`, `Charset`, `CSSNamespace`, `Import`,
13+
`Rule`, `DeclarationBlock`, `RuleSet`, `CSSFunction`, `Value` (#1225)
14+
- `Positionable` interface for CSS items that may have a position
15+
(line and perhaps column number) in the parsed CSS (#1221)
16+
1017
### Changed
1118

19+
- Implement `Positionable` in the following CSS item classes:
20+
`Comment`, `CSSList`, `SourceException`, `Charset`, `CSSNamespace`, `Import`,
21+
`Rule`, `DeclarationBlock`, `RuleSet`, `CSSFunction`, `Value` (#1225)
22+
1223
### Deprecated
1324

25+
- `getLineNo()` is deprecated in these classes (use `getLineNumber()` instead):
26+
`Comment`, `CSSList`, `SourceException`, `Charset`, `CSSNamespace`, `Import`,
27+
`Rule`, `DeclarationBlock`, `RuleSet`, `CSSFunction`, `Value` (#1225)
28+
- `Rule::getColNo()` is deprecated (use `getColumnNumber()` instead) (#1225)
29+
- Providing zero as the line number argument to `Rule::setPosition()` is
30+
deprecated (pass `null` instead if there is no line number) (#1225)
31+
1432
### Removed
1533

1634
### Fixed

‎src/CSSList/CSSList.php

+6-17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Sabberworm\CSS\Parsing\SourceException;
1010
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
1111
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
12+
use Sabberworm\CSS\Position\Position;
13+
use Sabberworm\CSS\Position\Positionable;
1214
use Sabberworm\CSS\Property\AtRule;
1315
use Sabberworm\CSS\Property\Charset;
1416
use Sabberworm\CSS\Property\CSSNamespace;
@@ -29,8 +31,10 @@
2931
*
3032
* It can also contain `Import` and `Charset` objects stemming from at-rules.
3133
*/
32-
abstract class CSSList implements Renderable, Commentable
34+
abstract class CSSList implements Commentable, Positionable, Renderable
3335
{
36+
use Position;
37+
3438
/**
3539
* @var array<array-key, Comment>
3640
*
@@ -45,21 +49,14 @@ abstract class CSSList implements Renderable, Commentable
4549
*/
4650
protected $aContents;
4751

48-
/**
49-
* @var int
50-
*
51-
* @internal since 8.8.0
52-
*/
53-
protected $iLineNo;
54-
5552
/**
5653
* @param int $iLineNo
5754
*/
5855
public function __construct($iLineNo = 0)
5956
{
6057
$this->aComments = [];
6158
$this->aContents = [];
62-
$this->iLineNo = $iLineNo;
59+
$this->setPosition($iLineNo);
6360
}
6461

6562
/**
@@ -258,14 +255,6 @@ private static function identifierIs($sIdentifier, $sMatch)
258255
?: preg_match("/^(-\\w+-)?$sMatch$/i", $sIdentifier) === 1;
259256
}
260257

261-
/**
262-
* @return int
263-
*/
264-
public function getLineNo()
265-
{
266-
return $this->iLineNo;
267-
}
268-
269258
/**
270259
* Prepends an item to the list of contents.
271260
*

‎src/Comment/Comment.php

+5-16
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44

55
use Sabberworm\CSS\OutputFormat;
66
use Sabberworm\CSS\Renderable;
7+
use Sabberworm\CSS\Position\Position;
8+
use Sabberworm\CSS\Position\Positionable;
79

8-
class Comment implements Renderable
10+
class Comment implements Positionable, Renderable
911
{
10-
/**
11-
* @var int
12-
*
13-
* @internal since 8.8.0
14-
*/
15-
protected $iLineNo;
12+
use Position;
1613

1714
/**
1815
* @var string
@@ -28,7 +25,7 @@ class Comment implements Renderable
2825
public function __construct($sComment = '', $iLineNo = 0)
2926
{
3027
$this->sComment = $sComment;
31-
$this->iLineNo = $iLineNo;
28+
$this->setPosition($iLineNo);
3229
}
3330

3431
/**
@@ -39,14 +36,6 @@ public function getComment()
3936
return $this->sComment;
4037
}
4138

42-
/**
43-
* @return int
44-
*/
45-
public function getLineNo()
46-
{
47-
return $this->iLineNo;
48-
}
49-
5039
/**
5140
* @param string $sComment
5241
*

‎src/Parsing/SourceException.php

+6-14
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,23 @@
22

33
namespace Sabberworm\CSS\Parsing;
44

5-
class SourceException extends \Exception
5+
use Sabberworm\CSS\Position\Position;
6+
use Sabberworm\CSS\Position\Positionable;
7+
8+
class SourceException extends \Exception implements Positionable
69
{
7-
/**
8-
* @var int
9-
*/
10-
private $iLineNo;
10+
use Position;
1111

1212
/**
1313
* @param string $sMessage
1414
* @param int $iLineNo
1515
*/
1616
public function __construct($sMessage, $iLineNo = 0)
1717
{
18-
$this->iLineNo = $iLineNo;
18+
$this->setPosition($iLineNo);
1919
if (!empty($iLineNo)) {
2020
$sMessage .= " [line no: $iLineNo]";
2121
}
2222
parent::__construct($sMessage);
2323
}
24-
25-
/**
26-
* @return int
27-
*/
28-
public function getLineNo()
29-
{
30-
return $this->iLineNo;
31-
}
3224
}

‎src/Property/CSSNamespace.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
use Sabberworm\CSS\Comment\Comment;
66
use Sabberworm\CSS\OutputFormat;
7+
use Sabberworm\CSS\Position\Position;
8+
use Sabberworm\CSS\Position\Positionable;
79

810
/**
911
* `CSSNamespace` represents an `@namespace` rule.
1012
*/
11-
class CSSNamespace implements AtRule
13+
class CSSNamespace implements AtRule, Positionable
1214
{
15+
use Position;
16+
1317
/**
1418
* @var string
1519
*/
@@ -41,18 +45,10 @@ public function __construct($mUrl, $sPrefix = null, $iLineNo = 0)
4145
{
4246
$this->mUrl = $mUrl;
4347
$this->sPrefix = $sPrefix;
44-
$this->iLineNo = $iLineNo;
48+
$this->setPosition($iLineNo);
4549
$this->aComments = [];
4650
}
4751

48-
/**
49-
* @return int
50-
*/
51-
public function getLineNo()
52-
{
53-
return $this->iLineNo;
54-
}
55-
5652
/**
5753
* @return string
5854
*

‎src/Property/Charset.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Sabberworm\CSS\Comment\Comment;
66
use Sabberworm\CSS\OutputFormat;
7+
use Sabberworm\CSS\Position\Position;
8+
use Sabberworm\CSS\Position\Positionable;
79
use Sabberworm\CSS\Value\CSSString;
810

911
/**
@@ -14,8 +16,10 @@
1416
* - May only appear at the very top of a Document’s contents.
1517
* - Must not appear more than once.
1618
*/
17-
class Charset implements AtRule
19+
class Charset implements AtRule, Positionable
1820
{
21+
use Position;
22+
1923
/**
2024
* @var CSSString
2125
*/
@@ -42,18 +46,10 @@ class Charset implements AtRule
4246
public function __construct(CSSString $oCharset, $iLineNo = 0)
4347
{
4448
$this->oCharset = $oCharset;
45-
$this->iLineNo = $iLineNo;
49+
$this->setPosition($iLineNo);
4650
$this->aComments = [];
4751
}
4852

49-
/**
50-
* @return int
51-
*/
52-
public function getLineNo()
53-
{
54-
return $this->iLineNo;
55-
}
56-
5753
/**
5854
* @param string|CSSString $oCharset
5955
*

‎src/Property/Import.php

+6-17
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
use Sabberworm\CSS\Comment\Comment;
66
use Sabberworm\CSS\OutputFormat;
7+
use Sabberworm\CSS\Position\Position;
8+
use Sabberworm\CSS\Position\Positionable;
79
use Sabberworm\CSS\Value\URL;
810

911
/**
1012
* Class representing an `@import` rule.
1113
*/
12-
class Import implements AtRule
14+
class Import implements AtRule, Positionable
1315
{
16+
use Position;
17+
1418
/**
1519
* @var URL
1620
*/
@@ -21,13 +25,6 @@ class Import implements AtRule
2125
*/
2226
private $sMediaQuery;
2327

24-
/**
25-
* @var int
26-
*
27-
* @internal since 8.8.0
28-
*/
29-
protected $iLineNo;
30-
3128
/**
3229
* @var array<array-key, Comment>
3330
*
@@ -44,18 +41,10 @@ public function __construct(URL $oLocation, $sMediaQuery, $iLineNo = 0)
4441
{
4542
$this->oLocation = $oLocation;
4643
$this->sMediaQuery = $sMediaQuery;
47-
$this->iLineNo = $iLineNo;
44+
$this->setPosition($iLineNo);
4845
$this->aComments = [];
4946
}
5047

51-
/**
52-
* @return int
53-
*/
54-
public function getLineNo()
55-
{
56-
return $this->iLineNo;
57-
}
58-
5948
/**
6049
* @param URL $oLocation
6150
*

‎src/Rule/Rule.php

+7-44
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Sabberworm\CSS\Parsing\ParserState;
99
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
1010
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
11+
use Sabberworm\CSS\Position\Position;
12+
use Sabberworm\CSS\Position\Positionable;
1113
use Sabberworm\CSS\Renderable;
1214
use Sabberworm\CSS\Value\RuleValueList;
1315
use Sabberworm\CSS\Value\Value;
@@ -17,8 +19,10 @@
1719
*
1820
* In CSS, `Rule`s are expressed as follows: “key: value[0][0] value[0][1], value[1][0] value[1][1];”
1921
*/
20-
class Rule implements Renderable, Commentable
22+
class Rule implements Commentable, Positionable, Renderable
2123
{
24+
use Position;
25+
2226
/**
2327
* @var string
2428
*/
@@ -39,18 +43,6 @@ class Rule implements Renderable, Commentable
3943
*/
4044
private $aIeHack;
4145

42-
/**
43-
* @var int
44-
*/
45-
protected $iLineNo;
46-
47-
/**
48-
* @var int
49-
*
50-
* @internal since 8.8.0
51-
*/
52-
protected $iColNo;
53-
5446
/**
5547
* @var array<array-key, Comment>
5648
*
@@ -69,8 +61,7 @@ public function __construct($sRule, $iLineNo = 0, $iColNo = 0)
6961
$this->mValue = null;
7062
$this->bIsImportant = false;
7163
$this->aIeHack = [];
72-
$this->iLineNo = $iLineNo;
73-
$this->iColNo = $iColNo;
64+
$this->setPosition($iLineNo, $iColNo);
7465
$this->aComments = [];
7566
}
7667

@@ -142,34 +133,6 @@ private static function listDelimiterForRule($sRule)
142133
}
143134
}
144135

145-
/**
146-
* @return int
147-
*/
148-
public function getLineNo()
149-
{
150-
return $this->iLineNo;
151-
}
152-
153-
/**
154-
* @return int
155-
*/
156-
public function getColNo()
157-
{
158-
return $this->iColNo;
159-
}
160-
161-
/**
162-
* @param int $iLine
163-
* @param int $iColumn
164-
*
165-
* @return void
166-
*/
167-
public function setPosition($iLine, $iColumn)
168-
{
169-
$this->iColNo = $iColumn;
170-
$this->iLineNo = $iLine;
171-
}
172-
173136
/**
174137
* @param string $sRule
175138
*
@@ -295,7 +258,7 @@ public function addValue($mValue, $sType = ' ')
295258
}
296259
if (!$this->mValue instanceof RuleValueList || $this->mValue->getListSeparator() !== $sType) {
297260
$mCurrentValue = $this->mValue;
298-
$this->mValue = new RuleValueList($sType, $this->iLineNo);
261+
$this->mValue = new RuleValueList($sType, $this->$this->getLineNumber());
299262
if ($mCurrentValue) {
300263
$this->mValue->addListComponent($mCurrentValue);
301264
}

‎src/RuleSet/DeclarationBlock.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,10 @@ public function render($oOutputFormat)
851851
$sResult = $oOutputFormat->comments($this);
852852
if (count($this->aSelectors) === 0) {
853853
// If all the selectors have been removed, this declaration block becomes invalid
854-
throw new OutputException("Attempt to print declaration block with missing selector", $this->iLineNo);
854+
throw new OutputException(
855+
'Attempt to print declaration block with missing selector',
856+
$this->getLineNumber()
857+
);
855858
}
856859
$sResult .= $oOutputFormat->sBeforeDeclarationBlock;
857860
$sResult .= $oOutputFormat->implode(

‎src/RuleSet/RuleSet.php

+6-17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Sabberworm\CSS\Parsing\ParserState;
99
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
1010
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
11+
use Sabberworm\CSS\Position\Position;
12+
use Sabberworm\CSS\Position\Positionable;
1113
use Sabberworm\CSS\Renderable;
1214
use Sabberworm\CSS\Rule\Rule;
1315

@@ -20,8 +22,10 @@
2022
* If you want to manipulate a `RuleSet`, use the methods `addRule(Rule $rule)`, `getRules()` and `removeRule($rule)`
2123
* (which accepts either a `Rule` or a rule name; optionally suffixed by a dash to remove all related rules).
2224
*/
23-
abstract class RuleSet implements Renderable, Commentable
25+
abstract class RuleSet implements Commentable, Positionable, Renderable
2426
{
27+
use Position;
28+
2529
/**
2630
* the rules in this rule set, using the property name as the key,
2731
* with potentially multiple rules per property name.
@@ -30,13 +34,6 @@ abstract class RuleSet implements Renderable, Commentable
3034
*/
3135
private $aRules;
3236

33-
/**
34-
* @var int
35-
*
36-
* @internal since 8.8.0
37-
*/
38-
protected $iLineNo;
39-
4037
/**
4138
* @var array<array-key, Comment>
4239
*
@@ -50,7 +47,7 @@ abstract class RuleSet implements Renderable, Commentable
5047
public function __construct($iLineNo = 0)
5148
{
5249
$this->aRules = [];
53-
$this->iLineNo = $iLineNo;
50+
$this->setPosition($iLineNo);
5451
$this->aComments = [];
5552
}
5653

@@ -102,14 +99,6 @@ public static function parseRuleSet(ParserState $oParserState, RuleSet $oRuleSet
10299
$oParserState->consume('}');
103100
}
104101

105-
/**
106-
* @return int
107-
*/
108-
public function getLineNo()
109-
{
110-
return $this->iLineNo;
111-
}
112-
113102
/**
114103
* @param Rule|null $oSibling
115104
*

‎src/Value/CSSFunction.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct($sName, $aArguments, $sSeparator = ',', $iLineNo = 0
3434
$aArguments = $aArguments->getListComponents();
3535
}
3636
$this->sName = $sName;
37-
$this->iLineNo = $iLineNo;
37+
$this->setPosition($iLineNo); // TODO: redundant?
3838
parent::__construct($aArguments, $sSeparator, $iLineNo);
3939
}
4040

‎src/Value/Value.php

+5-16
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,24 @@
66
use Sabberworm\CSS\Parsing\SourceException;
77
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
88
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
9+
use Sabberworm\CSS\Position\Position;
10+
use Sabberworm\CSS\Position\Positionable;
911
use Sabberworm\CSS\Renderable;
1012

1113
/**
1214
* Abstract base class for specific classes of CSS values: `Size`, `Color`, `CSSString` and `URL`, and another
1315
* abstract subclass `ValueList`.
1416
*/
15-
abstract class Value implements Renderable
17+
abstract class Value implements Positionable, Renderable
1618
{
17-
/**
18-
* @var int
19-
*
20-
* @internal since 8.8.0
21-
*/
22-
protected $iLineNo;
19+
use Position;
2320

2421
/**
2522
* @param int $iLineNo
2623
*/
2724
public function __construct($iLineNo = 0)
2825
{
29-
$this->iLineNo = $iLineNo;
26+
$this->setPosition($iLineNo);
3027
}
3128

3229
/**
@@ -218,12 +215,4 @@ private static function parseUnicodeRangeValue(ParserState $oParserState)
218215
} while (strlen($sRange) < $iCodepointMaxLength && preg_match("/[A-Fa-f0-9\?-]/", $oParserState->peek()));
219216
return "U+{$sRange}";
220217
}
221-
222-
/**
223-
* @return int
224-
*/
225-
public function getLineNo()
226-
{
227-
return $this->iLineNo;
228-
}
229218
}

0 commit comments

Comments
 (0)
Please sign in to comment.