Stray HTML tags in content when deleting text in writer field

A client just reported that a section is still showing up even though it’s empty. I found that there was an empty <h2></h2> remaining that made the isNotEmpty() condition false. Turns out that if you manually highlight all text and delete it (or if you just delete characters one by one using the backspace key), the heading element isn’t deleted, you have to do Cmd/Ctrl-A and backspace to delete everything. I have the feeling this is more a bug than a feature. You never know how people interact with fields and manually deleting the last remaining character should also delete any formatting that is remaining, or am I wrong?

Can you give us any more details to reproduce your problem?
Which field did you use? headline, writer, textarea etc.?
What does the code in the template for the output look like?

It is a writer field. The template code is pretty simple, like

$content = $page->example();
if($content->isNotEmpty()):
  echo($page->example());
endif;

Create a heading inside that field. Then place the cursor at the end of the text and delete it with the backspace key. There will be an empty heading element remaining that you can only get rid of by Cmd/Ctrl-A and backspace/delete.

As a first aid remedy, you could change your condition to check if

if (strip_tags($page->example()) !== '') {
  // do stuff
}