Refactor a couple hundred lines
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Mon, 16 May 2011 16:32:01 -0600
changeset 134 148bfcffd767
parent 133 90eaf6cf8106
child 135 c102770cdb86
Refactor a couple hundred lines

More to come. insertOrderedList will be a heck of a lot simpler when
I'm done.
editcommands.html
implementation.js
source.html
--- a/editcommands.html	Mon May 16 15:34:29 2011 -0600
+++ b/editcommands.html	Mon May 16 16:32:01 2011 -0600
@@ -270,8 +270,7 @@
 </ol>
 
 <p>To <dfn id=split-the-parent>split the parent</dfn> of a list <var title="">node list</var> of consecutive
-<a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-tree-sibling title=concept-tree-sibling>sibling</a> <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-node title=concept-node>nodes</a>, with <var title="">new parent</var> either null or an
-<code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#element>Element</a></code>:
+<a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-tree-sibling title=concept-tree-sibling>sibling</a> <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-node title=concept-node>nodes</a>:
 
 <ol>
   <li>Let <var title="">original parent</var> be 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 the first member of
@@ -305,32 +304,17 @@
   </div>
 
   <ol>
-    <li>If <var title="">new parent</var> is null:
-
-    <ol>
-      <li>If the last member of <var title="">node list</var> and the <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> of
-      <var title="">original parent</var> are both <a href=#inline-node title="inline node">inline
-      nodes</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
-      <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="">original parent</var>, then insert the result
-      into 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="">original parent</var> immediately after
-      <var title="">original parent</var>.
-
-      <li>For each <var title="">node</var> in <var title="">node list</var>, <em>in reverse
-      order</em>, insert <var title="">node</var> into 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="">original
-      parent</var> immediately after <var title="">original parent</var>,
-      <a href=#preserving-ranges>preserving ranges</a>.
-    </ol>
-
-    <li>Otherwise:
-
-    <ol>
-      <li>Insert <var title="">new parent</var> into 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="">original
-      parent</var> immediately after <var title="">original parent</var>.
-
-      <li>For each <var title="">node</var> in <var title="">node list</var>, <em>in reverse
-      order</em>, insert <var title="">node</var> as the first <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>child</a> of <var title="">new
-      parent</var>, <a href=#preserving-ranges>preserving ranges</a>.
-    </ol>
+    <li>If the last member of <var title="">node list</var> and the <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> of
+    <var title="">original parent</var> are both <a href=#inline-node title="inline node">inline
+    nodes</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
+    <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="">original parent</var>, then insert the result
+    into 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="">original parent</var> immediately after
+    <var title="">original parent</var>.
+
+    <li>For each <var title="">node</var> in <var title="">node list</var>, <em>in reverse
+    order</em>, insert <var title="">node</var> into 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="">original
+    parent</var> immediately after <var title="">original parent</var>, <a href=#preserving-ranges>preserving
+    ranges</a>.
 
     <li>Abort these steps.
   </ol>
@@ -358,30 +342,56 @@
     <a href=#preserving-ranges>preserving ranges</a>.
   </ol>
 
-  <li>If <var title="">new parent</var> is null:
-
-  <ol>
-    <li>If the first member of <var title="">node list</var> and the <code class=external data-anolis-spec=domcore title=dom-Node-previousSibling><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-previoussibling>previousSibling</a></code>
-    of <var title="">original parent</var> are both <a href=#inline-node title="inline node">inline
-    nodes</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
-    <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="">original parent</var>, then insert the result
-    into 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="">original parent</var> immediately before
-    <var title="">original parent</var>.
-
-    <li>For each <var title="">node</var> in <var title="">node list</var>, insert
-    <var title="">node</var> into 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="">original parent</var>
-    immediately before <var title="">original parent</var>, <a href=#preserving-ranges>preserving
-    ranges</a>.
-
-    <li>Abort these steps.
-  </ol>
-
-  <li>Insert <var title="">new parent</var> into 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="">original
-  parent</var> immediately before <var title="">original parent</var>.
+  <li>If the first member of <var title="">node list</var> and the <code class=external data-anolis-spec=domcore title=dom-Node-previousSibling><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-previoussibling>previousSibling</a></code>
+  of <var title="">original parent</var> are both <a href=#inline-node title="inline node">inline
+  nodes</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
+  <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="">original parent</var>, then insert the result into
+  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="">original parent</var> immediately before <var title="">original
+  parent</var>.
+
+  <li>For each <var title="">node</var> in <var title="">node list</var>, insert <var title="">node</var>
+  into 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="">original parent</var> immediately before
+  <var title="">original parent</var>, <a href=#preserving-ranges>preserving ranges</a>.
+</ol>
+
+<p>To <dfn id=wrap>wrap</dfn> a list <var title="">node list</var> of consecutive <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-tree-sibling title=concept-tree-sibling>sibling</a>
+<a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-node title=concept-node>nodes</a>, given <dfn id=sibling-criteria>sibling criteria</dfn> and <dfn id=new-parent-instructions>new parent
+instructions</dfn>:
+
+<ol>
+  <li>If <var title="">node list</var> is empty, or the first member of <var title="">node
+  list</var> is not <a href=#editable>editable</a>, abort these steps.
+
+  <li>If the <code class=external data-anolis-spec=domcore title=dom-Node-previousSibling><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-previoussibling>previousSibling</a></code> of the first member of <var title="">node list</var>
+  is <a href=#editable>editable</a> and meets the <a href=#sibling-criteria>sibling criteria</a>, then
+  for each <var title="">node</var> in <var title="">node list</var>, append <var title="">node</var> as
+  the last <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>child</a> of its <code class=external data-anolis-spec=domcore title=dom-Node-previousSibling><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-previoussibling>previousSibling</a></code>, <a href=#preserving-ranges>preserving
+  ranges</a>.  Then abort these steps.
+
+  <li>If the <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> of the last member of <var title="">node list</var> is
+  <a href=#editable>editable</a> and meets the <a href=#sibling-criteria>sibling criteria</a>, then for
+  each <var title="">node</var> in <var title="">node list</var>, <em>in reverse order</em>,
+  insert <var title="">node</var> as the first <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>child</a> of 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>,
+  <a href=#preserving-ranges>preserving ranges</a>.  Then abort these steps.
+
+  <li>Run the <a href=#new-parent-instructions>new parent instructions</a>, and let <var title="">new
+  parent</var> be the result.
+
+  <li>If <var title="">new parent</var> cannot be the <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>child</a> of 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 the
+  first member of <var title="">node list</var>, <a href=#split-the-parent>split the parent</a> of
+  <var title="">node list</var>.
+
+  <p class=XXX>"Cannot be the child" needs to be defined.
+
+  <li>Insert <var title="">new parent</var> into 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 the first member of
+  <var title="">node list</var> immediately before the first member of <var title="">node
+  list</var>.
 
   <li>For each <var title="">node</var> in <var title="">node list</var>, append <var title="">node</var>
   as the last <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>child</a> of <var title="">new parent</var>, <a href=#preserving-ranges>preserving
   ranges</a>.
+
+  <li><a href=#remove-extraneous-line-breaks>Remove extraneous line breaks</a> from <var title="">new parent</var>.
 </ol>
 
 <p>To <dfn id=remove-extraneous-line-breaks>remove extraneous line breaks</dfn> from a <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-node title=concept-node>node</a> <var title="">node</var>:
@@ -2242,23 +2252,11 @@
     <li>Let <var title="">tag</var> be the <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-element-local-name title=concept-element-local-name>local name</a> of 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="">first node</var>.
 
-    <li>If the <code class=external data-anolis-spec=domcore title=dom-Node-previousSibling><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-previoussibling>previousSibling</a></code> of the first member of <var title="">node list</var>
-    is an <a href=#html-element>HTML element</a> with <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-element-local-name title=concept-element-local-name>local name</a> <var title="">tag</var>, then for
-    each <var title="">node</var> in <var title="">node list</var>, append <var title="">node</var> as the
-    last <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>child</a> of its <code class=external data-anolis-spec=domcore title=dom-Node-previousSibling><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-previoussibling>previousSibling</a></code>, <a href=#preserving-ranges>preserving ranges</a>.
-    Then abort these steps.
-
-    <li>If the <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> of the last member of <var title="">node list</var> is an
-    <a href=#html-element>HTML element</a> with <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-element-local-name title=concept-element-local-name>local name</a> <var title="">tag</var>, then for each
-    <var title="">node</var> in <var title="">node list</var> <em>in reverse order</em>, insert
-    <var title="">node</var> as the first <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>child</a> of 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>,
-    <a href=#preserving-ranges>preserving ranges</a>.  Then abort these steps.
-
-    <li>Let <var title="">new parent</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(<var title="">tag</var>)</a></code> on
+    <li><a href=#wrap>Wrap</a> <var title="">node list</var>, with <a href=#sibling-criteria>sibling
+    criteria</a> matching only <a href=#html-element title="HTML element">HTML
+    elements</a> with <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-element-local-name title=concept-element-local-name>local name</a> <var title="">tag</var> and <a href=#new-parent-instructions>new parent
+    instructions</a> returning 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(<var title="">tag</var>)</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="">first node</var>.
-
-    <li>Insert <var title="">new parent</var> into 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="">first
-    node</var> immediately before <var title="">first node</var>.
     <!--
     This matches IE9, Firefox 4.0, and Chrome 12 dev.  If there's a preceding
     <li>, Opera 11.10 instead adds the new parent to the end of that <li>, so
@@ -2280,31 +2278,9 @@
     http://www.w3.org/Bugs/Public/show_bug.cgi?id=12609
     -->
 
-    <li>For each <var title="">node</var> in <var title="">node list</var>, append
-    <var title="">node</var> as the last <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>child</a> of <var title="">new parent</var>,
-    <a href=#preserving-ranges>preserving ranges</a>.
-
     <li>Abort these steps.
   </ol>
 
-  <li>If the <code class=external data-anolis-spec=domcore title=dom-Node-previousSibling><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-previoussibling>previousSibling</a></code> of the first member of <var title="">node list</var> is
-  an <a href=#indentation-element>indentation element</a>; its "display" property computes to
-  "block"; its "margin-left" and "margin-right" properties compute to "40px";
-  and its "margin-top" and "margin-bottom" properties compute to "0"; then
-  for each <var title="">node</var> in <var title="">node list</var>, append <var title="">node</var> as
-  the last <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>child</a> of its <code class=external data-anolis-spec=domcore title=dom-Node-previousSibling><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-previoussibling>previousSibling</a></code>, <a href=#preserving-ranges>preserving
-  ranges</a>.  Then abort these steps.
-
-  <p class=XXX>This might put things on the same line when they shouldn't be.
-
-  <li>If the <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> of the last member of <var title="">node list</var> is an
-  <a href=#indentation-element>indentation element</a>; its "display" property computes to "block";
-  its "margin-left" and "margin-right" properties compute to "40px"; and its
-  "margin-top" and "margin-bottom" properties compute to "0"; then for each
-  <var title="">node</var> in <var title="">node list</var>, <em>in reverse order</em>, insert
-  <var title="">node</var> as the first <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>child</a> of 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>,
-  <a href=#preserving-ranges>preserving ranges</a>.  Then abort these steps.
-
   <li>Let <var title="">tag</var> be "div" if the <a href=#css-styling-flag>CSS styling flag</a> is
   true, otherwise "blockquote".
   <!-- Firefox 4.0 is the only tested browser that respects the CSS styling
@@ -2315,13 +2291,13 @@
   and always create an extra element, to ensure consistency between CSS and
   non-CSS modes. -->
 
-  <li>Let <var title="">new parent</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(<var title="">tag</var>)</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="">first node</var>.
-
-  <li>Insert <var title="">new parent</var> into <var title="">node</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>
-  immediately before <var title="">first node</var>.
-
-  <li>Set the CSS property "margin" of <var title="">new parent</var> to "0 40px".
+  <li><a href=#wrap>Wrap</a> <var title="">node list</var>.  <a href=#sibling-criteria>Sibling criteria</a>
+  must match only an <a href=#indentation-element>indentation element</a> whose "display" property
+  computes to "block" and whose "margin-left" and "margin-right" properties
+  compute to "40px", and whose "margin-top" and "margin-bottom" properties
+  compute to "0".  The <a href=#new-parent-instructions>new parent instructions</a> are to 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(<var title="">tag</var>)</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="">first node</var>, then set the CSS property
+  "margin" of the returned node to "0 40px", then return the returned node.
 
   <p class=XXX>This indents on both sides, so we don't have to worry about
   directionality.  Preferably we should indent only on the start side, but
@@ -2339,12 +2315,6 @@
   to the long-term incorrectness of adding top/bottom margins or adding
   margins on both sides.  I can't think of any better way to do this at the
   moment.
-
-  <li>For each <var title="">node</var> in <var title="">node list</var>, append <var title="">node</var>
-  as the last <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>child</a> of <var title="">new parent</var>, <a href=#preserving-ranges>preserving
-  ranges</a>.
-
-  <li><a href=#remove-extraneous-line-breaks>Remove extraneous line breaks</a> from <var title="">new parent</var>.
 </ol>
 
 <dd><strong>State</strong>:
@@ -2809,8 +2779,7 @@
       an <code class=external data-anolis-spec=html title="the ol element"><a href=http://www.whatwg.org/html/#the-ol-element>ol</a></code> or <code class=external data-anolis-spec=html title="the ul element"><a href=http://www.whatwg.org/html/#the-ul-element>ul</a></code>, remove the first member from <var title="">node list</var>
       and append it to <var title="">sublist</var>.
 
-      <li><a href=#split-the-parent>Split the parent</a> of <var title="">sublist</var>, with <var title="">new
-      parent</var> null.
+      <li><a href=#split-the-parent>Split the parent</a> of <var title="">sublist</var>.
 
       <li><a href=#fix-orphaned-list-items>Fix orphaned list items</a> in <var title="">sublist</var>.
     </ol>
@@ -2826,24 +2795,12 @@
       an <code class=external data-anolis-spec=html title="the ol element"><a href=http://www.whatwg.org/html/#the-ol-element>ol</a></code> or <code class=external data-anolis-spec=html title="the ul element"><a href=http://www.whatwg.org/html/#the-ul-element>ul</a></code>, remove the first member from <var title="">node list</var>
       and append it to <var title="">sublist</var>.
 
-      <li>If the <code class=external data-anolis-spec=domcore title=dom-Node-previousSibling><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-previoussibling>previousSibling</a></code> of 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 the first member of
-      <var title="">sublist</var> is an <a href=#editable>editable</a> <code class=external data-anolis-spec=html title="the ol element"><a href=http://www.whatwg.org/html/#the-ol-element>ol</a></code>, and the
-      <code class=external data-anolis-spec=domcore title=dom-Node-previousSibling><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-previoussibling>previousSibling</a></code> of the first member of <var title="">sublist</var> is null,
-      then for each <var title="">node</var> in <var title="">sublist</var>, append
-      <var title="">node</var> as the last <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>child</a> of the <code class=external data-anolis-spec=domcore title=dom-Node-previousSibling><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-node-previoussibling>previousSibling</a></code> of 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="">node</var>, <a href=#preserving-ranges>preserving ranges</a>.
-
-      <li>Otherwise, if the <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> of 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 the first
-      member of <var title="">sublist</var> is an <a href=#editable>editable</a> <code class=external data-anolis-spec=html title="the ol element"><a href=http://www.whatwg.org/html/#the-ol-element>ol</a></code>, and the
-      <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> of the last member of <var title="">sublist</var> is null, then
-      for each <var title="">node</var> in <var title="">sublist</var> <em>in reverse order</em>,
-      insert <var title="">node</var> as the first <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>child</a> of the <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> of
-      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="">node</var>, <a href=#preserving-ranges>preserving ranges</a>.
-
-      <li>Otherwise, let <var title="">ol</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("ol")</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 the first member of <var title="">sublist</var>.  Then
-      <a href=#split-the-parent>split the parent</a> of <var title="">sublist</var>, with <var title="">new
-      parent</var> <var title="">ol</var>.
+      <li><a href=#split-the-parent>Split the parent</a> of <var title="">sublist</var>.
+
+      <li><a href=#wrap>Wrap</a> <var title="">sublist</var>, with <a href=#sibling-criteria>sibling
+      criteria</a> matching any <code class=external data-anolis-spec=html title="the ol element"><a href=http://www.whatwg.org/html/#the-ol-element>ol</a></code>, and with <a href=#new-parent-instructions>new parent
+      instructions</a> returning 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("ol")</a></code> on the
+      <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#context-object>context object</a>.
     </ol>
 
     <li>Otherwise:
@@ -2921,9 +2878,9 @@
 
         <ol>
           <li>If <var title="">original parent</var> is a <code class=external data-anolis-spec=html title="the p element"><a href=http://www.whatwg.org/html/#the-p-element>p</a></code>, <a href=#split-the-parent>split the
-          parent</a> of <var title="">sublist</var>, with <var title="">new parent</var> null,
-          and then set <var title="">original parent</var> 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 the
-          first member of <var title="">sublist</var>.
+          parent</a> of <var title="">sublist</var>, and then set <var title="">original
+          parent</var> 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 the first member of
+          <var title="">sublist</var>.
 
           <li>Let <var title="">ol</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("ol")</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 the first member of <var title="">sublist</var>.
@@ -3043,8 +3000,7 @@
     list</var> is not an <code class=external data-anolis-spec=html title="the ol element"><a href=http://www.whatwg.org/html/#the-ol-element>ol</a></code> or <code class=external data-anolis-spec=html title="the ul element"><a href=http://www.whatwg.org/html/#the-ul-element>ul</a></code>, remove the first member of <var title="">node
     list</var> and append it to <var title="">sublist</var>.
 
-    <li><a href=#split-the-parent>Split the parent</a> of <var title="">sublist</var>, with <var title="">new
-    parent</var> null.
+    <li><a href=#split-the-parent>Split the parent</a> of <var title="">sublist</var>.
 
     <li><a href=#fix-orphaned-list-items>Fix orphaned list items</a> in <var title="">sublist</var>.
   </ol>
--- a/implementation.js	Mon May 16 15:34:29 2011 -0600
+++ b/implementation.js	Mon May 16 16:32:01 2011 -0600
@@ -435,7 +435,7 @@
 	return children;
 }
 
-function splitParent(nodeList, newParent) {
+function splitParent(nodeList) {
 	// "Let original parent be the parent of the first member of node list."
 	var originalParent = nodeList[0].parentNode;
 
@@ -450,35 +450,20 @@
 	// and the nextSibling of the last member of node list is null:"
 	if (nodeList[0].previousSibling
 	&& !nodeList[nodeList.length - 1].nextSibling) {
-		// "If new parent is null:"
-		if (!newParent) {
-			// "If the last member of node list and the nextSibling of original
-			// parent are both inline nodes, call createElement("br") on the
-			// ownerDocument of original parent, then insert the result into
-			// the parent of original parent immediately after original
-			// parent."
-			if (isInlineNode(nodeList[nodeList.length - 1])
-			&& isInlineNode(originalParent.nextSibling)) {
-				originalParent.parentNode.insertBefore(originalParent.ownerDocument.createElement("br"), originalParent.nextSibling);
-			}
-
-			// "For each node in node list, in reverse order, insert node into
-			// the parent of original parent immediately after original parent,
-			// preserving ranges."
-			for (var i = nodeList.length - 1; i >= 0; i--) {
-				movePreservingRanges(nodeList[i], originalParent.parentNode, 1 + getNodeIndex(originalParent));
-			}
-		// "Otherwise:"
-		} else {
-			// "Insert new parent into the parent of original parent
-			// immediately after original parent."
-			originalParent.parentNode.insertBefore(newParent, originalParent.nextSibling);
-
-			// "For each node in node list, in reverse order, insert node as
-			// the first child of new parent, preserving ranges."
-			for (var i = nodeList.length - 1; i >= 0; i--) {
-				movePreservingRanges(nodeList[i], newParent, 0);
-			}
+		// "If the last member of node list and the nextSibling of original
+		// parent are both inline nodes, call createElement("br") on the
+		// ownerDocument of original parent, then insert the result into the
+		// parent of original parent immediately after original parent."
+		if (isInlineNode(nodeList[nodeList.length - 1])
+		&& isInlineNode(originalParent.nextSibling)) {
+			originalParent.parentNode.insertBefore(originalParent.ownerDocument.createElement("br"), originalParent.nextSibling);
+		}
+
+		// "For each node in node list, in reverse order, insert node into the
+		// parent of original parent immediately after original parent,
+		// preserving ranges."
+		for (var i = nodeList.length - 1; i >= 0; i--) {
+			movePreservingRanges(nodeList[i], originalParent.parentNode, 1 + getNodeIndex(originalParent));
 		}
 
 		// "Abort these steps."
@@ -503,36 +488,77 @@
 		}
 	}
 
-	// "If new parent is null:"
-	if (!newParent) {
-		// "If the first member of node list and the previousSibling of
-		// original parent are both inline nodes, call createElement("br") on
-		// the ownerDocument of original parent, then insert the result into
-		// the parent of original parent immediately before original parent."
-		if (isInlineNode(nodeList[0])
-		&& isInlineNode(originalParent.previousSibling)) {
-			originalParent.parentNode.insertBefore(originalParent.ownerDocument.createElement("br"), originalParent);
-		}
-
-		// "For each node in node list, insert node into the parent of original
-		// parent immediately before original parent, preserving ranges."
-		for (var i = 0; i < nodeList.length; i++) {
-			movePreservingRanges(nodeList[i], originalParent.parentNode, getNodeIndex(originalParent));
-		}
-
-		// "Abort these steps."
+	// "If the first member of node list and the previousSibling of original
+	// parent are both inline nodes, call createElement("br") on the
+	// ownerDocument of original parent, then insert the result into the parent
+	// of original parent immediately before original parent."
+	if (isInlineNode(nodeList[0])
+	&& isInlineNode(originalParent.previousSibling)) {
+		originalParent.parentNode.insertBefore(originalParent.ownerDocument.createElement("br"), originalParent);
+	}
+
+	// "For each node in node list, insert node into the parent of original
+	// parent immediately before original parent, preserving ranges."
+	for (var i = 0; i < nodeList.length; i++) {
+		movePreservingRanges(nodeList[i], originalParent.parentNode, getNodeIndex(originalParent));
+	}
+}
+
+function wrap(nodeList, siblingCriteria, newParentInstructions) {
+	// "If node list is empty, or the first member of node list is not
+	// editable, abort these steps."
+	if (!nodeList.length
+	|| !isEditable(nodeList[0])) {
 		return;
 	}
 
-	// "Insert new parent into the parent of original parent immediately before
-	// original parent."
-	originalParent.parentNode.insertBefore(newParent, originalParent);
+	// "If the previousSibling of the first member of node list is editable and
+	// meets the sibling criteria, then for each node in node list, append node
+	// as the last child of its previousSibling, preserving ranges. Then abort
+	// these steps."
+	if (isEditable(nodeList[0].previousSibling)
+	&& siblingCriteria(nodeList[0].previousSibling)) {
+		for (var i = 0; i < nodeList.length; i++) {
+			movePreservingRanges(nodeList[i], nodeList[i].previousSibling, -1);
+		}
+		return;
+	}
+
+	// "If the nextSibling of the last member of node list is editable and
+	// meets the sibling criteria, then for each node in node list, in reverse
+	// order, insert node as the first child of its nextSibling, preserving
+	// ranges. Then abort these steps."
+	if (isEditable(nodeList[nodeList.length - 1].nextSibling)
+	&& siblingCriteria(nodeList[nodeList.length - 1].nextSibling)) {
+		for (var i = nodeList.length - 1; i >= 0; i--) {
+			movePreservingRanges(nodeList[i], nodeList[i].nextSibling, 0);
+		}
+		return;
+	}
+
+	// "Run the new parent instructions, and let new parent be the result."
+	var newParent = newParentInstructions();
+
+	// "If new parent cannot be the child of the parent of the first member of
+	// node list, split the parent of node list."
+	//
+	// Hack for now, as usual.  Don't use this for inline elements!
+	if (isHtmlElement(nodeList[0].parentNode, "P")) {
+		splitParent(nodeList);
+	}
+
+	// "Insert new parent into the parent of the first member of node list
+	// immediately before the first member of node list."
+	nodeList[0].parentNode.insertBefore(newParent, nodeList[0]);
 
 	// "For each node in node list, append node as the last child of new
 	// parent, preserving ranges."
 	for (var i = 0; i < nodeList.length; i++) {
-		movePreservingRanges(nodeList[i], newParent, newParent.childNodes.length);
+		movePreservingRanges(nodeList[i], newParent, -1);
 	}
+
+	// "Remove extraneous line breaks from new parent."
+	removeExtraneousLineBreaks(newParent);
 }
 
 function removeExtraneousLineBreaks(node) {
@@ -2597,10 +2623,12 @@
 			// "If the first member of sublist is an ol, outdent it."
 			if (isHtmlElement(sublist[0], "OL")) {
 				outdentNode(sublist[0]);
+
 			// "Otherwise, if the first member of sublist is a ul, set the tag
 			// name of the first member of sublist to "ol"."
 			} else if (isHtmlElement(sublist[0], "UL")) {
 				setTagName(sublist[0], "ol");
+
 			// "Otherwise, if the parent of the first member of sublist is an
 			// editable ol:"
 			} else if (isHtmlElement(sublist[0].parentNode, "OL")
@@ -2617,7 +2645,7 @@
 				}
 
 				// "Split the parent of sublist, with new parent null."
-				splitParent(sublist, null);
+				splitParent(sublist);
 
 				// "Fix orphaned list items in sublist."
 				fixOrphanedListItems(sublist);
@@ -2637,38 +2665,16 @@
 					sublist.push(nodeList.shift());
 				}
 
-				// "If the previousSibling of the parent of the first member of
-				// sublist is an editable ol, and the previousSibling of the
-				// first member of sublist is null, then for each node in
-				// sublist, append node as the last child of the
-				// previousSibling of the parent of node, preserving ranges."
-				if (isEditable(sublist[0].parentNode.previousSibling)
-				&& isHtmlElement(sublist[0].parentNode.previousSibling, "OL")
-				&& !sublist[0].previousSibling) {
-					for (var i = 0; i < sublist.length; i++) {
-						movePreservingRanges(sublist[i], sublist[i].parentNode.previousSibling, -1);
-					}
-
-				// "Otherwise, if the nextSibling of the parent of the first
-				// member of sublist is an editable ol, and the nextSibling of
-				// the last member of sublist is null, then for each node in
-				// sublist in reverse order, insert node as the first child of
-				// the nextSibling of the parent of node, preserving ranges."
-				} else if (isEditable(sublist[0].parentNode.nextSibling)
-				&& isHtmlElement(sublist[0].parentNode.nextSibling, "OL")
-				&& !sublist[sublist.length - 1].nextSibling) {
-					for (var i = sublist.length - 1; i >= 0; i--) {
-						movePreservingRanges(sublist[i], sublist[i].parentNode.nextSibling, 0);
-					}
-
-				// "Otherwise, let ol be the result of calling
-				// createElement("ol") on the ownerDocument of the first member
-				// of sublist. Then split the parent of sublist, with new
-				// parent ol."
-				} else {
-					var ol = sublist[0].ownerDocument.createElement("ol");
-					splitParent(sublist, ol);
-				}
+				// "Split the parent of sublist."
+				splitParent(sublist);
+
+				// "Wrap sublist, with sibling criteria matching any ol, and
+				// with new parent instructions returning the result of calling
+				// createElement("ol") on the context object."
+				wrap(sublist,
+					function(node) { return isHtmlElement(node, "OL") },
+					function() { return document.createElement("ol") });
+
 			// "Otherwise:"
 			} else {
 				// "If the first member of sublist is a p or li or div, set the
@@ -2764,7 +2770,7 @@
 						// parent to the parent of the first member of
 						// sublist."
 						if (isHtmlElement(originalParent, "P")) {
-							splitParent(sublist, null);
+							splitParent(sublist);
 
 							originalParent = sublist[0].parentNode;
 						}
@@ -2919,7 +2925,7 @@
 			}
 
 			// "Split the parent of sublist, with new parent null."
-			splitParent(sublist, null);
+			splitParent(sublist);
 
 			// "Fix orphaned list items in sublist."
 			fixOrphanedListItems(sublist);
@@ -3136,108 +3142,45 @@
 		// "Let tag be the local name of the parent of first node."
 		var tag = firstNode.parentNode.tagName;
 
-		// "If the previousSibling of the first member of node list is an HTML
-		// element with local name tag, then for each node in node list, append
-		// node as the last child of its previousSibling, preserving ranges.
-		// Then abort these steps."
-		if (isHtmlElement(nodeList[0].previousSibling, tag)) {
-			for (var i = 0; i < nodeList.length; i++) {
-				movePreservingRanges(nodeList[i], nodeList[i].previousSibling, nodeList[i].previousSibling.childNodes.length);
-			}
-			return;
-		}
-
-		// "If the nextSibling of the last member of node list is an HTML
-		// element with local name tag, then for each node in node list in
-		// reverse order, insert node as the first child of its nextSibling,
-		// preserving ranges. Then abort these steps."
-		if (isHtmlElement(nodeList[nodeList.length - 1].nextSibling, tag)) {
-			for (var i = nodeList.length - 1; i >= 0; i--) {
-				movePreservingRanges(nodeList[i], nodeList[i].nextSibling, 0);
-			}
-			return;
-		}
-
-		// "Let new parent be the result of calling createElement(tag) on the
-		// ownerDocument of first node."
-		var newParent = firstNode.ownerDocument.createElement(tag);
-
-		// "Insert new parent into the parent of first node immediately before
-		// first node."
-		firstNode.parentNode.insertBefore(newParent, firstNode);
-
-		// "For each node in node list, append node as the last child of new
-		// parent, preserving ranges."
-		for (var i = 0; i < nodeList.length; i++) {
-			movePreservingRanges(nodeList[i], newParent, newParent.childNodes.length);
-		}
+		// "Wrap node list, with sibling criteria matching only HTML elements
+		// with local name tag and new parent instructions returning the result
+		// of calling createElement(tag) on the ownerDocument of first node."
+		wrap(nodeList,
+			function(node) { return isHtmlElement(node, tag) },
+			function() { return firstNode.ownerDocument.createElement(tag) });
 
 		// "Abort these steps."
 		return;
 	}
 
-	// "If the previousSibling of the first member of node list is an
-	// indentation element; its "display" property computes to "block"; its
-	// "margin-left" and "margin-right" properties compute to "40px"; and its
-	// "margin-top" and "margin-bottom" properties compute to "0"; then for
-	// each node in node list, append node as the last child of its
-	// previousSibling, preserving ranges. Then abort these steps."
-	if (isIndentationElement(nodeList[0].previousSibling)) {
-		var style = getComputedStyle(nodeList[0].previousSibling);
-		if (style.display == "block"
-		&& style.marginLeft == "40px"
-		&& style.marginRight == "40px"
-		&& style.marginTop == "0px"
-		&& style.marginBottom == "0px") {
-			for (var i = 0; i < nodeList.length; i++) {
-				movePreservingRanges(nodeList[i], nodeList[i].previousSibling, nodeList[i].previousSibling.childNodes.length);
-			}
-			return;
-		}
-	}
-
-	// "If the nextSibling of the last member of node list is an indentation
-	// element; its "display" property computes to "block"; its "margin-left"
-	// and "margin-right" properties compute to "40px"; and its "margin-top"
-	// and "margin-bottom" properties compute to "0"; then for each node in
-	// node list, in reverse order, insert node as the first child of its
-	// nextSibling, preserving ranges. Then abort these steps."
-	if (isIndentationElement(nodeList[nodeList.length - 1].nextSibling)) {
-		var style = getComputedStyle(nodeList[nodeList.length - 1].nextSibling);
-		if (style.display == "block"
-		&& style.marginLeft == "40px"
-		&& style.marginRight == "40px"
-		&& style.marginTop == "0px"
-		&& style.marginBottom == "0px") {
-			for (var i = nodeList.length - 1; i >= 0; i--) {
-				movePreservingRanges(nodeList[i], nodeList[i].nextSibling, 0);
-			}
-			return;
-		}
-	}
-
 	// "Let tag be "div" if the CSS styling flag is true, otherwise
 	// "blockquote"."
 	var tag = cssStylingFlag ? "div" : "blockquote";
 
-	// "Let new parent be the result of calling createElement(tag) on
-	// the ownerDocument of first node."
-	var newParent = firstNode.ownerDocument.createElement(tag);
-
-	// "Insert new parent into node's parent immediately before first node."
-	firstNode.parentNode.insertBefore(newParent, firstNode);
-
-	// "Set the CSS property "margin" of new parent to "0 40px"."
-	newParent.setAttribute("style", "margin: 0 40px");
-
-	// "For each node in node list, append node as the last child of new
-	// parent, preserving ranges."
-	for (var i = 0; i < nodeList.length; i++) {
-		movePreservingRanges(nodeList[i], newParent, newParent.childNodes.length);
-	}
-
-	// "Remove extraneous line breaks from new parent."
-	removeExtraneousLineBreaks(newParent);
+	// "Wrap node list. Sibling criteria must match only an indentation element
+	// whose "display" property computes to "block" and whose "margin-left" and
+	// "margin-right" properties compute to "40px", and whose "margin-top" and
+	// "margin-bottom" properties compute to "0". The new parent instructions
+	// are to call createElement(tag) on the ownerDocument of first node, then
+	// set the CSS property "margin" of the returned node to "0 40px", then
+	// return the returned node."
+	wrap(nodeList,
+		function(node) {
+			if (!isIndentationElement(node)) {
+				return false;
+			}
+			var style = getComputedStyle(node);
+			return style.display == "block"
+				&& style.marginLeft == "40px"
+				&& style.marginRight == "40px"
+				&& style.marginTop == "0px"
+				&& style.marginBottom == "0px";
+		},
+		function() {
+			var ret = firstNode.ownerDocument.createElement(tag);
+			ret.setAttribute("style", "margin: 0 40px");
+			return ret;
+		});
 }
 
 function outdentNode(node) {
--- a/source.html	Mon May 16 15:34:29 2011 -0600
+++ b/source.html	Mon May 16 16:32:01 2011 -0600
@@ -261,8 +261,7 @@
 </ol>
 
 <p>To <dfn>split the parent</dfn> of a list <var>node list</var> of consecutive
-[[sibling]] [[nodes]], with <var>new parent</var> either null or an
-[[element]]:
+[[sibling]] [[nodes]]:
 
 <ol>
   <li>Let <var>original parent</var> be the [[parent]] of the first member of
@@ -296,33 +295,18 @@
   </div>
 
   <ol>
-    <li>If <var>new parent</var> is null:
-
-    <ol>
-      <li>If the last member of <var>node list</var> and the [[nextsibling]] of
-      <var>original parent</var> are both <span title="inline node">inline
-      nodes</span>, call <code data-anolis-spec=domcore
-      title=dom-Document-createElement>createElement("br")</code> on the
-      [[ownerdocument]] of <var>original parent</var>, then insert the result
-      into the [[parent]] of <var>original parent</var> immediately after
-      <var>original parent</var>.
-
-      <li>For each <var>node</var> in <var>node list</var>, <em>in reverse
-      order</em>, insert <var>node</var> into the [[parent]] of <var>original
-      parent</var> immediately after <var>original parent</var>,
-      <span>preserving ranges</span>.
-    </ol>
-
-    <li>Otherwise:
-
-    <ol>
-      <li>Insert <var>new parent</var> into the [[parent]] of <var>original
-      parent</var> immediately after <var>original parent</var>.
-
-      <li>For each <var>node</var> in <var>node list</var>, <em>in reverse
-      order</em>, insert <var>node</var> as the first [[child]] of <var>new
-      parent</var>, <span>preserving ranges</span>.
-    </ol>
+    <li>If the last member of <var>node list</var> and the [[nextsibling]] of
+    <var>original parent</var> are both <span title="inline node">inline
+    nodes</span>, call <code data-anolis-spec=domcore
+    title=dom-Document-createElement>createElement("br")</code> on the
+    [[ownerdocument]] of <var>original parent</var>, then insert the result
+    into the [[parent]] of <var>original parent</var> immediately after
+    <var>original parent</var>.
+
+    <li>For each <var>node</var> in <var>node list</var>, <em>in reverse
+    order</em>, insert <var>node</var> into the [[parent]] of <var>original
+    parent</var> immediately after <var>original parent</var>, <span>preserving
+    ranges</span>.
 
     <li>Abort these steps.
   </ol>
@@ -351,31 +335,57 @@
     <span>preserving ranges</span>.
   </ol>
 
-  <li>If <var>new parent</var> is null:
-
-  <ol>
-    <li>If the first member of <var>node list</var> and the [[previoussibling]]
-    of <var>original parent</var> are both <span title="inline node">inline
-    nodes</span>, call <code data-anolis-spec=domcore
-    title=dom-Document-createElement>createElement("br")</code> on the
-    [[ownerdocument]] of <var>original parent</var>, then insert the result
-    into the [[parent]] of <var>original parent</var> immediately before
-    <var>original parent</var>.
-
-    <li>For each <var>node</var> in <var>node list</var>, insert
-    <var>node</var> into the [[parent]] of <var>original parent</var>
-    immediately before <var>original parent</var>, <span>preserving
-    ranges</span>.
-
-    <li>Abort these steps.
-  </ol>
-
-  <li>Insert <var>new parent</var> into the [[parent]] of <var>original
-  parent</var> immediately before <var>original parent</var>.
+  <li>If the first member of <var>node list</var> and the [[previoussibling]]
+  of <var>original parent</var> are both <span title="inline node">inline
+  nodes</span>, call <code data-anolis-spec=domcore
+  title=dom-Document-createElement>createElement("br")</code> on the
+  [[ownerdocument]] of <var>original parent</var>, then insert the result into
+  the [[parent]] of <var>original parent</var> immediately before <var>original
+  parent</var>.
+
+  <li>For each <var>node</var> in <var>node list</var>, insert <var>node</var>
+  into the [[parent]] of <var>original parent</var> immediately before
+  <var>original parent</var>, <span>preserving ranges</span>.
+</ol>
+
+<p>To <dfn>wrap</dfn> a list <var>node list</var> of consecutive [[sibling]]
+[[nodes]], given <dfn>sibling criteria</dfn> and <dfn>new parent
+instructions</dfn>:
+
+<ol>
+  <li>If <var>node list</var> is empty, or the first member of <var>node
+  list</var> is not <span>editable</span>, abort these steps.
+
+  <li>If the [[previoussibling]] of the first member of <var>node list</var>
+  is <span>editable</span> and meets the <span>sibling criteria</span>, then
+  for each <var>node</var> in <var>node list</var>, append <var>node</var> as
+  the last [[child]] of its [[previoussibling]], <span>preserving
+  ranges</span>.  Then abort these steps.
+
+  <li>If the [[nextsibling]] of the last member of <var>node list</var> is
+  <span>editable</span> and meets the <span>sibling criteria</span>, then for
+  each <var>node</var> in <var>node list</var>, <em>in reverse order</em>,
+  insert <var>node</var> as the first [[child]] of its [[nextsibling]],
+  <span>preserving ranges</span>.  Then abort these steps.
+
+  <li>Run the <span>new parent instructions</span>, and let <var>new
+  parent</var> be the result.
+
+  <li>If <var>new parent</var> cannot be the [[child]] of the [[parent]] of the
+  first member of <var>node list</var>, <span>split the parent</span> of
+  <var>node list</var>.
+
+  <p class=XXX>"Cannot be the child" needs to be defined.
+
+  <li>Insert <var>new parent</var> into the [[parent]] of the first member of
+  <var>node list</var> immediately before the first member of <var>node
+  list</var>.
 
   <li>For each <var>node</var> in <var>node list</var>, append <var>node</var>
   as the last [[child]] of <var>new parent</var>, <span>preserving
   ranges</span>.
+
+  <li><span>Remove extraneous line breaks</span> from <var>new parent</var>.
 </ol>
 
 <p>To <dfn>remove extraneous line breaks</dfn> from a [[node]] <var>node</var>:
@@ -2268,25 +2278,13 @@
     <li>Let <var>tag</var> be the [[localname]] of the [[parent]] of
     <var>first node</var>.
 
-    <li>If the [[previoussibling]] of the first member of <var>node list</var>
-    is an <span>HTML element</span> with [[localname]] <var>tag</var>, then for
-    each <var>node</var> in <var>node list</var>, append <var>node</var> as the
-    last [[child]] of its [[previoussibling]], <span>preserving ranges</span>.
-    Then abort these steps.
-
-    <li>If the [[nextsibling]] of the last member of <var>node list</var> is an
-    <span>HTML element</span> with [[localname]] <var>tag</var>, then for each
-    <var>node</var> in <var>node list</var> <em>in reverse order</em>, insert
-    <var>node</var> as the first [[child]] of its [[nextsibling]],
-    <span>preserving ranges</span>.  Then abort these steps.
-
-    <li>Let <var>new parent</var> be the result of calling <code
+    <li><span>Wrap</span> <var>node list</var>, with <span>sibling
+    criteria</span> matching only <span title="HTML element">HTML
+    elements</span> with [[localname]] <var>tag</var> and <span>new parent
+    instructions</span> returning the result of calling <code
     data-anolis-spec=domcore
     title=dom-Document-createElement>createElement(<var>tag</var>)</code> on
     the [[ownerdocument]] of <var>first node</var>.
-
-    <li>Insert <var>new parent</var> into the [[parent]] of <var>first
-    node</var> immediately before <var>first node</var>.
     <!--
     This matches IE9, Firefox 4.0, and Chrome 12 dev.  If there's a preceding
     <li>, Opera 11.10 instead adds the new parent to the end of that <li>, so
@@ -2308,31 +2306,9 @@
     http://www.w3.org/Bugs/Public/show_bug.cgi?id=12609
     -->
 
-    <li>For each <var>node</var> in <var>node list</var>, append
-    <var>node</var> as the last [[child]] of <var>new parent</var>,
-    <span>preserving ranges</span>.
-
     <li>Abort these steps.
   </ol>
 
-  <li>If the [[previoussibling]] of the first member of <var>node list</var> is
-  an <span>indentation element</span>; its "display" property computes to
-  "block"; its "margin-left" and "margin-right" properties compute to "40px";
-  and its "margin-top" and "margin-bottom" properties compute to "0"; then
-  for each <var>node</var> in <var>node list</var>, append <var>node</var> as
-  the last [[child]] of its [[previoussibling]], <span>preserving
-  ranges</span>.  Then abort these steps.
-
-  <p class=XXX>This might put things on the same line when they shouldn't be.
-
-  <li>If the [[nextsibling]] of the last member of <var>node list</var> is an
-  <span>indentation element</span>; its "display" property computes to "block";
-  its "margin-left" and "margin-right" properties compute to "40px"; and its
-  "margin-top" and "margin-bottom" properties compute to "0"; then for each
-  <var>node</var> in <var>node list</var>, <em>in reverse order</em>, insert
-  <var>node</var> as the first [[child]] of its [[nextsibling]],
-  <span>preserving ranges</span>.  Then abort these steps.
-
   <li>Let <var>tag</var> be "div" if the <span>CSS styling flag</span> is
   true, otherwise "blockquote".
   <!-- Firefox 4.0 is the only tested browser that respects the CSS styling
@@ -2343,15 +2319,15 @@
   and always create an extra element, to ensure consistency between CSS and
   non-CSS modes. -->
 
-  <li>Let <var>new parent</var> be the result of calling <code
+  <li><span>Wrap</span> <var>node list</var>.  <span>Sibling criteria</span>
+  must match only an <span>indentation element</span> whose "display" property
+  computes to "block" and whose "margin-left" and "margin-right" properties
+  compute to "40px", and whose "margin-top" and "margin-bottom" properties
+  compute to "0".  The <span>new parent instructions</span> are to call <code
   data-anolis-spec=domcore
-  title=dom-Document-createElement>createElement(<var>tag</var>)</code> on
-  the [[ownerdocument]] of <var>first node</var>.
-
-  <li>Insert <var>new parent</var> into <var>node</var>'s [[parent]]
-  immediately before <var>first node</var>.
-
-  <li>Set the CSS property "margin" of <var>new parent</var> to "0 40px".
+  title=dom-Document-createElement>createElement(<var>tag</var>)</code> on the
+  [[ownerdocument]] of <var>first node</var>, then set the CSS property
+  "margin" of the returned node to "0 40px", then return the returned node.
 
   <p class=XXX>This indents on both sides, so we don't have to worry about
   directionality.  Preferably we should indent only on the start side, but
@@ -2369,12 +2345,6 @@
   to the long-term incorrectness of adding top/bottom margins or adding
   margins on both sides.  I can't think of any better way to do this at the
   moment.
-
-  <li>For each <var>node</var> in <var>node list</var>, append <var>node</var>
-  as the last [[child]] of <var>new parent</var>, <span>preserving
-  ranges</span>.
-
-  <li><span>Remove extraneous line breaks</span> from <var>new parent</var>.
 </ol>
 
 <dd><strong>State</strong>:
@@ -2850,8 +2820,7 @@
       an [[ol]] or [[ul]], remove the first member from <var>node list</var>
       and append it to <var>sublist</var>.
 
-      <li><span>Split the parent</span> of <var>sublist</var>, with <var>new
-      parent</var> null.
+      <li><span>Split the parent</span> of <var>sublist</var>.
 
       <li><span>Fix orphaned list items</span> in <var>sublist</var>.
     </ol>
@@ -2867,26 +2836,14 @@
       an [[ol]] or [[ul]], remove the first member from <var>node list</var>
       and append it to <var>sublist</var>.
 
-      <li>If the [[previoussibling]] of the [[parent]] of the first member of
-      <var>sublist</var> is an <span>editable</span> [[ol]], and the
-      [[previoussibling]] of the first member of <var>sublist</var> is null,
-      then for each <var>node</var> in <var>sublist</var>, append
-      <var>node</var> as the last [[child]] of the [[previoussibling]] of the
-      [[parent]] of <var>node</var>, <span>preserving ranges</span>.
-
-      <li>Otherwise, if the [[nextsibling]] of the [[parent]] of the first
-      member of <var>sublist</var> is an <span>editable</span> [[ol]], and the
-      [[nextsibling]] of the last member of <var>sublist</var> is null, then
-      for each <var>node</var> in <var>sublist</var> <em>in reverse order</em>,
-      insert <var>node</var> as the first [[child]] of the [[nextsibling]] of
-      the [[parent]] of <var>node</var>, <span>preserving ranges</span>.
-
-      <li>Otherwise, let <var>ol</var> be the result of calling <code
+      <li><span>Split the parent</span> of <var>sublist</var>.
+
+      <li><span>Wrap</span> <var>sublist</var>, with <span>sibling
+      criteria</span> matching any [[ol]], and with <span>new parent
+      instructions</span> returning the result of calling <code
       data-anolis-spec=domcore
       title=dom-Document-createElement>createElement("ol")</code> on the
-      [[ownerdocument]] of the first member of <var>sublist</var>.  Then
-      <span>split the parent</span> of <var>sublist</var>, with <var>new
-      parent</var> <var>ol</var>.
+      [[contextobject]].
     </ol>
 
     <li>Otherwise:
@@ -2967,9 +2924,9 @@
 
         <ol>
           <li>If <var>original parent</var> is a [[p]], <span>split the
-          parent</span> of <var>sublist</var>, with <var>new parent</var> null,
-          and then set <var>original parent</var> to the [[parent]] of the
-          first member of <var>sublist</var>.
+          parent</span> of <var>sublist</var>, and then set <var>original
+          parent</var> to the [[parent]] of the first member of
+          <var>sublist</var>.
 
           <li>Let <var>ol</var> be the result of calling <code
           data-anolis-spec=domcore
@@ -3091,8 +3048,7 @@
     list</var> is not an [[ol]] or [[ul]], remove the first member of <var>node
     list</var> and append it to <var>sublist</var>.
 
-    <li><span>Split the parent</span> of <var>sublist</var>, with <var>new
-    parent</var> null.
+    <li><span>Split the parent</span> of <var>sublist</var>.
 
     <li><span>Fix orphaned list items</span> in <var>sublist</var>.
   </ol>