That happens if you do your replacement before Wakaba does its own formatting, because it will think the user typed in "<span class="spoiler">". You need to do it only after Wakaba has processed the comment (or possibly at the same time, if you're being clever).