Simplify "effectively contained" definition
authorAryeh Gregor <AryehGregor+gitcommit@gmail.com>
Thu, 04 Aug 2011 12:41:30 -0600
changeset 494 8da0a570e50b
parent 493 f9470ec1b290
child 495 4255184a354b
Simplify "effectively contained" definition

If you examine the previous definition, you'll find that if the range is
collapsed, nothing can be effectively contained in it. Thus I can move
four separate mentions of being collapsed to one at the beginning
instead. The latter two mentions were totally redundant, since they
required that the range be collapsed, which was impossible under the
circumstances.

Tim Down brought this to my attention by pointing out misnested
parentheses in the implementation. After thinking about it, I realized
that a) the misnesting only had the effect that the last range.collapsed
check did nothing, and b) this error would never actually affect the
result, so I could remove it from the definition.

Reported-By: Tim Down
editing.html
implementation.js
source.html
--- a/editing.html	Wed Aug 03 14:16:21 2011 -0600
+++ b/editing.html	Thu Aug 04 12:41:30 2011 -0600
@@ -62,7 +62,7 @@
 <body class=draft>
 <div class=head id=head>
 <h1>HTML Editing APIs</h1>
-<h2 class="no-num no-toc" id=work-in-progress-&mdash;-last-update-3-august-2011>Work in Progress &mdash; Last Update 3 August 2011</h2>
+<h2 class="no-num no-toc" id=work-in-progress-&mdash;-last-update-4-august-2011>Work in Progress &mdash; Last Update 4 August 2011</h2>
 <dl>
  <dt>Editor
  <dd>Aryeh Gregor &lt;<a href=mailto:ayg@aryeh.name>ayg@aryeh.name</a>&gt;
@@ -1397,7 +1397,8 @@
 and the &lt;b&gt; is neither effectively contained nor contained.
 
 <p>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> is <dfn id=effectively-contained>effectively contained</dfn> in a
-<a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range title=concept-range>range</a> <var title="">range</var> if at least one of the following holds:
+<a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range title=concept-range>range</a> <var title="">range</var> if <var title="">range</var> is not <code class=external data-anolis-spec=domrange title=dom-Range-collapsed><a href=http://html5.org/specs/dom-range.html#dom-range-collapsed>collapsed</a></code>, and
+at least one of the following holds:
 
 <ul>
   <li><var title="">node</var> is <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#contained>contained</a> in <var title="">range</var>.
@@ -1407,12 +1408,11 @@
   &lt;b&gt;f[oo&lt;/b&gt;}, but not &lt;b&gt;foo[&lt;/b&gt;} or &lt;b&gt;f[]oo&lt;/b&gt;.
 
   <p><var title="">node</var> is <var title="">range</var>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-start title=concept-range-start>start</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-node title=concept-boundary-point-node>node</a>, it is 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, its <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-node-length title=concept-node-length>length</a> is different from <var title="">range</var>'s
-  <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-start title=concept-range-start>start</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-offset title=concept-boundary-point-offset>offset</a>, and <var title="">range</var> is not <code class=external data-anolis-spec=domrange title=dom-Range-collapsed><a href=http://html5.org/specs/dom-range.html#dom-range-collapsed>collapsed</a></code>.
+  node, and its <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-node-length title=concept-node-length>length</a> is different from <var title="">range</var>'s
+  <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-start title=concept-range-start>start</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-offset title=concept-boundary-point-offset>offset</a>.
 
   <li><var title="">node</var> is <var title="">range</var>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-end title=concept-range-end>end</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-node title=concept-boundary-point-node>node</a>, it is 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,
-  <var title="">range</var>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-end title=concept-range-end>end</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-offset title=concept-boundary-point-offset>offset</a> is not 0, and <var title="">range</var> is not
-  <code class=external data-anolis-spec=domrange title=dom-Range-collapsed><a href=http://html5.org/specs/dom-range.html#dom-range-collapsed>collapsed</a></code>.
+  and <var title="">range</var>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-end title=concept-range-end>end</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-offset title=concept-boundary-point-offset>offset</a> is not 0.
 
   <li>
   <p class=comments>Basically, anything whose children are all effectively
@@ -1424,11 +1424,10 @@
   <p><var title="">node</var> has at least one <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 all its <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> are
   <a href=#effectively-contained>effectively contained</a> in <var title="">range</var>; and either
   <var title="">range</var>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-start title=concept-range-start>start</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-node title=concept-boundary-point-node>node</a> is not a <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-tree-descendant title=concept-tree-descendant>descendant</a> of <var title="">node</var>
-  or is not 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 or <var title="">range</var>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-start title=concept-range-start>start</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-offset title=concept-boundary-point-offset>offset</a> is zero or
-  <var title="">range</var> is <code class=external data-anolis-spec=domrange title=dom-Range-collapsed><a href=http://html5.org/specs/dom-range.html#dom-range-collapsed>collapsed</a></code>; and either <var title="">range</var>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-end title=concept-range-end>end</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-node title=concept-boundary-point-node>node</a>
-  is not a <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-tree-descendant title=concept-tree-descendant>descendant</a> of <var title="">node</var> or is not 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 or
-  <var title="">range</var>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-end title=concept-range-end>end</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-offset title=concept-boundary-point-offset>offset</a> is its <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-end title=concept-range-end>end</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-node title=concept-boundary-point-node>node</a>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-node-length title=concept-node-length>length</a> or
-  <var title="">range</var> is <code class=external data-anolis-spec=domrange title=dom-Range-collapsed><a href=http://html5.org/specs/dom-range.html#dom-range-collapsed>collapsed</a></code>.
+  or is not 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 or <var title="">range</var>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-start title=concept-range-start>start</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-offset title=concept-boundary-point-offset>offset</a> is zero; and
+  either <var title="">range</var>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-end title=concept-range-end>end</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-node title=concept-boundary-point-node>node</a> is not a <a class=external data-anolis-spec=domcore href=http://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#concept-tree-descendant title=concept-tree-descendant>descendant</a> of
+  <var title="">node</var> or is not 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 or <var title="">range</var>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-end title=concept-range-end>end</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-offset title=concept-boundary-point-offset>offset</a>
+  is its <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-range-end title=concept-range-end>end</a> <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-boundary-point-node title=concept-boundary-point-node>node</a>'s <a class=external data-anolis-spec=domrange href=http://html5.org/specs/dom-range.html#concept-node-length title=concept-node-length>length</a>.
 </ul>
 
 <p>A <dfn id=modifiable-element>modifiable element</dfn> is a <code class=external data-anolis-spec=html title="the b element"><a href=http://www.whatwg.org/html/#the-b-element>b</a></code>, <code class=external data-anolis-spec=html title="the em element"><a href=http://www.whatwg.org/html/#the-em-element>em</a></code>, <code class=external data-anolis-spec=html title="the i element"><a href=http://www.whatwg.org/html/#the-i-element>i</a></code>, <code class=external data-anolis-spec=html title="the s element"><a href=http://www.whatwg.org/html/#the-s-element>s</a></code>, <code class=external data-anolis-spec=html title="the span element"><a href=http://www.whatwg.org/html/#the-span-element>span</a></code>,
--- a/implementation.js	Wed Aug 03 14:16:21 2011 -0600
+++ b/implementation.js	Thu Aug 04 12:41:30 2011 -0600
@@ -1532,48 +1532,47 @@
 ///// Inline formatting command definitions /////
 //@{
 
-// "A node node is effectively contained in a range range if at least one of
-// the following holds:"
+// "A node node is effectively contained in a range range if range is not
+// collapsed, and at least one of the following holds:"
 function isEffectivelyContained(node, range) {
+	if (range.collapsed) {
+		return false;
+	}
+
 	// "node is contained in range."
 	if (isContained(node, range)) {
 		return true;
 	}
 
-	// "node is range's start node, it is a Text node, its length is different
-	// from range's start offset, and range is not collapsed."
+	// "node is range's start node, it is a Text node, and its length is
+	// different from range's start offset."
 	if (node == range.startContainer
 	&& node.nodeType == Node.TEXT_NODE
-	&& getNodeLength(node) != range.startOffset
-	&& !range.collapsed) {
+	&& getNodeLength(node) != range.startOffset) {
 		return true;
 	}
 
-	// "node is range's end node, it is a Text node, range's end offset is not
-	// 0, and range is not collapsed."
+	// "node is range's end node, it is a Text node, and range's end offset is
+	// not 0."
 	if (node == range.endContainer
 	&& node.nodeType == Node.TEXT_NODE
-	&& range.endOffset != 0
-	&& !range.collapsed) {
+	&& range.endOffset != 0) {
 		return true;
 	}
 
 	// "node has at least one child; and all its children are effectively
 	// contained in range; and either range's start node is not a descendant of
-	// node or is not a Text node or range's start offset is zero or range is
-	// collapsed; and either range's end node is not a descendant of node or is
-	// not a Text node or range's end offset is its end node's length or range
-	// is collapsed."
+	// node or is not a Text node or range's start offset is zero; and either
+	// range's end node is not a descendant of node or is not a Text node or
+	// range's end offset is its end node's length."
 	if (node.hasChildNodes()
 	&& [].every.call(node.childNodes, function(child) { return isEffectivelyContained(child, range) })
 	&& (!isDescendant(range.startContainer, node)
 	|| range.startContainer.nodeType != Node.TEXT_NODE
-	|| range.startOffset == 0
-	|| range.collapsed)
+	|| range.startOffset == 0)
 	&& (!isDescendant(range.endContainer, node)
 	|| range.endContainer.nodeType != Node.TEXT_NODE
-	|| range.endOffset == getNodeLength(range.endContainer
-	|| range.collapsed))) {
+	|| range.endOffset == getNodeLength(range.endContainer))) {
 		return true;
 	}
 
--- a/source.html	Wed Aug 03 14:16:21 2011 -0600
+++ b/source.html	Thu Aug 04 12:41:30 2011 -0600
@@ -1364,7 +1364,8 @@
 and the &lt;b> is neither effectively contained nor contained.
 
 <p>A [[node]] <var>node</var> is <dfn>effectively contained</dfn> in a
-[[range]] <var>range</var> if at least one of the following holds:
+[[range]] <var>range</var> if <var>range</var> is not [[rangecollapsed]], and
+at least one of the following holds:
 
 <ul>
   <li><var>node</var> is [[contained]] in <var>range</var>.
@@ -1374,12 +1375,11 @@
   &lt;b>f[oo&lt;/b>}, but not &lt;b>foo[&lt;/b>} or &lt;b>f[]oo&lt;/b>.
 
   <p><var>node</var> is <var>range</var>'s [[startnode]], it is a [[text]]
-  node, its [[nodelength]] is different from <var>range</var>'s
-  [[startoffset]], and <var>range</var> is not [[rangecollapsed]].
+  node, and its [[nodelength]] is different from <var>range</var>'s
+  [[startoffset]].
 
   <li><var>node</var> is <var>range</var>'s [[endnode]], it is a [[text]] node,
-  <var>range</var>'s [[endoffset]] is not 0, and <var>range</var> is not
-  [[rangecollapsed]].
+  and <var>range</var>'s [[endoffset]] is not 0.
 
   <li>
   <p class=comments>Basically, anything whose children are all effectively
@@ -1391,11 +1391,10 @@
   <p><var>node</var> has at least one [[child]]; and all its [[children]] are
   <span>effectively contained</span> in <var>range</var>; and either
   <var>range</var>'s [[startnode]] is not a [[descendant]] of <var>node</var>
-  or is not a [[text]] node or <var>range</var>'s [[startoffset]] is zero or
-  <var>range</var> is [[rangecollapsed]]; and either <var>range</var>'s [[endnode]]
-  is not a [[descendant]] of <var>node</var> or is not a [[text]] node or
-  <var>range</var>'s [[endoffset]] is its [[endnode]]'s [[length]] or
-  <var>range</var> is [[rangecollapsed]].
+  or is not a [[text]] node or <var>range</var>'s [[startoffset]] is zero; and
+  either <var>range</var>'s [[endnode]] is not a [[descendant]] of
+  <var>node</var> or is not a [[text]] node or <var>range</var>'s [[endoffset]]
+  is its [[endnode]]'s [[length]].
 </ul>
 
 <p>A <dfn>modifiable element</dfn> is a [[b]], [[em]], [[i]], [[s]], [[span]],