Closed Bug 1542029 Opened 6 years ago Closed 6 years ago

Range setStartAfter/setEndAfter puts caret before node

Categories

(Core :: DOM: Editor, defect)

66 Branch
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 832731

People

(Reporter: gezuru, Unassigned)

References

Details

User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0

Steps to reproduce:

When setting the caret position using range.set(Start|End)After, the cursor is not always set properly.

With this HTML:

<button id="go">Add selection on line 2 right before "b"</button>
<div id="el" contenteditable>a<br>b</div>

And this JS:

document.getElementById("go").addEventListener('click', () => {
const sel = document.getSelection();

// Get or create range
let range, created;
if (sel.rangeCount == 0) {
range = document.createRange();
created = true;
} else {
range = sel.getRangeAt(0);
created = false;
}

// Get DOM references
const el = document.getElementById("el");
const br = el.children[0];

// Add range
range.setStartAfter(br);
range.setEndAfter(br);
if (created) {
sel.addRange(range);
}
el.focus();
});

...when clicking the button, the caret position is on line 1 after the "a", instead of on line 2 before the "b".

Interactive codepen: https://codepen.io/anon/pen/GLZoxO

It works properly in Chromium.

Component: Untriaged → Editor
Product: Firefox → Core
See Also: → 832731

Thanks for linking the see also bug 832731, Alice0775 White.
Should we duplicate this as that one, Makoto-san?

Flags: needinfo?(m_kato)

(In reply to Hsin-Yi Tsai [:hsinyi] from comment #1)

Thanks for linking the see also bug 832731, Alice0775 White.
Should we duplicate this as that one, Makoto-san?

Yes, we should be dup of this bug.

Also, this may not editor issue. I guess that nsCaret::GetCaretFrameForNodeOffset returns the frame of end of line.

Status: UNCONFIRMED → RESOLVED
Closed: 6 years ago
Flags: needinfo?(m_kato)
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.