More refactoring
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Tue, 17 May 2011 15:06:07 -0600
changeset 141 5dd7f3cb4cc6
parent 140 d6de0dbfe523
child 142 8773fac2c993
More refactoring
editcommands.html
implementation.js
source.html
--- a/editcommands.html	Tue May 17 14:47:39 2011 -0600
+++ b/editcommands.html	Tue May 17 15:06:07 2011 -0600
@@ -2802,11 +2802,13 @@
     <li>If <var title="">node</var> is a <code class=external data-anolis-spec=html title="the ul element"><a href=http://www.whatwg.org/html/#the-ul-element>ul</a></code>, <a href=#set-the-tag-name>set the tag name</a> of
     <var title="">node</var> to "ol" and continue from the beginning of this loop.
 
+    <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 <var title="">node</var>.
+
     <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 consisting of
     <var title="">node</var>.
 
-    <li>If 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> 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>:
+    <li>If <var title="">original parent</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> 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>:
     <!-- Outdent -->
 
     <ol>
@@ -2818,32 +2820,17 @@
 
       <li><a href=#split-the-parent>Split the parent</a> of <var title="">sublist</var>.
 
+      <li>If <var title="">original parent</var> is a <code class=external data-anolis-spec=html title="the ul element"><a href=http://www.whatwg.org/html/#the-ul-element>ul</a></code>, <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>.
+
       <li><a href=#fix-orphaned-list-items>Fix orphaned list items</a> in <var title="">sublist</var>.
 
       <li>Continue from the beginning of this loop.
     </ol>
 
-    <li>If 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> is an <a href=#editable>editable</a>
-    <code class=external data-anolis-spec=html title="the ul element"><a href=http://www.whatwg.org/html/#the-ul-element>ul</a></code>:
-    <!-- Split up the parent -->
-
-    <ol>
-      <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>, and the first member of <var title="">node 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 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>.
-
-      <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>.
-
-      <li>Continue from the beginning of this loop.
-    </ol>
-
     <!-- General case.  Add an ol wrapper for each line.  <br> breaks apart
     <li>'s, and multiple consecutive <br>s or trailing <br>s become empty
     <li>s. -->
@@ -2876,8 +2863,6 @@
     <a href=#new-parent-instructions>new parent instructions</a> being the following:
 
     <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 <var title="">li</var>.
-
       <li>If <var title="">original parent</var> is not an <a href=#editable>editable</a>
       <a href=#indentation-element>indentation element</a>, or 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> is not 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>, call
--- a/implementation.js	Tue May 17 14:47:39 2011 -0600
+++ b/implementation.js	Tue May 17 15:06:07 2011 -0600
@@ -352,7 +352,7 @@
 function setTagName(element, newName) {
 	// "If element is an HTML element with local name equal to new name, return
 	// element."
-	if (isHtmlElement(element) && element.tagName == newName.toUpperCase()) {
+	if (isHtmlElement(element, newName.toUpperCase())) {
 		return element;
 	}
 
@@ -925,12 +925,12 @@
 		}
 
 		// "If element is a sup, return "super"."
-		if (isHtmlElement(element) && element.tagName == "SUP") {
+		if (isHtmlElement(element, "SUP")) {
 			return "super";
 		}
 
 		// "If element is a sub, return "sub"."
-		if (isHtmlElement(element) && element.tagName == "SUB") {
+		if (isHtmlElement(element, "SUB")) {
 			return "sub";
 		}
 
@@ -955,8 +955,7 @@
 	// "If command is "strikethrough" and element is a s or strike element,
 	// return "line-through"."
 	if (command == "strikethrough"
-	&& isHtmlElement(element)
-	&& (element.tagName == "S" || element.tagName == "STRIKE")) {
+	&& isHtmlElement(element, ["S", "STRIKE"])) {
 		return "line-through";
 	}
 
@@ -977,8 +976,7 @@
 	// "If command is "underline" and element is a u element, return
 	// "underline"."
 	if (command == "underline"
-	&& isHtmlElement(element)
-	&& element.tagName == "U") {
+	&& isHtmlElement(element, "U")) {
 		return "underline";
 	}
 
@@ -1368,14 +1366,13 @@
 	var newItem = null;
 
 	// "While item has an ol or ul child:"
-	while ([].some.call(item.childNodes, function (node) { return isHtmlElement(node, "OL") || isHtmlElement(node, "UL") })) {
+	while ([].some.call(item.childNodes, function (node) { return isHtmlElement(node, ["OL", "UL"]) })) {
 		// "Let child be the last child of item."
 		var child = item.lastChild;
 
 		// "If child is an ol or ul, or new item is null and child is a Text
 		// node whose data consists of zero of more space characters:"
-		if (isHtmlElement(child, "OL")
-		|| isHtmlElement(child, "UL")
+		if (isHtmlElement(child, ["OL", "UL"])
 		|| (!newItem && child.nodeType == Node.TEXT_NODE && /^[ \t\n\f\r]*$/.test(child.data))) {
 			// "Set new item to null."
 			newItem = null;
@@ -1609,8 +1606,7 @@
 
 	// "If element is an a element and command is "createLink" or "unlink",
 	// unset the href property of element."
-	if (isHtmlElement(element)
-	&& element.tagName == "A"
+	if (isHtmlElement(element, "A")
 	&& (command == "createlink" || command == "unlink")) {
 		element.removeAttribute("href");
 	}
@@ -2303,8 +2299,7 @@
 		for (var i = 0; i < nodeList.length; i++) {
 			var candidate = nodeList[i].parentNode;
 			while (candidate) {
-				if (isHtmlElement(candidate)
-				&& candidate.tagName == "A"
+				if (isHtmlElement(candidate, "A")
 				&& candidate.hasAttribute("href")) {
 					candidate.setAttribute("href", value);
 				}
@@ -2507,8 +2502,7 @@
 		// its previousSibling."
 		if (nodeList.length
 		&& isHtmlElement(nodeList[0], "LI")
-		&& (isHtmlElement(nodeList[0].parentNode, "OL")
-			|| isHtmlElement(nodeList[0].parentNode, "UL"))
+		&& isHtmlElement(nodeList[0].parentNode, ["OL", "UL"])
 		&& isHtmlElement(nodeList[0].previousSibling, "LI")) {
 			normalizeSublists(nodeList[0].previousSibling);
 		}
@@ -2642,10 +2636,8 @@
 			&& isContained(node, newRange)
 			&& (!nodeList.length || !isAncestor(nodeList[nodeList.length - 1], node))
 			&& !isPotentialIndentationElement(node)
-			&& (isHtmlElement(node, "OL")
-			|| isHtmlElement(node, "UL")
-			|| isHtmlElement(node.parentNode, "OL")
-			|| isHtmlElement(node.parentNode, "UL")
+			&& (isHtmlElement(node, ["OL", "UL"])
+			|| isHtmlElement(node.parentNode, ["OL", "UL"])
 			// As usual with content restrictions, we fake it for testing
 			// purposes.
 			|| !isHtmlElement(node)
@@ -2674,56 +2666,40 @@
 				continue;
 			}
 
+			// "Let original parent be the parent of node."
+			var originalParent = node.parentNode;
+
 			// "Let sublist be a list of nodes, initially consisting of node."
 			var sublist = [node];
 
-			// "If the parent of node is an editable ol:"
-			if (isHtmlElement(node.parentNode, "OL")
-			&& isEditable(node.parentNode)) {
+			// "If original parent is an editable ol or ul:"
+			if (isHtmlElement(originalParent, ["OL", "UL"])
+			&& isEditable(originalParent)) {
 				// "While node list is not empty, and the first member of node
 				// list is the nextSibling of the last member of sublist, and
 				// the first member of node list is not an ol or ul, remove the
 				// first member from node list and append it to sublist."
 				while (nodeList.length
 				&& nodeList[0] == sublist[sublist.length - 1].nextSibling
-				&& !isHtmlElement(nodeList[0], "OL")
-				&& !isHtmlElement(nodeList[0], "UL")) {
-					sublist.push(nodeList.shift());
-				}
-
-				// "Split the parent of sublist, with new parent null."
-				splitParent(sublist);
-
-				// "Fix orphaned list items in sublist."
-				fixOrphanedListItems(sublist);
-
-				// "Continue from the beginning of this loop."
-				continue;
-			}
-
-			// "If the parent of node is an editable ul:"
-			if (isHtmlElement(node.parentNode, "UL")
-			&& isEditable(node.parentNode)) {
-				// "While node list is not empty, and the first member of node
-				// list is the nextSibling of the last member of sublist, and
-				// the first member of node list is not an ol or ul, remove the
-				// first member from node list and append it to sublist."
-				while (nodeList.length
-				&& nodeList[0] == sublist[sublist.length -1].nextSibling
-				&& !isHtmlElement(nodeList[0], "OL")
-				&& !isHtmlElement(nodeList[0], "UL")) {
+				&& !isHtmlElement(nodeList[0], ["OL", "UL"])) {
 					sublist.push(nodeList.shift());
 				}
 
 				// "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") });
+				// "If original parent is a ul, wrap sublist, with sibling
+				// criteria matching any ol, and with new parent instructions
+				// returning the result of calling createElement("ol") on the
+				// context object."
+				if (isHtmlElement(originalParent, "UL")) {
+					wrap(sublist,
+						function(node) { return isHtmlElement(node, "OL") },
+						function() { return document.createElement("ol") });
+				}
+
+				// "Fix orphaned list items in sublist."
+				fixOrphanedListItems(sublist);
 
 				// "Continue from the beginning of this loop."
 				continue;
@@ -2771,9 +2747,6 @@
 			wrap([li],
 				function(node) { return isHtmlElement(node, "OL") },
 				function() {
-					// "Let original parent be the parent of li."
-					var originalParent = li.parentNode;
-
 					// "If original parent is not an editable indentation
 					// element, or the previousSibling of original parent is
 					// not an editable ol, call createElement("ol") on the
@@ -2869,11 +2842,9 @@
 			// "If node has no editable descendants, or is an ol or ul, or is
 			// an li whose parent is an ol or ul, append it to node list."
 			if (!hasEditableDescendants(node)
-			|| isHtmlElement(node, "OL")
-			|| isHtmlElement(node, "UL")
+			|| isHtmlElement(node, ["OL", "UL"])
 			|| (isHtmlElement(node, "LI")
-			&& (isHtmlElement(node.parentNode, "OL")
-			|| isHtmlElement(node.parentNode, "UL")))) {
+			&& isHtmlElement(node.parentNode, ["OL", "UL"]))) {
 				nodeList.push(node);
 			}
 		}
@@ -2884,10 +2855,8 @@
 			// the child of an ol or ul, outdent it and remove it from node
 			// list."
 			while (nodeList.length
-			&& (isHtmlElement(nodeList[0], "OL")
-			|| isHtmlElement(nodeList[0], "UL")
-			|| (!isHtmlElement(nodeList[0].parentNode, "OL")
-			&& !isHtmlElement(nodeList[0].parentNode, "UL")))) {
+			&& (isHtmlElement(nodeList[0], ["OL", "UL"])
+			|| !isHtmlElement(nodeList[0].parentNode, ["OL", "UL"]))) {
 				outdentNode(nodeList.shift());
 			}
 
@@ -2908,8 +2877,7 @@
 			// to sublist."
 			while (nodeList.length
 			&& nodeList[0] == sublist[sublist.length - 1].nextSibling
-			&& !isHtmlElement(nodeList[0], "OL")
-			&& !isHtmlElement(nodeList[0], "UL")) {
+			&& !isHtmlElement(nodeList[0], ["OL", "UL"])) {
 				sublist.push(nodeList.shift());
 			}
 
@@ -3126,8 +3094,7 @@
 	var firstNode = nodeList[0];
 
 	// "If first node's parent is an ol or ul:"
-	if (isHtmlElement(firstNode.parentNode, "OL")
-	|| isHtmlElement(firstNode.parentNode, "UL")) {
+	if (isHtmlElement(firstNode.parentNode, ["OL", "UL"])) {
 		// "Let tag be the local name of the parent of first node."
 		var tag = firstNode.parentNode.tagName;
 
@@ -3246,8 +3213,7 @@
 	// and node is an ol or ul:"
 	if ((!isEditable(currentAncestor)
 	|| !isPotentialIndentationElement(currentAncestor))
-	&& (isHtmlElement(node, "OL")
-	|| isHtmlElement(node, "UL"))) {
+	&& isHtmlElement(node, ["OL", "UL"])) {
 		// "Unset the reversed, start, and type attributes of node, if any are
 		// set."
 		node.removeAttribute("reversed");
@@ -3260,8 +3226,7 @@
 		// "If node has attributes, and its parent or not an ol or ul, set the
 		// tag name of node to "div"."
 		if (node.attributes.length
-		&& !isHtmlElement(node.parentNode, "OL")
-		&& !isHtmlElement(node.parentNode, "UL")) {
+		&& !isHtmlElement(node.parentNode, ["OL", "UL"])) {
 			setTagName(node, "div");
 
 		// "Otherwise remove node, preserving its descendants."
@@ -3343,14 +3308,12 @@
 		// "If item has no attributes and its parent is not an ol or ul, remove
 		// item, preserving its descendants."
 		if (!item.attributes.length
-		&& !isHtmlElement(item.parentNode, "OL")
-		&& !isHtmlElement(item.parentNode, "UL")) {
+		&& !isHtmlElement(item.parentNode, ["OL", "UL"])) {
 			removePreservingDescendants(item);
 
 		// "Otherwise, if item's parent is not an ol or ul, set the tag name of
 		// item to "div"."
-		} else if (!isHtmlElement(item.parentNode, "OL")
-		&& !isHtmlElement(item.parentNode, "UL")) {
+		} else if (!isHtmlElement(item.parentNode, ["OL", "UL"])) {
 			setTagName(item, "div");
 		}
 	}
--- a/source.html	Tue May 17 14:47:39 2011 -0600
+++ b/source.html	Tue May 17 15:06:07 2011 -0600
@@ -2844,11 +2844,13 @@
     <li>If <var>node</var> is a [[ul]], <span>set the tag name</span> of
     <var>node</var> to "ol" and continue from the beginning of this loop.
 
+    <li>Let <var>original parent</var> be the [[parent]] of <var>node</var>.
+
     <li>Let <var>sublist</var> be a list of [[nodes]], initially consisting of
     <var>node</var>.
 
-    <li>If the [[parent]] of <var>node</var> is an <span>editable</span>
-    [[ol]]:
+    <li>If <var>original parent</var> is an <span>editable</span> [[ol]] or
+    [[ul]]:
     <!-- Outdent -->
 
     <ol>
@@ -2860,31 +2862,15 @@
 
       <li><span>Split the parent</span> of <var>sublist</var>.
 
-      <li><span>Fix orphaned list items</span> in <var>sublist</var>.
-
-      <li>Continue from the beginning of this loop.
-    </ol>
-
-    <li>If the [[parent]] of <var>node</var> is an <span>editable</span>
-    [[ul]]:
-    <!-- Split up the parent -->
-
-    <ol>
-      <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>, and the first member of <var>node list</var> is not
-      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>.
-
-      <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
+      <li>If <var>original parent</var> is a [[ul]], <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
       [[contextobject]].
 
+      <li><span>Fix orphaned list items</span> in <var>sublist</var>.
+
       <li>Continue from the beginning of this loop.
     </ol>
 
@@ -2922,8 +2908,6 @@
     <span>new parent instructions</span> being the following:
 
     <ol>
-      <li>Let <var>original parent</var> be the [[parent]] of <var>li</var>.
-
       <li>If <var>original parent</var> is not an <span>editable</span>
       <span>indentation element</span>, or the [[previoussibling]] of
       <var>original parent</var> is not an <span>editable</span> [[ol]], call