Conversation
|
Shouldn't you rather than use the Aside: it seems |
How come? It seems to work OK as it is. |
| * 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.
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.
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.
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 theCSSListit would be more performant to remove items by the key given it's already known. The current method usesarray_searchwhich is wasteful in the specified scenario.