Hi, I also don't agree with the current behaviour.
I'd expect a <a href="whatever"></a> pair embracing my current selection, but not deleting it.
More over,
it seems somehow incoherent with other features in the editor, as example
the B, I, U buttons which just work in that way: adding a pair of tags around the selected text, without deleting it. Why should the <a> tag be different?
Maybe a tick could be added to the Insert Hyperlink dialogue in order to toggle that deletion on/off as desired?
(And of course, Kovid, forget about checking for valid/invalid HTML in either case: with or without deletion of the selected text. It's just TOO hard. I've just noticed that the only check the B/I/U buttons seem to do is that, if the selected text begins or ends in the middle of any tag, the added <b/i/u> tags avoid those incomplete tags. I do agree this is the
"good" behaviour, and possibly the only one you can easily and coherently code. But even doing that check, nothing guarantees you that the obtained HTML code is valid; an example: if the selected text is just a tag like "</div>" where you are in trouble it doesn't matter if you keep it or not).