Skip to content

The restore() function does not correctly set neither the caret position nor selection by code #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
angezid opened this issue Mar 18, 2025 · 0 comments
Labels
firefox A bug in FireFox itself

Comments

@angezid
Copy link

angezid commented Mar 18, 2025

This is not related only to FireFox!

See a demo of this bug and result of fixing: bug-demo.zip

...
const { anchorOffset, focusOffset } = getSelection(),
    // detects setting selection by code and deletion at the beginning of text node
    greaterThan = pos.start !== pos.end || anchorOffset === 0 && focusOffset === 0;

visit(editor, el => {
    if (el.nodeType !== Node.TEXT_NODE) return;
    const len = (el.nodeValue || '').length;

    if ( !greaterThan && current + len >= pos.start || greaterThan && current + len > pos.start) {
        if ( !startNode) {
            startNode = el;
            startOffset = pos.start - current;
        }
        if (current + len >= pos.end) {
            endNode = el;
            endOffset = pos.end - current;
            return 'stop';
        }
    }
    current += len;
});
...

For the pos.end the comparison should always be >=.

For the pos.start the comparisons should be:

  • for typing or deleting end character of a text node - >=
  • for programmatic selection - >
  • for deleting the start character of a text node or range that started at the beginning of a text node - >

The two conditions are used to switch necessary comparison:

  • pos.start !== pos.end detects programmatic selection
  • anchorOffset === 0 && focusOffset === 0 detects deletion at the start of a text node

It won't fix open #111 and #115 issues. I was messed up with testing related to the issues.

By the way, maybe it worthwhile adding if (typeof highlight === 'function') highlight(editor, pos)? It removes dependency on highlighting plugin.

@antonmedv antonmedv added the firefox A bug in FireFox itself label Mar 18, 2025
@angezid angezid changed the title Discovered bug in selection function The restore() function does not correctly set neither the caret position nor selection by code Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
firefox A bug in FireFox itself
Projects
None yet
Development

No branches or pull requests

2 participants