Skip to content
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

Remove from CSSList via key #224

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion lib/Sabberworm/CSS/CSSList/CSSList.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,18 @@ public function splice($iOffset, $iLength = null, $mReplacement = null)

/**
* Removes an item from the CSS list.
* @param RuleSet|Import|Charset|CSSList $oItemToRemove May be a RuleSet (most likely a DeclarationBlock), a Import, a Charset or another CSSList (most likely a MediaQuery)
*
* @param RuleSet|Import|Charset|CSSList|int $oItemToRemove May be a RuleSet (most likely a DeclarationBlock), a Import, a Charset or another CSSList (most likely a MediaQuery)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly recommend not mixing types likes this in a parameter. Instead, I propose a new, separate method removeByKey(int $key).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to make the change but are the points raised in #224 (comment) still a concern with this PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the the problem I had was specifically with the replace method. The explanation I provided with my workaround in ampproject/amp-wp@d7c565c was:

This is being used instead of CSSList::splice() because it uses array_splice() which does not work properly
if the array keys are not sequentially indexed from 0, which happens when CSSList::remove() is employed.

I did the following to maintain a 0-indexed array:

$contents = array_values( $css_list->getContents() ); // Required to obtain the offset instead of the index.
$offset   = array_search( $old_item, $contents, true );
if ( false !== $offset ) {
	array_splice( $contents, $offset, 1, $new_items );
	$css_list->setContents( $contents );
	return true;
}
return false;

* @return bool Whether the item was removed.
*/
public function remove($oItemToRemove)
{
if (is_numeric($oItemToRemove) && array_key_exists($oItemToRemove, $this->aContents)) {
unset($this->aContents[$oItemToRemove]);

return true;
}

$iKey = array_search($oItemToRemove, $this->aContents, true);
if ($iKey !== false) {
unset($this->aContents[$iKey]);
Expand Down
12 changes: 12 additions & 0 deletions tests/Sabberworm/CSS/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,18 @@ public function testListValueRemoval()
#unrelated {other: yes;}', $oDoc->render());
}

public function testListValueRemovalByKey()
{
$oDoc = $this->parsedStructureForFile('atrules');
foreach ($oDoc->getContents() as $key => $oItem) {
if ($oItem instanceof AtRule) {
$oDoc->remove($key);
}
}

$this->assertSame('html, body {font-size: -.6em;}', $oDoc->render());
}

/**
* @expectedException Sabberworm\CSS\Parsing\OutputException
*/
Expand Down