I realize your rolling your own, but if you are interested in an available editor, we have SnapEditor which does exactly that.
One of the core features is that it keeps the nesting of blocks and tables consistent. All P, H1, H2, etc. tags are always at the top level. If they are not (for example, after a Paste), we automatically fix the HTML so that it is. In this way, you always have consistent, predictable, HTML which makes it easy to style and reason about.
It also works both as a form-based editor (traditional) or an inline editor (like Aloha editor).
Sunny