contenteditable div backspace and deleting text node problems

After breaking down how google uses contenteditable divs in their google plus user tagging, I landed on a much more reasonable solution. Maybe it will help someone else out.

Google Plus Post Widget

After adding 1 tag, you can already see a lot of differences in the html browser to browser.

Google Chrome Source

In Google Chrome, a space is added with each tag. The button tag is used. And the chrome-only contenteditable="plaintext-only" is used.

Google Chroem Source

When I backspace the space in chrome, a BR tag is then appended.

enter image description here

In Firefox the BR tag is added immediately with the first tag. No spaces are needed. And an input tag is used instead of the button tag.

The BR tag was the single greatest break-through I had while digging through this. Before adding this, there was a lot of quirky behavior with deleting tags, as well as focus issues.

enter image description here

In IE, more interesting changes were made. A span with contenteditable false is used for the tags here. No spaces or BR tags, but an empty text node.

With all of that, you don't have to copy google exactly.

The important parts:

If you're rendering HTML, do the following...

1. Chrome should use the button tag

2. Firefox/IE should use the input tag

For range/selection you generally want to treat things like tags as a single character. You can build this into your range/selection logic, but the behavior of the input/button tags is much more consistent, and way less code.

IE behaves better in IE7-8 using a span. Just from a UI standpoint. But if you don't care if your site is pretty in old versions of IE, the input has the correct behaviour in IE as well as firefox.

3. Chrome only, use the contenteditable="plaintext-only" attribute on your editable div.

Otherwise, a lot of weird issues happen not only when a user tries to paste rich-text, but also when deleting html elements sometimes the styles can get transferred to the div, I noted many strange issues with this.

4. If you need to set the caret position to the end of the div, set the end of the range before the BR.

for FireFox:

range.setEndBefore($(el).find('br')[0]);