Skip to content

Commit 3163c9f

Browse files
ziegenbergFrederic Massart
and
Frederic Massart
authored
[FEATURE] Add support for inserting an item in a CSSList (#545)
Signed-off-by: Daniel Ziegenberg <[email protected]> Co-authored-by: Frederic Massart <[email protected]>
1 parent 690e330 commit 3163c9f

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

CHANGELOG.md

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

88
### Added
9-
9+
- Add support for inserting an item in a CSS list (#545)
1010
- Add a class diagram to the README (#482)
1111
- Add support for the `dvh`, `lvh` and `svh` length units (#415)
1212
- Add more tests (#449)

src/CSSList/CSSList.php

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

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

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(): void
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)