selection.deleteFromDocument() throws for collapsed selection

RESOLVED FIXED in mozilla13



8 years ago
7 years ago


(Reporter: ayg, Assigned: ayg)


Bug Flags:
in-testsuite +

Firefox Tracking Flags

(Not tracked)



(1 attachment, 1 obsolete attachment)


data:text/html,<!doctype html>
try {
getSelection().collapse(document.head, 0);
} catch(e) {
document.body.textContent = e;
throw e;
document.body.textContent = "success";

In IE9, Chrome 17 dev, and Opera Next 12.00 alpha, this outputs "success".  In Firefox 12.0a1 (2012-01-17), it outputs

[Exception... "Component returned failure code: 0x80004001 (NS_ERROR_NOT_IMPLEMENTED) [nsISelection.deleteFromDocument]" nsresult: "0x80004001 (NS_ERROR_NOT_IMPLEMENTED)" location: "JS frame :: data:text/html,<!doctype%20html><body><script>try%20{getSelection().collapse(document.head,%200);getSelection().deleteFromDocument();}%20catch(e)%20{document.body.textContent%20=%20e;throw%20e;}document.body.textContent%20=%20"success";</script> :: <TOP_LEVEL> :: line 1" data: no]

This causes test failures in <>.  The specification requires the non-Gecko behavior, which is IMO less surprising:

The deleteFromDocument() method must do nothing if the context object's range is null, and otherwise must invoke the deleteContents() method on the context object's range.

deleteContents() on a collapsed selection is therefore a no-op.
Posted patch Patch v1 (obsolete) — Splinter Review
Since I didn't have enough time left today to start on transitions/animations testing, and none of the transforms stuff left on my plate looks particularly easy, I figured I'd write a quick Selection patch to round out my day.  Turns out the behavior was actually odder than I thought: it only throws in some cases, and in other cases it does something that looks quite surprising.
Assignee: nobody → ayg
Attachment #597167 - Flags: review?
Attachment #597167 - Flags: review? → review?(bzbarsky)
Whiteboard: [autoland]


7 years ago
Whiteboard: [autoland] → [autoland-in-queue]
Autoland Patchset:
	Patches: 597167
	Branch: mozilla-central => try
Try run started, revision 4f40044ba6c3. To cancel or monitor the job, see:
Try run for 4f40044ba6c3 is complete.
Detailed breakdown of the results available here:
Results (out of 211 total builds):
    exception: 1
    success: 179
    warnings: 16
    failure: 15
Builds (or logs if builds failed) available at:


7 years ago
Whiteboard: [autoland-in-queue]
Comment on attachment 597167 [details] [diff] [review]
Patch v1

Goig to punt this to Ehsan, but it looks like that code was trying to deal with deleting the previous char as needed... is that not necessary?
Attachment #597167 - Flags: review?(bzbarsky) → review?(ehsan)
It's not what any other browser does.  If the idea was to make it work like execCommand("delete"), it looks far too crude to be useful.  To get an effect anything like hitting the backspace key, you need way way more code than that.
OK.  I assume you did look at the blame for this code and there was nothing useful there, right?
It seemed to go back to the original hg commit.  I didn't look in CVS.  I just did that and it seems to go back to CVS commit 3.1 (?):

3.1 <> 2000-03-21 15:57
Cleaning up selection: changed 'RangeList' to 'Selection', thus changing implementation class of nsIDOMSelection and nsIFrameSelection to nsSelection. File nsRangeList.cpp replaced with new nsSelection.cpp

That's as far back as I was able to find. is a starting point for going further back.

That gets us to:

  1.31 <> 1999-01-29 17:02
  Add IsCollapsed; add collapsed case to DeleteFromDocument

Not helpful, indeed.  I don't think it's worth getting in touch with Akkana to find out what she meant here...  OK.

Comment 9

7 years ago
Comment on attachment 597167 [details] [diff] [review]
Patch v1

Review of attachment 597167 [details] [diff] [review]:

::: layout/generic/test/test_bug719503.html
@@ +11,5 @@
> +<pre id="test">
> +<script>
> +getSelection().collapse(document.head, 0);
> +getSelection().deleteFromDocument();
> +ok(true, "Passed, no exception");

This is not the right way of testing this.  This way, if something throws, ok() will never get executed and the test will pass silently.  Please add a try/catch block around those two calls, and ok(false) in the catch block.  (Note that you still need to keep the current ok(true) so that the test would print _something_ when successful, to keep the test framework from complaining about a test finished with no checks run.)

r=me with that.
Attachment #597167 - Flags: review?(ehsan) → review+
Posted patch Patch v2Splinter Review
Attachment #597167 - Attachment is obsolete: true
Attachment #599608 - Flags: review+
Patch updated as requested.
Keywords: checkin-needed
Flags: in-testsuite+
Keywords: checkin-needed
OS: Linux → All
Hardware: x86 → All
Target Milestone: --- → mozilla13
Closed: 7 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.