insertHTML shouldn't create invalid selections
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Wed, 13 Jul 2011 09:31:55 -0600
changeset 411 2a28ff5625f3
parent 410 4cb683673c4e
child 412 acbbdc6ca892
insertHTML shouldn't create invalid selections

Previously we were putting the cursor inside the last node it added,
which we don't want if it was a comment. Also, this puts the cursor
after empty elements like <img> or <hr> instead of inside.
editcommands.html
implementation.js
source.html
tests.js
--- a/editcommands.html	Wed Jul 13 09:20:53 2011 -0600
+++ b/editcommands.html	Wed Jul 13 09:31:55 2011 -0600
@@ -38,7 +38,7 @@
 <body class=draft>
 <div class=head id=head>
 <h1>HTML Editing Commands</h1>
-<h2 class="no-num no-toc" id=work-in-progress-&mdash;-last-update-12-july-2011>Work in Progress &mdash; Last Update 12 July 2011</h2>
+<h2 class="no-num no-toc" id=work-in-progress-&mdash;-last-update-13-july-2011>Work in Progress &mdash; Last Update 13 July 2011</h2>
 <dl>
  <dt>Editor
  <dd>Aryeh Gregor &lt;ayg+spec@aryeh.name&gt;
@@ -6177,8 +6177,9 @@
 
   <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>Set 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> and <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-end title=concept-range-end>end</a> to
-  (<var title="">last child</var>, <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-node-length title=concept-node-length>length</a> of <var title="">last child</var>).
+  <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.
   <!-- Need to do this before fixing disallowed ancestors, since otherwise the
   last child might have been removed (e.g., it's an li). -->
 
--- a/implementation.js	Wed Jul 13 09:20:53 2011 -0600
+++ b/implementation.js	Wed Jul 13 09:31:55 2011 -0600
@@ -6337,10 +6337,11 @@
 		// "Call insertNode(frag) on the active range."
 		getActiveRange().insertNode(frag);
 
-		// "Set the active range's start and end to (last child, length of last
-		// child)."
-		getActiveRange().setStart(lastChild, getNodeLength(lastChild));
-		getActiveRange().setEnd(lastChild, getNodeLength(lastChild));
+		// "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."
+		getActiveRange().setStart(lastChild.parentNode, 1 + getNodeIndex(lastChild));
+		getActiveRange().setEnd(lastChild.parentNode, 1 + getNodeIndex(lastChild));
 
 		// "Fix disallowed ancestors of each member of descendants."
 		for (var i = 0; i < descendants.length; i++) {
--- a/source.html	Wed Jul 13 09:20:53 2011 -0600
+++ b/source.html	Wed Jul 13 09:31:55 2011 -0600
@@ -6172,8 +6172,9 @@
 
   <li>Call [[insertnode|<var>frag</var>]] on the <span>active range</span>.
 
-  <li>Set the <span>active range</span>'s [[rangestart]] and [[rangeend]] to
-  (<var>last child</var>, [[nodelength]] of <var>last child</var>).
+  <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.
   <!-- Need to do this before fixing disallowed ancestors, since otherwise the
   last child might have been removed (e.g., it's an li). -->
 
--- a/tests.js	Wed Jul 13 09:20:53 2011 -0600
+++ b/tests.js	Wed Jul 13 09:31:55 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>'],
+		['<!--abc-->', '<p>[foo]</p>'],
 	],
 	//@}
 	insertimage: [