Insert <br> if necessary after insertHTML
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Wed, 13 Jul 2011 09:39:59 -0600
changeset 412 acbbdc6ca892
parent 411 2a28ff5625f3
child 413 874af72e14e0
Insert <br> if necessary after insertHTML
editcommands.html
implementation.js
source.html
tests.js
--- a/editcommands.html	Wed Jul 13 09:31:55 2011 -0600
+++ b/editcommands.html	Wed Jul 13 09:39:59 2011 -0600
@@ -6177,6 +6177,15 @@
 
   <li>Call <code class=external data-anolis-spec=domrange title=dom-Range-insertNode><a href=http://html5.org/specs/dom-range.html#dom-range-insertnode>insertNode(<var title="">frag</var>)</a></code> on the <a href=#active-range>active range</a>.
 
+  <li>If the <a href=#active-range>active range</a>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-start title=concept-range-start>start</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-node title=concept-boundary-point-node>node</a> is a <a href=#block-node>block
+  node</a> with no <a href=#visible>visible</a> <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-tree-child title=concept-tree-child>children</a>, call
+  <code class=external data-anolis-spec=domcore title=dom-Document-createElement><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-document-createelement>createElement("br")</a></code> on the <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#context-object>context object</a> and append the result as the
+  last child of the <a href=#active-range>active range</a>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-start title=concept-range-start>start</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-node title=concept-boundary-point-node>node</a>.
+  <!-- In case we remove all the contents, then remove the extra <br>, then
+  only add a comment or something.  In that case we want to re-add the extra
+  <br>.  We don't try fixing the actual inserted content: that's the author's
+  lookout. -->
+
   <li>Call <code class=external data-anolis-spec=domrange title=dom-Selection-collapse><a href=http://html5.org/specs/dom-range.html#dom-selection-collapse>collapse()</a></code> on the <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#context-object>context object</a>'s <code class=external data-anolis-spec=domrange><a href=http://html5.org/specs/dom-range.html#selection>Selection</a></code>, with
   <var title="">last child</var>'s <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-tree-parent title=concept-tree-parent>parent</a> as the first argument and one plus its
   <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-indexof title=concept-indexof>index</a> as the second.
--- a/implementation.js	Wed Jul 13 09:31:55 2011 -0600
+++ b/implementation.js	Wed Jul 13 09:39:59 2011 -0600
@@ -6337,6 +6337,14 @@
 		// "Call insertNode(frag) on the active range."
 		getActiveRange().insertNode(frag);
 
+		// "If the active range's start node is a block node with no visible
+		// children, call createElement("br") on the context object and append
+		// the result as the last child of the active range's start node."
+		if (isBlockNode(getActiveRange().startContainer)
+		&& ![].some.call(getActiveRange().startContainer.childNodes, isVisible)) {
+			getActiveRange().startContainer.appendChild(document.createElement("br"));
+		}
+
 		// "Call collapse() on the context object's Selection, with last
 		// child's parent as the first argument and one plus its index as the
 		// second."
--- a/source.html	Wed Jul 13 09:31:55 2011 -0600
+++ b/source.html	Wed Jul 13 09:39:59 2011 -0600
@@ -6172,6 +6172,15 @@
 
   <li>Call [[insertnode|<var>frag</var>]] on the <span>active range</span>.
 
+  <li>If the <span>active range</span>'s [[startnode]] is a <span>block
+  node</span> with no <span>visible</span> [[children]], call
+  [[createelement|"br"]] on the [[contextobject]] and append the result as the
+  last child of the <span>active range</span>'s [[startnode]].
+  <!-- In case we remove all the contents, then remove the extra <br>, then
+  only add a comment or something.  In that case we want to re-add the extra
+  <br>.  We don't try fixing the actual inserted content: that's the author's
+  lookout. -->
+
   <li>Call [[selcollapse|]] on the [[contextobject]]'s [[selection]], with
   <var>last child</var>'s [[parent]] as the first argument and one plus its
   [[index]] as the second.
--- a/tests.js	Wed Jul 13 09:31:55 2011 -0600
+++ b/tests.js	Wed Jul 13 09:39:59 2011 -0600
@@ -1711,6 +1711,7 @@
 		['<p>abc', '<font color=red>foo[]bar</font>'],
 		['<p>abc', '<span style=color:red>foo[]bar</span>'],
 		['<p>abc', '<span style=font-variant:small-caps>foo[]bar</span>'],
+		['<span style=display:none></span>', '<p>[foo]</p>'],
 		['<!--abc-->', '<p>[foo]</p>'],
 	],
 	//@}