Improve dt/dd handling for insertParagraph
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Sun, 29 May 2011 14:44:34 -0600
changeset 185 a2c816de561d
parent 184 593863f0275e
child 186 da049543cff1
Improve dt/dd handling for insertParagraph
editcommands.html
implementation.js
preprocess
source.html
--- a/editcommands.html	Sun May 29 14:21:59 2011 -0600
+++ b/editcommands.html	Sun May 29 14:44:34 2011 -0600
@@ -2719,11 +2719,26 @@
 
 <p>To <dfn id=fix-disallowed-ancestors>fix disallowed ancestors</dfn> of 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>:
 
-<p class=XXX>This works okay for stuff nested in paragraphs or such, but for
-fixing lis/table stuff/etc. we probably don't want to do this at all; we
-probably want to add ancestor wrappers or something.
-
 <ol>
+  <li>If <var title="">node</var> is an <code class=external data-anolis-spec=html title="the li element"><a href=http://www.whatwg.org/html/#the-li-element>li</a></code> 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 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>,
+  unset its <code class=external data-anolis-spec=html title=attr-li-value><a href=http://www.whatwg.org/html/#attr-li-value>value</a></code>
+  attribute, if set.
+  <!-- IE9, Firefox 4.0, and Opera 11.10 keep the value attribute even if the
+  parent is now a ul.  Chrome 12 dev strips it even if the parent is now an ol.
+  The spec makes more sense. -->
+
+  <li>If <var title="">node</var> is an <code class=external data-anolis-spec=html title="the li element"><a href=http://www.whatwg.org/html/#the-li-element>li</a></code> with no attributes 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
+  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>, or is a <code class=external data-anolis-spec=html title="the dt element"><a href=http://www.whatwg.org/html/#the-dt-element>dt</a></code> or <code class=external data-anolis-spec=html title="the dd element"><a href=http://www.whatwg.org/html/#the-dd-element>dd</a></code> with no attributes 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 not a <code class=external data-anolis-spec=html title="the dl element"><a href=http://www.whatwg.org/html/#the-dl-element>dl</a></code>, remove <var title="">node</var>, <a href=#preserving-its-descendants>preserving its
+  descendants</a>.  Then abort these steps.
+
+  <li>If <var title="">node</var> is an <code class=external data-anolis-spec=html title="the li element"><a href=http://www.whatwg.org/html/#the-li-element>li</a></code> 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 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>, or is a <code class=external data-anolis-spec=html title="the dt element"><a href=http://www.whatwg.org/html/#the-dt-element>dt</a></code> or <code class=external data-anolis-spec=html title="the dd element"><a href=http://www.whatwg.org/html/#the-dd-element>dd</a></code> 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 not a <code class=external data-anolis-spec=html title="the dl element"><a href=http://www.whatwg.org/html/#the-dl-element>dl</a></code>,
+  <a href=#set-the-tag-name>set the tag name</a> of <var title="">node</var> to "div".  Then abort these
+  steps.
+
+  <p class=XXX>Should convert it to the default single-line container type.
+
   <li>If <var title="">node</var> is an <a href=#allowed-child>allowed child</a> of 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>, or
   it is not an <a href=#allowed-child>allowed child</a> of any of its <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-tree-ancestor title=concept-tree-ancestor>ancestors</a> <a href=#in-the-same-editing-host>in
   the same editing host</a>, abort these steps and do nothing.
@@ -2833,28 +2848,6 @@
   <li><a href=#fix-disallowed-ancestors>Fix disallowed ancestors</a> of <var title="">new parent</var>.
 </ol>
 
-<p>To <dfn id=fix-orphaned-list-items>fix orphaned list items</dfn> in 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> <var title="">node
-list</var>:
-
-<ol>
-  <li>For each <code class=external data-anolis-spec=html title="the li element"><a href=http://www.whatwg.org/html/#the-li-element>li</a></code> <var title="">item</var> in <var title="">node list</var>:
-
-  <ol>
-      <li>If <var title="">item</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> 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>, unset
-      <var title="">item</var>'s <code class=external data-anolis-spec=html title=attr-li-value><a href=http://www.whatwg.org/html/#attr-li-value>value</a></code> attribute, if set.
-      <!-- IE9, Firefox 4.0, and Opera 11.10 keep the value attribute even if
-      the parent is now a ul.  Chrome 12 dev strips it even if the parent is
-      now an ol.  The spec makes more sense. -->
-
-      <li>If <var title="">item</var> has no attributes 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 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 <var title="">item</var>, <a href=#preserving-its-descendants>preserving its
-      descendants</a>.
-
-      <li>Otherwise, if <var title="">item</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> 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>, <a href=#set-the-tag-name>set the tag name</a> of <var title="">item</var> to "div".
-  </ol>
-</ol>
-
 <p>To <dfn id=normalize-sublists>normalize sublists</dfn> in 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="">item</var>:
 <!--
 This algorithm implies that we don't support a sublist in the middle of an
@@ -3148,7 +3141,8 @@
 
     <li>Otherwise remove <var title="">node</var>, <a href=#preserving-its-descendants>preserving its descendants</a>.
 
-    <li><a href=#fix-orphaned-list-items>Fix orphaned list items</a> in <var title="">children</var>.
+    <li><a href=#fix-disallowed-ancestors>Fix disallowed ancestors</a> of each member of
+    <var title="">children</var>.
 
     <li>Abort these steps.
   </ol>
@@ -3577,7 +3571,8 @@
 
     <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>.
+    <li><a href=#fix-disallowed-ancestors>Fix disallowed ancestors</a> of each member of
+    <var title="">sublist</var>.
   </ol>
 
   <li>Otherwise, while <var title="">node list</var> is not empty:
@@ -4240,15 +4235,17 @@
     <li>Abort these steps.
   </ol>
 
-  <li>If <var title="">container</var> is an <code class=external data-anolis-spec=html title="the li element"><a href=http://www.whatwg.org/html/#the-li-element>li</a></code>, and either it has no <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>children</a>
-  or it has a single <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> and that <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> is a <code class=external data-anolis-spec=html title="the br element"><a href=http://www.whatwg.org/html/#the-br-element>br</a></code>:
+  <li>If <var title="">container</var>'s <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> is "li", "dt", or "dd"; and
+  either it has no <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>children</a> or it has a single <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> and that <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>
+  is a <code class=external data-anolis-spec=html title="the br element"><a href=http://www.whatwg.org/html/#the-br-element>br</a></code>:
+  <!-- Including dt/dd here follows Firefox 5.0b2, as with the special dt/dd
+  handling below. -->
 
   <ol>
     <li><a href=#split-the-parent>Split the parent</a> of the one-<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> list consisting of
     <var title="">container</var>.
 
-    <li><a href=#fix-orphaned-list-items>Fix orphaned list items</a> in the one-<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> list
-    consisting of <var title="">container</var>.
+    <li><a href=#fix-disallowed-ancestors>Fix disallowed ancestors</a> of <var title="">container</var>.
 
     <li>Abort these steps.
   </ol>
@@ -4397,7 +4394,8 @@
 
     <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>.
+    <li><a href=#fix-disallowed-ancestors>Fix disallowed ancestors</a> of each member of
+    <var title="">sublist</var>.
   </ol>
 </ol>
 
--- a/implementation.js	Sun May 29 14:21:59 2011 -0600
+++ b/implementation.js	Sun May 29 14:44:34 2011 -0600
@@ -3290,18 +3290,17 @@
 			return;
 		}
 
-		// "If container is an li, and either it has no children or it has a
-		// single child and that child is a br:"
-		if (isHtmlElement(container, "li")
+		// "If container's local name is "li", "dt", or "dd"; and either it has
+		// no children or it has a single child and that child is a br:"
+		if (["LI", "DT", "DD"].indexOf(container.tagName) != -1
 		&& (!container.hasChildNodes()
 		|| (container.childNodes.length == 1
 		&& isHtmlElement(container.firstChild, "br")))) {
 			// "Split the parent of the one-node list consisting of container."
 			splitParent([container]);
 
-			// "Fix orphaned list items in the one-node list consisting of
-			// container."
-			fixOrphanedListItems([container]);
+			// "Fix disallowed ancestors of container."
+			fixDisallowedAncestors(container);
 
 			// "Abort these steps."
 			return;
@@ -3494,8 +3493,10 @@
 			// "Split the parent of sublist, with new parent null."
 			splitParent(sublist);
 
-			// "Fix orphaned list items in sublist."
-			fixOrphanedListItems(sublist);
+			// "Fix disallowed ancestors of each member of sublist."
+			for (var i = 0; i < sublist.length; i++) {
+				fixDisallowedAncestor(sublist[i]);
+			}
 		}
 		break;
 
@@ -3695,6 +3696,36 @@
 }
 
 function fixDisallowedAncestors(node) {
+	// "If node is an li and its parent is not an ol, unset its value
+	// attribute, if set."
+	if (isHtmlElement(node, "li")
+	&& !isHtmlElement(node, "ol")) {
+		node.removeAttribute("value");
+	}
+
+	// "If node is an li with no attributes and its parent is not an ol or ul,
+	// or is a dt or dd with no attributes and its parent is not a dl, remove
+	// node, preserving its descendants. Then abort these steps."
+	if ((isHtmlElement(node, "li")
+	&& !node.attributes.length
+	&& !isHtmlElement(node.parentNode, ["ol", "ul"]))
+	|| (isHtmlElement(node, ["dt", "dd"])
+	&& !node.attributes.length
+	&& !isHtmlElement(node.parentNode, "dl"))) {
+		removePreservingDescendants(node);
+	}
+
+	// "If node is an li and its parent is not an ol or ul, or is a dt or dd
+	// and its parent is not a dl, set the tag name of node to "div". Then
+	// abort these steps."
+	if ((isHtmlElement(node, "li")
+	&& !isHtmlElement(node.parentNode, ["ol", "ul"]))
+	|| (isHtmlElement(node, ["dt", "dd"])
+	&& !isHtmlElement(node.parentNode, "dl"))) {
+		setTagName(node, "div");
+		return;
+	}
+
 	// "If node is an allowed child of its parent, or it is not an allowed
 	// child of any of its ancestors in the same editing host, abort these
 	// steps and do nothing."
@@ -3854,8 +3885,10 @@
 			removePreservingDescendants(node);
 		}
 
-		// "Fix orphaned list items in children."
-		fixOrphanedListItems(children);
+		// "Fix disallowed ancestors of each member of children."
+		for (var i = 0; i < children.length; i++) {
+			fixDisallowedAncestors(children[i]);
+		}
 
 		// "Abort these steps."
 		return;
@@ -4006,8 +4039,10 @@
 			// "Split the parent of sublist."
 			splitParent(sublist);
 
-			// "Fix orphaned list items in sublist."
-			fixOrphanedListItems(sublist);
+			// "Fix disallowed ancestors of each member of sublist."
+			for (var i = 0; i < sublist.length; i++) {
+				fixDisallowedAncestors(sublist[i]);
+			}
 		}
 
 	// "Otherwise, while node list is not empty:"
@@ -4165,34 +4200,6 @@
 	}
 }
 
-function fixOrphanedListItems(nodeList) {
-	// "For each li item in node list:"
-	for (var i = 0; i < nodeList.length; i++) {
-		var item = nodeList[i];
-		if (!isHtmlElement(item, "LI")) {
-			continue;
-		}
-
-		// "If item's parent is not an ol, unset item's value attribute, if
-		// set."
-		if (!isHtmlElement(item.parentNode, "OL")) {
-			item.removeAttribute("value");
-		}
-
-		// "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", "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", "UL"])) {
-			setTagName(item, "div");
-		}
-	}
-}
-
 // "A potential indentation element is either a blockquote, or a div that has a
 // style attribute that sets "margin" or some subproperty of it."
 function isPotentialIndentationElement(node) {
--- a/preprocess	Sun May 29 14:21:59 2011 -0600
+++ b/preprocess	Sun May 29 14:44:34 2011 -0600
@@ -29,7 +29,9 @@
     'descendant': '<span data-anolis-spec=domcore title=concept-tree-descendant>descendant</span>',
     'directionality': '<span data-anolis-spec=html title="the directionality">directionality</span>',
     'div': '<code data-anolis-spec=html title="the div element">div</code>',
+    'dd': '<code data-anolis-spec=html title="the dd element">dd</code>',
     'dl': '<code data-anolis-spec=html title="the dl element">dl</code>',
+    'dt': '<code data-anolis-spec=html title="the dt element">dt</code>',
     'document': '<code data-anolis-spec=domcore>Document</code>',
     'documentfragment': '<code data-anolis-spec=domcore>DocumentFragment</code>',
     'documenttype': '<code data-anolis-spec=domcore>DocumentType</code>',
--- a/source.html	Sun May 29 14:21:59 2011 -0600
+++ b/source.html	Sun May 29 14:44:34 2011 -0600
@@ -2720,11 +2720,26 @@
 
 <p>To <dfn>fix disallowed ancestors</dfn> of a [[node]] <var>node</var>:
 
-<p class=XXX>This works okay for stuff nested in paragraphs or such, but for
-fixing lis/table stuff/etc. we probably don't want to do this at all; we
-probably want to add ancestor wrappers or something.
-
 <ol>
+  <li>If <var>node</var> is an [[li]] and its [[parent]] is not an [[ol]],
+  unset its <code data-anolis-spec=html title=attr-li-value>value</code>
+  attribute, if set.
+  <!-- IE9, Firefox 4.0, and Opera 11.10 keep the value attribute even if the
+  parent is now a ul.  Chrome 12 dev strips it even if the parent is now an ol.
+  The spec makes more sense. -->
+
+  <li>If <var>node</var> is an [[li]] with no attributes and its [[parent]] is
+  not an [[ol]] or [[ul]], or is a [[dt]] or [[dd]] with no attributes and its
+  [[parent]] is not a [[dl]], remove <var>node</var>, <span>preserving its
+  descendants</span>.  Then abort these steps.
+
+  <li>If <var>node</var> is an [[li]] and its [[parent]] is not an [[ol]] or
+  [[ul]], or is a [[dt]] or [[dd]] and its [[parent]] is not a [[dl]],
+  <span>set the tag name</span> of <var>node</var> to "div".  Then abort these
+  steps.
+
+  <p class=XXX>Should convert it to the default single-line container type.
+
   <li>If <var>node</var> is an <span>allowed child</span> of its [[parent]], or
   it is not an <span>allowed child</span> of any of its [[ancestors]] <span>in
   the same editing host</span>, abort these steps and do nothing.
@@ -2838,29 +2853,6 @@
   <li><span>Fix disallowed ancestors</span> of <var>new parent</var>.
 </ol>
 
-<p>To <dfn>fix orphaned list items</dfn> in a list of [[nodes]] <var>node
-list</var>:
-
-<ol>
-  <li>For each [[li]] <var>item</var> in <var>node list</var>:
-
-  <ol>
-      <li>If <var>item</var>'s [[parent]] is not an [[ol]], unset
-      <var>item</var>'s <code data-anolis-spec=html
-      title=attr-li-value>value</code> attribute, if set.
-      <!-- IE9, Firefox 4.0, and Opera 11.10 keep the value attribute even if
-      the parent is now a ul.  Chrome 12 dev strips it even if the parent is
-      now an ol.  The spec makes more sense. -->
-
-      <li>If <var>item</var> has no attributes and its [[parent]] is not an
-      [[ol]] or [[ul]], remove <var>item</var>, <span>preserving its
-      descendants</span>.
-
-      <li>Otherwise, if <var>item</var>'s [[parent]] is not an [[ol]] or
-      [[ul]], <span>set the tag name</span> of <var>item</var> to "div".
-  </ol>
-</ol>
-
 <p>To <dfn>normalize sublists</dfn> in a [[node]] <var>item</var>:
 <!--
 This algorithm implies that we don't support a sublist in the middle of an
@@ -3159,7 +3151,8 @@
 
     <li>Otherwise remove <var>node</var>, <span>preserving its descendants</span>.
 
-    <li><span>Fix orphaned list items</span> in <var>children</var>.
+    <li><span>Fix disallowed ancestors</span> of each member of
+    <var>children</var>.
 
     <li>Abort these steps.
   </ol>
@@ -3588,7 +3581,8 @@
 
     <li><span>Split the parent</span> of <var>sublist</var>.
 
-    <li><span>Fix orphaned list items</span> in <var>sublist</var>.
+    <li><span>Fix disallowed ancestors</span> of each member of
+    <var>sublist</var>.
   </ol>
 
   <li>Otherwise, while <var>node list</var> is not empty:
@@ -4275,15 +4269,17 @@
     <li>Abort these steps.
   </ol>
 
-  <li>If <var>container</var> is an [[li]], and either it has no [[children]]
-  or it has a single [[child]] and that [[child]] is a [[br]]:
+  <li>If <var>container</var>'s [[localname]] is "li", "dt", or "dd"; and
+  either it has no [[children]] or it has a single [[child]] and that [[child]]
+  is a [[br]]:
+  <!-- Including dt/dd here follows Firefox 5.0b2, as with the special dt/dd
+  handling below. -->
 
   <ol>
     <li><span>Split the parent</span> of the one-[[node]] list consisting of
     <var>container</var>.
 
-    <li><span>Fix orphaned list items</span> in the one-[[node]] list
-    consisting of <var>container</var>.
+    <li><span>Fix disallowed ancestors</span> of <var>container</var>.
 
     <li>Abort these steps.
   </ol>
@@ -4436,7 +4432,8 @@
 
     <li><span>Split the parent</span> of <var>sublist</var>.
 
-    <li><span>Fix orphaned list items</span> in <var>sublist</var>.
+    <li><span>Fix disallowed ancestors</span> of each member of
+    <var>sublist</var>.
   </ol>
 </ol>