Range collapses when startOffset is after the last character

By design Issue #15684911

Details

Author
Kelly P.
Created
Jan 29, 2018
Privacy
This issue is public.
Found in
  • Microsoft Edge
Found in build #
16.16299
Reports
Reported by 1 person

Sign in to watch or report this issue.

Steps to reproduce

When setting the startOffset of a Range, if you set it to the length of the child nodes, it collapses to the endOffset.

var node = document.querySelector('#last')
var text = node.firstChild
var selection = document.getSelection()
var range = document.createRange()

// Create a range where the startOffset is the length of the node:
range.setStart(text, text.length)
range.setEnd(text, 1)

// The range will collapse to the endOffset
console.log(range.startOffset, range.endOffset)

selection.addRange(range)
selection.deleteFromDocument()

https://jsfiddle.net/pyj0um7d/2/

Attachments

0 attachments

    Comments and activity

    • Microsoft Edge Team

      Changed Assigned To to “Steven K.”

    • Hi Kelly,

      I believe this is proper behavior for a range set with the start position lower in the document/text than the end position.  E.g. start is a higher number than the end.

      https://developer.mozilla.org/en-US/docs/Web/API/Range/setStart

      “Setting the start point below (lower in the document) the end point will result in a collapsed range with the start and end points both set to the specified start position.”

      I believe this might be confusing due to the anchor and the offset in the selection API allowing either ordering.

      https://developer.mozilla.org/en-US/docs/Web/API/Selection

      "A user may make a selection from left to right (in document order) or right to left (reverse of document order). The 

      anchor
       
      is where the user began the selection and the 

      focus
       
      is where the user ends the selection. If you make a selection with a desktop mouse, the anchor is placed where you pressed the mouse button and the focus is placed where you released the mouse button. Anchor and focus should not be confused with the start and end positions of a selection, since anchor can be placed before the focus or vice versa, depending on the direction you made your selection."

      Steve

    • Ah, my mistake.

    • Microsoft Edge Team

      Changed Status to “By design”

    • Thanks for the quick response and of course, no worries.  Apparently, this happens enough to warrant a note in the documentation.  :)

      Thank you for the submission and the support,

      The MS Edge Team

    You need to sign in to your Microsoft account to add a comment.

    Sign in