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>
Created attachment 27121 [details] test case
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.
(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.
I think this in the end is just another dupe of bug 15256.
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!
Probably fixed by the live range selection work that Ryosuke is doing.
Indeed, this test case passes once live range selection is enabled.
*** This bug has been marked as a duplicate of bug 216325 ***