Skip to content

[FEATURE] Add support for inserting an item in a CSSList #545

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 11 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[FEATURE] Support for inserting an item in a CSSList
Signed-off-by: Daniel Ziegenberg <daniel@ziegenberg.at>
Co-authored-by: Frederic Massart <fred@moodle.com>
  • Loading branch information
ziegenberg and Frederic Massart committed Jun 19, 2024
commit d4744a6ed9ca70db291f97804734f10b201ab103
21 changes: 21 additions & 0 deletions src/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,27 @@ public function splice($iOffset, $iLength = null, $mReplacement = null)
array_splice($this->aContents, $iOffset, $iLength, $mReplacement);
}

/**
* Insert an item before or after its sibling.
*
* @param RuleSet|CSSList|Import|Charset $oItem The item.
* @param RuleSet|CSSList|Import|Charset $oSibling The sibling.
* @param string $sPosition The position.
*
* @return void
*/
public function insert($oItem, $oSibling, string $sPosition = 'before'): void
{
$iIndex = in_array($oSibling, $this->aContents);
if ($iIndex === false) {
$this->append($oItem);
} elseif ($sPosition === 'before') {
$this->replace($oSibling, array($oItem, $oSibling));
} else {
$this->replace($oSibling, array($oSibling, $oItem));
}
}

/**
* Removes an item from the CSS list.
*
Expand Down
69 changes: 69 additions & 0 deletions tests/CSSList/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sabberworm\CSS\Tests\CSSList;

use Generator;
use PHPUnit\Framework\TestCase;
use Sabberworm\CSS\Comment\Commentable;
use Sabberworm\CSS\CSSList\Document;
Expand Down Expand Up @@ -85,4 +86,72 @@ public function setContentsReplacesContentsSetInPreviousCall(): void

self::assertSame($contents2, $this->subject->getContents());
}

/**
* @return Generator
*/
public static function insertDataProvider(): Generator
{

$bogusOne = new DeclarationBlock();
$bogusOne->setSelectors('.bogus-one');
$bogusTwo = new DeclarationBlock();
$bogusTwo->setSelectors('.bogus-two');

$oItem = new DeclarationBlock();
$oItem->setSelectors('.item');

$oSibling = new DeclarationBlock();
$oSibling->setSelectors('.sibling');

$oOrphan = new DeclarationBlock();
$oOrphan->setSelectors('.forever-alone');

yield 'insert before' => [
'initialContent' => [$bogusOne, $oSibling, $bogusTwo],
'oItem' => $oItem,
'oSibling' => $oSibling,
'position' => 'before',
'expectedContent' => [$bogusOne, $oItem, $oSibling, $bogusTwo],
];
yield 'insert after' => [
'initialContent' => [$bogusOne, $oSibling, $bogusTwo],
'oItem' => $oItem,
'oSibling' => $oSibling,
'position' => 'after',
'expectedContent' => [$bogusOne, $oSibling, $oItem, $bogusTwo],
];
yield 'append if not found' => [
'initialContent' => [$bogusOne, $oSibling, $bogusTwo],
'oItem' => $oItem,
'oSibling' => $oOrphan,
'position' => 'before',
'expectedContent' => [$bogusOne, $oSibling, $bogusTwo, $oItem],
];
}

/**
* @test
*
* @param array $contents
*
* @dataProvider insertDataProvider
*/
public function insertContent(
array $initialContent,
DeclarationBlock $oItem,
DeclarationBlock $oSibling,
string $sPosition,
array $expectedContent
) {

$this->subject->setContents($initialContent);

self::assertCount(3, $this->subject->getContents());

$this->subject->insert($oItem, $oSibling, $sPosition);

self::assertCount(4, $this->subject->getContents());
self::assertSame($expectedContent, $this->subject->getContents());
}
}