Skip to content

Commit 47c9cda

Browse files
ziegenbergJakeQZ
authored andcommitted
[FEATURE] Add support for inserting an item in a CSSList (#545)
1 parent ef8c59c commit 47c9cda

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
66
## x.y.z
77

88
### Added
9+
- Add support for inserting an item in a CSS list (#545)
910

1011
### Changed
1112

src/CSSList/CSSList.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,22 @@ public function splice($iOffset, $iLength = null, $mReplacement = null)
296296
array_splice($this->aContents, $iOffset, $iLength, $mReplacement);
297297
}
298298

299+
/**
300+
* Inserts an item in the CSS list before its sibling. If the desired sibling cannot be found,
301+
* the item is appended at the end.
302+
*
303+
* @param RuleSet|CSSList|Import|Charset $item
304+
* @param RuleSet|CSSList|Import|Charset $sibling
305+
*/
306+
public function insertBefore($item, $sibling): void
307+
{
308+
if (in_array($sibling, $this->aContents, true)) {
309+
$this->replace($sibling, [$item, $sibling]);
310+
} else {
311+
$this->append($item);
312+
}
313+
}
314+
299315
/**
300316
* Removes an item from the CSS list.
301317
*

tests/CSSList/DocumentTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,59 @@ public function setContentsReplacesContentsSetInPreviousCall()
8585

8686
self::assertSame($contents2, $this->subject->getContents());
8787
}
88+
89+
/**
90+
* @test
91+
*/
92+
public function insertContentBeforeInsertsContentBeforeSibbling()
93+
{
94+
$bogusOne = new DeclarationBlock();
95+
$bogusOne->setSelectors('.bogus-one');
96+
$bogusTwo = new DeclarationBlock();
97+
$bogusTwo->setSelectors('.bogus-two');
98+
99+
$item = new DeclarationBlock();
100+
$item->setSelectors('.item');
101+
102+
$sibling = new DeclarationBlock();
103+
$sibling->setSelectors('.sibling');
104+
105+
$this->subject->setContents([$bogusOne, $sibling, $bogusTwo]);
106+
107+
self::assertCount(3, $this->subject->getContents());
108+
109+
$this->subject->insertBefore($item, $sibling);
110+
111+
self::assertCount(4, $this->subject->getContents());
112+
self::assertSame([$bogusOne, $item, $sibling, $bogusTwo], $this->subject->getContents());
113+
}
114+
115+
/**
116+
* @test
117+
*/
118+
public function insertContentBeforeAppendsIfSibblingNotFound()
119+
{
120+
$bogusOne = new DeclarationBlock();
121+
$bogusOne->setSelectors('.bogus-one');
122+
$bogusTwo = new DeclarationBlock();
123+
$bogusTwo->setSelectors('.bogus-two');
124+
125+
$item = new DeclarationBlock();
126+
$item->setSelectors('.item');
127+
128+
$sibling = new DeclarationBlock();
129+
$sibling->setSelectors('.sibling');
130+
131+
$orphan = new DeclarationBlock();
132+
$orphan->setSelectors('.forever-alone');
133+
134+
$this->subject->setContents([$bogusOne, $sibling, $bogusTwo]);
135+
136+
self::assertCount(3, $this->subject->getContents());
137+
138+
$this->subject->insertBefore($item, $orphan);
139+
140+
self::assertCount(4, $this->subject->getContents());
141+
self::assertSame([$bogusOne, $sibling, $bogusTwo, $item], $this->subject->getContents());
142+
}
88143
}

0 commit comments

Comments
 (0)