I'm worried that you're doing a slice on the `contents` here and assuming that the `li` is the first content. If you have a text node before the `$firstPreviousLi`, then that text node will get dropped. Right?

But honestly, I don't understand exactly what case this is trying to handle and why it's doing what it's doing. Maybe comments and new method names will help.