Spec behavior for collapsed selections
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Wed, 13 Apr 2011 10:53:15 -0600
changeset 65 bc18f082f7f4
parent 64 a191bb9c310c
child 66 cd94129545fa
Spec behavior for collapsed selections

Which is . . . nothing. Invoking bold (for instance) when the selection
doesn't contain anything just does nothing. I expected it to insert an
empty <b></b> with the cursor inside so that if you start typing it's
bold. But apparently, that doesn't happen. So do editors have to fake
up that effect themselves, or am I missing something? Puzzling.
autoimplementation.html
editcommands.html
source.html
--- a/autoimplementation.html	Wed Apr 13 10:28:40 2011 -0600
+++ b/autoimplementation.html	Wed Apr 13 10:53:15 2011 -0600
@@ -54,6 +54,7 @@
 	backcolor: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'<p>foo[bar]baz',
 		'<p>foo]bar[baz',
 		'<div><p>foo[bar]baz</p></div>',
@@ -73,6 +74,7 @@
 	bold: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'foo[bar]baz',
 		'foo]bar[baz',
 		'{<p><p> <p>foo</p>}',
@@ -173,6 +175,7 @@
 	createlink: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'foo[bar]baz',
 		'foo]bar[baz',
 		'{<p><p> <p>foo</p>}',
@@ -213,6 +216,7 @@
 	fontname: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'foo[bar]baz',
 		'foo]bar[baz',
 		'{<p><p> <p>foo</p>}',
@@ -267,6 +271,7 @@
 	fontsize: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		["1", 'foo[bar]baz'],
 		["0", 'foo[bar]baz'],
 		["-5", 'foo[bar]baz'],
@@ -344,6 +349,7 @@
 	forecolor: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'foo[bar]baz',
 		'foo]bar[baz',
 		'{<p><p> <p>foo</p>}',
@@ -370,6 +376,7 @@
 	hilitecolor: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'foo[bar]baz',
 		'foo]bar[baz',
 		'{<p><p> <p>foo</p>}',
@@ -397,7 +404,9 @@
 	inserthorizontalrule: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'<div><b>foo</b>{}<b>bar</b></div>',
+		'<div><b>foo[</b><b>]bar</b></div>',
 		'<div><b>foo</b>{<b>bar</b>}<b>baz</b></div>',
 		'<b>foo[]bar</b>',
 		'<b id=abc>foo[]bar</b>',
@@ -408,6 +417,7 @@
 	insertimage: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		["", 'foo[bar]baz'],
 		'foo[bar]baz',
 		'foo<b>{bar}</b>baz',
@@ -415,6 +425,7 @@
 	italic: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'foo[bar]baz',
 		'foo]bar[baz',
 		'{<p><p> <p>foo</p>}',
@@ -484,6 +495,7 @@
 	removeformat: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'[foo<b>bar</b>baz]',
 		'foo[<b>bar</b>baz]',
 		'foo[<b>bar</b>]baz',
@@ -598,6 +610,7 @@
 	strikethrough: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'foo[bar]baz',
 		'foo]bar[baz',
 		'{<p><p> <p>foo</p>}',
@@ -673,6 +686,7 @@
 	subscript: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'foo[bar]baz',
 		'foo]bar[baz',
 		'{<p><p> <p>foo</p>}',
@@ -706,6 +720,7 @@
 	superscript: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'foo[bar]baz',
 		'foo]bar[baz',
 		'{<p><p> <p>foo</p>}',
@@ -739,6 +754,7 @@
 	underline: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'foo[bar]baz',
 		'foo]bar[baz',
 		'{<p><p> <p>foo</p>}',
@@ -814,6 +830,7 @@
 	unlink: [
 		'foo[]bar',
 		'<span>foo</span>{}<span>bar</span>',
+		'<span>foo[</span><span>]bar</span>',
 		'<a href=http://www.google.com/>foo[bar]baz</a>',
 		'<a href=http://www.google.com/>foo[barbaz</a>}',
 		'{<a href=http://www.google.com/>foobar]baz</a>',
--- a/editcommands.html	Wed Apr 13 10:28:40 2011 -0600
+++ b/editcommands.html	Wed Apr 13 10:53:15 2011 -0600
@@ -152,11 +152,6 @@
   don't want to have to explicitly refer to DOM methods like insertBefore()
   every time I want to move things.
 
-  <li>I haven't put any thought yet into collapsed ranges or selections.
-  Currently my algorithms mostly do nothing if the selection is collapsed,
-  which is of course wrong.  E.g., bold with collapsed selection should put
-  &lt;b&gt;&lt;/b&gt; at the cursor, generally.
-
   <li>Some more thought needs to go into what happens to the selection when you
   mutate the DOM.  In some cases the results are pretty arbitrary.  It might
   make sense to do some kind of normalization.
@@ -1267,6 +1262,11 @@
 
 
 <dt><code title=""><dfn id=command-bold title=command-bold>bold</dfn></code>
+<!-- If the selection is collapsed (but not if it contains nothing but is not
+collapsed), IE9 wraps the whole line in a <strong>.  This seems bizarre and no
+one else does it, so I don't do it.  It's a similar story for similar commands
+(fontName, italic, etc.).  Except not for strikethrough, where it just does
+nothing if the selection is empty.  Why strikethrough?  I don't know. -->
 
 <dd><strong>Action</strong>: <a href=#decompose>Decompose</a> the <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range title=concept-range>range</a>.  If the
 state of the <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range title=concept-range>range</a> for this command is then true, <a href=#set-the-value>set the
@@ -1297,6 +1297,10 @@
 <dt><code title=""><dfn id=command-createlink title=command-createlink>createLink</dfn></code>
 
 <dd><strong>Action</strong>: The user agent must run the following steps:
+<!-- If the selection doesn't contain anything (meaning, e.g., deleteContents()
+doesn't change anything), then Chrome 12 dev inserts a link at the selection
+start, with the text equal to the link URL.  Other browsers don't do it, so I
+don't either. -->
 
 <ol>
   <li>If <var title="">value</var> is the empty string, abort these steps and do
--- a/source.html	Wed Apr 13 10:28:40 2011 -0600
+++ b/source.html	Wed Apr 13 10:53:15 2011 -0600
@@ -139,11 +139,6 @@
   don't want to have to explicitly refer to DOM methods like insertBefore()
   every time I want to move things.
 
-  <li>I haven't put any thought yet into collapsed ranges or selections.
-  Currently my algorithms mostly do nothing if the selection is collapsed,
-  which is of course wrong.  E.g., bold with collapsed selection should put
-  &lt;b>&lt;/b> at the cursor, generally.
-
   <li>Some more thought needs to go into what happens to the selection when you
   mutate the DOM.  In some cases the results are pretty arbitrary.  It might
   make sense to do some kind of normalization.
@@ -1280,6 +1275,11 @@
 
 
 <dt><code title><dfn title=command-bold>bold</dfn></code>
+<!-- If the selection is collapsed (but not if it contains nothing but is not
+collapsed), IE9 wraps the whole line in a <strong>.  This seems bizarre and no
+one else does it, so I don't do it.  It's a similar story for similar commands
+(fontName, italic, etc.).  Except not for strikethrough, where it just does
+nothing if the selection is empty.  Why strikethrough?  I don't know. -->
 
 <dd><strong>Action</strong>: <span>Decompose</span> the [[range]].  If the
 state of the [[range]] for this command is then true, <span>set the
@@ -1310,6 +1310,10 @@
 <dt><code title><dfn title=command-createlink>createLink</dfn></code>
 
 <dd><strong>Action</strong>: The user agent must run the following steps:
+<!-- If the selection doesn't contain anything (meaning, e.g., deleteContents()
+doesn't change anything), then Chrome 12 dev inserts a link at the selection
+start, with the text equal to the link URL.  Other browsers don't do it, so I
+don't either. -->
 
 <ol>
   <li>If <var>value</var> is the empty string, abort these steps and do