Use insertNode() for insertHorizontalRule too
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Fri, 13 May 2011 16:10:30 -0600
changeset 121 02f81db3e26b
parent 120 577781808581
child 122 4d3b2ecb3bf3
Use insertNode() for insertHorizontalRule too
editcommands.html
implementation.js
source.html
--- a/editcommands.html	Fri May 13 15:54:19 2011 -0600
+++ b/editcommands.html	Fri May 13 16:10:30 2011 -0600
@@ -2360,39 +2360,16 @@
 <ol>
   <li>Run <code class=external data-anolis-spec=domrange title=dom-Range-deleteContents><a href=http://html5.org/specs/dom-range.html#dom-range-deletecontents>deleteContents()</a></code> on the <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range title=concept-range>range</a>.
 
-  <li>Let (<var title="">node</var>, <var title="">offset</var>) be the <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range title=concept-range>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>.
-
-  <li>If <var title="">node</var> is a <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> or <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#comment>Comment</a></code> node and its <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>
-  is null, abort these steps and do nothing further.
-
   <li>Let <var title="">hr</var> be the result of calling <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("hr")</a></code> on the
-  <code class=external data-anolis-spec=domcore title=dom-Node-ownerDocument><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-ownerdocument>ownerDocument</a></code> of <var title="">node</var> (or on <var title="">node</var> itself if it's a
-  <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#document>Document</a></code>).
-
-  <li>If <var title="">node</var> is a <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> node, and <var title="">offset</var> is not
-  equal to 0 or the <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="">node</var>, run <code class=external data-anolis-spec=domcore title=dom-Text-splitText><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-text-splittext>splitText(<var title="">offset</var>)</a></code> on
-  <var title="">node</var>.
-
-  <li>If <var title="">node</var> is a <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> node, and <var title="">offset</var> is equal to
-  the <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="">node</var>, set <var title="">node</var> to its
-  <code class=external data-anolis-spec=domcore title=dom-Node-nextSibling><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-nextsibling>nextSibling</a></code>.
-
-  <li>If <var title="">node</var> is null or is a <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> or <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#comment>Comment</a></code> node, run
-  <code class=external data-anolis-spec=domcore title=dom-Node-insertBefore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-insertbefore>insertBefore(<var title="">hr</var>,
-  <var title="">node</var>)</a></code> on the parent of <var title="">node</var>.
-
-  <li>Otherwise, let <var title="">child</var> be the <var title="">offset</var>th child of
-  <var title="">node</var> (or null if there is no such child), and run <code class=external data-anolis-spec=domcore title=dom-Node-insertBefore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-insertbefore>insertBefore(<var title="">hr</var>,
-  <var title="">child</var>)</a></code> on <var title="">node</var>.
-
-  <p class=XXX>This is wrong: it can insert the new element inside an inline
-  element, which is invalid.  Browsers break up ancestor inlines to avoid this.
-  So far I've managed to avoid having to do this, which is good because it
-  causes problems (e.g., what if an inline ancestor has an id?), but I don't
-  see any way around it here.  Maybe I need to revisit the insistence on only
-  modifying "modifiable elements"; if non-modifiable elements sneak in somehow,
-  perhaps we need to break them up anyway sometimes.
+  <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#context-object>context object</a>.
+
+  <li>Run <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="">hr</var>)</a></code> on the <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range title=concept-range>range</a>.
+  <!-- This might put it in an inline element, where it's invalid.  This is
+  what IE9 and Chrome 12 dev appear to do.  Firefox 4.0 breaks up inline
+  ancestors, and Opera 11.10 seems to put it before the first inline ancestor
+  instead of where the cursor is.  Breaking up elements is kind of evil, so
+  I'll go with IE/WebKit.  Even though it produces invalid markup, it does
+  still work. -->
 
   <li>Run <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 <code class=external data-anolis-spec=domrange><a href=http://html5.org/specs/dom-range.html#selection>Selection</a></code>, with
   first argument equal to the <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> of <var title="">hr</var> and the second
--- a/implementation.js	Fri May 13 15:54:19 2011 -0600
+++ b/implementation.js	Fri May 13 16:10:30 2011 -0600
@@ -2460,52 +2460,12 @@
 		// "Run deleteContents() on the range."
 		range.deleteContents();
 
-		// "Let (node, offset) be the range's start."
-		var node = range.startContainer;
-		var offset = range.startOffset;
-
-		// "If node is a Text or Comment node and its parent is null, abort
-		// these steps and do nothing."
-		if (!node.parentNode
-		&& (node.nodeType == Node.TEXT_NODE
-		|| node.nodeType == Node.COMMENT_NODE)) {
-			return;
-		}
-
 		// "Let hr be the result of calling createElement("hr") on the
-		// ownerDocument of node (or on node itself if it's a Document)."
-		var hr = (node.nodeType == Node.DOCUMENT_NODE
-			? node : node.ownerDocument).createElement("hr");
-
-		// "If node is a Text node, and offset is not equal to 0 or the length
-		// of node, run splitText(offset) on node."
-		if (node.nodeType == Node.TEXT_NODE
-		&& offset != 0
-		&& offset != node.length) {
-			node.splitText(offset);
-		}
-
-		// "If node is a Text node, and offset is equal to the length of node,
-		// set node to its nextSibling."
-		if (node.nodeType == Node.TEXT_NODE
-		&& offset == node.length) {
-			node = node.nextSibling;
-		}
-
-		// "If node is null or is a Text or Comment node, run insertBefore(hr,
-		// node) on the parent of node."
-		if (!node
-		|| node.nodeType == Node.TEXT_NODE
-		|| node.nodeType == Node.COMMENT_NODE) {
-			node.parentNode.insertBefore(hr, node);
-		// "Otherwise, let child be the offsetth child of node (or null if
-		// there is no such child), and run insertBefore(img, child) on node."
-		} else {
-			var child = node.childNodes.length == offset
-				? null
-				: node.childNodes[offset];
-			node.insertBefore(hr, child);
-		}
+		// context object."
+		var hr = document.createElement("hr");
+
+		// "Run insertNode(hr) on the range."
+		range.insertNode(hr);
 
 		// "Run collapse() on the Selection, with first argument equal to the
 		// parent of hr and the second argument equal to one plus the index of
--- a/source.html	Fri May 13 15:54:19 2011 -0600
+++ b/source.html	Fri May 13 16:10:30 2011 -0600
@@ -2391,46 +2391,19 @@
   <li>Run <code data-anolis-spec=domrange
   title=dom-Range-deleteContents>deleteContents()</code> on the [[range]].
 
-  <li>Let (<var>node</var>, <var>offset</var>) be the [[range]]'s
-  [[rangestart]].
-
-  <li>If <var>node</var> is a [[text]] or [[comment]] node and its [[parent]]
-  is null, abort these steps and do nothing further.
-
   <li>Let <var>hr</var> be the result of calling <code
   data-anolis-spec=domcore
   title=dom-Document-createElement>createElement("hr")</code> on the
-  [[ownerdocument]] of <var>node</var> (or on <var>node</var> itself if it's a
-  [[document]]).
-
-  <li>If <var>node</var> is a [[text]] node, and <var>offset</var> is not
-  equal to 0 or the [[nodelength]] of <var>node</var>, run <code
-  data-anolis-spec=domcore
-  title=dom-Text-splitText>splitText(<var>offset</var>)</code> on
-  <var>node</var>.
-
-  <li>If <var>node</var> is a [[text]] node, and <var>offset</var> is equal to
-  the [[nodelength]] of <var>node</var>, set <var>node</var> to its
-  [[nextsibling]].
-
-  <li>If <var>node</var> is null or is a [[text]] or [[comment]] node, run
-  <code data-anolis-spec=domcore
-  title=dom-Node-insertBefore>insertBefore(<var>hr</var>,
-  <var>node</var>)</code> on the parent of <var>node</var>.
-
-  <li>Otherwise, let <var>child</var> be the <var>offset</var>th child of
-  <var>node</var> (or null if there is no such child), and run <code
-  data-anolis-spec=domcore
-  title=dom-Node-insertBefore>insertBefore(<var>hr</var>,
-  <var>child</var>)</code> on <var>node</var>.
-
-  <p class=XXX>This is wrong: it can insert the new element inside an inline
-  element, which is invalid.  Browsers break up ancestor inlines to avoid this.
-  So far I've managed to avoid having to do this, which is good because it
-  causes problems (e.g., what if an inline ancestor has an id?), but I don't
-  see any way around it here.  Maybe I need to revisit the insistence on only
-  modifying "modifiable elements"; if non-modifiable elements sneak in somehow,
-  perhaps we need to break them up anyway sometimes.
+  [[contextobject]].
+
+  <li>Run <code data-anolis-spec=domrange
+  title=dom-Range-insertNode>insertNode(<var>hr</var>)</code> on the [[range]].
+  <!-- This might put it in an inline element, where it's invalid.  This is
+  what IE9 and Chrome 12 dev appear to do.  Firefox 4.0 breaks up inline
+  ancestors, and Opera 11.10 seems to put it before the first inline ancestor
+  instead of where the cursor is.  Breaking up elements is kind of evil, so
+  I'll go with IE/WebKit.  Even though it produces invalid markup, it does
+  still work. -->
 
   <li>Run <code data-anolis-spec=domrange
   title=dom-Selection-collapse>collapse()</code> on the [[selection]], with