If you make it content editable, you are implicitly allowing the user to change the content of the HTML.
Pressing return should insert some kind of newline - either as closing a paragraph (</p>
) and starting a new one (<p>
), or entering a line break (<br>
). Both of which in HTML require HTML tags, for the simple fact that a standard newline character (eg. \n
or \n\r
) is rendered as a single space, which is not what the user would expect - so inserting a raw newline as a "soft wrap" would not make sense and will ultimately lead to users impotently slamming their enter key getting mad at your site for not inserting a break when they think it should.
An even more fun fact is that if a user highlights some text, they can (or should) be able to bold and italicize text using keyboard shortcuts, which will again insert HTML.
Short of writing Javascript to override this behaviour, I am not sure you can disable the enter key inserting HTML tags to render the requested newlines.
To demonstrate this, here is a really simple page:
<html>
<body>
<div contentEditable="true"> Some things.</div>
</body>
</html>
(In Internet Explorer at least) If you double click on the text it becomes editable. Move to the end of line and type the following:
- Enter - ( A new paragaph is made (wrapping the prior text in
p
tags).
- Type "
Words
", the select it and hit Crtl + b - the text is now wrapped in <strong>
tags.
- Hit Shift + Enter - a line break (
<br>
) is now inserted.
- Type "
More words
", select it and hit Crtl + i Its now italicised in <em>
tags.
And the source should look like:
<html>
<body>
<div contentEditable="true">
<p>Some things.</p>
<p>
<strong>Words</strong>
<br>
<em>More words</em>
</p>
</div>
</body>
</html>
If you want complete control over the content going into the area, I'd recommend using a WYSIWYG editor, or alternative, accept that the browser probably knows what its doing and let it do its thing so you don't need to worry about it.
[Enter]
causing HTML bothers me is because it's incredibly difficult to remember not to hit[Enter]
, and because it is inconsistent between browsers. – Gratification