Require SYNTAX_ERR for bad values
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Wed, 29 Jun 2011 14:43:51 -0600
changeset 352 f5af9c0dc81c
parent 351 b5ccdd3f1c55
child 353 2e4a02cf573b
Require SYNTAX_ERR for bad values

Not sure if this will stick, but I think it's a good idea if we can do
it.
editcommands.html
implementation.js
preprocess
source.html
tests.js
--- a/editcommands.html	Wed Jun 29 13:59:06 2011 -0600
+++ b/editcommands.html	Wed Jun 29 14:43:51 2011 -0600
@@ -320,6 +320,13 @@
 
   <li>Have to make sure that in all the places where we set a selection, it's
   valid.
+
+  <li>Where methods take a value, I've decided they should throw SYNTAX_ERR
+  instead of doing nothing if the value makes no sense.  As a rule, this
+  doesn't match browsers.  I think it's more useful to authors, however,
+  especially since in many cases, the command's effect isn't currently
+  interoperable anyway.  I'll switch them back to doing nothing if implementers
+  think this is a problem.
 </ul>
 
 <p class=XXX>A variety of other issues are also noted in the text, formatted
@@ -2259,14 +2266,19 @@
 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
-  nothing.
-  <!-- This matches Firefox 4b11 and Chrome 11 dev.  IE 9 RC and Opera 11 both
-  treat the request literally.  Gecko and WebKit probably have it right here:
-  users who enter no URL are very unlikely to want to link to a relative URL
-  resolving to the current document.  If they really want to, they can always
-  specify "#" for the value, or the author can rewrite it, so it's not like
-  this makes the API less useful. -->
+  <li>If <var title="">value</var> is the empty string, raise a <code class=external data-anolis-spec=domcore title=dom-DOMException-SYNTAX_ERR><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-domexception-syntax_err>SYNTAX_ERR</a></code>
+  exception.
+  <!--
+  Firefox 4b11 and Chrome 11 dev both silently do nothing in this case.
+  IE 9 RC and Opera 11 both treat the request literally.  Gecko and WebKit
+  probably have it right here: users who enter no URL are very unlikely to want
+  to link to a relative URL resolving to the current document.  If they really
+  want to, they can always specify "#" for the value, or the author can rewrite
+  it, so it's not like this makes the API less useful.
+
+  As in other cases, I raise a SYNTAX_ERR instead of silently failing, because
+  I think that's more useful.  See issues.
+  -->
 
   <li><a href=#decompose>Decompose</a> the <a href=#active-range>active range</a>, and let <var title="">node
   list</var> be the result.
@@ -2402,8 +2414,8 @@
 respecifying non-interoperable behavior anyway.
 -->
 <ol>
-  <li>If <var title="">value</var> is the empty string, do nothing and abort these
-  steps.
+  <li>If <var title="">value</var> is the empty string, raise a <code class=external data-anolis-spec=domcore title=dom-DOMException-SYNTAX_ERR><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-domexception-syntax_err>SYNTAX_ERR</a></code>
+  exception.
 
   <li><a class=external data-anolis-spec=html href=http://www.whatwg.org/html/#strip-leading-and-trailing-whitespace>Strip leading and trailing whitespace</a>
   from <var title="">value</var>.
@@ -2459,7 +2471,7 @@
 
   <li>If <var title="">value</var> is not one of the strings "xx-small", "x-small",
   "small", "medium", "large", "x-large", "xx-large", "xxx-large", and is not a
-  valid CSS absolute length, then do nothing and abort these steps.
+  valid CSS absolute length, then raise a <code class=external data-anolis-spec=domcore title=dom-DOMException-SYNTAX_ERR><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-domexception-syntax_err>SYNTAX_ERR</a></code> exception.
 
   <p class=XXX>Not sure this is the best way to do it.  We don't want to allow
   relative lengths, because those can have very weird user-visible behavior.
@@ -2602,7 +2614,7 @@
   <li>If <var title="">value</var> is not a valid CSS color, prepend "#" to it.
 
   <li>If <var title="">value</var> is still not a valid CSS color, or if it is
-  currentColor, do nothing and abort these steps.
+  currentColor, raise a <code class=external data-anolis-spec=domcore title=dom-DOMException-SYNTAX_ERR><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-domexception-syntax_err>SYNTAX_ERR</a></code> exception.
   <!-- currentColor is bad for the same reason as relative font sizes.  It will
   confuse the algorithm, and doesn't seem very useful anyway. -->
 
@@ -2662,7 +2674,7 @@
   <li>If <var title="">value</var> is not a valid CSS color, prepend "#" to it.
 
   <li>If <var title="">value</var> is still not a valid CSS color, or if it is
-  currentColor, do nothing and abort these steps.
+  currentColor, raise a <code class=external data-anolis-spec=domcore title=dom-DOMException-SYNTAX_ERR><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-domexception-syntax_err>SYNTAX_ERR</a></code> exception.
   <!-- currentColor is bad for the same reason as relative font sizes.  It will
   confuse the algorithm, and doesn't seem very useful anyway.  For hiliteColor
   you could conceive of it being useful, but it will still confuse the
@@ -5313,7 +5325,7 @@
   lowercase</a>.
 
   <li>If <var title="">value</var> is not "address", "div", "h1", "h2", "h3", "h4",
-  "h5", "h6", "p", or "pre", then do nothing and abort these steps.
+  "h5", "h6", "p", or "pre", raise a <code class=external data-anolis-spec=domcore title=dom-DOMException-SYNTAX_ERR><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-domexception-syntax_err>SYNTAX_ERR</a></code> exception.
 
   <p class=XXX>We should support dt and dd here, since they make sense here and
   there's no other way to create them.
@@ -5897,13 +5909,14 @@
 <p><a href=#action>Action</a>:
 
 <ol>
-  <li>If <var title="">value</var> is the empty string, abort these steps and do
-  nothing.
+  <li>If <var title="">value</var> is the empty string, raise a <code class=external data-anolis-spec=domcore title=dom-DOMException-SYNTAX_ERR><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-domexception-syntax_err>SYNTAX_ERR</a></code>
+  exception.
   <!-- Similar logic to createLink, except even more compelling, since an HTML
   document linking to itself as an image is just silly.  In fact, the current
   HTML spec instructs UAs to not even try displaying the image, and just fail
-  immediately if the URL is empty.  Firefox 4b11 bails out on an empty string,
-  but the other three browsers I tested stick in the <img> anyway. -->
+  immediately if the URL is empty.  Firefox 4b11 silently does nothing on an
+  empty string, but the other three browsers I tested stick in the <img>
+  anyway. -->
 
   <li>Let <var title="">range</var> be the <a href=#active-range>active range</a>.
 
--- a/implementation.js	Wed Jun 29 13:59:06 2011 -0600
+++ b/implementation.js	Wed Jun 29 14:43:51 2011 -0600
@@ -2737,9 +2737,9 @@
 //@{
 commands.createlink = {
 	action: function(value) {
-		// "If value is the empty string, abort these steps and do nothing."
+		// "If value is the empty string, raise a SYNTAX_ERR exception."
 		if (value === "") {
-			return;
+			throw "SYNTAX_ERR";
 		}
 
 		// "Decompose the active range, and let node list be the result."
@@ -2799,9 +2799,9 @@
 //@{
 commands.fontsize = {
 	action: function(value) {
-		// "If value is the empty string, do nothing and abort these steps."
+		// "If value is the empty string, raise a SYNTAX_ERR exception."
 		if (value === "") {
-			return;
+			throw "SYNTAX_ERR";
 		}
 
 		// "Strip leading and trailing whitespace from value."
@@ -2872,12 +2872,12 @@
 
 		// "If value is not one of the strings "xx-small", "x-small", "small",
 		// "medium", "large", "x-large", "xx-large", "xxx-large", and is not a
-		// valid CSS absolute length, then do nothing and abort these steps."
+		// valid CSS absolute length, then raise a SYNTAX_ERR exception."
 		//
 		// More cheap hacks to skip valid CSS absolute length checks.
 		if (["xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"].indexOf(value) == -1
 		&& !/^[0-9]+(\.[0-9]+)?(cm|mm|in|pt|pc)$/.test(value)) {
-			return;
+			throw "SYNTAX_ERR";
 		}
 
 		// "Decompose the active range, then set the value of each returned
@@ -2949,7 +2949,7 @@
 		// "If value is not a valid CSS color, prepend "#" to it."
 		//
 		// "If value is still not a valid CSS color, or if it is currentColor,
-		// do nothing and abort these steps."
+		// raise a SYNTAX_ERR exception."
 		//
 		// Cheap hack for testing, no attempt to be comprehensive.
 		if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) {
@@ -2961,7 +2961,7 @@
 		&& value != "red"
 		&& value != "cornsilk"
 		&& value != "transparent") {
-			return;
+			throw "SYNTAX_ERR";
 		}
 
 		// "Decompose the active range, then set the value of each returned
@@ -3007,7 +3007,7 @@
 		// "If value is not a valid CSS color, prepend "#" to it."
 		//
 		// "If value is still not a valid CSS color, or if it is currentColor,
-		// do nothing and abort these steps."
+		// raise a SYNTAX_ERR exception."
 		//
 		// Cheap hack for testing, no attempt to be comprehensive.
 		if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) {
@@ -3019,7 +3019,7 @@
 		&& value != "red"
 		&& value != "cornsilk"
 		&& value != "transparent") {
-			return;
+			throw "SYNTAX_ERR";
 		}
 
 		// "Decompose the active range, then set the value of each returned
@@ -5451,10 +5451,10 @@
 		value = value.toLowerCase();
 
 		// "If value is not "address", "div", "h1", "h2", "h3", "h4", "h5",
-		// "h6", "p", or "pre", then do nothing and abort these steps."
+		// "h6", "p", or "pre", raise a SYNTAX_ERR exception."
 		if (["ADDRESS", "DIV", "H1", "H2", "H3", "H4", "H5", "H6", "P",
 		"PRE"].indexOf(value.toUpperCase()) == -1) {
-			return;
+			throw "SYNTAX_ERR";
 		}
 
 		// "Block-extend the active range, and let new range be the result."
@@ -6040,9 +6040,9 @@
 //@{
 commands.insertimage = {
 	action: function(value) {
-		// "If value is the empty string, abort these steps and do nothing."
+		// "If value is the empty string, raise a SYNTAX_ERR exception."
 		if (value === "") {
-			return;
+			throw "SYNTAX_ERR";
 		}
 
 		// "Let range be the active range."
--- a/preprocess	Wed Jun 29 13:59:06 2011 -0600
+++ b/preprocess	Wed Jun 29 14:43:51 2011 -0600
@@ -108,6 +108,7 @@
     'INVALID_ACCESS_ERR': '<code data-anolis-spec=domcore title=dom-DOMException-INVALID_ACCESS_ERR>INVALID_ACCESS_ERR</code>',
     'NOT_SUPPORTED_ERR': '<code data-anolis-spec=domcore title=dom-DOMException-NOT_SUPPORTED_ERR>NOT_SUPPORTED_ERR</code>',
     'SECURITY_ERR': '<code data-anolis-spec=domcore title=dom-DOMException-SECURITY_ERR>SECURITY_ERR</code>',
+    'SYNTAX_ERR': '<code data-anolis-spec=domcore title=dom-DOMException-SYNTAX_ERR>SYNTAX_ERR</code>',
 }
 
 s = open("source.html", "r").read()
--- a/source.html	Wed Jun 29 13:59:06 2011 -0600
+++ b/source.html	Wed Jun 29 14:43:51 2011 -0600
@@ -249,6 +249,13 @@
 
   <li>Have to make sure that in all the places where we set a selection, it's
   valid.
+
+  <li>Where methods take a value, I've decided they should throw SYNTAX_ERR
+  instead of doing nothing if the value makes no sense.  As a rule, this
+  doesn't match browsers.  I think it's more useful to authors, however,
+  especially since in many cases, the command's effect isn't currently
+  interoperable anyway.  I'll switch them back to doing nothing if implementers
+  think this is a problem.
 </ul>
 
 <p class=XXX>A variety of other issues are also noted in the text, formatted
@@ -2238,14 +2245,19 @@
 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
-  nothing.
-  <!-- This matches Firefox 4b11 and Chrome 11 dev.  IE 9 RC and Opera 11 both
-  treat the request literally.  Gecko and WebKit probably have it right here:
-  users who enter no URL are very unlikely to want to link to a relative URL
-  resolving to the current document.  If they really want to, they can always
-  specify "#" for the value, or the author can rewrite it, so it's not like
-  this makes the API less useful. -->
+  <li>If <var>value</var> is the empty string, raise a [[SYNTAX_ERR]]
+  exception.
+  <!--
+  Firefox 4b11 and Chrome 11 dev both silently do nothing in this case.
+  IE 9 RC and Opera 11 both treat the request literally.  Gecko and WebKit
+  probably have it right here: users who enter no URL are very unlikely to want
+  to link to a relative URL resolving to the current document.  If they really
+  want to, they can always specify "#" for the value, or the author can rewrite
+  it, so it's not like this makes the API less useful.
+
+  As in other cases, I raise a SYNTAX_ERR instead of silently failing, because
+  I think that's more useful.  See issues.
+  -->
 
   <li><span>Decompose</span> the <span>active range</span>, and let <var>node
   list</var> be the result.
@@ -2381,8 +2393,8 @@
 respecifying non-interoperable behavior anyway.
 -->
 <ol>
-  <li>If <var>value</var> is the empty string, do nothing and abort these
-  steps.
+  <li>If <var>value</var> is the empty string, raise a [[SYNTAX_ERR]]
+  exception.
 
   <li><span data-anolis-spec=html>Strip leading and trailing whitespace</span>
   from <var>value</var>.
@@ -2438,7 +2450,7 @@
 
   <li>If <var>value</var> is not one of the strings "xx-small", "x-small",
   "small", "medium", "large", "x-large", "xx-large", "xxx-large", and is not a
-  valid CSS absolute length, then do nothing and abort these steps.
+  valid CSS absolute length, then raise a [[SYNTAX_ERR]] exception.
 
   <p class=XXX>Not sure this is the best way to do it.  We don't want to allow
   relative lengths, because those can have very weird user-visible behavior.
@@ -2581,7 +2593,7 @@
   <li>If <var>value</var> is not a valid CSS color, prepend "#" to it.
 
   <li>If <var>value</var> is still not a valid CSS color, or if it is
-  currentColor, do nothing and abort these steps.
+  currentColor, raise a [[SYNTAX_ERR]] exception.
   <!-- currentColor is bad for the same reason as relative font sizes.  It will
   confuse the algorithm, and doesn't seem very useful anyway. -->
 
@@ -2641,7 +2653,7 @@
   <li>If <var>value</var> is not a valid CSS color, prepend "#" to it.
 
   <li>If <var>value</var> is still not a valid CSS color, or if it is
-  currentColor, do nothing and abort these steps.
+  currentColor, raise a [[SYNTAX_ERR]] exception.
   <!-- currentColor is bad for the same reason as relative font sizes.  It will
   confuse the algorithm, and doesn't seem very useful anyway.  For hiliteColor
   you could conceive of it being useful, but it will still confuse the
@@ -5319,7 +5331,7 @@
   lowercase</span>.
 
   <li>If <var>value</var> is not "address", "div", "h1", "h2", "h3", "h4",
-  "h5", "h6", "p", or "pre", then do nothing and abort these steps.
+  "h5", "h6", "p", or "pre", raise a [[SYNTAX_ERR]] exception.
 
   <p class=XXX>We should support dt and dd here, since they make sense here and
   there's no other way to create them.
@@ -5905,13 +5917,14 @@
 <p><span>Action</span>:
 
 <ol>
-  <li>If <var>value</var> is the empty string, abort these steps and do
-  nothing.
+  <li>If <var>value</var> is the empty string, raise a [[SYNTAX_ERR]]
+  exception.
   <!-- Similar logic to createLink, except even more compelling, since an HTML
   document linking to itself as an image is just silly.  In fact, the current
   HTML spec instructs UAs to not even try displaying the image, and just fail
-  immediately if the URL is empty.  Firefox 4b11 bails out on an empty string,
-  but the other three browsers I tested stick in the <img> anyway. -->
+  immediately if the URL is empty.  Firefox 4b11 silently does nothing on an
+  empty string, but the other three browsers I tested stick in the <img>
+  anyway. -->
 
   <li>Let <var>range</var> be the <span>active range</span>.
 
--- a/tests.js	Wed Jun 29 13:59:06 2011 -0600
+++ b/tests.js	Wed Jun 29 14:43:51 2011 -0600
@@ -279,6 +279,8 @@
 
 		'<a name=abc>foo[bar]baz</a>',
 		'<a name=abc><b>foo[bar]baz</b></a>',
+
+		['', 'foo[bar]baz'],
 	],
 	//@}
 	// Opera requires this to be quoted, contrary to ES5 11.1.5 which allows
@@ -3441,16 +3443,21 @@
 	} catch (e) {
 		specCell.firstChild.contentEditable = "inherit";
 		specCell.firstChild.removeAttribute("spellcheck");
-		specCell.parentNode.className = "alert";
-		specCell.lastChild.className = "alert";
 		specCell.lastChild.textContent = "Exception: " + e;
-		if (typeof e == "object" && "stack" in e) {
-			specCell.lastChild.textContent += " (stack: " + e.stack + ")";
+
+		// If it's "SYNTAX_ERR", then we threw it, so all is well.  Otherwise,
+		// flag as an alert.
+		if (e !== "SYNTAX_ERR") {
+			specCell.parentNode.className = "alert";
+			specCell.lastChild.className = "alert";
+			if (typeof e == "object" && "stack" in e) {
+				specCell.lastChild.textContent += " (stack: " + e.stack + ")";
+			}
+
+			// Don't bother comparing to localStorage, this is always wrong no
+			// matter what.
+			return;
 		}
-
-		// Don't bother comparing to localStorage, this is always wrong no
-		// matter what.
-		return;
 	}
 
 	var key = "execcommand-" + command