Add special behavior for backspacing a table
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Tue, 07 Jun 2011 14:41:16 -0600
changeset 251 1ce2c6a84a33
parent 250 2f6bc0e1d5e0
child 252 2730e74c619a
Add special behavior for backspacing a table

Select the whole thing, so it gets deleted if you hit backspace again.
editcommands.html
implementation.js
preprocess
source.html
--- a/editcommands.html	Tue Jun 07 14:19:06 2011 -0600
+++ b/editcommands.html	Tue Jun 07 14:41:16 2011 -0600
@@ -2576,6 +2576,9 @@
     <li><a href=#remove-extraneous-line-breaks-before>Remove extraneous line breaks before</a> <var title="">start
     node</var>.
 
+    <p class=XXX>This removes the line break even if nothing winds up getting
+    merged (e.g., cursor at the start of a table).
+
     <li>Set <var title="">start offset</var> to the <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-indexof title=concept-indexof>index</a> of <var title="">start node</var>.
 
     <li>Set <var title="">start node</var> to 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>.
@@ -2600,6 +2603,21 @@
     then subtract one from <var title="">start offset</var>.
 
     <!--
+    Another special case:
+
+      <table>...</table><p>[]foo</p>
+      -> {<table>...</table>}<p>foo</p>
+
+    same as happens with <table>...</table>[]foo (see below).
+    -->
+    <li>Otherwise, if 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 <var title="">start node</var> with <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-indexof title=concept-indexof>index</a>
+    <var title="">start offset</var> minus one is a <code class=external data-anolis-spec=html title="the table element"><a href=http://www.whatwg.org/html/#the-table-element>table</a></code>, call
+    <code class=external data-anolis-spec=domrange title=dom-Selection-collapse><a href=http://html5.org/specs/dom-range.html#dom-selection-collapse>collapse(<var title="">start node</var>, <var title="">start offset</var> &minus; 1)</a></code> on
+    the <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#context-object>context object</a>'s <code class=external data-anolis-spec=domrange><a href=http://html5.org/specs/dom-range.html#selection>Selection</a></code>, then call <code class=external data-anolis-spec=domrange title=dom-Selection-extend><a href=http://html5.org/specs/dom-range.html#dom-selection-extend>extend(<var title="">start
+    node</var>, <var title="">start offset</var>)</a></code> on the <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#context-object>context object</a>'s
+    <code class=external data-anolis-spec=domrange><a href=http://html5.org/specs/dom-range.html#selection>Selection</a></code>, then abort these steps.
+
+    <!--
     Regular case: for <p>foo</p><p>[]bar</p>, we delete <p>foo{</p><p>}bar</p>.
     This will also happen if the preceding element is a span or something, but
     that doesn't hurt.
@@ -2623,6 +2641,23 @@
   </ol>
 
   <li>If <var title="">node</var> has a <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> with <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-indexof title=concept-indexof>index</a> <var title="">offset</var>
+  &minus; 1, 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 table element"><a href=http://www.whatwg.org/html/#the-table-element>table</a></code>:
+  <!-- If you try backspacing into a table, select it.  This doesn't match any
+  browser; it matches the recommendation of the "behavior when typing in
+  contentEditable elements" document.  The idea is that then you can delete it
+  with a second backspace. -->
+
+  <ol>
+    <li>Call <code class=external data-anolis-spec=domrange title=dom-Selection-collapse><a href=http://html5.org/specs/dom-range.html#dom-selection-collapse>collapse(<var title="">node</var>, <var title="">offset</var> &minus; 1)</a></code> on
+    the <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#context-object>context object</a>'s <code class=external data-anolis-spec=domrange><a href=http://html5.org/specs/dom-range.html#selection>Selection</a></code>.
+
+    <li>Call <code class=external data-anolis-spec=domrange title=dom-Selection-extend><a href=http://html5.org/specs/dom-range.html#dom-selection-extend>extend(<var title="">node</var>, <var title="">offset</var>)</a></code> on the
+    <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#context-object>context object</a>'s <code class=external data-anolis-spec=domrange><a href=http://html5.org/specs/dom-range.html#selection>Selection</a></code>.
+
+    <li>Abort these steps.
+  </ol>
+
+  <li>If <var title="">node</var> has a <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> with <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-indexof title=concept-indexof>index</a> <var title="">offset</var>
   &minus; 1, 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 <a href=#prohibited-paragraph-child>prohibited paragraph child</a>:
 
   <ol>
--- a/implementation.js	Tue Jun 07 14:19:06 2011 -0600
+++ b/implementation.js	Tue Jun 07 14:41:16 2011 -0600
@@ -3311,6 +3311,16 @@
 				offset = startOffset;
 				startOffset--;
 
+			// "Otherwise, if the child of start node with index start offset
+			// minus one is a table, call collapse(start node, start offset −
+			// 1) on the context object's Selection, then call extend(start
+			// node, start offset) on the context object's Selection, then
+			// abort these steps."
+			} else if (isHtmlElement(startNode.childNodes[startOffset - 1], "table")) {
+				range.setStart(startNode, startOffset - 1);
+				range.setEnd(startNode, startOffset);
+				return;
+
 			// "Otherwise, set start node to its child with index start offset
 			// minus one, then set start offset to the length of start node."
 			} else {
@@ -3338,8 +3348,24 @@
 		}
 
 		// "If node has a child with index offset − 1, and that child is a
+		// table:"
+		if (0 <= offset - 1
+		&& offset - 1 < node.childNodes.length
+		&& isHtmlElement(node.childNodes[offset - 1], "table")) {
+			// "Call collapse(node, offset − 1) on the context object's
+			// Selection."
+			range.setStart(node, offset - 1);
+
+			// "Call extend(node, offset) on the context object's Selection."
+			range.setEnd(node, offset);
+
+			// "Abort these steps."
+			return;
+		}
+
+		// "If node has a child with index offset − 1, and that child is a
 		// prohibited paragraph child:"
-		if (0 <= offset -1
+		if (0 <= offset - 1
 		&& offset - 1 < node.childNodes.length
 		&& isProhibitedParagraphChild(node.childNodes[offset - 1])) {
 			// "Let start node be the child of node with index offset − 1."
--- a/preprocess	Tue Jun 07 14:19:06 2011 -0600
+++ b/preprocess	Tue Jun 07 14:41:16 2011 -0600
@@ -86,6 +86,7 @@
     'style': '<code data-anolis-spec=html title="the style attribute">style</code>',
     'sub': '<code data-anolis-spec=html title="the sub and sup elements">sub</code>',
     'sup': '<code data-anolis-spec=html title="the sub and sup elements">sup</code>',
+    'table': '<code data-anolis-spec=html title="the table element">table</code>',
     'tbody': '<code data-anolis-spec=html title="the tbody element">tbody</code>',
     'td': '<code data-anolis-spec=html title="the td element">td</code>',
     'tfoot': '<code data-anolis-spec=html title="the tfoot element">tfoot</code>',
@@ -113,6 +114,7 @@
 fnreplace = {
     'createelement': '<code data-anolis-spec=domcore title=dom-Document-createElement>createElement(\\1)</code>',
     'deletedata': '<code data-anolis-spec=domcore title=dom-CharacterData-deleteData>deleteData(\\1)</code>',
+    'extend': '<code data-anolis-spec=domrange title=dom-Selection-extend>extend(\\1)</code>',
     'insertnode': '<code data-anolis-spec=domrange title=dom-Range-insertNode>insertNode(\\1)</code>',
     'selcollapse': '<code data-anolis-spec=domrange title=dom-Selection-collapse>collapse(\\1)</code>',
 }
--- a/source.html	Tue Jun 07 14:19:06 2011 -0600
+++ b/source.html	Tue Jun 07 14:41:16 2011 -0600
@@ -2557,6 +2557,9 @@
     <li><span>Remove extraneous line breaks before</span> <var>start
     node</var>.
 
+    <p class=XXX>This removes the line break even if nothing winds up getting
+    merged (e.g., cursor at the start of a table).
+
     <li>Set <var>start offset</var> to the [[index]] of <var>start node</var>.
 
     <li>Set <var>start node</var> to its [[parent]].
@@ -2581,6 +2584,21 @@
     then subtract one from <var>start offset</var>.
 
     <!--
+    Another special case:
+
+      <table>...</table><p>[]foo</p>
+      -> {<table>...</table>}<p>foo</p>
+
+    same as happens with <table>...</table>[]foo (see below).
+    -->
+    <li>Otherwise, if the [[child]] of <var>start node</var> with [[index]]
+    <var>start offset</var> minus one is a [[table]], call
+    [[selcollapse|<var>start node</var>, <var>start offset</var> &minus; 1]] on
+    the [[contextobject]]'s [[selection]], then call [[extend|<var>start
+    node</var>, <var>start offset</var>]] on the [[contextobject]]'s
+    [[selection]], then abort these steps.
+
+    <!--
     Regular case: for <p>foo</p><p>[]bar</p>, we delete <p>foo{</p><p>}bar</p>.
     This will also happen if the preceding element is a span or something, but
     that doesn't hurt.
@@ -2604,6 +2622,23 @@
   </ol>
 
   <li>If <var>node</var> has a [[child]] with [[index]] <var>offset</var>
+  &minus; 1, and that [[child]] is a [[table]]:
+  <!-- If you try backspacing into a table, select it.  This doesn't match any
+  browser; it matches the recommendation of the "behavior when typing in
+  contentEditable elements" document.  The idea is that then you can delete it
+  with a second backspace. -->
+
+  <ol>
+    <li>Call [[selcollapse|<var>node</var>, <var>offset</var> &minus; 1]] on
+    the [[contextobject]]'s [[selection]].
+
+    <li>Call [[extend|<var>node</var>, <var>offset</var>]] on the
+    [[contextobject]]'s [[selection]].
+
+    <li>Abort these steps.
+  </ol>
+
+  <li>If <var>node</var> has a [[child]] with [[index]] <var>offset</var>
   &minus; 1, and that [[child]] is a <span>prohibited paragraph child</span>:
 
   <ol>