This improves results not only for the comment-related tests I added
(which are corner cases), but also for "<p>foo<br>{}</p><p>extra</p>".
Previously it would do nothing, now it wraps the first <p>. Granted,
that's also a corner case, since such selections are pathological, but
I've pretty much finished speccing the non-corner-case stuff, so . . .

--- a/implementation.js Sun Jul 10 11:39:52 2011 -0600+++ b/implementation.js Sun Jul 10 12:08:36 2011 -0600@@ -3680,21 +3680,31 @@ startOffset = getNodeIndex(startNode); startNode = startNode.parentNode;- // "Otherwise, if start offset is start node's length and start node's- // last child is an inline node that's not a br, subtract one from- // start offset."- } else if (startOffset == getNodeLength(startNode)- && isInlineNode(startNode.lastChild)- && !isHtmlElement(startNode.lastChild, "br")) {+ // "Otherwise, if start node's child with index start offset minus one+ // is invisible, subtract one from start offset."+ } else if (isInvisible(startNode.childNodes[startOffset - 1])) { startOffset--;- // "Otherwise, if start node has a child with index start offset, and- // that child and its previousSibling are both inline nodes and the- // previousSibling isn't a br, subtract one from start offset."- } else if (startOffset < startNode.childNodes.length- && isInlineNode(startNode.childNodes[startOffset])- && isInlineNode(startNode.childNodes[startOffset].previousSibling)- && !isHtmlElement(startNode.childNodes[startOffset].previousSibling, "BR")) {+ // "Otherwise, if start node has no visible children with index greater+ // than or equal to start offset and start node's last visible child is+ // an inline node that's not a br, subtract one from start offset."+ } else if (![].slice.call(startNode.childNodes, startOffset).some(isVisible)+ && isInlineNode([].filter.call(startNode.childNodes, isVisible).slice(-1)[0])+ && !isHtmlElement([].filter.call(startNode.childNodes, isVisible).slice(-1)[0], "br")) {+ startOffset--;++ // "Otherwise, if start node has a visible child with index greater+ // than or equal to start offset, and the first such child is an inline+ // node, and start node's child with index start offset minus one is an+ // inline node other than a br, subtract one from start offset."+ //+ // The functional programming here might be a bit heavy, but it would+ // be a pain to write it differently.+ } else if (isInlineNode([].filter.call(startNode.childNodes, function(child, i) {+ return isVisible(child) && i >= startOffset;+ })[0])+ && isInlineNode(startNode.childNodes[startOffset - 1])+ && !isHtmlElement(startNode.childNodes[startOffset - 1], "br")) { startOffset--; // "Otherwise, break from this loop."@@ -3728,20 +3738,28 @@ endOffset = 1 + getNodeIndex(endNode); endNode = endNode.parentNode;- // "Otherwise, if end offset is 0 and end node's first child is an- // inline node that's not a br, add one to end offset."- } else if (endOffset == 0- && isInlineNode(endNode.firstChild)- && !isHtmlElement(endNode.firstChild, "br")) {+ // "Otherwise, if end node's child with index end offset is invisible,+ // add one to end offset."+ } else if (isInvisible(endNode.childNodes[endOffset])) { endOffset++;- // "Otherwise, if end node has a child with index end offset, and that- // child and its previousSibling are both inline nodes, and the- // previousSibling isn't a br, add one to end offset."- } else if (endOffset < endNode.childNodes.length+ // "Otherwise, if end node has no visible children with index less than+ // end offset and end node's first visible child is an inline node+ // that's not a br, add one to end offset."+ } else if (![].slice.call(endNode.childNodes, 0, endOffset).some(isVisible)+ && isInlineNode([].filter.call(endNode.childNodes, isVisible)[0])+ && !isHtmlElement([].filter.call(endNode.childNodes, isVisible)[0], "br")) {+ endOffset++;++ // "Otherwise, if end node has a visible child with index less than end+ // offset, and the last such child is an inline node, and end node's+ // child with index end offset is an inline node other than a br, add+ // one to end offset."+ } else if (isInlineNode([].filter.call(endNode.childNodes, function(child, i) {+ return isVisible(child) && i < endOffset;+ }).slice(-1)[0]) && isInlineNode(endNode.childNodes[endOffset])- && isInlineNode(endNode.childNodes[endOffset].previousSibling)- && !isHtmlElement(endNode.childNodes[endOffset], "BR")) {+ && !isHtmlElement(endNode.childNodes[endOffset], "br")) { endOffset++; // "Otherwise, break from this loop."

--- a/source.html Sun Jul 10 11:39:52 2011 -0600+++ b/source.html Sun Jul 10 12:08:36 2011 -0600@@ -3250,24 +3250,27 @@ of <var>start node</var> and then set <var>start node</var> to its [[parent]].- <li>Otherwise, if <var>start offset</var> is <var>start node</var>'s- [[nodelength]] and <var>start node</var>'s last [[child]] is an- <span>inline node</span> that's not a [[br]], subtract one from <var>start- offset</var>.+ <li>Otherwise, if <var>start node</var>'s [[child]] with [[index]]+ <var>start offset</var> minus one is <span>invisible</span>, subtract one+ from <var>start offset</var>.++ <li>Otherwise, if <var>start node</var> has no <span>visible</span>+ [[children]] with [[index]] greater than or equal to <var>start+ offset</var> and <var>start node</var>'s last <span>visible</span>+ [[child]] is an <span>inline node</span> that's not a [[br]], subtract one+ from <var>start offset</var>. <!-- So if you have a collapsed selection at the end of a block, for instance, it will extend backwards into a block. -->- <li>Otherwise, if <var>start node</var> has a [[child]] with [[index]]- <var>start offset</var>, and that [[child]] and its [[previoussibling]] are- both <span title="inline node">inline nodes</span> and the- [[previoussibling]] isn't a [[br]], subtract one from <var>start- offset</var>.+ <li>Otherwise, if <var>start node</var> has a <span>visible</span>+ [[child]] with [[index]] greater than or equal to <var>start offset</var>,+ and the first such [[child]] is an <span>inline node</span>, and <var>start+ node</var>'s [[child]] with [[index]] <var>start offset</var> minus one is+ an <span>inline node</span> other than a [[br]], subtract one from+ <var>start offset</var>. <!-- IE also includes <br> (at least for the purposes of the indent command), but this is unlikely to match user expectations. -->- <p class=XXX>This is wrong in the presence of comments or display: none or- probably other things. Do we care?- <li>Otherwise, break from this loop. </ol>@@ -3283,15 +3286,19 @@ <var>end offset</var> to one plus the [[index]] of <var>end node</var> and then set <var>end node</var> to its [[parent]].- <li>Otherwise, if <var>end offset</var> is 0 and <var>end node</var>'s- first [[child]] is an <span>inline node</span> that's not a [[br]], add one- to <var>end offset</var>.-- <li>Otherwise, if <var>end node</var> has a [[child]] with [[index]]- <var>end offset</var>, and that [[child]] and its [[previoussibling]] are- both <span title="inline node">inline nodes</span>, and the [[child]] of- <var>end node</var> with [[index]] <var>end offset</var> isn't a [[br]],- add one to <var>end offset</var>.+ <li>Otherwise, if <var>end node</var>'s [[child]] with [[index]] <var>end+ offset</var> is <span>invisible</span>, add one to <var>end offset</var>.++ <li>Otherwise, if <var>end node</var> has no <span>visible</span>+ [[children]] with [[index]] less than <var>end offset</var> and <var>end+ node</var>'s first <span>visible</span> [[child]] is an <span>inline+ node</span> that's not a [[br]], add one to <var>end offset</var>.++ <li>Otherwise, if <var>end node</var> has a <span>visible</span> [[child]]+ with [[index]] less than <var>end offset</var>, and the last such [[child]]+ is an <span>inline node</span>, and <var>end node</var>'s [[child]] with+ [[index]] <var>end offset</var> is an <span>inline node</span> other than a+ [[br]], add one to <var>end offset</var>. <li>Otherwise, break from this loop. </ol>