After reading this guide, you will
- Know about browser specific issues with empty block level elements
- Understand how Aloha Editor will handle paragraphs and line breaks
- Contents will be deleted
1 Empty Block Level Elements
Generally, empty block level elements are not rendered by browsers. This will lead to some problems when it comes to contenteditable.
1.1 Problems while editing
Suppose you start editing with the following HTML (where  marks the current cursor position):
After hitting the Enter key, you would expect to get
<p>First paragraph</p> <p></p>
Visually, you should see two paragraphs now, the first one containing the words “First Paragraph”, while the second should be empty (but consuming one line in height) with the cursor blinking in it. But, since most browsers will not render empty paragraphs (even in contenteditable areas), you would not properly see the second paragraph.
To fix this situation, Aloha Editor will instead generate HTML like this (note the additional <br>s added):
<p>First paragraph<br /></p> <p><br /></p>
Everything is fine now, the additional <br>s at the end of <p>s do not change the rendering of non-empty paragraphs, but make the empty ones appear.
At least, everything would be fine, if there was no Internet Explorer. IE is the only browser, that renders even empty <p>, when found in contenteditable areas, and – even worse – would add additional empty lines if <br>s are found at the end of <p>s.
When using Internet Exporer 7, however, things get much worse: Internet Exporer 7 renders empty <p> only, if they are the last elements. If you have an empty <p> between two non-empty <p>, it will not be rendered. If the last <p> contains the propping end
, it would show an additional line (but this is only true for the last <p>. Therefore, the Implementation of Aloha Editor for Internet Exporer 7 is special, as described below.
1.2 Main goal in Aloha Editor
The main goals in Aloha Editor are
- Identical editing experience across all browsers and platforms. Therefore, browser differences must be compensated by implementation wherever possible.
- What You See Is What You Get: If an editor hits the Enter key multiple times, and creates multiple empty paragraphs, that’s exactly what the resulting HTML code should render. Therefore “empty” paragraphs must not be really empty, but need to contain at least a single <br> in the resulting HTML code.
1.3 Implemented behaviour
The current implementation of Aloha Editor regarding block level elements is like follows:
When initializing an editable:
- remove all empty block level elements (since they are invisible anyway)
- add zero-width spaces to all empty block level elements when using Internet Explorer 7
- add <br /> to all <li> elements, except when in Internet Exporer
- remove any trailing
element in block level elements if in Internet Explorer
- For every operation, that might lead to an empty block level elements (hitting ENTER, deleting, …), fix the empty block level element by appending a <br /> (except when editing in Internet Explorer). For Internet Explorer 7, instead of <br /> the zero-width spaces will be used.
When getting the editable’s content (e.g. for saving) with the getContents() method:
- Remove all end <br /> tags contained in <li>.
The current implementation handles
- <h1> – <h6>
The removal of trailing line break elements (
<br/>) in Internet Explorer at the point of initializing Editables is a work-around for IE’s nonconformance to the W3C specification. The consequence of this work-around ought to be noted:
In the common case of persisting changes to documents edited with Aloha Editor, if a block level element contains numerous trailing
<br/>s, each time the document is opened for editing and then saved, it will loose one of the trailing line breaks until eventually none is left—even if no manual editing is done in the editable. This would be another reason not to use line break elements to space your content; there are better means to achieve the same.
Initialization and getContents behaviour is implemented in the Blockelement Content Handler.
2 Paragraphs & Line Breaks
Paragraphs and line breaks are a fundamental part of editing text. Aloha Editor will decide for you which kind of line break to use if certain preconditions are met. Both kind of line breaks can be created using the enter key.
Pressing enter when contents are selected will remove those contents.
2.1 Creating Paragraphs <p>
To have the enter key create a new paragraph the cursor has to be located inside an existing paragraph. Every time you press enter, the existing paragraph will be split at the cursor position resulting in two paragraphs.
2.2 Creating Line Breaks <br>
If the cursor is within an editable, but no wrapping paragraph can be detected a <br> is inserted instead of a <p>. If you are inside a paragraph you may insert a line break at any time by pressing Shift+Enter.
3 Deletion of Contents
If you want to delete contents you may use the Del Button to issue a forwardDelete command or the Backspace key to trigger a delete command.
If contents have been selected both forwardDelete and delete will remove the selected contents in the same manner.
Forward delete means deleting characters that are right of your current caret position. If the selection is collapsed every time the Del key is hit a single character will be deleted. If you keep pressing Del and an inline formatting (like <b>) is encountered, the formatting will be kept for the remaining characters unless all of the formatted content is gone. If you encounter a phrasing element like a <h1> tag the contents will be unwrapped before character deletion is continued.
The delete command will delte characters left of your current caret position. Deleting inline formattings will work the same way as with forwardDelete, but deleting phrasing elements will open up the end of the phrasing element which will then engulf the rest of the contents until the end of the current paragraph or comparable wrapping phrasing element is hit.