Skip to content

Commit 9b52615

Browse files
authored
[TASK] Implement Positionable (#1225)
Closes #1207.
1 parent 36ed5cd commit 9b52615

13 files changed

+80
-173
lines changed

Diff for: CHANGELOG.md

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ Please also have a look at our
1010

1111
### Added
1212

13+
- Methods `getLineNumber` and `getColumnNumber` which return a nullable `int`
14+
for the following classes:
15+
`Comment`, `CSSList`, `SourceException`, `Charset`, `CSSNamespace`, `Import`,
16+
`Rule`, `DeclarationBlock`, `RuleSet`, `CSSFunction`, `Value` (#1225)
17+
- `Positionable` interface for CSS items that may have a position
18+
(line and perhaps column number) in the parsed CSS (#1221)
1319
- Partial support for CSS Color Module Level 4:
1420
- `rgb` and `rgba`, and `hsl` and `hsla` are now aliases (#797}
1521
- Parse color functions that use the "modern" syntax (#800)
@@ -20,6 +26,9 @@ Please also have a look at our
2026

2127
### Changed
2228

29+
- Implement `Positionable` in the following CSS item classes:
30+
`Comment`, `CSSList`, `SourceException`, `Charset`, `CSSNamespace`, `Import`,
31+
`Rule`, `DeclarationBlock`, `RuleSet`, `CSSFunction`, `Value` (#1225)
2332
- Initialize `KeyFrame` properties to sensible defaults (#1146)
2433
- Make `OutputFormat` `final` (#1128)
2534
- Make `Selector` a `Renderable` (#1017)
@@ -33,6 +42,13 @@ Please also have a look at our
3342

3443
### Deprecated
3544

45+
- `getLineNo()` is deprecated in these classes (use `getLineNumber()` instead):
46+
`Comment`, `CSSList`, `SourceException`, `Charset`, `CSSNamespace`, `Import`,
47+
`Rule`, `DeclarationBlock`, `RuleSet`, `CSSFunction`, `Value` (#1225)
48+
- `Rule::getColNo()` is deprecated (use `getColumnNumber()` instead) (#1225)
49+
- Providing zero as the line number argument to `Rule::setPosition()` is
50+
deprecated (pass `null` instead if there is no line number) (#1225)
51+
3652
### Removed
3753

3854
- Remove `__toString()` (#1046)

Diff for: README.md

+12
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@ classDiagram
627627
class Renderable {
628628
<<interface>>
629629
}
630+
class Positionable {
631+
<<interface>>
632+
}
630633
class CSSListItem {
631634
<<interface>>
632635
}
@@ -720,23 +723,30 @@ classDiagram
720723
RuleSet <|-- DeclarationBlock: inheritance
721724
Renderable <|-- CSSListItem: inheritance
722725
Commentable <|-- CSSListItem: inheritance
726+
Positionable <|.. RuleSet: realization
723727
CSSListItem <|.. RuleSet: realization
724728
RuleSet <|-- AtRuleSet: inheritance
725729
AtRule <|.. AtRuleSet: realization
726730
Renderable <|.. Selector: realization
727731
Selector <|-- KeyframeSelector: inheritance
728732
CSSListItem <|-- AtRule: inheritance
733+
Positionable <|.. Charset: realization
729734
AtRule <|.. Charset: realization
735+
Positionable <|.. Import: realization
730736
AtRule <|.. Import: realization
737+
Positionable <|.. CSSNamespace: realization
731738
AtRule <|.. CSSNamespace: realization
732739
Renderable <|.. Rule: realization
740+
Positionable <|.. Rule: realization
733741
Commentable <|.. Rule: realization
734742
SourceException <|-- OutputException: inheritance
735743
UnexpectedTokenException <|-- UnexpectedEOFException: inheritance
736744
Exception <|-- SourceException: inheritance
745+
Positionable <|.. SourceException: realization
737746
SourceException <|-- UnexpectedTokenException: inheritance
738747
CSSList <|-- CSSBlockList: inheritance
739748
CSSBlockList <|-- Document: inheritance
749+
Positionable <|.. CSSList: realization
740750
CSSListItem <|.. CSSList: realization
741751
CSSList <|-- KeyFrame: inheritance
742752
AtRule <|.. KeyFrame: realization
@@ -749,12 +759,14 @@ classDiagram
749759
CSSFunction <|-- CalcFunction: inheritance
750760
ValueList <|-- LineName: inheritance
751761
Renderable <|.. Value: realization
762+
Positionable <|.. Value: realization
752763
PrimitiveValue <|-- Size: inheritance
753764
PrimitiveValue <|-- CSSString: inheritance
754765
Value <|-- PrimitiveValue: inheritance
755766
ValueList <|-- CSSFunction: inheritance
756767
ValueList <|-- RuleValueList: inheritance
757768
Renderable <|.. Comment: realization
769+
Positionable <|.. Comment: realization
758770
759771
%% end of the generated part
760772

Diff for: src/CSSList/CSSList.php

+5-17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Sabberworm\CSS\Parsing\SourceException;
1111
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
1212
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
13+
use Sabberworm\CSS\Position\Position;
14+
use Sabberworm\CSS\Position\Positionable;
1315
use Sabberworm\CSS\Property\AtRule;
1416
use Sabberworm\CSS\Property\Charset;
1517
use Sabberworm\CSS\Property\CSSNamespace;
@@ -32,9 +34,10 @@
3234
* Note that `CSSListItem` extends both `Commentable` and `Renderable`,
3335
* so those interfaces must also be implemented by concrete subclasses.
3436
*/
35-
abstract class CSSList implements CSSListItem
37+
abstract class CSSList implements CSSListItem, Positionable
3638
{
3739
use CommentContainer;
40+
use Position;
3841

3942
/**
4043
* @var array<int<0, max>, CSSListItem>
@@ -43,19 +46,12 @@ abstract class CSSList implements CSSListItem
4346
*/
4447
protected $contents = [];
4548

46-
/**
47-
* @var int<0, max>
48-
*
49-
* @internal since 8.8.0
50-
*/
51-
protected $lineNumber;
52-
5349
/**
5450
* @param int<0, max> $lineNumber
5551
*/
5652
public function __construct(int $lineNumber = 0)
5753
{
58-
$this->lineNumber = $lineNumber;
54+
$this->setPosition($lineNumber);
5955
}
6056

6157
/**
@@ -249,14 +245,6 @@ private static function identifierIs(string $identifier, string $match): bool
249245
?: \preg_match("/^(-\\w+-)?$match$/i", $identifier) === 1;
250246
}
251247

252-
/**
253-
* @return int<0, max>
254-
*/
255-
public function getLineNo(): int
256-
{
257-
return $this->lineNumber;
258-
}
259-
260248
/**
261249
* Prepends an item to the list of contents.
262250
*/

Diff for: src/Comment/Comment.php

+5-16
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66

77
use Sabberworm\CSS\OutputFormat;
88
use Sabberworm\CSS\Renderable;
9+
use Sabberworm\CSS\Position\Position;
10+
use Sabberworm\CSS\Position\Positionable;
911

10-
class Comment implements Renderable
12+
class Comment implements Positionable, Renderable
1113
{
12-
/**
13-
* @var int<0, max>
14-
*
15-
* @internal since 8.8.0
16-
*/
17-
protected $lineNumber;
14+
use Position;
1815

1916
/**
2017
* @var string
@@ -29,22 +26,14 @@ class Comment implements Renderable
2926
public function __construct(string $commentText = '', int $lineNumber = 0)
3027
{
3128
$this->commentText = $commentText;
32-
$this->lineNumber = $lineNumber;
29+
$this->setPosition($lineNumber);
3330
}
3431

3532
public function getComment(): string
3633
{
3734
return $this->commentText;
3835
}
3936

40-
/**
41-
* @return int<0, max>
42-
*/
43-
public function getLineNo(): int
44-
{
45-
return $this->lineNumber;
46-
}
47-
4837
public function setComment(string $commentText): void
4938
{
5039
$this->commentText = $commentText;

Diff for: src/Parsing/SourceException.php

+6-14
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,22 @@
44

55
namespace Sabberworm\CSS\Parsing;
66

7-
class SourceException extends \Exception
7+
use Sabberworm\CSS\Position\Position;
8+
use Sabberworm\CSS\Position\Positionable;
9+
10+
class SourceException extends \Exception implements Positionable
811
{
9-
/**
10-
* @var int<0, max>
11-
*/
12-
private $lineNumber;
12+
use Position;
1313

1414
/**
1515
* @param int<0, max> $lineNumber
1616
*/
1717
public function __construct(string $message, int $lineNumber = 0)
1818
{
19-
$this->lineNumber = $lineNumber;
19+
$this->setPosition($lineNumber);
2020
if ($lineNumber !== 0) {
2121
$message .= " [line no: $lineNumber]";
2222
}
2323
parent::__construct($message);
2424
}
25-
26-
/**
27-
* @return int<0, max>
28-
*/
29-
public function getLineNo(): int
30-
{
31-
return $this->lineNumber;
32-
}
3325
}

Diff for: src/Property/CSSNamespace.php

+5-15
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66

77
use Sabberworm\CSS\Comment\CommentContainer;
88
use Sabberworm\CSS\OutputFormat;
9+
use Sabberworm\CSS\Position\Position;
10+
use Sabberworm\CSS\Position\Positionable;
911
use Sabberworm\CSS\Value\CSSString;
1012
use Sabberworm\CSS\Value\URL;
1113

1214
/**
1315
* `CSSNamespace` represents an `@namespace` rule.
1416
*/
15-
class CSSNamespace implements AtRule
17+
class CSSNamespace implements AtRule, Positionable
1618
{
1719
use CommentContainer;
20+
use Position;
1821

1922
/**
2023
* @var CSSString|URL
@@ -26,11 +29,6 @@ class CSSNamespace implements AtRule
2629
*/
2730
private $prefix;
2831

29-
/**
30-
* @var int<0, max> $lineNumber
31-
*/
32-
private $lineNumber;
33-
3432
/**
3533
* @param CSSString|URL $url
3634
* @param int<0, max> $lineNumber
@@ -39,15 +37,7 @@ public function __construct($url, ?string $prefix = null, int $lineNumber = 0)
3937
{
4038
$this->url = $url;
4139
$this->prefix = $prefix;
42-
$this->lineNumber = $lineNumber;
43-
}
44-
45-
/**
46-
* @return int<0, max>
47-
*/
48-
public function getLineNo(): int
49-
{
50-
return $this->lineNumber;
40+
$this->setPosition($lineNumber);
5141
}
5242

5343
/**

Diff for: src/Property/Charset.php

+5-17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Sabberworm\CSS\Comment\CommentContainer;
88
use Sabberworm\CSS\OutputFormat;
9+
use Sabberworm\CSS\Position\Position;
10+
use Sabberworm\CSS\Position\Positionable;
911
use Sabberworm\CSS\Value\CSSString;
1012

1113
/**
@@ -16,37 +18,23 @@
1618
* - May only appear at the very top of a Document’s contents.
1719
* - Must not appear more than once.
1820
*/
19-
class Charset implements AtRule
21+
class Charset implements AtRule, Positionable
2022
{
2123
use CommentContainer;
24+
use Position;
2225

2326
/**
2427
* @var CSSString
2528
*/
2629
private $charset;
2730

28-
/**
29-
* @var int<0, max>
30-
*
31-
* @internal since 8.8.0
32-
*/
33-
protected $lineNumber;
34-
3531
/**
3632
* @param int<0, max> $lineNumber
3733
*/
3834
public function __construct(CSSString $charset, int $lineNumber = 0)
3935
{
4036
$this->charset = $charset;
41-
$this->lineNumber = $lineNumber;
42-
}
43-
44-
/**
45-
* @return int<0, max>
46-
*/
47-
public function getLineNo(): int
48-
{
49-
return $this->lineNumber;
37+
$this->setPosition($lineNumber);
5038
}
5139

5240
/**

Diff for: src/Property/Import.php

+5-17
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66

77
use Sabberworm\CSS\Comment\CommentContainer;
88
use Sabberworm\CSS\OutputFormat;
9+
use Sabberworm\CSS\Position\Position;
10+
use Sabberworm\CSS\Position\Positionable;
911
use Sabberworm\CSS\Value\URL;
1012

1113
/**
1214
* Class representing an `@import` rule.
1315
*/
14-
class Import implements AtRule
16+
class Import implements AtRule, Positionable
1517
{
1618
use CommentContainer;
19+
use Position;
1720

1821
/**
1922
* @var URL
@@ -25,29 +28,14 @@ class Import implements AtRule
2528
*/
2629
private $mediaQuery;
2730

28-
/**
29-
* @var int<0, max>
30-
*
31-
* @internal since 8.8.0
32-
*/
33-
protected $lineNumber;
34-
3531
/**
3632
* @param int<0, max> $lineNumber
3733
*/
3834
public function __construct(URL $location, ?string $mediaQuery, int $lineNumber = 0)
3935
{
4036
$this->location = $location;
4137
$this->mediaQuery = $mediaQuery;
42-
$this->lineNumber = $lineNumber;
43-
}
44-
45-
/**
46-
* @return int<0, max>
47-
*/
48-
public function getLineNo(): int
49-
{
50-
return $this->lineNumber;
38+
$this->setPosition($lineNumber);
5139
}
5240

5341
public function setLocation(URL $location): void

0 commit comments

Comments
 (0)