Make it easier to work with lists of siblings
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Sun, 22 May 2011 13:02:16 -0600
changeset 156 654d90186940
parent 155 4cc6f69fa78b
child 157 257bb7b1473a
Make it easier to work with lists of siblings
editcommands.html
implementation.js
source.html
--- a/editcommands.html	Sun May 22 12:52:49 2011 -0600
+++ b/editcommands.html	Sun May 22 13:02:16 2011 -0600
@@ -568,6 +568,32 @@
   <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-offset title=concept-boundary-point-offset>offset</a>.
 </ol>
 
+<p>The <dfn id=adjacent-sibling-runs>adjacent sibling runs</dfn> of a list <var title="">node list</var> of
+<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> comprise the list of list of <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> returned by the following
+algorithm:
+
+<ol>
+  <li>Let <var title="">list to return</var> be the empty list.
+
+  <li>While <var title="">node list</var> is not empty:
+
+  <ol>
+    <li>Let <var title="">sublist</var> be the empty list.
+
+    <li>Remove the first member of <var title="">node list</var> and append it to
+    <var title="">sublist</var>.
+
+    <li>While <var title="">node list</var> is not empty, and the first member of
+    <var title="">node list</var> is 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>, remove the first member of <var title="">node list</var> and
+    append it to <var title="">sublist</var>.
+
+    <li>Append <var title="">sublist</var> to <var title="">list to return</var>.
+  </ol>
+
+  <li>Return <var title="">list to return</var>.
+</ol>
+
 
 <h2 id=methods-of-the-htmldocument-interface><span class=secno>5 </span>Methods of the <code class=external data-anolis-spec=html><a href=http://www.whatwg.org/html/#htmldocument>HTMLDocument</a></code> interface</h2>
 
@@ -2991,20 +3017,10 @@
   <!-- Otherwise the last child of the previous sibling might be a list, which
   the li wouldn't get appended to. -->
 
-  <li>While <var title="">node list</var> is not empty:
-
-  <ol>
-    <li>Let <var title="">sublist</var> be a list of <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>, initially empty.
-
-    <li>Remove the first member of <var title="">node list</var> and append it to
-    <var title="">sublist</var>.
-
-    <li>While the first member of <var title="">node list</var> is 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>, remove the first member of
-    <var title="">node list</var> and append it to <var title="">sublist</var>.
-
-    <li><a href=#indent>Indent</a> <var title="">sublist</var>.
-  </ol>
+  <li>Let <var title="">adjacent sibling runs</var> be the <a href=#adjacent-sibling-runs>adjacent sibling
+  runs</a> of <var title="">node list</var>.
+
+  <li><a href=#indent>Indent</a> each member of <var title="">adjacent sibling runs</var>.
 </ol>
 
 
--- a/implementation.js	Sun May 22 12:52:49 2011 -0600
+++ b/implementation.js	Sun May 22 13:02:16 2011 -0600
@@ -1278,6 +1278,34 @@
 	globalRange.setEnd(end[0], end[1]);
 }
 
+function getAdjacentSiblingRuns(nodeList) {
+	// "Let list to return be the empty list."
+	var listToReturn = [];
+
+	// "While node list is not empty:"
+	while (nodeList.length) {
+		// "Let sublist be the empty list."
+		var sublist = [];
+
+		// "Remove the first member of node list and append it to sublist."
+		sublist.push(nodeList.shift());
+
+		// "While node list is not empty, and the first member of node list is
+		// the nextSibling of the last member of sublist, remove the first
+		// member of node list and append it to sublist."
+		while (nodeList.length
+		&& nodeList[0] == sublist[sublist.length - 1].nextSibling) {
+			sublist.push(nodeList.shift());
+		}
+
+		// "Append sublist to list to return."
+		listToReturn.push(sublist);
+	}
+
+	// "Return list to return."
+	return listToReturn;
+}
+
 function decomposeRange(range) {
 	// "If range's start and end are the same, return an empty list."
 	if (range.startContainer == range.endContainer
@@ -2504,24 +2532,13 @@
 			normalizeSublists(nodeList[0].previousSibling);
 		}
 
-		// "While node list is not empty:"
-		while (nodeList.length) {
-			// "Let sublist be a list of nodes, initially empty."
-			var sublist = [];
-
-			// "Remove the first member of node list and append it to sublist."
-			sublist.push(nodeList.shift());
-
-			// "While the first member of node list is the nextSibling of the
-			// last member of sublist, remove the first member of node list and
-			// append it to sublist."
-			while (nodeList.length
-			&& nodeList[0] == sublist[sublist.length - 1].nextSibling) {
-				sublist.push(nodeList.shift());
-			}
-
-			// "Indent sublist."
-			indentNodes(sublist);
+		// "Let adjacent sibling runs be the adjacent sibling runs of node
+		// list."
+		var adjacentSiblingRuns = getAdjacentSiblingRuns(nodeList);
+
+		// "Indent each member of adjacent sibling runs."
+		for (var i = 0; i < adjacentSiblingRuns.length; i++) {
+			indentNodes(adjacentSiblingRuns[i]);
 		}
 		break;
 
--- a/source.html	Sun May 22 12:52:49 2011 -0600
+++ b/source.html	Sun May 22 13:02:16 2011 -0600
@@ -532,6 +532,32 @@
   [[bpoffset]].
 </ol>
 
+<p>The <dfn>adjacent sibling runs</dfn> of a list <var>node list</var> of
+[[nodes]] comprise the list of list of [[nodes]] returned by the following
+algorithm:
+
+<ol>
+  <li>Let <var>list to return</var> be the empty list.
+
+  <li>While <var>node list</var> is not empty:
+
+  <ol>
+    <li>Let <var>sublist</var> be the empty list.
+
+    <li>Remove the first member of <var>node list</var> and append it to
+    <var>sublist</var>.
+
+    <li>While <var>node list</var> is not empty, and the first member of
+    <var>node list</var> is the [[nextsibling]] of the last member of
+    <var>sublist</var>, remove the first member of <var>node list</var> and
+    append it to <var>sublist</var>.
+
+    <li>Append <var>sublist</var> to <var>list to return</var>.
+  </ol>
+
+  <li>Return <var>list to return</var>.
+</ol>
+
 
 <h2>Methods of the <code data-anolis-spec=html>HTMLDocument</code> interface</h2>
 
@@ -3010,20 +3036,10 @@
   <!-- Otherwise the last child of the previous sibling might be a list, which
   the li wouldn't get appended to. -->
 
-  <li>While <var>node list</var> is not empty:
-
-  <ol>
-    <li>Let <var>sublist</var> be a list of [[nodes]], initially empty.
-
-    <li>Remove the first member of <var>node list</var> and append it to
-    <var>sublist</var>.
-
-    <li>While the first member of <var>node list</var> is the [[nextsibling]]
-    of the last member of <var>sublist</var>, remove the first member of
-    <var>node list</var> and append it to <var>sublist</var>.
-
-    <li><span>Indent</span> <var>sublist</var>.
-  </ol>
+  <li>Let <var>adjacent sibling runs</var> be the <span>adjacent sibling
+  runs</span> of <var>node list</var>.
+
+  <li><span>Indent</span> each member of <var>adjacent sibling runs</var>.
 </ol>