-
Notifications
You must be signed in to change notification settings - Fork 148
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
base: main
Are you sure you want to change the base?
Conversation
Shouldn't you rather than use the Aside: it seems |
How come? It seems to work OK as it is. |
@@ -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) |
There was a problem hiding this comment.
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)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 usesarray_splice()
which does not work properly
if the array keys are not sequentially indexed from 0, which happens whenCSSList::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;
If you're looping over
getContents()
and want to remove items from theCSSList
it would be more performant to remove items by the key given it's already known. The current method usesarray_search
which is wasteful in the specified scenario.