Bug 23600 - Selection.collapse does not work as expected on empty text nodes
Summary: Selection.collapse does not work as expected on empty text nodes
Status: RESOLVED DUPLICATE of bug 216325
Alias: None
Product: WebKit
Classification: Unclassified
Component: HTML Editing (show other bugs)
Version: 528+ (Nightly build)
Hardware: Mac OS X 10.5
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on: 15256 23601
Blocks:
  Show dependency treegraph
 
Reported: 2009-01-28 13:49 PST by Eric Seidel (no email)
Modified: 2022-12-17 22:05 PST (History)
7 users (show)

See Also:


Attachments
test case (1.06 KB, text/html)
2009-01-28 13:50 PST, Eric Seidel (no email)
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Seidel (no email) 2009-01-28 13:49:50 PST
Ojan and I were trying to use comparePoint in a test case.  It didn't work like we thought it would.  After some investigation, it just seems our ranges are borked.  This is probably a dupe of the "VisualPosiition shouldn't be used as much as they are" bug.

Test case:
<html>
<body>
    <div id='log'></div>
<script>
function debug(str) {
    var logger = document.getElementById('log');
    logger.appendChild(document.createTextNode(str));
    logger.appendChild(document.createElement('br'));
}
var div = document.createElement('div');
document.body.appendChild(div);
var textNode = document.createTextNode("");
div.appendChild(textNode);

var sel = window.getSelection();
sel.collapse(textNode, 0);
var range = sel.getRangeAt(0);

debug("comparing point with <text>, offset 0:");
var result = range.comparePoint(textNode, 0);
if (result == 0) {
    debug("PASSED: result = 0");
} else {
    debug("FAILED: result = " + result + " expected 0");
}

debug("range:");
debug("startContainer: " + range.startContainer);
debug("startOffset: " + range.startOffset);
debug("endContainer: " + range.endContainer);
debug("endOffset: " + range.endOffset);

debug("selection:");
debug("anchorNode: " + sel.anchorNode);
debug("anchorOffset: " + sel.anchorOffset);
debug("focusNode: " + sel.focusNode);
debug("focusOffset: " + sel.focusOffset);
</script>
</body>
</html>
Comment 1 Eric Seidel (no email) 2009-01-28 13:50:08 PST
Created attachment 27121 [details]
test case
Comment 2 Eric Seidel (no email) 2009-01-28 13:54:29 PST
Ok, turns out there are at least two bugs at play.  Repurposing this one to cover "Range.collapse does not work as expected on empty text nodes", and will file a separate bug with slightly modified test case showing more clearly that getRangeAt(0) is mutating the Range in a bad bad way.
Comment 3 Justin Garcia 2009-01-28 15:16:13 PST
(In reply to comment #2)
> Ok, turns out there are at least two bugs at play.  Repurposing this one to
> cover "Range.collapse does not work as expected on empty text nodes

Looks like you're using Selection.collapse not Range.collapse.
Comment 4 Eric Seidel (no email) 2009-03-16 17:44:58 PDT
I think this in the end is just another dupe of bug 15256.
Comment 5 Ahmad Saleem 2022-12-16 16:14:26 PST
Using attached test case:

*** Safari Technology Preview 160 ***

comparing point with <text>, offset 0:
FAILED: result = 1 expected 0
range:
startContainer: [object HTMLBodyElement]
startOffset: 0
endContainer: [object HTMLBodyElement]
endOffset: 0
selection:
anchorNode: [object HTMLBodyElement]
anchorOffset: 0
focusNode: [object HTMLBodyElement]
focusOffset: 0

*** Chrome Canary 110 & Firefox Nightly 110 ***

comparing point with <text>, offset 0:
PASSED: result = 0
range:
startContainer: [object Text]
startOffset: 0
endContainer: [object Text]
endOffset: 0
selection:
anchorNode: [object Text]
anchorOffset: 0
focusNode: [object Text]
focusOffset: 0

____

Just wanted to share updated test result. Thanks!
Comment 6 Darin Adler 2022-12-17 19:20:58 PST
Probably fixed by the live range selection work that Ryosuke is doing.
Comment 7 Ryosuke Niwa 2022-12-17 22:05:15 PST
Indeed, this test case passes once live range selection is enabled.
Comment 8 Ryosuke Niwa 2022-12-17 22:05:29 PST

*** This bug has been marked as a duplicate of bug 216325 ***