Skip to content

[BUGFIX] Ensure valid position after AddRule #1271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Methods `getLineNumber` and `getColumnNumber` which return a nullable `int`
for the following classes:
`Comment`, `CSSList`, `SourceException`, `Charset`, `CSSNamespace`, `Import`,
`Rule`, `DeclarationBlock`, `RuleSet`, `CSSFunction`, `Value` (#1225)
`Rule`, `DeclarationBlock`, `RuleSet`, `CSSFunction`, `Value` (#1225, #1263)
- `Positionable` interface for CSS items that may have a position
(line and perhaps column number) in the parsed CSS (#1221)

Expand Down Expand Up @@ -55,6 +55,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).

### Fixed

- Set line number when `RuleSet::addRule()` called with only column number set
(#1265)
- Ensure first rule added with `RuleSet::addRule()` has valid position (#1262)

## 8.8.0: Bug fixes and deprecations

### Added
Expand Down
9 changes: 7 additions & 2 deletions src/RuleSet/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,19 @@ public function addRule(Rule $oRule, $oSibling = null)
$oRule->setPosition($oSibling->getLineNo(), $oSibling->getColNo() - 1);
}
}
if ($oRule->getLineNo() === 0 && $oRule->getColNo() === 0) {
if ($oRule->getLineNumber() === null) {
//this node is added manually, give it the next best line
$columnNumber = $oRule->getColNo();
$rules = $this->getRules();
$pos = count($rules);
if ($pos > 0) {
$last = $rules[$pos - 1];
$oRule->setPosition($last->getLineNo() + 1, 0);
$oRule->setPosition($last->getLineNo() + 1, $columnNumber);
} else {
$oRule->setPosition(1, $columnNumber);
}
} elseif ($oRule->getColumnNumber() === null) {
$oRule->setPosition($oRule->getLineNumber(), 0);
}

array_splice($this->aRules[$sRule], $iPosition, 0, [$oRule]);
Expand Down
13 changes: 3 additions & 10 deletions tests/Unit/RuleSet/RuleSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ public function addRuleWithoutSiblingAddsRuleAfterInitialRulesAndSetsValidLineAn
array $initialPropertyNames,
string $propertyNameToAdd
) {
if ($initialPropertyNames === []) {
self::markTestSkipped('currently broken - first rule added does not have valid line number set');
}

$subject = new ConcreteRuleSet();
$ruleToAdd = new Rule($propertyNameToAdd);
self::setRulesFromPropertyNames($subject, $initialPropertyNames);
Expand All @@ -106,8 +102,6 @@ public function addRuleWithOnlyLineNumberAddsRuleAndSetsColumnNumberPreservingLi
array $initialPropertyNames,
string $propertyNameToAdd
) {
self::markTestSkipped('currently broken - does not set column number');

$subject = new ConcreteRuleSet();
$ruleToAdd = new Rule($propertyNameToAdd);
$ruleToAdd->setPosition(42);
Expand All @@ -128,20 +122,19 @@ public function addRuleWithOnlyLineNumberAddsRuleAndSetsColumnNumberPreservingLi
*
* @param list<string> $initialPropertyNames
*/
public function addRuleWithOnlyColumnNumberAddsRuleAndSetsLineNumberPreservingColumnNumber(
public function addRuleWithOnlyColumnNumberAddsRuleAfterInitialRulesAndSetsLineNumberPreservingColumnNumber(
array $initialPropertyNames,
string $propertyNameToAdd
) {
self::markTestSkipped('currently broken - does not preserve column number');

$subject = new ConcreteRuleSet();
$ruleToAdd = new Rule($propertyNameToAdd);
$ruleToAdd->setPosition(null, 42);
self::setRulesFromPropertyNames($subject, $initialPropertyNames);

$subject->addRule($ruleToAdd);

self::assertContains($ruleToAdd, $subject->getRules());
$rules = $subject->getRules();
self::assertSame($ruleToAdd, \end($rules));
self::assertInternalType('int', $ruleToAdd->getLineNumber(), 'line number not set');
self::assertGreaterThanOrEqual(1, $ruleToAdd->getLineNumber(), 'line number not valid');
self::assertSame(42, $ruleToAdd->getColumnNumber(), 'column number not preserved');
Expand Down