Fix bug in state algorithms
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Thu, 23 Jun 2011 14:53:12 -0600
changeset 318 6bbae37e774b
parent 317 067950bfb8f5
child 319 3d219108571e
Fix bug in state algorithms

It was returning a state of true if there were no nodes at all in the
selection.
editcommands.html
implementation.js
source.html
--- a/editcommands.html	Thu Jun 23 14:47:37 2011 -0600
+++ b/editcommands.html	Thu Jun 23 14:53:12 2011 -0600
@@ -2015,7 +2015,8 @@
 
 <p><a href=#state>State</a>: True if every <a href=#editable>editable</a> <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> node that
 is <a href=#effectively-contained>effectively contained</a> in the <a href=#active-range>active range</a> has
-<a href=#effective-value>effective value</a> at least 700.  Otherwise false.
+<a href=#effective-value>effective value</a> at least 700, and there is at least one such
+<code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> node.  Otherwise false.
 <!-- For bold and similar commands, IE 9 RC seems to consider the state true or
 false depending on the first element.  All other browsers follow the same
 general idea as the spec, considering a range bold only if all text in it is
@@ -2365,7 +2366,8 @@
 
 <p><a href=#state>State</a>: True if every <a href=#editable>editable</a> <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> node that
 is <a href=#effectively-contained>effectively contained</a> in the <a href=#active-range>active range</a> has
-<a href=#effective-value>effective value</a> either "italic" or "oblique".  Otherwise false.
+<a href=#effective-value>effective value</a> either "italic" or "oblique", and there is at
+least one such <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> node.  Otherwise false.
 
 <p><a href=#relevant-css-property>Relevant CSS property</a>: "font-style"
 
@@ -2503,7 +2505,8 @@
 
 <p><a href=#state>State</a>: True if every <a href=#editable>editable</a> <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> node that
 is <a href=#effectively-contained>effectively contained</a> in the <a href=#active-range>active range</a> has
-<a href=#effective-value>effective value</a> "line-through".  Otherwise false.
+<a href=#effective-value>effective value</a> "line-through", and there is at least one such
+<code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> node.  Otherwise false.
 
 
 <h3 id=the-subscript-command><span class=secno>6.18 </span><dfn>The <code title="">subscript</code> command</dfn></h3>
@@ -2526,7 +2529,8 @@
 
 <p><a href=#state>State</a>: True if every <a href=#editable>editable</a> <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> node that
 is <a href=#effectively-contained>effectively contained</a> in the <a href=#active-range>active range</a> has
-<a href=#effective-value>effective value</a> "sub".  Otherwise false.
+<a href=#effective-value>effective value</a> "sub", and there is at least one such <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code>
+node.  Otherwise false.
 
 <p><a href=#relevant-css-property>Relevant CSS property</a>: "vertical-align"
 
@@ -2551,7 +2555,8 @@
 
 <p><a href=#state>State</a>: True if every <a href=#editable>editable</a> <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> node that
 is <a href=#effectively-contained>effectively contained</a> in the <a href=#active-range>active range</a> has
-<a href=#effective-value>effective value</a> "super".  Otherwise false.
+<a href=#effective-value>effective value</a> "super", and there is at least one such <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code>
+node.  Otherwise false.
 
 <p><a href=#relevant-css-property>Relevant CSS property</a>: "vertical-align"
 
@@ -2613,7 +2618,8 @@
 
 <p><a href=#state>State</a>: True if every <a href=#editable>editable</a> <code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> node that
 is <a href=#effectively-contained>effectively contained</a> in the <a href=#active-range>active range</a> has
-<a href=#effective-value>effective value</a> "underline".  Otherwise false.
+<a href=#effective-value>effective value</a> "underline", and there is at least one such
+<code class=external data-anolis-spec=domcore><a href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#text>Text</a></code> node.  Otherwise false.
 
 
 <h3 id=the-unlink-command><span class=secno>6.21 </span><dfn>The <code title="">unlink</code> command</dfn></h3>
--- a/implementation.js	Thu Jun 23 14:47:37 2011 -0600
+++ b/implementation.js	Thu Jun 23 14:53:12 2011 -0600
@@ -203,8 +203,8 @@
 }
 
 // For computing states of the form "True if every editable Text node that is
-// effectively contained in the active range (has property X).  Otherwise
-// false."
+// effectively contained in the active range (has property X), and there is at
+// least one such Text node.  Otherwise false."
 function stateHelper(callback) {
 	var range = getActiveRange();
 	// XXX: This algorithm for getting all effectively contained nodes might be
@@ -215,6 +215,7 @@
 	}
 	var stop = nextNodeDescendants(range.endContainer);
 
+	var atLeastOne = false;
 	for (; node && node != stop; node = nextNode(node)) {
 		if (!isEffectivelyContained(node, range)
 		|| node.nodeType != Node.TEXT_NODE
@@ -222,12 +223,14 @@
 			continue;
 		}
 
+		atLeastOne = true;
+
 		if (!callback(node)) {
 			return false;
 		}
 	}
 
-	return true;
+	return atLeastOne;
 }
 
 //@}
@@ -2611,7 +2614,8 @@
 		}
 	}, state: function() { return stateHelper(function(node) {
 		// "True if every editable Text node that is effectively contained in
-		// the active range has effective value at least 700. Otherwise false."
+		// the active range has effective value at least 700, and there is at
+		// least one such text node. Otherwise false."
 		var fontWeight = getEffectiveValue(node, "bold");
 		return fontWeight === "bold"
 			|| fontWeight === "700"
@@ -2846,8 +2850,8 @@
 		}
 	}, state: function() { return stateHelper(function(node) {
 		// "True if every editable Text node that is effectively contained in
-		// the active range has effective value either "italic" or "oblique".
-		// Otherwise false."
+		// the active range has effective value either "italic" or "oblique",
+		// and there is at least one such Text node.  Otherwise false."
 		var value = getEffectiveValue(node, "italic");
 		return value == "italic" || value == "oblique";
 	})}, relevantCssProperty: "fontStyle"
@@ -2947,8 +2951,8 @@
 		}
 	}, state: function() { return stateHelper(function(node) {
 		// "True if every editable Text node that is effectively contained in
-		// the active range has effective value "line-through". Otherwise
-		// false."
+		// the active range has effective value "line-through", and there is at
+		// least one such Text node. Otherwise false."
 		return getEffectiveValue(node, "strikethrough") == "line-through";
 	})}
 };
@@ -2979,7 +2983,8 @@
 		}
 	}, state: function() { return stateHelper(function(node) {
 		// "True if every editable Text node that is effectively contained in
-		// the active range has effective value "sub". Otherwise false."
+		// the active range has effective value "sub", and there is at least
+		// one such Text node. Otherwise false."
 		return getEffectiveValue(node, "subscript") == "sub";
 	})}, relevantCssProperty: "verticalAlign"
 };
@@ -3010,7 +3015,8 @@
 		}
 	}, state: function() { return stateHelper(function(node) {
 		// "True if every editable Text node that is effectively contained in
-		// the active range has effective value "super". Otherwise false."
+		// the active range has effective value "super", and there is at least
+		// one such Text node. Otherwise false."
 		return getEffectiveValue(node, "superscript") == "super";
 	})}, relevantCssProperty: "verticalAlign"
 };
@@ -3030,7 +3036,8 @@
 		}
 	}, state: function() { return stateHelper(function(node) {
 		// "True if every editable Text node that is effectively contained in
-		// the active range has effective value "underline". Otherwise false."
+		// the active range has effective value "underline", and there is at
+		// least one such Text node. Otherwise false."
 		return getEffectiveValue(node, "underline") === "underline";
 	})}
 };
--- a/source.html	Thu Jun 23 14:47:37 2011 -0600
+++ b/source.html	Thu Jun 23 14:53:12 2011 -0600
@@ -1991,7 +1991,8 @@
 
 <p><span>State</span>: True if every <span>editable</span> [[text]] node that
 is <span>effectively contained</span> in the <span>active range</span> has
-<span>effective value</span> at least 700.  Otherwise false.
+<span>effective value</span> at least 700, and there is at least one such
+[[text]] node.  Otherwise false.
 <!-- For bold and similar commands, IE 9 RC seems to consider the state true or
 false depending on the first element.  All other browsers follow the same
 general idea as the spec, considering a range bold only if all text in it is
@@ -2341,7 +2342,8 @@
 
 <p><span>State</span>: True if every <span>editable</span> [[text]] node that
 is <span>effectively contained</span> in the <span>active range</span> has
-<span>effective value</span> either "italic" or "oblique".  Otherwise false.
+<span>effective value</span> either "italic" or "oblique", and there is at
+least one such [[text]] node.  Otherwise false.
 
 <p><span>Relevant CSS property</span>: "font-style"
 <!-- @} -->
@@ -2480,7 +2482,8 @@
 
 <p><span>State</span>: True if every <span>editable</span> [[text]] node that
 is <span>effectively contained</span> in the <span>active range</span> has
-<span>effective value</span> "line-through".  Otherwise false.
+<span>effective value</span> "line-through", and there is at least one such
+[[text]] node.  Otherwise false.
 <!-- @} -->
 
 <h3><dfn>The <code title>subscript</code> command</dfn></h3>
@@ -2503,7 +2506,8 @@
 
 <p><span>State</span>: True if every <span>editable</span> [[text]] node that
 is <span>effectively contained</span> in the <span>active range</span> has
-<span>effective value</span> "sub".  Otherwise false.
+<span>effective value</span> "sub", and there is at least one such [[text]]
+node.  Otherwise false.
 
 <p><span>Relevant CSS property</span>: "vertical-align"
 <!-- @} -->
@@ -2528,7 +2532,8 @@
 
 <p><span>State</span>: True if every <span>editable</span> [[text]] node that
 is <span>effectively contained</span> in the <span>active range</span> has
-<span>effective value</span> "super".  Otherwise false.
+<span>effective value</span> "super", and there is at least one such [[text]]
+node.  Otherwise false.
 
 <p><span>Relevant CSS property</span>: "vertical-align"
 <!-- @} -->
@@ -2592,7 +2597,8 @@
 
 <p><span>State</span>: True if every <span>editable</span> [[text]] node that
 is <span>effectively contained</span> in the <span>active range</span> has
-<span>effective value</span> "underline".  Otherwise false.
+<span>effective value</span> "underline", and there is at least one such
+[[text]] node.  Otherwise false.
 <!-- @} -->
 
 <h3><dfn>The <code title>unlink</code> command</dfn></h3>